1 #ifndef NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 2 #define NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 4 #if defined(_MSC_VER) || \ 5 (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ 6 (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 10 #include "yaml-cpp/exceptions.h" 11 #include "yaml-cpp/node/detail/memory.h" 12 #include "yaml-cpp/node/detail/node.h" 13 #include "yaml-cpp/node/iterator.h" 14 #include "yaml-cpp/node/node.h" 20 : m_isValid(true), m_invalidKey{}, m_pMemory(
nullptr), m_pNode(
nullptr) {}
22 inline Node::Node(NodeType::value type)
25 m_pMemory(
new detail::memory_holder),
26 m_pNode(&m_pMemory->create_node()) {
27 m_pNode->set_type(type);
31 inline Node::Node(
const T& rhs)
34 m_pMemory(
new detail::memory_holder),
35 m_pNode(&m_pMemory->create_node()) {
39 inline Node::Node(
const detail::iterator_value& rhs)
40 : m_isValid(rhs.m_isValid),
41 m_invalidKey(rhs.m_invalidKey),
42 m_pMemory(rhs.m_pMemory),
43 m_pNode(rhs.m_pNode) {}
45 inline Node::Node(
const Node&) =
default;
47 inline Node::Node(Zombie)
48 : m_isValid(false), m_invalidKey{}, m_pMemory{}, m_pNode(
nullptr) {}
50 inline Node::Node(Zombie,
const std::string& key)
51 : m_isValid(false), m_invalidKey(key), m_pMemory{}, m_pNode(
nullptr) {}
53 inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
54 : m_isValid(true), m_invalidKey{}, m_pMemory(pMemory), m_pNode(&node) {}
56 inline Node::~Node() =
default;
58 inline void Node::EnsureNodeExists()
const {
60 throw InvalidNode(m_invalidKey);
62 m_pMemory.reset(
new detail::memory_holder);
63 m_pNode = &m_pMemory->create_node();
68 inline bool Node::IsDefined()
const {
72 return m_pNode ? m_pNode->is_defined() :
true;
75 inline Mark Node::Mark()
const {
77 throw InvalidNode(m_invalidKey);
79 return m_pNode ? m_pNode->mark() : Mark::null_mark();
82 inline NodeType::value Node::Type()
const {
84 throw InvalidNode(m_invalidKey);
85 return m_pNode ? m_pNode->type() : NodeType::Null;
91 template <
typename T,
typename S>
93 explicit as_if(
const Node& node_) : node(node_) {}
96 T operator()(
const S& fallback)
const {
101 if (convert<T>::decode(node, t))
107 template <
typename S>
108 struct as_if<
std::string, S> {
109 explicit as_if(
const Node& node_) : node(node_) {}
112 std::string operator()(
const S& fallback)
const {
113 if (node.Type() == NodeType::Null)
115 if (node.Type() != NodeType::Scalar)
117 return node.Scalar();
121 template <
typename T>
122 struct as_if<T, void> {
123 explicit as_if(
const Node& node_) : node(node_) {}
126 T operator()()
const {
128 throw TypedBadConversion<T>(node.Mark());
131 if (convert<T>::decode(node, t))
133 throw TypedBadConversion<T>(node.Mark());
138 struct as_if<
std::string, void> {
139 explicit as_if(
const Node& node_) : node(node_) {}
142 std::string operator()()
const {
143 if (node.Type() == NodeType::Null)
145 if (node.Type() != NodeType::Scalar)
146 throw TypedBadConversion<std::string>(node.Mark());
147 return node.Scalar();
152 template <
typename T>
153 inline T Node::as()
const {
155 throw InvalidNode(m_invalidKey);
156 return as_if<T, void>(*this)();
159 template <
typename T,
typename S>
160 inline T Node::as(
const S& fallback)
const {
163 return as_if<T, S>(*this)(fallback);
166 inline const std::string& Node::Scalar()
const {
168 throw InvalidNode(m_invalidKey);
169 return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
172 inline const std::string& Node::Tag()
const {
174 throw InvalidNode(m_invalidKey);
175 return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
178 inline void Node::SetTag(
const std::string& tag) {
180 m_pNode->set_tag(tag);
183 inline EmitterStyle::value Node::Style()
const {
185 throw InvalidNode(m_invalidKey);
186 return m_pNode ? m_pNode->style() : EmitterStyle::Default;
189 inline void Node::SetStyle(EmitterStyle::value style) {
191 m_pNode->set_style(style);
195 inline bool Node::is(
const Node& rhs)
const {
196 if (!m_isValid || !rhs.m_isValid)
197 throw InvalidNode(m_invalidKey);
198 if (!m_pNode || !rhs.m_pNode)
200 return m_pNode->is(*rhs.m_pNode);
203 template <
typename T>
204 inline Node& Node::operator=(
const T& rhs) {
209 inline Node& Node::operator=(
const Node& rhs) {
216 inline void Node::reset(
const YAML::Node& rhs) {
217 if (!m_isValid || !rhs.m_isValid)
218 throw InvalidNode(m_invalidKey);
219 m_pMemory = rhs.m_pMemory;
220 m_pNode = rhs.m_pNode;
223 template <
typename T>
224 inline void Node::Assign(
const T& rhs) {
226 throw InvalidNode(m_invalidKey);
227 AssignData(convert<T>::encode(rhs));
231 inline void Node::Assign(
const std::string& rhs) {
233 m_pNode->set_scalar(rhs);
236 inline void Node::Assign(
const char* rhs) {
238 m_pNode->set_scalar(rhs);
241 inline void Node::Assign(
char* rhs) {
243 m_pNode->set_scalar(rhs);
246 inline void Node::AssignData(
const Node& rhs) {
248 rhs.EnsureNodeExists();
250 m_pNode->set_data(*rhs.m_pNode);
251 m_pMemory->merge(*rhs.m_pMemory);
254 inline void Node::AssignNode(
const Node& rhs) {
256 throw InvalidNode(m_invalidKey);
257 rhs.EnsureNodeExists();
260 m_pNode = rhs.m_pNode;
261 m_pMemory = rhs.m_pMemory;
265 m_pNode->set_ref(*rhs.m_pNode);
266 m_pMemory->merge(*rhs.m_pMemory);
267 m_pNode = rhs.m_pNode;
271 inline std::size_t Node::size()
const {
273 throw InvalidNode(m_invalidKey);
274 return m_pNode ? m_pNode->size() : 0;
277 inline const_iterator Node::begin()
const {
279 return const_iterator();
280 return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
284 inline iterator Node::begin() {
287 return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
290 inline const_iterator Node::end()
const {
292 return const_iterator();
293 return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
296 inline iterator Node::end() {
299 return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
303 template <
typename T>
304 inline void Node::push_back(
const T& rhs) {
306 throw InvalidNode(m_invalidKey);
307 push_back(Node(rhs));
310 inline void Node::push_back(
const Node& rhs) {
312 rhs.EnsureNodeExists();
314 m_pNode->push_back(*rhs.m_pNode, m_pMemory);
315 m_pMemory->merge(*rhs.m_pMemory);
318 template<
typename Key>
319 std::string key_to_string(
const Key& key) {
320 return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
324 template <
typename Key>
325 inline const Node Node::operator[](
const Key& key)
const {
327 detail::node* value =
328 static_cast<const detail::node&
>(*m_pNode).get(key, m_pMemory);
330 return Node(ZombieNode, key_to_string(key));
332 return Node(*value, m_pMemory);
335 template <
typename Key>
336 inline Node Node::operator[](
const Key& key) {
338 detail::node& value = m_pNode->get(key, m_pMemory);
339 return Node(value, m_pMemory);
342 template <
typename Key>
343 inline bool Node::remove(
const Key& key) {
345 return m_pNode->remove(key, m_pMemory);
348 inline const Node Node::operator[](
const Node& key)
const {
350 key.EnsureNodeExists();
351 m_pMemory->merge(*key.m_pMemory);
352 detail::node* value =
353 static_cast<const detail::node&
>(*m_pNode).get(*key.m_pNode, m_pMemory);
355 return Node(ZombieNode, key_to_string(key));
357 return Node(*value, m_pMemory);
360 inline Node Node::operator[](
const Node& key) {
362 key.EnsureNodeExists();
363 m_pMemory->merge(*key.m_pMemory);
364 detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory);
365 return Node(value, m_pMemory);
368 inline bool Node::remove(
const Node& key) {
370 key.EnsureNodeExists();
371 return m_pNode->remove(*key.m_pNode, m_pMemory);
375 template <
typename Key,
typename Value>
376 inline void Node::force_insert(
const Key& key,
const Value& value) {
378 m_pNode->force_insert(key, value, m_pMemory);
382 inline bool operator==(
const Node& lhs,
const Node& rhs) {
return lhs.is(rhs); }
385 #endif // NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 Definition: gtest-internal.h:1322
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString's.
Definition: DualString.h:208