Recast  1
Game with custom magic
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Parcel.hpp
Go to the documentation of this file.
1 
13 #ifndef RECAST_SERIALIZABLE_H
14 #define RECAST_SERIALIZABLE_H
15 
16 
17 #include <vector>
18 #include <string>
19 
20 template<class T>
21 void inline setByte(char byte, int number, T *var) {
22  *(char *) (((void *) var) + number) = byte;
23 }
24 
25 template<class T>
26 char inline getByte(T var, int number) {
27  return (char) ((var >> ((sizeof(T) - number - 1) * 8)) & 255);
28 }
29 
35 class Parcel {
36 public:
37  Parcel();
38 
39  Parcel(std::vector<char> vector) : data(vector) {}
40 
41  Parcel(Parcel &other) = delete;
42 
43  void putString(std::string var);
44 
45  template<class T>
46  void put(T var) {
47  unsigned char const *p = reinterpret_cast<unsigned char const *>(&var);
48  for (int i = 0; i < sizeof(T); i++)
49  data.push_back((char &&) p[i]);
50  }
51 
52  int readInt();
53 
54  std::string readString();
55 
56  float readFloat();
57 
58  const std::vector<char> *getVector() const { return &data; }
59 
60 private:
61  int curPos;
62  std::vector<char> data;
63 };
64 
65 
66 #endif //RECAST_SERIALIZABLE_H
char getByte(T var, int number)
Definition: Parcel.hpp:26
void setByte(char byte, int number, T *var)
Definition: Parcel.hpp:21
std::string readString()
Definition: Parcel.cpp:33
int readInt()
Definition: Parcel.cpp:25
Parcel(std::vector< char > vector)
Definition: Parcel.hpp:39
Parcel()
Definition: Parcel.cpp:21
void putString(std::string var)
Definition: Parcel.cpp:50
void put(T var)
Definition: Parcel.hpp:46
Definition: Parcel.hpp:35
float readFloat()
Definition: Parcel.cpp:42
const std::vector< char > * getVector() const
Definition: Parcel.hpp:58