Samchon Framework for CPP  1.0.0
ByteArray.hpp
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 
6 namespace samchon
7 {
29  class ByteArray
30  : public std::vector<unsigned char>
31  {
32  private:
33  typedef std::vector<unsigned char> super;
34 
42  size_t position;//{0};
43 
44  public:
45  /* --------------------------------------------------------------
46  CONSTRUCTROS
47  -------------------------------------------------------------- */
48  //using super::super;
49 
53  ByteArray() : super()
54  {
55  position = 0;
56  };
57 
61  ByteArray(const ByteArray &obj) : super(obj)
62  {
63  position = 0;
64  };
65 
69  ByteArray(ByteArray &&obj) : super(move(obj))
70  {
71  position = 0;
72  };
73 
74  auto operator=(const ByteArray &obj) -> ByteArray&
75  {
76  assign(obj.begin(), obj.end());
77  position = obj.position;
78 
79  return *this;
80  };
81 
82  auto operator=(ByteArray &&obj) -> ByteArray&
83  {
84  super::operator=(move(obj));
85  position = obj.position;
86 
87  return *this;
88  };
89 
90  /* --------------------------------------------------------------
91  POSITION
92  -------------------------------------------------------------- */
98  auto get_position() const -> size_t
99  {
100  return position;
101  };
102 
108  void set_position(size_t val)
109  {
110  position = val;
111  };
112 
113  auto left_size() const -> size_t
114  {
115  return this->size() - position;
116  };
117 
125  template <typename T> static auto reverse(const T &val) -> T
126  {
127  T res;
128  int size = sizeof(T);
129 
130  unsigned char *originalBytes = (unsigned char*)&val;
131  unsigned char *reversedBytes = (unsigned char*)&res;
132 
133  for (int i = 0; i < size; i++)
134  reversedBytes[i] = originalBytes[size - i - 1];
135 
136  return res;
137  };
138 
139  /* --------------------------------------------------------------
140  READ BYTES
141  -------------------------------------------------------------- */
150  template <typename T> auto read() const -> T
151  {
152  T *ptr = (T*)(data() + position);
153  ((ByteArray*)this)->position += sizeof(T);
154 
155  return *ptr;
156  };
157  template<> auto read() const -> std::string
158  {
159  if (position >= size())
160  return "";
161 
162  std::string str = (char*)(data() + position);
163  ((ByteArray*)this)->position = str.size();
164 
165  return str;
166  };
167 
181  template <typename T> auto readReversely() const -> T
182  {
183  T val = read<T>();
184  return reverse(val);
185  }
186 
187  /* --------------------------------------------------------------
188  WRITE BYTES
189  -------------------------------------------------------------- */
197  template <typename T> void write(const T &val)
198  {
199  unsigned char *ptr = (unsigned char*)&val;
200  insert(end(), ptr, ptr + sizeof(T));
201  };
202  template<> void write(const std::string &str)
203  {
204  unsigned char *begin = (unsigned char*)str.data();
205  insert(end(), begin, begin + str.size());
206  };
207  template<> void write(const ByteArray &byte_array)
208  {
209  insert(end(), byte_array.begin(), byte_array.end());
210  };
211 
222  template<typename T> void writeReversely(const T &val)
223  {
224  write(reverse(val));
225  };
226 
227  /* --------------------------------------------------------------
228  COMPRESS & DECOMPRESS
229  -------------------------------------------------------------- */
239  auto compress() const -> ByteArray
240  {
241  return{};
242  };
243 
253  auto decompress() const -> ByteArray
254  {
255  return{};
256  };
257  };
258 };
size_t position
Current position of the ByteArray. .
Definition: ByteArray.hpp:42
ByteArray()
Default Constructor.
Definition: ByteArray.hpp:53
void set_position(size_t val)
Set poisition.
Definition: ByteArray.hpp:108
ByteArray(const ByteArray &obj)
Copy Constructor.
Definition: ByteArray.hpp:61
static auto reverse(const T &val) -> T
Reverse byte ordering.
Definition: ByteArray.hpp:125
auto read() const -> T
Read data.
Definition: ByteArray.hpp:150
void writeReversely(const T &val)
Write a data.
Definition: ByteArray.hpp:222
ByteArray(ByteArray &&obj)
Move Constructor.
Definition: ByteArray.hpp:69
Binary data class.
Definition: ByteArray.hpp:29
auto decompress() const -> ByteArray
Decompress the binary data.
Definition: ByteArray.hpp:253
auto compress() const -> ByteArray
Compress the binary data .
Definition: ByteArray.hpp:239
auto get_position() const -> size_t
Get position.
Definition: ByteArray.hpp:98
auto readReversely() const -> T
Read a reversed data.
Definition: ByteArray.hpp:181
void write(const T &val)
Write a data.
Definition: ByteArray.hpp:197