Lluvia
PushConstants.h
Go to the documentation of this file.
1 
8 #ifndef LLUVIA_CORE_NODE_PUSH_CONSTANTS_H_
9 #define LLUVIA_CORE_NODE_PUSH_CONSTANTS_H_
10 
11 #include "lluvia/core/error.h"
12 
13 #include <cstdint>
14 #include <cstring>
15 #include <memory>
16 #include <vector>
17 
18 namespace ll {
19 
21 
22 public:
23  PushConstants() = default;
24  PushConstants(const PushConstants&) = default;
26 
27  ~PushConstants() = default;
28 
31 
32  template <typename T>
33  void set(T&& data)
34  {
35 
36  const auto dataSize = sizeof(data);
37  m_data.resize(dataSize);
38 
39  std::memcpy(static_cast<void*>(&m_data[0]), &data, dataSize);
40  }
41 
42  template <typename T>
43  T get() const
44  {
45 
46  if (sizeof(T) != m_data.size()) {
48  "Size of receiving object must be equal to the size of internal buffer, expected: "
49  + std::to_string(m_data.size())
50  + " got: " + std::to_string(sizeof(T)));
51  }
52 
53  T out {};
54  std::memcpy(&out, &m_data[0], sizeof(out));
55 
56  return out;
57  }
58 
59  template <typename T>
60  void push(T&& data)
61  {
62 
63  const auto currentSize = getSize();
64  if (currentSize == 0) {
65  set(std::forward<T>(data));
66  return;
67  }
68 
69  const auto dataSize = sizeof(data);
70 
71  auto new_data = std::vector<uint8_t>(currentSize + dataSize);
72  std::memcpy(&new_data[0], &m_data[0], currentSize);
73  std::memcpy(&new_data[currentSize], &data, dataSize);
74 
75  m_data = std::move(new_data);
76  }
77 
78  inline void* getPtr() const noexcept
79  {
80  return (void*)(&m_data[0]);
81  }
82 
83  size_t getSize() const noexcept
84  {
85  return m_data.size();
86  }
87 
88  void pushFloat(const float& d) { push(d); };
89  void setFloat(const float& d) { set(d); }
90  float getFloat() const { return get<float>(); }
91 
92  void pushInt32(const int32_t& d) { push(d); };
93  void setInt32(const int32_t& d) { set(d); }
94  int32_t getInt32() const { return get<int32_t>(); }
95 
96 private:
97  std::vector<uint8_t> m_data {};
98 };
99 
100 } // namespace ll
101 
102 #endif // LLUVIA_CORENODE__PUSH_CONSTANTS_H_
Definition: PushConstants.h:20
PushConstants(const PushConstants &)=default
void * getPtr() const noexcept
Definition: PushConstants.h:78
void setInt32(const int32_t &d)
Definition: PushConstants.h:93
size_t getSize() const noexcept
Definition: PushConstants.h:83
float getFloat() const
Definition: PushConstants.h:90
void pushFloat(const float &d)
Definition: PushConstants.h:88
PushConstants(PushConstants &&)=default
PushConstants()=default
void setFloat(const float &d)
Definition: PushConstants.h:89
~PushConstants()=default
void set(T &&data)
Definition: PushConstants.h:33
int32_t getInt32() const
Definition: PushConstants.h:94
PushConstants & operator=(PushConstants &&)=default
T get() const
Definition: PushConstants.h:43
void push(T &&data)
Definition: PushConstants.h:60
PushConstants & operator=(const PushConstants &)=default
void pushInt32(const int32_t &d)
Definition: PushConstants.h:92
error related classes and methods.
Definition: Buffer.h:28
void throwSystemError(ll::ErrorCode errorCode, T &&msg)
Throws a std::system_error exception with error code and message.
Definition: error.h:173