Struct.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. //
  2. // Struct.h
  3. //
  4. // $Id: //poco/Main/Foundation/include/Poco/Dynamic/Struct.h#9 $
  5. //
  6. // Library: Foundation
  7. // Package: Dynamic
  8. // Module: Struct
  9. //
  10. // Definition of the Struct class.
  11. //
  12. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Struct_INCLUDED
  18. #define Foundation_Struct_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Dynamic/Var.h"
  21. #include "Poco/Dynamic/VarHolder.h"
  22. #include "Poco/SharedPtr.h"
  23. #include <map>
  24. #include <set>
  25. namespace Poco {
  26. namespace Dynamic {
  27. template <typename K>
  28. class Struct
  29. /// Struct allows to define a named collection of Var objects.
  30. {
  31. public:
  32. typedef typename std::map<K, Var> Data;
  33. typedef typename std::set<K> NameSet;
  34. typedef typename Data::iterator Iterator;
  35. typedef typename Data::const_iterator ConstIterator;
  36. typedef typename Struct<K>::Data::value_type ValueType;
  37. typedef typename Struct<K>::Data::size_type SizeType;
  38. typedef typename std::pair<typename Struct<K>::Iterator, bool> InsRetVal;
  39. typedef typename Poco::SharedPtr<Struct<K> > Ptr;
  40. Struct(): _data()
  41. /// Creates an empty Struct
  42. {
  43. }
  44. Struct(const Data& val): _data(val)
  45. /// Creates the Struct from the given value.
  46. {
  47. }
  48. template <typename T>
  49. Struct(const std::map<K, T>& val)
  50. {
  51. typedef typename std::map<K, T>::const_iterator MapConstIterator;
  52. MapConstIterator it = val.begin();
  53. MapConstIterator end = val.end();
  54. for (; it != end; ++it) _data.insert(ValueType(it->first, Var(it->second)));
  55. }
  56. virtual ~Struct()
  57. /// Destroys the Struct.
  58. {
  59. }
  60. inline Var& operator [] (const K& name)
  61. /// Returns the Var with the given name, creates an entry if not found.
  62. {
  63. return _data[name];
  64. }
  65. const Var& operator [] (const K& name) const
  66. /// Returns the Var with the given name, throws a
  67. /// NotFoundException if the data member is not found.
  68. {
  69. ConstIterator it = find(name);
  70. if (it == end()) throw NotFoundException(name);
  71. return it->second;
  72. }
  73. inline bool contains(const K& name) const
  74. /// Returns true if the Struct contains a member with the given name
  75. {
  76. return find(name) != end();
  77. }
  78. inline Iterator find(const K& name)
  79. /// Returns an iterator, pointing to the <name,Var> pair containing
  80. /// the element, or it returns end() if the member was not found
  81. {
  82. return _data.find(name);
  83. }
  84. inline ConstIterator find(const K& name) const
  85. /// Returns a const iterator, pointing to the <name,Var> pair containing
  86. /// the element, or it returns end() if the member was not found
  87. {
  88. return _data.find(name);
  89. }
  90. inline Iterator end()
  91. /// Returns the end iterator for the Struct
  92. {
  93. return _data.end();
  94. }
  95. inline ConstIterator end() const
  96. /// Returns the end const iterator for the Struct
  97. {
  98. return _data.end();
  99. }
  100. inline Iterator begin()
  101. /// Returns the begin iterator for the Struct
  102. {
  103. return _data.begin();
  104. }
  105. inline ConstIterator begin() const
  106. /// Returns the begin const iterator for the Struct
  107. {
  108. return _data.begin();
  109. }
  110. template <typename T>
  111. inline InsRetVal insert(const K& key, const T& value)
  112. /// Inserts a <name, Var> pair into the Struct,
  113. /// returns a pair containing the iterator and a boolean which
  114. /// indicates success or not (is true, when insert succeeded, false,
  115. /// when already another element was present, in this case Iterator
  116. /// points to that other element)
  117. {
  118. // fix: SunPro C++ is silly ...
  119. ValueType valueType(key, value);
  120. return insert(valueType);
  121. }
  122. inline InsRetVal insert(const ValueType& aPair)
  123. /// Inserts a <name, Var> pair into the Struct,
  124. /// returns a pair containing the iterator and a boolean which
  125. /// indicates success or not (is true, when insert succeeded, false,
  126. /// when already another element was present, in this case Iterator
  127. /// points to that other element)
  128. {
  129. return _data.insert(aPair);
  130. }
  131. inline SizeType erase(const K& key)
  132. /// Erases the element if found, returns number of elements deleted
  133. {
  134. return _data.erase(key);
  135. }
  136. inline void erase(Iterator& it)
  137. /// Erases the element at the given position
  138. {
  139. _data.erase(it);
  140. }
  141. inline bool empty() const
  142. /// Returns true if the Struct doesn't contain any members
  143. {
  144. return _data.empty();
  145. }
  146. SizeType size() const
  147. /// Returns the number of members the Struct contains
  148. {
  149. return _data.size();
  150. }
  151. inline NameSet members() const
  152. /// Returns a sorted collection containing all member names
  153. {
  154. NameSet keys;
  155. ConstIterator it = begin();
  156. ConstIterator itEnd = end();
  157. for (; it != itEnd; ++it) keys.insert(it->first);
  158. return keys;
  159. }
  160. std::string toString()
  161. {
  162. std::string str;
  163. Var(*this).convert<std::string>(str);
  164. return str;
  165. }
  166. private:
  167. Data _data;
  168. };
  169. template <>
  170. class VarHolderImpl<Struct<std::string> >: public VarHolder
  171. {
  172. public:
  173. VarHolderImpl(const Struct<std::string>& val): _val(val)
  174. {
  175. }
  176. ~VarHolderImpl()
  177. {
  178. }
  179. const std::type_info& type() const
  180. {
  181. return typeid(Struct<std::string>);
  182. }
  183. void convert(Int8&) const
  184. {
  185. throw BadCastException("Cannot cast Struct type to Int8");
  186. }
  187. void convert(Int16&) const
  188. {
  189. throw BadCastException("Cannot cast Struct type to Int16");
  190. }
  191. void convert(Int32&) const
  192. {
  193. throw BadCastException("Cannot cast Struct type to Int32");
  194. }
  195. void convert(Int64&) const
  196. {
  197. throw BadCastException("Cannot cast Struct type to Int64");
  198. }
  199. void convert(UInt8&) const
  200. {
  201. throw BadCastException("Cannot cast Struct type to UInt8");
  202. }
  203. void convert(UInt16&) const
  204. {
  205. throw BadCastException("Cannot cast Struct type to UInt16");
  206. }
  207. void convert(UInt32&) const
  208. {
  209. throw BadCastException("Cannot cast Struct type to UInt32");
  210. }
  211. void convert(UInt64&) const
  212. {
  213. throw BadCastException("Cannot cast Struct type to UInt64");
  214. }
  215. void convert(bool&) const
  216. {
  217. throw BadCastException("Cannot cast Struct type to bool");
  218. }
  219. void convert(float&) const
  220. {
  221. throw BadCastException("Cannot cast Struct type to float");
  222. }
  223. void convert(double&) const
  224. {
  225. throw BadCastException("Cannot cast Struct type to double");
  226. }
  227. void convert(char&) const
  228. {
  229. throw BadCastException("Cannot cast Struct type to char");
  230. }
  231. void convert(std::string& val) const
  232. {
  233. val.append("{ ");
  234. Struct<std::string>::ConstIterator it = _val.begin();
  235. Struct<std::string>::ConstIterator itEnd = _val.end();
  236. if (!_val.empty())
  237. {
  238. Var key(it->first);
  239. Impl::appendJSONKey(val, key);
  240. val.append(" : ");
  241. Impl::appendJSONValue(val, it->second);
  242. ++it;
  243. }
  244. for (; it != itEnd; ++it)
  245. {
  246. val.append(", ");
  247. Var key(it->first);
  248. Impl::appendJSONKey(val, key);
  249. val.append(" : ");
  250. Impl::appendJSONValue(val, it->second);
  251. }
  252. val.append(" }");
  253. }
  254. void convert(Poco::DateTime&) const
  255. {
  256. throw BadCastException("Struct -> Poco::DateTime");
  257. }
  258. void convert(Poco::LocalDateTime&) const
  259. {
  260. throw BadCastException("Struct -> Poco::LocalDateTime");
  261. }
  262. void convert(Poco::Timestamp&) const
  263. {
  264. throw BadCastException("Struct -> Poco::Timestamp");
  265. }
  266. VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
  267. {
  268. return cloneHolder(pVarHolder, _val);
  269. }
  270. const Struct<std::string>& value() const
  271. {
  272. return _val;
  273. }
  274. bool isArray() const
  275. {
  276. return false;
  277. }
  278. bool isStruct() const
  279. {
  280. return true;
  281. }
  282. bool isInteger() const
  283. {
  284. return false;
  285. }
  286. bool isSigned() const
  287. {
  288. return false;
  289. }
  290. bool isNumeric() const
  291. {
  292. return false;
  293. }
  294. bool isString() const
  295. {
  296. return false;
  297. }
  298. std::size_t size() const
  299. {
  300. return _val.size();
  301. }
  302. Var& operator [] (const std::string& name)
  303. {
  304. return _val[name];
  305. }
  306. const Var& operator [] (const std::string& name) const
  307. {
  308. return _val[name];
  309. }
  310. private:
  311. Struct<std::string> _val;
  312. };
  313. template <>
  314. class VarHolderImpl<Struct<int> >: public VarHolder
  315. {
  316. public:
  317. VarHolderImpl(const Struct<int>& val): _val(val)
  318. {
  319. }
  320. ~VarHolderImpl()
  321. {
  322. }
  323. const std::type_info& type() const
  324. {
  325. return typeid(Struct<int>);
  326. }
  327. void convert(Int8&) const
  328. {
  329. throw BadCastException("Cannot cast Struct type to Int8");
  330. }
  331. void convert(Int16&) const
  332. {
  333. throw BadCastException("Cannot cast Struct type to Int16");
  334. }
  335. void convert(Int32&) const
  336. {
  337. throw BadCastException("Cannot cast Struct type to Int32");
  338. }
  339. void convert(Int64&) const
  340. {
  341. throw BadCastException("Cannot cast Struct type to Int64");
  342. }
  343. void convert(UInt8&) const
  344. {
  345. throw BadCastException("Cannot cast Struct type to UInt8");
  346. }
  347. void convert(UInt16&) const
  348. {
  349. throw BadCastException("Cannot cast Struct type to UInt16");
  350. }
  351. void convert(UInt32&) const
  352. {
  353. throw BadCastException("Cannot cast Struct type to UInt32");
  354. }
  355. void convert(UInt64&) const
  356. {
  357. throw BadCastException("Cannot cast Struct type to UInt64");
  358. }
  359. void convert(bool&) const
  360. {
  361. throw BadCastException("Cannot cast Struct type to bool");
  362. }
  363. void convert(float&) const
  364. {
  365. throw BadCastException("Cannot cast Struct type to float");
  366. }
  367. void convert(double&) const
  368. {
  369. throw BadCastException("Cannot cast Struct type to double");
  370. }
  371. void convert(char&) const
  372. {
  373. throw BadCastException("Cannot cast Struct type to char");
  374. }
  375. void convert(std::string& val) const
  376. {
  377. val.append("{ ");
  378. Struct<int>::ConstIterator it = _val.begin();
  379. Struct<int>::ConstIterator itEnd = _val.end();
  380. if (!_val.empty())
  381. {
  382. Var key(it->first);
  383. Impl::appendJSONKey(val, key);
  384. val.append(" : ");
  385. Impl::appendJSONValue(val, it->second);
  386. ++it;
  387. }
  388. for (; it != itEnd; ++it)
  389. {
  390. val.append(", ");
  391. Var key(it->first);
  392. Impl::appendJSONKey(val, key);
  393. val.append(" : ");
  394. Impl::appendJSONValue(val, it->second);
  395. }
  396. val.append(" }");
  397. }
  398. void convert(Poco::DateTime&) const
  399. {
  400. throw BadCastException("Struct -> Poco::DateTime");
  401. }
  402. void convert(Poco::LocalDateTime&) const
  403. {
  404. throw BadCastException("Struct -> Poco::LocalDateTime");
  405. }
  406. void convert(Poco::Timestamp&) const
  407. {
  408. throw BadCastException("Struct -> Poco::Timestamp");
  409. }
  410. VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
  411. {
  412. return cloneHolder(pVarHolder, _val);
  413. }
  414. const Struct<int>& value() const
  415. {
  416. return _val;
  417. }
  418. bool isArray() const
  419. {
  420. return false;
  421. }
  422. bool isStruct() const
  423. {
  424. return true;
  425. }
  426. bool isInteger() const
  427. {
  428. return false;
  429. }
  430. bool isSigned() const
  431. {
  432. return false;
  433. }
  434. bool isNumeric() const
  435. {
  436. return false;
  437. }
  438. bool isString() const
  439. {
  440. return false;
  441. }
  442. std::size_t size() const
  443. {
  444. return _val.size();
  445. }
  446. Var& operator [] (int name)
  447. {
  448. return _val[name];
  449. }
  450. const Var& operator [] (int name) const
  451. {
  452. return _val[name];
  453. }
  454. private:
  455. Struct<int> _val;
  456. };
  457. } // namespace Dynamic
  458. typedef Dynamic::Struct<std::string> DynamicStruct;
  459. } // namespace Poco
  460. #endif // Foundation_Struct_INCLUDED