impl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #ifndef NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include "yaml-cpp/node/node.h"
  9. #include "yaml-cpp/node/iterator.h"
  10. #include "yaml-cpp/node/detail/memory.h"
  11. #include "yaml-cpp/node/detail/node.h"
  12. #include "yaml-cpp/exceptions.h"
  13. #include <string>
  14. namespace YAML {
  15. inline Node::Node() : m_isValid(true), m_pNode(NULL) {}
  16. inline Node::Node(NodeType::value type)
  17. : m_isValid(true),
  18. m_pMemory(new detail::memory_holder),
  19. m_pNode(&m_pMemory->create_node()) {
  20. m_pNode->set_type(type);
  21. }
  22. template <typename T>
  23. inline Node::Node(const T& rhs)
  24. : m_isValid(true),
  25. m_pMemory(new detail::memory_holder),
  26. m_pNode(&m_pMemory->create_node()) {
  27. Assign(rhs);
  28. }
  29. inline Node::Node(const detail::iterator_value& rhs)
  30. : m_isValid(rhs.m_isValid),
  31. m_pMemory(rhs.m_pMemory),
  32. m_pNode(rhs.m_pNode) {}
  33. inline Node::Node(const Node& rhs)
  34. : m_isValid(rhs.m_isValid),
  35. m_pMemory(rhs.m_pMemory),
  36. m_pNode(rhs.m_pNode) {}
  37. inline Node::Node(Zombie) : m_isValid(false), m_pNode(NULL) {}
  38. inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
  39. : m_isValid(true), m_pMemory(pMemory), m_pNode(&node) {}
  40. inline Node::~Node() {}
  41. inline void Node::EnsureNodeExists() const {
  42. if (!m_isValid)
  43. throw InvalidNode();
  44. if (!m_pNode) {
  45. m_pMemory.reset(new detail::memory_holder);
  46. m_pNode = &m_pMemory->create_node();
  47. m_pNode->set_null();
  48. }
  49. }
  50. inline bool Node::IsDefined() const {
  51. if (!m_isValid) {
  52. return false;
  53. }
  54. return m_pNode ? m_pNode->is_defined() : true;
  55. }
  56. inline Mark Node::Mark() const {
  57. if (!m_isValid) {
  58. throw InvalidNode();
  59. }
  60. return m_pNode ? m_pNode->mark() : Mark::null_mark();
  61. }
  62. inline NodeType::value Node::Type() const {
  63. if (!m_isValid)
  64. throw InvalidNode();
  65. return m_pNode ? m_pNode->type() : NodeType::Null;
  66. }
  67. // access
  68. // template helpers
  69. template <typename T, typename S>
  70. struct as_if {
  71. explicit as_if(const Node& node_) : node(node_) {}
  72. const Node& node;
  73. T operator()(const S& fallback) const {
  74. if (!node.m_pNode)
  75. return fallback;
  76. T t;
  77. if (convert<T>::decode(node, t))
  78. return t;
  79. return fallback;
  80. }
  81. };
  82. template <typename S>
  83. struct as_if<std::string, S> {
  84. explicit as_if(const Node& node_) : node(node_) {}
  85. const Node& node;
  86. std::string operator()(const S& fallback) const {
  87. if (node.Type() != NodeType::Scalar)
  88. return fallback;
  89. return node.Scalar();
  90. }
  91. };
  92. template <typename T>
  93. struct as_if<T, void> {
  94. explicit as_if(const Node& node_) : node(node_) {}
  95. const Node& node;
  96. T operator()() const {
  97. if (!node.m_pNode)
  98. throw TypedBadConversion<T>(node.Mark());
  99. T t;
  100. if (convert<T>::decode(node, t))
  101. return t;
  102. throw TypedBadConversion<T>(node.Mark());
  103. }
  104. };
  105. template <>
  106. struct as_if<std::string, void> {
  107. explicit as_if(const Node& node_) : node(node_) {}
  108. const Node& node;
  109. std::string operator()() const {
  110. if (node.Type() != NodeType::Scalar)
  111. throw TypedBadConversion<std::string>(node.Mark());
  112. return node.Scalar();
  113. }
  114. };
  115. // access functions
  116. template <typename T>
  117. inline T Node::as() const {
  118. if (!m_isValid)
  119. throw InvalidNode();
  120. return as_if<T, void>(*this)();
  121. }
  122. template <typename T, typename S>
  123. inline T Node::as(const S& fallback) const {
  124. if (!m_isValid)
  125. return fallback;
  126. return as_if<T, S>(*this)(fallback);
  127. }
  128. inline const std::string& Node::Scalar() const {
  129. if (!m_isValid)
  130. throw InvalidNode();
  131. return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar;
  132. }
  133. inline const std::string& Node::Tag() const {
  134. if (!m_isValid)
  135. throw InvalidNode();
  136. return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar;
  137. }
  138. inline void Node::SetTag(const std::string& tag) {
  139. if (!m_isValid)
  140. throw InvalidNode();
  141. EnsureNodeExists();
  142. m_pNode->set_tag(tag);
  143. }
  144. inline EmitterStyle::value Node::Style() const {
  145. if (!m_isValid)
  146. throw InvalidNode();
  147. return m_pNode ? m_pNode->style() : EmitterStyle::Default;
  148. }
  149. inline void Node::SetStyle(EmitterStyle::value style) {
  150. if (!m_isValid)
  151. throw InvalidNode();
  152. EnsureNodeExists();
  153. m_pNode->set_style(style);
  154. }
  155. // assignment
  156. inline bool Node::is(const Node& rhs) const {
  157. if (!m_isValid || !rhs.m_isValid)
  158. throw InvalidNode();
  159. if (!m_pNode || !rhs.m_pNode)
  160. return false;
  161. return m_pNode->is(*rhs.m_pNode);
  162. }
  163. template <typename T>
  164. inline Node& Node::operator=(const T& rhs) {
  165. if (!m_isValid)
  166. throw InvalidNode();
  167. Assign(rhs);
  168. return *this;
  169. }
  170. inline void Node::reset(const YAML::Node& rhs) {
  171. if (!m_isValid || !rhs.m_isValid)
  172. throw InvalidNode();
  173. m_pMemory = rhs.m_pMemory;
  174. m_pNode = rhs.m_pNode;
  175. }
  176. template <typename T>
  177. inline void Node::Assign(const T& rhs) {
  178. if (!m_isValid)
  179. throw InvalidNode();
  180. AssignData(convert<T>::encode(rhs));
  181. }
  182. template <>
  183. inline void Node::Assign(const std::string& rhs) {
  184. if (!m_isValid)
  185. throw InvalidNode();
  186. EnsureNodeExists();
  187. m_pNode->set_scalar(rhs);
  188. }
  189. inline void Node::Assign(const char* rhs) {
  190. if (!m_isValid)
  191. throw InvalidNode();
  192. EnsureNodeExists();
  193. m_pNode->set_scalar(rhs);
  194. }
  195. inline void Node::Assign(char* rhs) {
  196. if (!m_isValid)
  197. throw InvalidNode();
  198. EnsureNodeExists();
  199. m_pNode->set_scalar(rhs);
  200. }
  201. inline Node& Node::operator=(const Node& rhs) {
  202. if (!m_isValid || !rhs.m_isValid)
  203. throw InvalidNode();
  204. if (is(rhs))
  205. return *this;
  206. AssignNode(rhs);
  207. return *this;
  208. }
  209. inline void Node::AssignData(const Node& rhs) {
  210. if (!m_isValid || !rhs.m_isValid)
  211. throw InvalidNode();
  212. EnsureNodeExists();
  213. rhs.EnsureNodeExists();
  214. m_pNode->set_data(*rhs.m_pNode);
  215. m_pMemory->merge(*rhs.m_pMemory);
  216. }
  217. inline void Node::AssignNode(const Node& rhs) {
  218. if (!m_isValid || !rhs.m_isValid)
  219. throw InvalidNode();
  220. rhs.EnsureNodeExists();
  221. if (!m_pNode) {
  222. m_pNode = rhs.m_pNode;
  223. m_pMemory = rhs.m_pMemory;
  224. return;
  225. }
  226. m_pNode->set_ref(*rhs.m_pNode);
  227. m_pMemory->merge(*rhs.m_pMemory);
  228. m_pNode = rhs.m_pNode;
  229. }
  230. // size/iterator
  231. inline std::size_t Node::size() const {
  232. if (!m_isValid)
  233. throw InvalidNode();
  234. return m_pNode ? m_pNode->size() : 0;
  235. }
  236. inline const_iterator Node::begin() const {
  237. if (!m_isValid)
  238. return const_iterator();
  239. return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
  240. : const_iterator();
  241. }
  242. inline iterator Node::begin() {
  243. if (!m_isValid)
  244. return iterator();
  245. return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
  246. }
  247. inline const_iterator Node::end() const {
  248. if (!m_isValid)
  249. return const_iterator();
  250. return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
  251. }
  252. inline iterator Node::end() {
  253. if (!m_isValid)
  254. return iterator();
  255. return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
  256. }
  257. // sequence
  258. template <typename T>
  259. inline void Node::push_back(const T& rhs) {
  260. if (!m_isValid)
  261. throw InvalidNode();
  262. push_back(Node(rhs));
  263. }
  264. inline void Node::push_back(const Node& rhs) {
  265. if (!m_isValid || !rhs.m_isValid)
  266. throw InvalidNode();
  267. EnsureNodeExists();
  268. rhs.EnsureNodeExists();
  269. m_pNode->push_back(*rhs.m_pNode, m_pMemory);
  270. m_pMemory->merge(*rhs.m_pMemory);
  271. }
  272. // helpers for indexing
  273. namespace detail {
  274. template <typename T>
  275. struct to_value_t {
  276. explicit to_value_t(const T& t_) : t(t_) {}
  277. const T& t;
  278. typedef const T& return_type;
  279. const T& operator()() const { return t; }
  280. };
  281. template <>
  282. struct to_value_t<const char*> {
  283. explicit to_value_t(const char* t_) : t(t_) {}
  284. const char* t;
  285. typedef std::string return_type;
  286. const std::string operator()() const { return t; }
  287. };
  288. template <>
  289. struct to_value_t<char*> {
  290. explicit to_value_t(char* t_) : t(t_) {}
  291. const char* t;
  292. typedef std::string return_type;
  293. const std::string operator()() const { return t; }
  294. };
  295. template <std::size_t N>
  296. struct to_value_t<char[N]> {
  297. explicit to_value_t(const char* t_) : t(t_) {}
  298. const char* t;
  299. typedef std::string return_type;
  300. const std::string operator()() const { return t; }
  301. };
  302. // converts C-strings to std::strings so they can be copied
  303. template <typename T>
  304. inline typename to_value_t<T>::return_type to_value(const T& t) {
  305. return to_value_t<T>(t)();
  306. }
  307. }
  308. // indexing
  309. template <typename Key>
  310. inline const Node Node::operator[](const Key& key) const {
  311. if (!m_isValid)
  312. throw InvalidNode();
  313. EnsureNodeExists();
  314. detail::node* value = static_cast<const detail::node&>(*m_pNode)
  315. .get(detail::to_value(key), m_pMemory);
  316. if (!value) {
  317. return Node(ZombieNode);
  318. }
  319. return Node(*value, m_pMemory);
  320. }
  321. template <typename Key>
  322. inline Node Node::operator[](const Key& key) {
  323. if (!m_isValid)
  324. throw InvalidNode();
  325. EnsureNodeExists();
  326. detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory);
  327. return Node(value, m_pMemory);
  328. }
  329. template <typename Key>
  330. inline bool Node::remove(const Key& key) {
  331. if (!m_isValid)
  332. throw InvalidNode();
  333. EnsureNodeExists();
  334. return m_pNode->remove(detail::to_value(key), m_pMemory);
  335. }
  336. inline const Node Node::operator[](const Node& key) const {
  337. if (!m_isValid || !key.m_isValid)
  338. throw InvalidNode();
  339. EnsureNodeExists();
  340. key.EnsureNodeExists();
  341. m_pMemory->merge(*key.m_pMemory);
  342. detail::node* value =
  343. static_cast<const detail::node&>(*m_pNode).get(*key.m_pNode, m_pMemory);
  344. if (!value) {
  345. return Node(ZombieNode);
  346. }
  347. return Node(*value, m_pMemory);
  348. }
  349. inline Node Node::operator[](const Node& key) {
  350. if (!m_isValid || !key.m_isValid)
  351. throw InvalidNode();
  352. EnsureNodeExists();
  353. key.EnsureNodeExists();
  354. m_pMemory->merge(*key.m_pMemory);
  355. detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory);
  356. return Node(value, m_pMemory);
  357. }
  358. inline bool Node::remove(const Node& key) {
  359. if (!m_isValid || !key.m_isValid)
  360. throw InvalidNode();
  361. EnsureNodeExists();
  362. key.EnsureNodeExists();
  363. return m_pNode->remove(*key.m_pNode, m_pMemory);
  364. }
  365. // map
  366. template <typename Key, typename Value>
  367. inline void Node::force_insert(const Key& key, const Value& value) {
  368. if (!m_isValid)
  369. throw InvalidNode();
  370. EnsureNodeExists();
  371. m_pNode->force_insert(detail::to_value(key), detail::to_value(value),
  372. m_pMemory);
  373. }
  374. // free functions
  375. inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); }
  376. }
  377. #endif // NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66