YSTest: ValueNode.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ValueNode.h
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2012 - 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YSL_INC_Core_ValueNode_h_
29 #define YSL_INC_Core_ValueNode_h_ 1
30
31 #include "yobject.h"
32
33 YSL_BEGIN
34
42 class YF_API ValueNode
43 {
44 public:
45 typedef set<ValueNode> Container;
46
47 private:
48 string name;
49
50 public:
52 mutable ValueObject Value;
53
54 DefDeCtor(ValueNode)
60 template<typename _tString, typename... _tParams>
61 inline
62 ValueNode(int, _tString&& str, _tParams&&... args)
63 : name(yforward(str)), Value(yforward(args)...)
64 {}
69 template<typename _tIn>
70 inline
71 ValueNode(const pair<_tIn, _tIn>& pr)
72 : name(), Value(Container(pr.first, pr.second))
73 {}
78 template<typename _tIn, typename _tString>
79 inline
80 ValueNode(const pair<_tIn, _tIn>& pr, _tString&& str)
81 : name(yforward(str)), Value(Container(pr.first, pr.second))
82 {}
83 DefDeCopyCtor(ValueNode)
84 DefDeMoveCtor(ValueNode)
85
86 DefDeCopyAssignment(ValueNode)
87 DefDeMoveAssignment(ValueNode)
88
90
91 PDefHOp(bool, !, ) const ynothrow
92 ImplRet(!Value)
93
94 bool
95 operator+=(const ValueNode&);
97 bool
98 operator+=(ValueNode&&);
99
100 bool
101 operator-=(const ValueNode&);
102 PDefHOp(bool, -=, const string& str)
103 ImplRet(*this -= {0, str})
110 PDefHOp(const ValueNode&, /=, const ValueNode& node) const
111 ImplRet(*this %= node, *this)
112 PDefHOp(const ValueNode&, /=, ValueNode&& node) const
113 ImplRet(*this %= std::move(node), *this)
115
121 const ValueNode&
122 operator%=(const ValueNode& node) const
123 {
124 const auto& n((*this)[node.name]);
125
126 return n.Value = node.Value, n;
127 }
128 const ValueNode&
129 operator%=(const ValueNode&& node) const
130 {
131 const auto& n((*this)[node.name]);
132
133 return n.Value = std::move(node.Value), n;
134 }
136
137 PDefHOp(bool, ==, const ValueNode& node) const
138 ImplRet(name == node.name)
139
140 PDefHOp(bool, <, const ValueNode& node) const
141 ImplRet(name < node.name)
142
144 const ValueNode&
145 operator[](const string&) const;
146
148 explicit DefCvt(const ynothrow, bool, bool(Value))
149 DefCvt(const ynothrow, const string&, name);
150
151 DefGetter(, Container::iterator, Begin, GetContainer().begin())
152 DefGetter(const, Container::const_iterator, Begin, GetContainer().begin())
154 DefGetter(const, Container&, Container, Value.Access<Container>())
156 DefGetter(const ynothrow, Container*, ContainerPtr,
157 Value.AccessPtr<Container>())
158 DefGetter(, Container::iterator, End, GetContainer().end())
159 DefGetter(const, Container::const_iterator, End, GetContainer().end())
160 DefGetter(const ynothrow, const string&, Name, name)
162 const ValueNode&
163 GetNode(const string&) const;
164 size_t
165 GetSize() const ynothrow;
166
167 private:
169 Container&
170 CheckNodes() const;
171
172 public:
173 PDefH(void, Clear, )
174 ImplExpr(Value.Clear())
175 };
176
183 inline auto
184 begin(ValueNode& node) -> decltype(node.GetBegin())
185 {
186 return node.GetBegin();
187 }
188 inline auto
189 begin(const ValueNode& node) -> decltype(node.GetBegin())
190 {
191 return node.GetBegin();
192 }
193
194 inline auto
195 end(ValueNode& node) -> decltype(node.GetEnd())
196 {
197 return node.GetEnd();
198 }
199 inline auto
200 end(const ValueNode& node) -> decltype(node.GetEnd())
201 {
202 return node.GetEnd();
203 }
205
211 template<typename _type>
212 inline _type&
213 Access(const ValueNode& node)
214 {
215 return node.Value.Access<_type>();
216 }
217
222 template<typename _type>
223 inline _type*
224 AccessPtr(const ValueNode& node) ynothrow
225 {
226 return node.Value.AccessPtr<_type>();
227 }
228
229
234 YF_API const ValueNode&
235 AccessNode(const ValueNode::Container&, const string&);
236
242 template<typename _type>
243 inline _type&
244 AccessChild(ValueNode& node, const string& name)
245 {
246 return Access<_type>(node.GetNode(name));
247 }
253 template<typename _type>
254 inline const _type&
255 AccessChild(const ValueNode& node, const string& name)
256 {
257 return Access<_type>(node.GetNode(name));
258 }
259
260
265 template<typename _tString, typename... _tParams>
266 inline ValueNode
267 MakeNode(_tString&& name, _tParams&&... args)
268 {
269 return {0, yforward(name), ystdex::decay_copy(yforward(args))...};
270 }
271
277 template<typename _tString, typename... _tParams>
278 inline ValueNode
279 StringifyToNode(_tString&& name, _tParams&&... args)
280 {
281 return {0, yforward(name), to_string(yforward(args)...)};
282 }
283
289 inline const ValueNode&
290 UnpackToNode(const ValueNode& arg)
291 {
292 return arg;
293 }
294 inline ValueNode&&
295 UnpackToNode(ValueNode&& arg)
296 {
297 return std::move(arg);
298 }
300
305 template<class _tPack>
306 inline ValueNode
307 UnpackToNode(_tPack&& pk)
308 {
309 return {0, get<0>(yforward(pk)),
310 ValueObject(ystdex::decay_copy(get<1>(yforward(pk))))};
311 }
312
317 template<typename... _tParams>
318 inline unique_ptr<ValueNode::Container>
319 CollectNodes(_tParams&&... args)
320 {
321 auto p(make_unique<ValueNode::Container>());
322
323 ystdex::seq_insert(*p, yforward(args)...);
324 return std::move(p);
325 }
326
331 template<typename _tString, typename... _tParams>
332 inline ValueNode
333 PackNodes(_tString&& name, _tParams&&... args)
334 {
335 return {0, yforward(name), CollectNodes(UnpackToNode(
336 yforward(args))...).release(), PointerTag()};
337 }
338
339 YSL_END
340
341 #endif
342