Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Common.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <windows.h>
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <list>
8 #include <deque>
9 #include <memory>
10 
11 namespace Common
12 {
13  // Type of data stored by an Object
14  enum obj_type
15  {
16  TYPE_NIL = 0, // undefined type
23  };
24 
25 
26  // --------------------------------------------------------------------
27  // Class: Object
28  // Provides an interface between Lua and Phasor objects. The derived
29  // classes provide specific types.
30  class Object
31  {
32  private:
33  obj_type type;
34 
35  protected:
36 
37  // Should be called by derived classes to set the object type.
38  Object(obj_type type);
39  Object(const Object& other);
40 
41  public:
42  typedef std::unique_ptr<Object> unique_ptr;
43  typedef std::list<unique_ptr> unique_list;
44  typedef std::deque<unique_ptr> unique_deque;
45 
46  // Creates a nil object
47  Object();
48  virtual ~Object();
49 
50  //virtual Object& operator=(const Object &rhs);
51 
52  // Create a new, independent copy of this object.
53  virtual std::unique_ptr<Object> NewCopy() const;
54 
55  // Converts the object to the specified type, if possible.
56  // If no conversion is possible false is returned.
57  virtual bool ConvertTo(obj_type type, std::unique_ptr<Object>* out) const;
58 
59  // Returns the type of this object
60  obj_type GetType() const;
61  };
62 
63  // --------------------------------------------------------------------
64  // Class: ObjBool
65  // Wrapper for a boolean type
66  class ObjBool : public Object
67  {
68  private:
69  bool b;
70 
71  public:
72  ObjBool(bool b);
73  ~ObjBool();
74 
75  ObjBool & operator=(const ObjBool &rhs);
76  ObjBool(const ObjBool& other );
77 
78  // Create a new, independent copy of this object.
79  virtual std::unique_ptr<Object> NewCopy() const;
80 
81  // Converts the object to the specified type, if possible.
82  // If no conversion is possible false is returned.
83  virtual bool ConvertTo(obj_type type, std::unique_ptr<Object>* out) const;
84 
85  // Return the value stored in this object.
86  bool GetValue() const;
87  };
88 
89  // --------------------------------------------------------------------
90  // Class: ObjNumber
91  // Wrapper for a number type
92  class ObjNumber : public Object
93  {
94  private:
95  double value;
96 
97  public:
98  ObjNumber(int value);
99  ObjNumber(DWORD value);
100  ObjNumber(double value);
101  ObjNumber(float value);
102  ~ObjNumber();
103 
104  ObjNumber & operator=(const ObjNumber &rhs);
105  ObjNumber(const ObjNumber& other );
106 
107  // Create a new, independent copy of this object.
108  virtual std::unique_ptr<Object> NewCopy() const;
109 
110  // Converts the object to the specified type, if possible.
111  // If no conversion is possible false is returned.
112  virtual bool ConvertTo(obj_type type, std::unique_ptr<Object>* out) const;
113 
114  // Return the value stored in this object
115  double GetValue() const;
116  };
117 
118  // --------------------------------------------------------------------
119  // Class: ObjString
120  // Wrapper for a string type
121  template <class T, class _Tc>
122  class ObjStr : public Object
123  {
124  private:
125  T str;
126 
127  template <class StrType>
128  const bool ToNumber(double& out) const;
129 
130  template <>
131  const bool ToNumber<std::string>(double& out) const
132  {
133  char* end;
134  double value = strtod(str.c_str(), &end);
135  if (end == str.c_str()) return false;
136  out = value;
137  return true;
138  }
139 
140  template <>
141  const bool ToNumber<std::wstring>(double& out) const
142  {
143  wchar_t* end;
144  double value = wcstod(str.c_str(), &end);
145  if (end == str.c_str()) return false;
146  out = value;
147  return true;
148  }
149 
150  public:
151  ObjStr(const T& str) : Object(TYPE_STRING), str(str)
152  {
153  }
154 
156  {
157  str = other.str;
158  }
159 
160  // Create a new, independent copy of this object.
161  virtual std::unique_ptr<Object> NewCopy() const
162  {
163  return std::unique_ptr<Object>(new ObjStr<T, _Tc>(*this));
164  }
165 
166 
167  // Converts the object to the specified type, if possible.
168  // If no conversion is possible false is returned.
169  virtual bool ConvertTo(obj_type type, std::unique_ptr<Object>* out) const
170  {
171  bool success = true;
172  switch (type)
173  {
174  case TYPE_NUMBER:
175  {
176  double val;
177  success = ToNumber<T>(val);
178  if (success) out->reset(new ObjNumber(val));
179  } break;
180  case TYPE_BOOL:
181  {
182  double val;
183  success = ToNumber<T>(val);
184  if (success) {
185  if (val == 0 || val == 1) out->reset(new ObjBool(val == 1));
186  else success = false;
187  }
188  } break;
189  default:
190  {
191  success = false;
192  } break;
193  }
194  return success;
195  }
196 
197  // Return the value stored in this object
198  const _Tc* GetValue() const
199  {
200  return str.c_str();
201  }
202  };
203 
206 
207  class ObjBlob : public Object
208  {
209  private:
210  std::vector<BYTE> data;
211  public:
212  ObjBlob(BYTE* data, size_t size);
213  ObjBlob(const ObjBlob& other);
214 
215  BYTE* GetData(size_t& size);
216 
217  // Create a new, independent copy of this object.
218  virtual std::unique_ptr<Object> NewCopy() const;
219  };
220 
221  // --------------------------------------------------------------------
222  // Class: ObjTable
223  // Wrapper for an associative container
224  class ObjTable : public Object
225  {
226  public:
227  typedef std::pair<Object::unique_ptr, Object::unique_ptr> pair_t;
228  typedef std::map<Object::unique_ptr, Object::unique_ptr> table_t;
229  private:
230  table_t table;
231 
232  // Copies other into this object
233  void CopyTable(const ObjTable& other);
234 
235  // Frees the table associated with this object
236  void FreeTable();
237 
238  public:
239  ObjTable(const std::map<std::string, std::string>& table);
240  ObjTable(const std::map<std::string, std::unique_ptr<Object>>& table);
241  ObjTable(const std::vector<std::string>& data, size_t firstkey);
242  ObjTable();
243  ~ObjTable();
244 
245  ObjTable & operator=(const ObjTable &rhs);
246  ObjTable(const ObjTable& other );
247 
248  // Create a new, independent copy of this object.
249  virtual std::unique_ptr<Object> NewCopy() const;
250 
251  size_t size() const;
252  table_t::const_iterator begin() const;
253  table_t::const_iterator end() const;
254  void insert(pair_t pair);
255  // Get value at index
256  //const Object* operator [] (size_t i);
257  // Returns the value associated with the specified key
258  //const Object& operator [] (const Object& key);
259  };
260 
261  // --------------------------------------------------------------------
262  // Memory commands
263  BOOL WriteBytes(DWORD dwAddress, const LPVOID lpBuffer, DWORD dwCount);
264  BOOL WriteString(DWORD dwAddress, const char* str);
265  BOOL ReadBytes(DWORD dwAddress, LPVOID lpBuffer, DWORD dwCount);
266  std::vector<DWORD> FindSignatures(LPBYTE sigBuffer, LPBYTE sigWildCard,
267  DWORD sigSize, LPBYTE pBuffer, DWORD size);
268  bool FindSignature(LPBYTE sigBuffer, LPBYTE sigWildCard,
269  DWORD sigSize, LPBYTE pBuffer, DWORD size, DWORD occurance, DWORD& result);
270  BOOL CreateCodeCave(DWORD dwAddress, BYTE cbSize, VOID (*pFunction)());
271 }