File size: 1,407 Bytes
f091ba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <mutex>
#include <optional>
#include <string>
#include <vector>

#ifndef BUFFER_SIZE
#define BUFFER_SIZE = 128
#endif

#ifndef NAME_LENGTH
#define NAME_LENGTH = 128
#endif

using OptionalNumber = std::optional<std::size_t>;
using BufferEntry = std::pair<OptionalNumber, const char*>;

class NameBuffer {
 public:
  static inline BufferEntry Add(const std::string& name) {
    return getBuffer().add(name);
  }

  static inline const char* Get(std::size_t index) {
    return getBuffer().get(index);
  }

 private:
  NameBuffer() : m_buffer(BUFFER_SIZE, nullptr), m_index(0ul) {
    for (std::size_t index = 0ul, end = m_buffer.size(); index < end; ++index)
      m_buffer[index] = new char[NAME_LENGTH];
  }

  std::mutex m_mutex;
  std::vector<char*> m_buffer;
  std::size_t m_index;

  static inline NameBuffer& getBuffer() {
    static NameBuffer buffer;
    return buffer;
  }

  BufferEntry add(const std::string& name) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_index >= BUFFER_SIZE || name.size() > NAME_LENGTH)
      return std::make_pair(std::nullopt, nullptr);

    auto index = m_index++;
    name.copy(m_buffer[index], name.size());
    return std::make_pair(index, m_buffer[index]);
  }

  const char* get(std::size_t index) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (index >= BUFFER_SIZE) return nullptr;
    return m_buffer[index];
  }
};