PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/CS/migrated/tags/PRE_CROSSBUILD_REMOVAL/plugins/aws2/object.h

#
C Header | 594 lines | 523 code | 29 blank | 42 comment | 0 complexity | eab638bf7fd945e1060cdc045a795eb3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 2005 by Christopher Nelson
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; if not, write to the Free
  13. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef _KEILA_OBJECT_H__
  16. #define _KEILA_OBJECT_H__
  17. #include "csutil/ref.h"
  18. #include "csutil/refcount.h"
  19. #include "iaws/aws2.h"
  20. #include <string>
  21. #include <vector>
  22. namespace aws
  23. {
  24. namespace autom
  25. {
  26. class object;
  27. class string;
  28. class integer;
  29. class floating;
  30. class list;
  31. class nil;
  32. /**
  33. * The reference counted pointer class that lets keila objects go away when
  34. * they're not needed anymore.
  35. */
  36. typedef csRef<object> keeper;
  37. /** Base class for all Keila objects. */
  38. class object : virtual public iObject
  39. {
  40. private:
  41. csString name;
  42. TYPE otype;
  43. public:
  44. SCF_DECLARE_IBASE;
  45. object(TYPE _otype);
  46. virtual ~object();
  47. object(const object& o, TYPE _otype):name(o.name), otype(_otype) {}
  48. /**
  49. * Returns the type of the object, a member of the object::TYPE
  50. * enumeration.
  51. */
  52. virtual TYPE ObjectType() { return otype; }
  53. /** Sets the name of the object. */
  54. virtual void SetName(iString* _name) { name=*_name; }
  55. /** Gets the name of the object. */
  56. virtual csRef<iString> GetName()
  57. { return csPtr<iString> (new scfString (name)); }
  58. /** Converts the object into a string object if possible. */
  59. virtual string ToString()=0;
  60. /** Converts the object into an integer object, if possible. */
  61. virtual integer ToInt()=0;
  62. /** Converts the object into a float object, if possible. */
  63. virtual floating ToFloat()=0;
  64. /**
  65. * Converts the object into the text representation of it. This is the
  66. * inverse of parsing.
  67. */
  68. virtual csRef<iString> ReprObject()=0;
  69. /**
  70. * Parses an object out of a string. The string is known to hold the
  71. * whole representation of some object.
  72. */
  73. virtual bool parseObject(std::string::iterator &pos,
  74. const std::string::iterator &end)=0;
  75. };
  76. /** Encapsulates a string object. */
  77. class string : public object
  78. {
  79. csString value;
  80. public:
  81. string():object(T_STRING) {}
  82. virtual ~string() {}
  83. string(const string& s):iObject(), object(s, T_STRING), value(s.value) {}
  84. string(const csString &s):object(T_STRING), value(s) {}
  85. string(const char *s):object(T_STRING), value(s) {}
  86. explicit string(object& o):object(o, T_STRING)
  87. {
  88. string s = o.ToString();
  89. value = s.value;
  90. }
  91. /** Returns a string containing the contents of this string object. */
  92. const csString& Value() { return value; }
  93. /** Returns a quoted string, with all double quotes properly escaped. */
  94. csString QuotedValue()
  95. {
  96. std::string temp, _value(value.GetData());
  97. temp+='"';
  98. for(std::string::iterator it=_value.begin(); it!=_value.end(); ++it)
  99. {
  100. if (*it=='"') temp+='\\';
  101. temp+=(*it);
  102. }
  103. temp+='"';
  104. return csString(temp.c_str());
  105. }
  106. /** Converts the object into a string object if possible. */
  107. virtual string ToString();
  108. /** Converts the object into an integer object, if possible. */
  109. virtual integer ToInt();
  110. /** Converts the object into a float object, if possible. */
  111. virtual floating ToFloat();
  112. /**
  113. * Converts the object into the text representation of it. This is the
  114. * inverse of parsing.
  115. */
  116. virtual csRef<iString> ReprObject()
  117. {
  118. return csPtr<iString> (new scfString(QuotedValue()));
  119. }
  120. /**
  121. * Parses an object out of a string. The string is known to hold the
  122. * whole representation of some object.
  123. */
  124. virtual bool parseObject(std::string::iterator &pos,
  125. const std::string::iterator &end);
  126. /** Concatenates the strings. */
  127. string operator+(const string& o)
  128. {
  129. csString tmp(value + o.value);
  130. return string(tmp);
  131. }
  132. };
  133. /** Encapsulates an integer value. */
  134. class integer : public object
  135. {
  136. longlong value;
  137. public:
  138. integer():object(T_INT) {}
  139. /** Copy constructor. */
  140. integer(const integer& i):iObject(), object(i, T_INT), value(i.value) {}
  141. integer(longlong i):object(T_INT), value(i) {}
  142. longlong Value() { return value; }
  143. explicit integer(object& o):object(o, T_INT)
  144. {
  145. integer i = o.ToInt();
  146. value=i.value;
  147. }
  148. /** Converts the object into a string object if possible. */
  149. virtual string ToString();
  150. /** Converts the object into an integer object, if possible. */
  151. virtual integer ToInt();
  152. /** Converts the object into a float object, if possible. */
  153. virtual floating ToFloat();
  154. /**
  155. * Converts the object into the text representation of it. This is the
  156. * inverse of parsing.
  157. */
  158. virtual csRef<iString> ReprObject();
  159. /**
  160. * Parses an object out of a string. The string is known to hold the
  161. * whole representation of some object.
  162. */
  163. virtual bool parseObject(std::string::iterator &pos,
  164. const std::string::iterator &end);
  165. /** Adds two integers together. */
  166. integer operator+(const integer &o)
  167. {
  168. return integer(value+o.value);
  169. }
  170. /** Subtracts two integers. */
  171. integer operator-(const integer &o)
  172. {
  173. return integer(value-o.value);
  174. }
  175. /** Multiplies two integers. */
  176. integer operator*(const integer &o)
  177. {
  178. return integer(value*o.value);
  179. }
  180. /** Divides two integers. */
  181. integer operator/(const integer &o)
  182. {
  183. return integer(value/o.value);
  184. }
  185. /** Modular division of two integers. */
  186. integer operator%(const integer &o)
  187. {
  188. return integer(value%o.value);
  189. }
  190. /** Less comparison of two integers. */
  191. integer operator<(const integer &o)
  192. {
  193. return integer(value<o.value);
  194. }
  195. /** Less comparison of two integers. */
  196. integer operator>(const integer &o)
  197. {
  198. return integer(value>o.value);
  199. }
  200. /** Equal comparison of two integers. */
  201. integer operator==(const integer &o)
  202. {
  203. return integer(value==o.value);
  204. }
  205. };
  206. /** Encapsulates a floating point value. */
  207. class floating : public object
  208. {
  209. double value;
  210. public:
  211. floating():object(T_FLOAT) {}
  212. /** Copy constructor. */
  213. floating(const floating& i):iObject(), object(i, T_FLOAT), value(i.value) {}
  214. floating(double i):object(T_FLOAT), value(i) {}
  215. double Value() { return value; }
  216. explicit floating(object& o):object(o)
  217. {
  218. floating f = o.ToFloat();
  219. value=f.value;
  220. }
  221. /** Converts the object into a string object if possible. */
  222. virtual string ToString();
  223. /** Converts the object into an floating object, if possible. */
  224. virtual integer ToInt();
  225. /** Converts the object into a float object, if possible. */
  226. virtual floating ToFloat();
  227. /**
  228. * Converts the object into the text representation of it. This is the
  229. * inverse of parsing.
  230. */
  231. virtual csRef<iString> ReprObject();
  232. /**
  233. * Parses an object out of a string. The string is known to hold the
  234. * whole representation of some object.
  235. */
  236. virtual bool parseObject(std::string::iterator &pos,
  237. const std::string::iterator &end);
  238. /** Adds two floatings together. */
  239. floating operator+(const floating &o)
  240. {
  241. return floating(value+o.value);
  242. }
  243. /** Subtracts two floatings. */
  244. floating operator-(const floating &o)
  245. {
  246. return floating(value-o.value);
  247. }
  248. /** Multiplies two floatings. */
  249. floating operator*(const floating &o)
  250. {
  251. return floating(value*o.value);
  252. }
  253. /** Divides two floatings. */
  254. floating operator/(const floating &o)
  255. {
  256. return floating(value/o.value);
  257. }
  258. };
  259. /** Encapsulates a list object. */
  260. class list : public object
  261. {
  262. typedef std::vector<keeper> list_type;
  263. /** The list of objects. */
  264. list_type value;
  265. public:
  266. list():object(T_LIST) {}
  267. virtual ~list() {}
  268. /** Copy constructor. */
  269. list(const list& s):iObject(), object(s, T_LIST)
  270. {
  271. value.insert(value.end(), s.value.begin(), s.value.end());
  272. }
  273. /**
  274. * Assumes that the string holds the text representation of a list, and
  275. * parses it.
  276. */
  277. list(std::string &s);
  278. /** Returns a reference to the list. */
  279. list_type &Value() { return value; }
  280. /** Converts the object into a string object if possible. */
  281. virtual string ToString();
  282. /** Converts the object into an integer object, if possible. */
  283. virtual integer ToInt();
  284. /** Converts the object into a float object, if possible. */
  285. virtual floating ToFloat();
  286. /**
  287. * Converts the object into the text representation of it. This is the
  288. * inverse of parsing.
  289. */
  290. virtual csRef<iString> ReprObject();
  291. /**
  292. * Parses an object out of a string. The string is known to hold the
  293. * whole representation of some object.
  294. */
  295. virtual bool parseObject(std::string::iterator &pos,
  296. const std::string::iterator &end);
  297. /** Concatenates the lists. */
  298. list operator+(const list& o)
  299. {
  300. list nl;
  301. nl.value.insert(nl.value.end(), value.begin(), value.end());
  302. nl.value.insert(nl.value.end(), o.value.begin(), o.value.end());
  303. return nl;
  304. }
  305. /** Appends an object to this list. */
  306. list operator+=(const keeper &k);
  307. /**
  308. * Returns a keeper to the object at index i of the list. If I is out of
  309. * bounds, then a keeper to NIL is returned.
  310. */
  311. keeper at(size_t i);
  312. /** Returns the size of the list. */
  313. size_t size() { return value.size(); }
  314. };
  315. class function;
  316. /** Encapsulates a reference object. */
  317. class reference : public object
  318. {
  319. csString value;
  320. function *fn;
  321. public:
  322. reference():object(T_REFERENCE), fn(0) {}
  323. virtual ~reference() {}
  324. reference(const reference& s):iObject(), object(s, T_REFERENCE),
  325. value(s.value), fn(s.fn) {}
  326. void setParent(function *_fn) { fn=_fn; }
  327. /** Returns a string containing the contents of this reference object. */
  328. const csString& Value() { return value; }
  329. /** Converts the object into a string object if possible. */
  330. virtual string ToString();
  331. /** Converts the object into an integer object, if possible. */
  332. virtual integer ToInt();
  333. /** Converts the object into a float object, if possible. */
  334. virtual floating ToFloat();
  335. /**
  336. * Converts the object into the text representation of it. This is the
  337. * inverse of parsing.
  338. */
  339. virtual csRef<iString> ReprObject();
  340. /**
  341. * Parses an object out of a string. The string is known to hold the
  342. * whole representation of some object.
  343. */
  344. virtual bool parseObject(std::string::iterator &pos,
  345. const std::string::iterator &end);
  346. };
  347. /** Encapsulates a nil object. */
  348. class nil: public object
  349. {
  350. public:
  351. nil():object(T_NIL) {}
  352. virtual ~nil() {}
  353. /** Copy constructor. */
  354. nil(const nil& s):iObject(), object(s, T_NIL) {}
  355. /** Converts the object into a string object if possible. */
  356. virtual string ToString();
  357. /** Converts the object into an integer object, if possible. */
  358. virtual integer ToInt();
  359. /** Converts the object into a float object, if possible. */
  360. virtual floating ToFloat();
  361. /**
  362. * Converts the object into the text representation of it. This is the
  363. * inverse of parsing.
  364. */
  365. virtual csRef<iString> ReprObject();
  366. /**
  367. * Parses an object out of a string. The string is known to hold the
  368. * whole representation of some object.
  369. */
  370. virtual bool parseObject(std::string::iterator &pos,
  371. const std::string::iterator &end);
  372. };
  373. /** Encapsulates a blob object. */
  374. class blob: public object
  375. {
  376. public:
  377. /** The return type for raw data. */
  378. typedef std::vector<unsigned char> raw_data_t;
  379. private:
  380. /** A string object that holds the encoded data. */
  381. std::string encoded;
  382. protected:
  383. /** Encodes a tuple into ascii85 format. */
  384. void encode_tuple(uint tuple, int count);
  385. /** Decodes a tuple from ascii85 into binary format. */
  386. void decode_tuple(uint tuple, int bytes, raw_data_t &output);
  387. public:
  388. blob():object(T_BLOB) {}
  389. virtual ~blob() {}
  390. /** Copy constructor. */
  391. blob(const blob& s): iObject(), object(s, T_BLOB), encoded(s.encoded)
  392. {
  393. }
  394. /** Encodes a data buffer into ascii85 format. */
  395. void encode(unsigned char *data, uint size);
  396. /** Decodes the encoded tuple into an output vector. */
  397. bool decode(raw_data_t &output);
  398. /** Converts the object into a string object if possible. */
  399. virtual string ToString();
  400. /** Converts the object into an integer object, if possible. */
  401. virtual integer ToInt();
  402. /** Converts the object into a float object, if possible. */
  403. virtual floating ToFloat();
  404. /**
  405. * Converts the object into the text representation of it. This is the
  406. * inverse of parsing.
  407. */
  408. virtual csRef<iString> ReprObject();
  409. /**
  410. * Parses an object out of a string. The string is known to hold the
  411. * whole representation of some object.
  412. */
  413. virtual bool parseObject(std::string::iterator &pos,
  414. const std::string::iterator &end);
  415. /** Returns a clone of this object. */
  416. //virtual keeper execObject();
  417. };
  418. // Predecleration of scope.
  419. class scope;
  420. /** Encapsulates a variable object. */
  421. class var: public object
  422. {
  423. /** The id of the variable. */
  424. uint id;
  425. /** The scope where the variable is found. */
  426. scope *sc;
  427. public:
  428. var():object(T_VAR) {}
  429. virtual ~var() {}
  430. /** Copy constructor. */
  431. var(const var& s):iObject(), object(s, T_VAR), id(0) {}
  432. /** Set the scope for this object. */
  433. void setScope(scope *_sc) { sc=_sc; }
  434. /** Converts the object into a string object if possible. */
  435. virtual string ToString();
  436. /** Converts the object into an integer object, if possible. */
  437. virtual integer ToInt();
  438. /** Converts the object into a float object, if possible. */
  439. virtual floating ToFloat();
  440. /** Converts the object into the text representation of it.
  441. * This is the inverse of parsing. */
  442. virtual csRef<iString> ReprObject();
  443. /** Parses an object out of a string. The string is known to
  444. * hold the whole representation of some object. */
  445. virtual bool parseObject(std::string::iterator &pos, const std::string::iterator &end);
  446. };
  447. } // namespace autom
  448. } // namespace aws
  449. #endif