Samchon Framework for CPP  1.0.0
ByteArray.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <vector>
5 #include <string>
6 
7 namespace samchon
8 {
30  class SAMCHON_FRAMEWORK_API ByteArray
31  : public std::vector<unsigned char>
32  {
33  private:
34  typedef std::vector<unsigned char> super;
35 
43  size_t position;//{0};
44 
45  public:
46  /* --------------------------------------------------------------
47  CONSTRUCTROS
48  -------------------------------------------------------------- */
49  //using super::super;
50 
54  ByteArray();
55 
59  ByteArray(const ByteArray &);
60 
64  ByteArray(ByteArray &&);
65 
66  auto operator=(const ByteArray &) -> ByteArray&;
67 
68  auto operator=(ByteArray &&) -> ByteArray&;
69 
70  /* --------------------------------------------------------------
71  POSITION
72  -------------------------------------------------------------- */
78  auto getPosition() const -> size_t;
79 
85  void setPosition(size_t);
86 
87  auto leftSize() const -> size_t;
88 
96  template <typename T> static auto reverse(const T &val) -> T
97  {
98  T res;
99  int size = sizeof(T);
100 
101  unsigned char *originalBytes = (unsigned char*)&val;
102  unsigned char *reversedBytes = (unsigned char*)&res;
103 
104  for (int i = 0; i < size; i++)
105  reversedBytes[i] = originalBytes[size - i - 1];
106 
107  return res;
108  };
109 
110  /* --------------------------------------------------------------
111  READ BYTES
112  -------------------------------------------------------------- */
121  template <typename T> auto read() const -> T
122  {
123  T *ptr = (T*)(data() + position);
124  ((ByteArray*)this)->position += sizeof(T);
125 
126  return *ptr;
127  };
128  template<> auto read() const -> std::string
129  {
130  if (position >= size())
131  return "";
132 
133  std::string str = (char*)(data() + position);
134  ((ByteArray*)this)->position = str.size();
135 
136  return str;
137  };
138 
152  template <typename T> auto readReversely() const -> T
153  {
154  T val = read<T>();
155  return reverse(val);
156  }
157 
158  /* --------------------------------------------------------------
159  WRITE BYTES
160  -------------------------------------------------------------- */
168  template <typename T> void write(const T &val)
169  {
170  unsigned char *ptr = (unsigned char*)&val;
171  insert(end(), ptr, ptr + sizeof(T));
172  };
173  template<> void write(const std::string &str)
174  {
175  unsigned char *begin = (unsigned char*)str.data();
176  insert(end(), begin, begin + str.size());
177  };
178  template<> void write(const ByteArray &byteArray)
179  {
180  insert(end(), byteArray.begin(), byteArray.end());
181  };
182 
193  template<typename T> void writeReversely(const T &val)
194  {
195  write(reverse(val));
196  };
197 
198  /* --------------------------------------------------------------
199  COMPRESS & DECOMPRESS
200  -------------------------------------------------------------- */
210  auto compress() const -> ByteArray;
211 
221  auto decompress() const -> ByteArray;
222  };
223 };
size_t position
Current position of the ByteArray. .
Definition: ByteArray.hpp:43
auto read() const -> T
Read data.
Definition: ByteArray.hpp:121
void writeReversely(const T &val)
Write a data.
Definition: ByteArray.hpp:193
Binary data class.
Definition: ByteArray.hpp:30
auto readReversely() const -> T
Read a reversed data.
Definition: ByteArray.hpp:152
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
void write(const T &val)
Write a data.
Definition: ByteArray.hpp:168