YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ValueNode.h
浏览该文件的文档.
1 /*
2  © 2012-2014 FrankHB.
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 "YModules.h"
32 #include YFM_YSLib_Core_YObject
33 #include <ystdex/path.hpp>
34 
35 namespace YSLib
36 {
37 
46 {
47 public:
48  using Container = set<ValueNode>;
50  using iterator = Container::iterator;
52  using const_iterator = Container::const_iterator;
53 
54 private:
55  string name;
56 
57 public:
59  mutable ValueObject Value;
60 
67  template<typename _tString, typename... _tParams>
68  inline
69  ValueNode(int, _tString&& str, _tParams&&... args)
70  : name(yforward(str)), Value(yforward(args)...)
71  {}
76  template<typename _tIn>
77  inline
78  ValueNode(const pair<_tIn, _tIn>& pr)
79  : name(), Value(Container(pr.first, pr.second))
80  {}
85  template<typename _tIn, typename _tString>
86  inline
87  ValueNode(const pair<_tIn, _tIn>& pr, _tString&& str)
88  : name(yforward(str)), Value(Container(pr.first, pr.second))
89  {}
92 
95 
97  PDefHOp(bool, !, ) const ynothrow
98  ImplRet(!Value)
99 
101 
102  PDefHOp(const ValueNode&, +=, const ValueNode& node) const
103  ImplRet(Add(node), *this)
104  PDefHOp(const ValueNode&, +=, ValueNode&& node) const
105  ImplRet(Add(std::move(node)), *this)
106 
107  PDefHOp(const ValueNode&, -=, const ValueNode& node) const
108  ImplRet(Remove(node), *this)
109  PDefHOp(const ValueNode&, -=, const string& str) const
110  ImplRet(Remove(str), *this)
112 
118  PDefHOp(const ValueNode&, /=, const ValueNode& node) const
119  ImplRet(*this %= node, *this)
120  PDefHOp(const ValueNode&, /=, ValueNode&& node) const
121  ImplRet(*this %= std::move(node), *this)
123 
129  const ValueNode&
130  operator%=(const ValueNode& node) const
131  {
132  const auto& n((*this)[node.name]);
133 
134  return n.Value = node.Value, n;
135  }
136  const ValueNode&
137  operator%=(const ValueNode&& node) const
138  {
139  const auto& n((*this)[node.name]);
140 
141  return n.Value = std::move(node.Value), n;
142  }
144 
145  PDefHOp(bool, ==, const ValueNode& node) const
146  ImplRet(name == node.name)
147 
148  PDefHOp(bool, <, const ValueNode& node) const
149  ImplRet(name < node.name)
150 
152  const ValueNode&
153  operator[](const string&) const;
155  template<class _tCon>
156  const ValueNode&
157  operator[](const ystdex::path<_tCon>& pth) const
158  {
159  auto p(this);
160 
161  for(const auto& n : pth)
162  p = &(*p)[n];
163  return *p;
164  }
165 
167  explicit DefCvt(const ynothrow, bool, bool(Value))
168  DefCvt(const ynothrow, const string&, name);
169 
171  DefGetter(const, Container&, Container, Value.Access<Container>())
173  DefGetter(const ynothrow, Container*, ContainerPtr,
174  Value.AccessPtr<Container>())
175  DefGetter(const ynothrow, const string&, Name, name)
176  size_t
177  GetSize() const ynothrow;
178 
180  bool
181  Add(const ValueNode&) const;
183  bool
184  Add(ValueNode&&) const;
185 
186 private:
188  Container&
189  CheckNodes() const;
190 
191 public:
192  PDefH(void, Clear, )
193  ImplExpr(Value.Clear())
194 
196  bool
197  Remove(const ValueNode&) const;
199  PDefH(bool, Remove, const string& str) const
200  ImplRet(Remove({0, str}))
201 
203  const ValueNode&
204  at(const string&) const;
205 
207 
208  PDefH(iterator, begin, )
209  ImplRet(GetContainer().begin())
210  PDefH(const_iterator, begin, ) const
211  ImplRet(GetContainer().begin())
212 
213  PDefH(iterator, end, )
214  ImplRet(GetContainer().end())
215  PDefH(const_iterator, end, ) const
216  ImplRet(GetContainer().end())
218 };
219 
226 template<typename _type>
227 inline _type&
228 Access(const ValueNode& node)
229 {
230  return node.Value.Access<_type>();
231 }
232 
237 template<typename _type>
238 inline _type*
240 {
241  return node.Value.AccessPtr<_type>();
242 }
247 template<typename _type>
248 inline _type*
250 {
251  return p_node ? AccessPtr<_type>(*p_node) : nullptr;
252 }
253 
254 
260 YF_API const ValueNode&
262 AccessNode(const ValueNode::Container*, const string&);
263 inline const ValueNode&
264 AccessNode(const ValueNode::Container& con, const string& name)
265 {
266  return AccessNode(&con, name);
267 }
269 
275 YF_API const ValueNode*
276 AccessNodePtr(const ValueNode::Container&, const string&);
277 inline const ValueNode*
278 AccessNodePtr(const ValueNode::Container* p_con, const string& name)
279 {
280  return p_con ? AccessNodePtr(*p_con, name) : nullptr;
281 }
283 
290 template<typename _type>
291 inline _type&
292 AccessChild(const ValueNode& node, const string& name)
293 {
294  return Access<_type>(node.at(name));
295 }
296 
304 template<typename _type>
305 inline _type*
306 AccessChildPtr(const ValueNode& node, const string& name)
307 {
308  return AccessPtr<_type>(AccessNodePtr(node.GetContainerPtr(), name));
309 }
310 template<typename _type>
311 inline _type*
312 AccessChildPtr(const ValueNode* p_node, const string& name)
313 {
314  return p_node ? AccessChildPtr<_type>(*p_node, name) : nullptr;
315 }
317 
318 
323 template<typename _tString, typename... _tParams>
324 inline ValueNode
325 MakeNode(_tString&& name, _tParams&&... args)
326 {
327  return {0, yforward(name), ystdex::decay_copy(args)...};
328 }
329 
335 template<typename _tString, typename... _tParams>
336 inline ValueNode
337 StringifyToNode(_tString&& name, _tParams&&... args)
338 {
339  return {0, yforward(name), to_string(yforward(args)...)};
340 }
341 
347 inline const ValueNode&
349 {
350  return arg;
351 }
352 inline ValueNode&&
354 {
355  return std::move(arg);
356 }
358 
363 template<class _tPack>
364 inline ValueNode
365 UnpackToNode(_tPack&& pk)
366 {
367  return {0, get<0>(yforward(pk)),
368  ValueObject(ystdex::decay_copy(get<1>(yforward(pk))))};
369 }
370 
375 template<typename... _tParams>
376 inline unique_ptr<ValueNode::Container>
377 CollectNodes(_tParams&&... args)
378 {
379  auto p(make_unique<ValueNode::Container>());
380 
382  return p;
383 }
384 
389 template<typename _tString, typename... _tParams>
390 inline ValueNode
391 PackNodes(_tString&& name, _tParams&&... args)
392 {
393  return {0, yforward(name), CollectNodes(UnpackToNode(yforward(args))...),
394  PointerTag()};
395 }
396 
397 
403 YF_API bool
404 IsPrefixedIndex(const string&, char = '$');
405 
406 } // namespace YSLib;
407 
408 #endif
409 
set< ValueNode > Container
Definition: ValueNode.h:48
ValueNode(const pair< _tIn, _tIn > &pr, _tString &&str)
构造:使用输入迭代器对、字符串引用和值参数。
Definition: ValueNode.h:87
const ValueNode & at(const string &) const
Definition: ValueNode.cpp:83
static auto first(const _tIterator &i) -> decltype((i->first))
Definition: iterator.hpp:759
#define ImplRet(...)
Definition: YBaseMacro.h:97
void seq_insert(_tCon &c, _tParams &&...args)
顺序插入值至指定容器。
Definition: container.hpp:338
unique_ptr< ValueNode::Container > CollectNodes(_tParams &&...args)
取指定值类型节点为成员的节点容器。
Definition: ValueNode.h:377
yconstfn const string _tParams && args
Definition: Loader.h:111
YF_API bool IsPrefixedIndex(const string &, char= '$')
判断字符串是否是一个指定字符和非负整数的组合。
Definition: ValueNode.cpp:107
#define DefDeCtor(_t)
Definition: YBaseMacro.h:131
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
抽象路径模板。
#define YF_API
Definition: Platform.h:64
ValueNode PackNodes(_tString &&name, _tParams &&...args)
取以指定分量为参数对应初始化得到的值类型节点为子节点的值类型节点。
Definition: ValueNode.h:391
ValueObject Value
Definition: ValueNode.h:59
_type * AccessChildPtr(const ValueNode &node, const string &name)
访问指定名称的子节点的指定类型对象的指针。 空实例或类型检查失败 。
Definition: ValueNode.h:306
const ValueNode & operator%=(const ValueNode &&node) const
Definition: ValueNode.h:137
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
ValueNode(const pair< _tIn, _tIn > &pr)
构造:使用输入迭代器对。
Definition: ValueNode.h:78
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
指示指针的标记。
Definition: yobject.h:63
值类型对象类。
Definition: yobject.h:281
YF_API const ValueNode & AccessNode(const ValueNode::Container *, const string &)
访问容器中的节点。
Definition: ValueNode.cpp:90
ValueNode StringifyToNode(_tString &&name, _tParams &&...args)
取指定名称和转换为字符串的值类型节点。
Definition: ValueNode.h:337
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
#define ImplExpr(...)
Definition: YBaseMacro.h:93
_type * AccessPtr(const ValueNode &node) ynothrow
访问节点的指定类型对象指针。
Definition: ValueNode.h:239
YF_API const ValueNode * AccessNodePtr(const ValueNode::Container &, const string &)
访问容器中的节点指针。
Definition: ValueNode.cpp:98
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
yconstfn const string & name
Definition: Loader.h:110
值类型节点。
Definition: ValueNode.h:45
_type & AccessChild(const ValueNode &node, const string &name)
访问指定名称的子节点的指定类型对象。 空实例或类型检查失败 。
Definition: ValueNode.h:292
std::string to_string(unsigned char val)
转换为字符串。
Definition: string.hpp:353
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:159
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
Container::const_iterator const_iterator
Definition: ValueNode.h:52
Container::iterator iterator
Definition: ValueNode.h:50
const ValueNode & UnpackToNode(const ValueNode &arg)
从引用参数取值类型节点:返回自身。
Definition: ValueNode.h:348
static auto second(const _tIterator &i) -> decltype((i->second))
Definition: iterator.hpp:765
ValueNode MakeNode(_tString &&name, _tParams &&...args)
取指定名称和退化参数的值类型节点。
Definition: ValueNode.h:325
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
decay_t< _type > decay_copy(_type &&arg)
退化复制。
Definition: utility.hpp:168