BRE12
impl.h
1 #ifndef NODE_DETAIL_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define NODE_DETAIL_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
3 
4 #if defined(_MSC_VER) || \
5  (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
6  (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
7 #pragma once
8 #endif
9 
10 #include "yaml-cpp/node/detail/node.h"
11 #include "yaml-cpp/node/detail/node_data.h"
12 #include <type_traits>
13 
14 namespace YAML {
15 namespace detail {
16 template <typename Key, typename Enable = void>
17 struct get_idx {
18  static node* get(const std::vector<node*>& /* sequence */,
19  const Key& /* key */, shared_memory_holder /* pMemory */) {
20  return 0;
21  }
22 };
23 
24 template <typename Key>
25 struct get_idx<Key,
26  typename std::enable_if<std::is_unsigned<Key>::value &&
27  !std::is_same<Key, bool>::value>::type> {
28  static node* get(const std::vector<node*>& sequence, const Key& key,
29  shared_memory_holder /* pMemory */) {
30  return key < sequence.size() ? sequence[key] : 0;
31  }
32 
33  static node* get(std::vector<node*>& sequence, const Key& key,
34  shared_memory_holder pMemory) {
35  if (key > sequence.size() || (key > 0 && !sequence[key-1]->is_defined()))
36  return 0;
37  if (key == sequence.size())
38  sequence.push_back(&pMemory->create_node());
39  return sequence[key];
40  }
41 };
42 
43 template <typename Key>
44 struct get_idx<Key, typename std::enable_if<std::is_signed<Key>::value>::type> {
45  static node* get(const std::vector<node*>& sequence, const Key& key,
46  shared_memory_holder pMemory) {
47  return key >= 0 ? get_idx<std::size_t>::get(
48  sequence, static_cast<std::size_t>(key), pMemory)
49  : 0;
50  }
51  static node* get(std::vector<node*>& sequence, const Key& key,
52  shared_memory_holder pMemory) {
53  return key >= 0 ? get_idx<std::size_t>::get(
54  sequence, static_cast<std::size_t>(key), pMemory)
55  : 0;
56  }
57 };
58 
59 template <typename T>
60 inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
61  T lhs;
62  if (convert<T>::decode(Node(*this, pMemory), lhs)) {
63  return lhs == rhs;
64  }
65  return false;
66 }
67 
68 inline bool node::equals(const char* rhs, shared_memory_holder pMemory) {
69  return equals<std::string>(rhs, pMemory);
70 }
71 
72 // indexing
73 template <typename Key>
74 inline node* node_data::get(const Key& key,
75  shared_memory_holder pMemory) const {
76  switch (m_type) {
77  case NodeType::Map:
78  break;
79  case NodeType::Undefined:
80  case NodeType::Null:
81  return NULL;
82  case NodeType::Sequence:
83  if (node* pNode = get_idx<Key>::get(m_sequence, key, pMemory))
84  return pNode;
85  return NULL;
86  case NodeType::Scalar:
87  throw BadSubscript();
88  }
89 
90  for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) {
91  if (it->first->equals(key, pMemory)) {
92  return it->second;
93  }
94  }
95 
96  return NULL;
97 }
98 
99 template <typename Key>
100 inline node& node_data::get(const Key& key, shared_memory_holder pMemory) {
101  switch (m_type) {
102  case NodeType::Map:
103  break;
104  case NodeType::Undefined:
105  case NodeType::Null:
106  case NodeType::Sequence:
107  if (node* pNode = get_idx<Key>::get(m_sequence, key, pMemory)) {
108  m_type = NodeType::Sequence;
109  return *pNode;
110  }
111 
112  convert_to_map(pMemory);
113  break;
114  case NodeType::Scalar:
115  throw BadSubscript();
116  }
117 
118  for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) {
119  if (it->first->equals(key, pMemory)) {
120  return *it->second;
121  }
122  }
123 
124  node& k = convert_to_node(key, pMemory);
125  node& v = pMemory->create_node();
126  insert_map_pair(k, v);
127  return v;
128 }
129 
130 template <typename Key>
131 inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) {
132  if (m_type != NodeType::Map)
133  return false;
134 
135  for (kv_pairs::iterator it = m_undefinedPairs.begin();
136  it != m_undefinedPairs.end();) {
137  kv_pairs::iterator jt = std::next(it);
138  if (it->first->equals(key, pMemory))
139  m_undefinedPairs.erase(it);
140  it = jt;
141  }
142 
143  for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
144  if (it->first->equals(key, pMemory)) {
145  m_map.erase(it);
146  return true;
147  }
148  }
149 
150  return false;
151 }
152 
153 // map
154 template <typename Key, typename Value>
155 inline void node_data::force_insert(const Key& key, const Value& value,
156  shared_memory_holder pMemory) {
157  switch (m_type) {
158  case NodeType::Map:
159  break;
160  case NodeType::Undefined:
161  case NodeType::Null:
162  case NodeType::Sequence:
163  convert_to_map(pMemory);
164  break;
165  case NodeType::Scalar:
166  throw BadInsert();
167  }
168 
169  node& k = convert_to_node(key, pMemory);
170  node& v = convert_to_node(value, pMemory);
171  insert_map_pair(k, v);
172 }
173 
174 template <typename T>
175 inline node& node_data::convert_to_node(const T& rhs,
176  shared_memory_holder pMemory) {
177  Node value = convert<T>::encode(rhs);
178  value.EnsureNodeExists();
179  pMemory->merge(*value.m_pMemory);
180  return *value.m_pNode;
181 }
182 }
183 }
184 
185 #endif // NODE_DETAIL_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
Definition: exceptions.h:233
Definition: exceptions.h:217
Definition: _tbb_windef.h:37
Definition: convert.h:28
Definition: node.h:19
Definition: DrawableObjectLoader.h:10
Definition: impl.h:17
Definition: node.h:29
Definition: traits.h:89