BDE.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_BDE_H_
  36. #define _D_BDE_H_
  37. #include "common.h"
  38. #include <map>
  39. #include <string>
  40. #include <deque>
  41. #include "SharedHandle.h"
  42. #include "DlAbortEx.h"
  43. namespace aria2 {
  44. class BDE;
  45. class BDE {
  46. public:
  47. typedef std::map<std::string, BDE> Dict;
  48. typedef std::deque<BDE> List;
  49. typedef int64_t Integer;
  50. private:
  51. enum TYPE{
  52. TYPE_NONE,
  53. TYPE_INTEGER,
  54. TYPE_STRING,
  55. TYPE_DICT,
  56. TYPE_LIST,
  57. };
  58. class BObject {
  59. public:
  60. virtual ~BObject() {}
  61. ////////////////////////////////////////////////////////////////////////////
  62. // Integer Interface
  63. // Returns Integer.
  64. virtual Integer i() const
  65. {
  66. throw DL_ABORT_EX("Not Integer");
  67. }
  68. ////////////////////////////////////////////////////////////////////////////
  69. // String Interface
  70. // Returns std::string.
  71. virtual const std::string& s() const
  72. {
  73. throw DL_ABORT_EX("Not String");
  74. }
  75. // Returns std::string.data() casted to unsigned char*.
  76. // Use s().size() to get length.
  77. virtual const unsigned char* uc() const
  78. {
  79. throw DL_ABORT_EX("Not String");
  80. }
  81. ////////////////////////////////////////////////////////////////////////////
  82. // Dictionary Interface
  83. // Returns the reference to BDE object associated with given key.
  84. // If the key is not found, new pair with that key is created
  85. // using default values, which is then returned. In other words,
  86. // this is the same behavior of std::map's operator[].
  87. virtual BDE& operator[](const std::string& key)
  88. {
  89. throw DL_ABORT_EX("Not Dict");
  90. }
  91. // Returns true if the given key is found in dict.
  92. virtual bool containsKey(const std::string& key) const
  93. {
  94. throw DL_ABORT_EX("Not Dict");
  95. }
  96. // Removes specified key from dict.
  97. virtual void removeKey(const std::string& key)
  98. {
  99. throw DL_ABORT_EX("Not Dict");
  100. }
  101. // Returns a read/write iterator that points to the first pair in
  102. // the dict.
  103. virtual Dict::iterator dictBegin()
  104. {
  105. throw DL_ABORT_EX("Not Dict");
  106. }
  107. // Returns a read/write read-only iterator that points to one past
  108. // the last pair in the dict.
  109. virtual Dict::iterator dictEnd()
  110. {
  111. throw DL_ABORT_EX("Not Dict");
  112. }
  113. ////////////////////////////////////////////////////////////////////////////
  114. // List Interface
  115. // Appends given bde to list.
  116. virtual void append(const BDE& bde)
  117. {
  118. throw DL_ABORT_EX("Not List");
  119. }
  120. // Alias for append()
  121. virtual void operator<<(const BDE& bde)
  122. {
  123. throw DL_ABORT_EX("Not List");
  124. }
  125. // Returns the reference of the object at the given index.
  126. virtual BDE& operator[](size_t index)
  127. {
  128. throw DL_ABORT_EX("Not List");
  129. }
  130. // Returns a read/write iterator that points to the first object
  131. // in list.
  132. virtual List::iterator listBegin()
  133. {
  134. throw DL_ABORT_EX("Not List");
  135. }
  136. // Returns a read/write iterator that points to the one past the
  137. // last object in list.
  138. virtual List::iterator listEnd()
  139. {
  140. throw DL_ABORT_EX("Not List");
  141. }
  142. // Returns size of list or dict.
  143. virtual size_t size() const
  144. {
  145. throw DL_ABORT_EX("Neither Dict nor List");
  146. }
  147. // Returns true if size of list or dict is 0.
  148. virtual bool empty() const
  149. {
  150. throw DL_ABORT_EX("Neither Dict nor List");
  151. }
  152. };
  153. class BInteger : public BObject {
  154. private:
  155. Integer _integer;
  156. public:
  157. BInteger(Integer i):_integer(i) {}
  158. virtual BDE::Integer i() const
  159. {
  160. return _integer;
  161. }
  162. };
  163. class BString : public BObject {
  164. private:
  165. std::string _string;
  166. public:
  167. BString(const std::string& string):_string(string) {}
  168. virtual const std::string& s() const
  169. {
  170. return _string;
  171. }
  172. virtual const unsigned char* uc() const
  173. {
  174. return reinterpret_cast<const unsigned char*>(_string.data());
  175. }
  176. };
  177. class BDict : public BObject {
  178. private:
  179. Dict _dict;
  180. public:
  181. virtual BDE& operator[](const std::string& key)
  182. {
  183. return _dict[key];
  184. }
  185. virtual bool containsKey(const std::string& key) const
  186. {
  187. return _dict.find(key) != _dict.end();
  188. }
  189. virtual void removeKey(const std::string& key)
  190. {
  191. _dict.erase(key);
  192. }
  193. virtual BDE::Dict::iterator dictBegin()
  194. {
  195. return _dict.begin();
  196. }
  197. virtual BDE::Dict::iterator dictEnd()
  198. {
  199. return _dict.end();
  200. }
  201. virtual size_t size() const
  202. {
  203. return _dict.size();
  204. }
  205. virtual bool empty() const
  206. {
  207. return _dict.empty();
  208. }
  209. };
  210. class BList : public BObject {
  211. private:
  212. List _list;
  213. public:
  214. virtual void append(const BDE& bde)
  215. {
  216. _list.push_back(bde);
  217. }
  218. virtual void operator<<(const BDE& bde)
  219. {
  220. _list.push_back(bde);
  221. }
  222. virtual BDE& operator[](size_t index)
  223. {
  224. return _list[index];
  225. }
  226. virtual BDE::List::iterator listBegin()
  227. {
  228. return _list.begin();
  229. }
  230. virtual BDE::List::iterator listEnd()
  231. {
  232. return _list.end();
  233. }
  234. virtual size_t size() const
  235. {
  236. return _list.size();
  237. }
  238. virtual bool empty() const
  239. {
  240. return _list.empty();
  241. }
  242. };
  243. TYPE _type;
  244. SharedHandle<BObject> _bobject;
  245. public:
  246. BDE();
  247. static BDE dict();
  248. static BDE list();
  249. static const BDE none;
  250. // Test for Null data
  251. // Return true if the type of this object is None.
  252. bool isNone() const;
  253. //////////////////////////////////////////////////////////////////////////////
  254. // Integer Interface
  255. BDE(Integer integer);
  256. // Returns true if the type of this object is Integer.
  257. bool isInteger() const;
  258. // Returns Integer. Requires this object to be Integer.
  259. Integer i() const;
  260. //////////////////////////////////////////////////////////////////////////////
  261. // String Interface
  262. BDE(const std::string& string);
  263. // Made explicit to avoid ambiguity with BDE(Integer).
  264. explicit BDE(const char* cstring);
  265. BDE(const char* data, size_t length);
  266. BDE(const unsigned char* data, size_t length);
  267. // Returns true if the type of this object is String.
  268. bool isString() const;
  269. // Returns std::string. Requires this object to be String
  270. const std::string& s() const;
  271. // Returns std::string.data() casted to unsigned char*.
  272. // Use s().size() to get length.
  273. const unsigned char* uc() const;
  274. //////////////////////////////////////////////////////////////////////////////
  275. // Dictionary Interface
  276. // Returns true if the type of this object is Dict.
  277. bool isDict() const;
  278. // Returns the reference to BDE object associated with given key.
  279. // If the key is not found, new pair with that key is created using default
  280. // values, which is then returned. In other words, this is the same behavior
  281. // of std::map's operator[].
  282. // Requires this object to be Dict.
  283. BDE& operator[](const std::string& key);
  284. // Returns the const reference to BDE ojbect associated with given key.
  285. // If the key is not found, BDE::none is returned.
  286. // Requires this object to be Dict.
  287. const BDE& operator[](const std::string& key) const;
  288. // Returns true if the given key is found in dict.
  289. // Requires this object to be Dict.
  290. bool containsKey(const std::string& key) const;
  291. // Removes specified key from dict.
  292. // Requires this object to be Dict.
  293. void removeKey(const std::string& key);
  294. // Returns a read/write iterator that points to the first pair in the dict.
  295. // Requires this object to be Dict.
  296. Dict::iterator dictBegin();
  297. // Returns a read/write read-only iterator that points to the first pair in
  298. // the dict.
  299. // Requires this object to be Dict.
  300. Dict::const_iterator dictBegin() const;
  301. // Returns a read/write read-only iterator that points to one past the last
  302. // pair in the dict.
  303. // Requires this object to be Dict.
  304. Dict::iterator dictEnd();
  305. // Returns a read/write read-only iterator that points to one past the last
  306. // pair in the dict.
  307. // Requires this object to be Dict.
  308. Dict::const_iterator dictEnd() const;
  309. //////////////////////////////////////////////////////////////////////////////
  310. // List Interface
  311. // Returns true if the type of this object is List.
  312. bool isList() const;
  313. // Appends given bde to list. Required the type of this object to be List.
  314. void append(const BDE& bde);
  315. // Alias for append()
  316. void operator<<(const BDE& bde);
  317. // Returns the reference of the object at the given index. Required this
  318. // object to be List.
  319. BDE& operator[](size_t index);
  320. // Returns the const reference of the object at the given index.
  321. // Required this object to be List.
  322. const BDE& operator[](size_t index) const;
  323. // Returns a read/write iterator that points to the first object in list.
  324. // Required this object to be List.
  325. List::iterator listBegin();
  326. // Returns a read/write read-only iterator that points to the first object
  327. // in list. Required this object to be List.
  328. List::const_iterator listBegin() const;
  329. // Returns a read/write iterator that points to the one past the last object
  330. // in list. Required this object to be List.
  331. List::iterator listEnd();
  332. // Returns a read/write read-only iterator that points to the one past the
  333. // last object in list. Required this object to be List.
  334. List::const_iterator listEnd() const;
  335. // For List type: Returns size of list.
  336. // For Dict type: Returns size of dict.
  337. size_t size() const;
  338. // For List type: Returns true if size of list is 0.
  339. // For Dict type: Returns true if size of dict is 0.
  340. bool empty() const;
  341. };
  342. } // namespace aria2
  343. #endif // _D_BDE_H_