impl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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::Scalar)
  93. return fallback;
  94. return node.Scalar();
  95. }
  96. };
  97. template <typename T>
  98. struct as_if<T, void> {
  99. explicit as_if(const Node& node_) : node(node_) {}
  100. const Node& node;
  101. T operator()() const {
  102. if (!node.m_pNode)
  103. throw TypedBadConversion<T>(node.Mark());
  104. T t;
  105. if (convert<T>::decode(node, t))
  106. return t;
  107. throw TypedBadConversion<T>(node.Mark());
  108. }
  109. };
  110. template <>
  111. struct as_if<std::string, void> {
  112. explicit as_if(const Node& node_) : node(node_) {}
  113. const Node& node;
  114. std::string operator()() const {
  115. if (node.Type() != NodeType::Scalar)
  116. throw TypedBadConversion<std::string>(node.Mark());
  117. return node.Scalar();
  118. }
  119. };
  120. // access functions
  121. template <typename T>
  122. inline T Node::as() const {
  123. if (!m_isValid)
  124. throw InvalidNode(m_invalidKey);
  125. return as_if<T, void>(*this)();
  126. }
  127. template <typename T, typename S>
  128. inline T Node::as(const S& fallback) const {
  129. if (!m_isValid)
  130. return fallback;
  131. return as_if<T, S>(*this)(fallback);
  132. }
  133. inline const std::string& Node::Scalar() const {
  134. if (!m_isValid)
  135. throw InvalidNode(m_invalidKey);
  136. return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
  137. }
  138. inline const std::string& Node::Tag() const {
  139. if (!m_isValid)
  140. throw InvalidNode(m_invalidKey);
  141. return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
  142. }
  143. inline void Node::SetTag(const std::string& tag) {
  144. EnsureNodeExists();
  145. m_pNode->set_tag(tag);
  146. }
  147. inline EmitterStyle::value Node::Style() const {
  148. if (!m_isValid)
  149. throw InvalidNode(m_invalidKey);
  150. return m_pNode ? m_pNode->style() : EmitterStyle::Default;
  151. }
  152. inline void Node::SetStyle(EmitterStyle::value style) {
  153. EnsureNodeExists();
  154. m_pNode->set_style(style);
  155. }
  156. // assignment
  157. inline bool Node::is(const Node& rhs) const {
  158. if (!m_isValid || !rhs.m_isValid)
  159. throw InvalidNode(m_invalidKey);
  160. if (!m_pNode || !rhs.m_pNode)
  161. return false;
  162. return m_pNode->is(*rhs.m_pNode);
  163. }
  164. template <typename T>
  165. inline Node& Node::operator=(const T& rhs) {
  166. Assign(rhs);
  167. return *this;
  168. }
  169. inline Node& Node::operator=(const Node& rhs) {
  170. if (is(rhs))
  171. return *this;
  172. AssignNode(rhs);
  173. return *this;
  174. }
  175. inline void Node::reset(const YAML::Node& rhs) {
  176. if (!m_isValid || !rhs.m_isValid)
  177. throw InvalidNode(m_invalidKey);
  178. m_pMemory = rhs.m_pMemory;
  179. m_pNode = rhs.m_pNode;
  180. }
  181. template <typename T>
  182. inline void Node::Assign(const T& rhs) {
  183. if (!m_isValid)
  184. throw InvalidNode(m_invalidKey);
  185. AssignData(convert<T>::encode(rhs));
  186. }
  187. template <>
  188. inline void Node::Assign(const std::string& rhs) {
  189. EnsureNodeExists();
  190. m_pNode->set_scalar(rhs);
  191. }
  192. inline void Node::Assign(const char* rhs) {
  193. EnsureNodeExists();
  194. m_pNode->set_scalar(rhs);
  195. }
  196. inline void Node::Assign(char* rhs) {
  197. EnsureNodeExists();
  198. m_pNode->set_scalar(rhs);
  199. }
  200. inline void Node::AssignData(const Node& rhs) {
  201. EnsureNodeExists();
  202. rhs.EnsureNodeExists();
  203. m_pNode->set_data(*rhs.m_pNode);
  204. m_pMemory->merge(*rhs.m_pMemory);
  205. }
  206. inline void Node::AssignNode(const Node& rhs) {
  207. if (!m_isValid)
  208. throw InvalidNode(m_invalidKey);
  209. rhs.EnsureNodeExists();
  210. if (!m_pNode) {
  211. m_pNode = rhs.m_pNode;
  212. m_pMemory = rhs.m_pMemory;
  213. return;
  214. }
  215. m_pNode->set_ref(*rhs.m_pNode);
  216. m_pMemory->merge(*rhs.m_pMemory);
  217. m_pNode = rhs.m_pNode;
  218. }
  219. // size/iterator
  220. inline std::size_t Node::size() const {
  221. if (!m_isValid)
  222. throw InvalidNode(m_invalidKey);
  223. return m_pNode ? m_pNode->size() : 0;
  224. }
  225. inline const_iterator Node::begin() const {
  226. if (!m_isValid)
  227. return const_iterator();
  228. return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
  229. : const_iterator();
  230. }
  231. inline iterator Node::begin() {
  232. if (!m_isValid)
  233. return iterator();
  234. return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
  235. }
  236. inline const_iterator Node::end() const {
  237. if (!m_isValid)
  238. return const_iterator();
  239. return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
  240. }
  241. inline iterator Node::end() {
  242. if (!m_isValid)
  243. return iterator();
  244. return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
  245. }
  246. // sequence
  247. template <typename T>
  248. inline void Node::push_back(const T& rhs) {
  249. if (!m_isValid)
  250. throw InvalidNode(m_invalidKey);
  251. push_back(Node(rhs));
  252. }
  253. inline void Node::push_back(const Node& rhs) {
  254. EnsureNodeExists();
  255. rhs.EnsureNodeExists();
  256. m_pNode->push_back(*rhs.m_pNode, m_pMemory);
  257. m_pMemory->merge(*rhs.m_pMemory);
  258. }
  259. // helpers for indexing
  260. namespace detail {
  261. template <typename T>
  262. struct to_value_t {
  263. explicit to_value_t(const T& t_) : t(t_) {}
  264. const T& t;
  265. using return_type = const T &;
  266. const T& operator()() const { return t; }
  267. };
  268. template <>
  269. struct to_value_t<const char*> {
  270. explicit to_value_t(const char* t_) : t(t_) {}
  271. const char* t;
  272. using return_type = std::string;
  273. const std::string operator()() const { return t; }
  274. };
  275. template <>
  276. struct to_value_t<char*> {
  277. explicit to_value_t(char* t_) : t(t_) {}
  278. const char* t;
  279. using return_type = std::string;
  280. const std::string operator()() const { return t; }
  281. };
  282. template <std::size_t N>
  283. struct to_value_t<char[N]> {
  284. explicit to_value_t(const char* t_) : t(t_) {}
  285. const char* t;
  286. using return_type = std::string;
  287. const std::string operator()() const { return t; }
  288. };
  289. // converts C-strings to std::strings so they can be copied
  290. template <typename T>
  291. inline typename to_value_t<T>::return_type to_value(const T& t) {
  292. return to_value_t<T>(t)();
  293. }
  294. } // namespace detail
  295. template<typename Key>
  296. std::string key_to_string(const Key& key) {
  297. return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
  298. }
  299. // indexing
  300. template <typename Key>
  301. inline const Node Node::operator[](const Key& key) const {
  302. EnsureNodeExists();
  303. detail::node* value = static_cast<const detail::node&>(*m_pNode).get(
  304. detail::to_value(key), m_pMemory);
  305. if (!value) {
  306. return Node(ZombieNode, key_to_string(key));
  307. }
  308. return Node(*value, m_pMemory);
  309. }
  310. template <typename Key>
  311. inline Node Node::operator[](const Key& key) {
  312. EnsureNodeExists();
  313. detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory);
  314. return Node(value, m_pMemory);
  315. }
  316. template <typename Key>
  317. inline bool Node::remove(const Key& key) {
  318. EnsureNodeExists();
  319. return m_pNode->remove(detail::to_value(key), m_pMemory);
  320. }
  321. inline const Node Node::operator[](const Node& key) const {
  322. EnsureNodeExists();
  323. key.EnsureNodeExists();
  324. m_pMemory->merge(*key.m_pMemory);
  325. detail::node* value =
  326. static_cast<const detail::node&>(*m_pNode).get(*key.m_pNode, m_pMemory);
  327. if (!value) {
  328. return Node(ZombieNode, key_to_string(key));
  329. }
  330. return Node(*value, m_pMemory);
  331. }
  332. inline Node Node::operator[](const Node& key) {
  333. EnsureNodeExists();
  334. key.EnsureNodeExists();
  335. m_pMemory->merge(*key.m_pMemory);
  336. detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory);
  337. return Node(value, m_pMemory);
  338. }
  339. inline bool Node::remove(const Node& key) {
  340. EnsureNodeExists();
  341. key.EnsureNodeExists();
  342. return m_pNode->remove(*key.m_pNode, m_pMemory);
  343. }
  344. // map
  345. template <typename Key, typename Value>
  346. inline void Node::force_insert(const Key& key, const Value& value) {
  347. EnsureNodeExists();
  348. m_pNode->force_insert(detail::to_value(key), detail::to_value(value),
  349. m_pMemory);
  350. }
  351. // free functions
  352. inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); }
  353. } // namespace YAML
  354. #endif // NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66