Struct.h 10 KB

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