impl.h 9.2 KB

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