Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
6.6 KiB

  1. #ifndef _SQUIRREL_OBJECT_H_
  2. #define _SQUIRREL_OBJECT_H_
  3. class SquirrelObject
  4. {
  5. friend class SquirrelVM;
  6. public:
  7. SquirrelObject();
  8. virtual ~SquirrelObject();
  9. SquirrelObject(SquirrelObject & o);
  10. SquirrelObject(HSQOBJECT o);
  11. template <typename _ty>
  12. SquirrelObject(const _ty & val) { sq_resetobject(&_o); Set((_ty &)val); } // Cast away const to avoid compiler SqPlus::Push() match issue.
  13. template <typename _ty>
  14. SquirrelObject(_ty & val) { sq_resetobject(&_o); Set(val); }
  15. template <typename _ty>
  16. SquirrelObject(_ty * val) { sq_resetobject(&_o); SetByValue(val); } // Set() would also be OK here. SetByValue() to save potential compiler overhead.
  17. SquirrelObject & operator =(const SquirrelObject &o);
  18. SquirrelObject & operator =(int n);
  19. void AttachToStackObject(int idx);
  20. void Reset(void); // Release (any) reference and reset _o.
  21. SquirrelObject Clone();
  22. BOOL SetValue(const SquirrelObject &key,const SquirrelObject &val);
  23. BOOL SetValue(INT key,const SquirrelObject &val);
  24. BOOL SetValue(INT key,bool b); // Compiler treats SQBool as INT.
  25. BOOL SetValue(INT key,INT n);
  26. BOOL SetValue(INT key,FLOAT f);
  27. BOOL SetValue(INT key,const SQChar *s);
  28. BOOL SetValue(const SQChar *key,const SquirrelObject &val);
  29. BOOL SetValue(const SQChar *key,bool b);
  30. BOOL SetValue(const SQChar *key,INT n);
  31. BOOL SetValue(const SQChar *key,FLOAT f);
  32. BOOL SetValue(const SQChar *key,const SQChar *s);
  33. BOOL SetUserPointer(const SQChar * key,SQUserPointer up);
  34. SQUserPointer GetUserPointer(const SQChar * key);
  35. BOOL SetUserPointer(INT key,SQUserPointer up);
  36. SQUserPointer GetUserPointer(INT key);
  37. BOOL NewUserData(const SQChar * key,INT size,SQUserPointer * typetag=0);
  38. BOOL GetUserData(const SQChar * key,SQUserPointer * data,SQUserPointer * typetag=0);
  39. BOOL RawGetUserData(const SQChar * key,SQUserPointer * data,SQUserPointer * typetag=0);
  40. // === BEGIN Arrays ===
  41. BOOL ArrayResize(INT newSize);
  42. BOOL ArrayExtend(INT amount);
  43. BOOL ArrayReverse(void);
  44. SquirrelObject ArrayPop(SQBool returnPoppedVal=SQTrue);
  45. void ArrayAppend(const SquirrelObject &o);
  46. template<typename T>
  47. BOOL ArrayAppend(T item);
  48. // === END Arrays ===
  49. BOOL SetInstanceUP(SQUserPointer up);
  50. BOOL IsNull() const;
  51. int IsNumeric() const;
  52. int Len() const;
  53. BOOL SetDelegate(SquirrelObject &obj);
  54. SquirrelObject GetDelegate();
  55. const SQChar* ToString();
  56. bool ToBool();
  57. SQInteger ToInteger();
  58. SQFloat ToFloat();
  59. SQUserPointer GetInstanceUP(SQUserPointer tag) const;
  60. SquirrelObject GetValue(const SQChar *key) const;
  61. BOOL Exists(const SQChar *key) const;
  62. FLOAT GetFloat(const SQChar *key) const;
  63. INT GetInt(const SQChar *key) const;
  64. const SQChar *GetString(const SQChar *key) const;
  65. bool GetBool(const SQChar *key) const;
  66. SquirrelObject GetValue(INT key) const;
  67. FLOAT GetFloat(INT key) const;
  68. INT GetInt(INT key) const;
  69. const SQChar *GetString(INT key) const;
  70. bool GetBool(INT key) const;
  71. SquirrelObject GetAttributes(const SQChar *key = NULL);
  72. SQObjectType GetType();
  73. HSQOBJECT & GetObjectHandle(){return _o;}
  74. BOOL BeginIteration();
  75. BOOL Next(SquirrelObject &key,SquirrelObject &value);
  76. void EndIteration();
  77. BOOL GetTypeTag(SQUserPointer * typeTag);
  78. // === Get the type name of item/object through string key in a table or class. Returns NULL if the type name is not set (not an SqPlus registered type).
  79. const SQChar * GetTypeName(const SQChar * key);
  80. // === Get the type name of item/object through INT key in a table or class. Returns NULL if the type name is not set (not an SqPlus registered type).
  81. const SQChar * GetTypeName(INT key);
  82. // === Get the type name of this object, else return NULL if not an SqPlus registered type.
  83. const SQChar * GetTypeName(void);
  84. // === BEGIN code suggestion from the Wiki ===
  85. // Get any bound type from this SquirrelObject. Note that Squirrel's handling of references and pointers still holds here.
  86. template<typename _ty>
  87. inline _ty Get(void);
  88. // Set any bound type to this SquirrelObject. Note that Squirrel's handling of references and pointers still holds here.
  89. template<typename _ty>
  90. inline SquirrelObject SetByValue(_ty val);
  91. // Set any bound type to this SquirrelObject. Note that Squirrel's handling of references and pointers still holds here.
  92. template<typename _ty>
  93. SquirrelObject Set(_ty & val);
  94. // === END code suggestion from the Wiki ===
  95. private:
  96. BOOL GetSlot(const SQChar *name) const;
  97. BOOL RawGetSlot(const SQChar *name) const;
  98. BOOL GetSlot(INT key) const;
  99. HSQOBJECT _o;
  100. };
  101. struct StackHandler {
  102. StackHandler(HSQUIRRELVM v) {
  103. _top = sq_gettop(v);
  104. this->v = v;
  105. }
  106. SQFloat GetFloat(int idx) {
  107. SQFloat x = 0.0f;
  108. if(idx > 0 && idx <= _top) {
  109. sq_getfloat(v,idx,&x);
  110. }
  111. return x;
  112. }
  113. SQInteger GetInt(int idx) {
  114. SQInteger x = 0;
  115. if(idx > 0 && idx <= _top) {
  116. sq_getinteger(v,idx,&x);
  117. }
  118. return x;
  119. }
  120. HSQOBJECT GetObjectHandle(int idx) {
  121. HSQOBJECT x;
  122. if(idx > 0 && idx <= _top) {
  123. sq_resetobject(&x);
  124. sq_getstackobj(v,idx,&x);
  125. }
  126. else
  127. {
  128. x._type = OT_NULL;
  129. x._unVal.pUserPointer = 0;
  130. }
  131. return x;
  132. }
  133. const SQChar *GetString(int idx)
  134. {
  135. const SQChar *x = NULL;
  136. if(idx > 0 && idx <= _top) {
  137. sq_getstring(v,idx,&x);
  138. }
  139. return x;
  140. }
  141. SQUserPointer GetUserPointer(int idx)
  142. {
  143. SQUserPointer x = 0;
  144. if(idx > 0 && idx <= _top) {
  145. sq_getuserpointer(v,idx,&x);
  146. }
  147. return x;
  148. }
  149. SQUserPointer GetInstanceUp(int idx,SQUserPointer tag)
  150. {
  151. SQUserPointer self;
  152. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer*)&self,tag)))
  153. return NULL;
  154. return self;
  155. }
  156. SQUserPointer GetUserData(int idx,SQUserPointer tag=0)
  157. {
  158. SQUserPointer otag;
  159. SQUserPointer up;
  160. if(idx > 0 && idx <= _top) {
  161. if(SQ_SUCCEEDED(sq_getuserdata(v,idx,&up,&otag))) {
  162. if(tag == otag)
  163. return up;
  164. }
  165. }
  166. return NULL;
  167. }
  168. bool GetBool(int idx)
  169. {
  170. SQBool ret;
  171. if(idx > 0 && idx <= _top) {
  172. if(SQ_SUCCEEDED(sq_getbool(v,idx,&ret)))
  173. return !!ret;
  174. }
  175. return false;
  176. }
  177. int GetType(int idx)
  178. {
  179. if(idx > 0 && idx <= _top) {
  180. return sq_gettype(v,idx);
  181. }
  182. return -1;
  183. }
  184. int GetParamCount() {
  185. return _top;
  186. }
  187. int Return(const SQChar *s)
  188. {
  189. sq_pushstring(v,s,-1);
  190. return 1;
  191. }
  192. int Return(FLOAT f)
  193. {
  194. sq_pushfloat(v,f);
  195. return 1;
  196. }
  197. int Return(INT i)
  198. {
  199. sq_pushinteger(v,i);
  200. return 1;
  201. }
  202. int Return(bool b)
  203. {
  204. sq_pushbool(v,b);
  205. return 1;
  206. }
  207. int Return(SQUserPointer p) {
  208. sq_pushuserpointer(v,p);
  209. return 1;
  210. }
  211. int Return(SquirrelObject &o)
  212. {
  213. sq_pushobject(v,o.GetObjectHandle());
  214. return 1;
  215. }
  216. int Return() { return 0; }
  217. int ThrowError(const SQChar *error) {
  218. return sq_throwerror(v,error);
  219. }
  220. HSQUIRRELVM GetVMPtr() { return v; }
  221. private:
  222. int _top;
  223. HSQUIRRELVM v;
  224. };
  225. #endif //_SQUIRREL_OBJECT_H_