YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ValueNode.cpp
浏览该文件的文档.
1 /*
2  © 2012-2013 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 #include "YSLib/Core/YModules.h"
29 #include YFM_YSLib_Core_ValueNode
30 
31 namespace YSLib
32 {
33 
34 const ValueNode&
35 ValueNode::operator[](const string& name) const
36 {
37  auto& con(CheckNodes());
38  auto i(con.lower_bound({0, name}));
39 
40  if(i == con.end() || con.key_comp()({0, name}, *i))
41  // TODO: Use %emplace_hint.
42  i = con.insert(i, {0, name});
43  return *i;
44 }
45 
46 size_t
48 {
49  const auto p_con(GetContainerPtr());
50 
51  return p_con ? p_con->size() : 0;
52 }
53 
56 {
57  if(!Value)
58  Value = Container();
59  return GetContainer();
60 }
61 
62 bool
63 ValueNode::Add(const ValueNode& node) const
64 {
65  return CheckNodes().insert(node).second;
66 }
67 bool
69 {
70  // TODO: Use %emplace.
71  return CheckNodes().insert(std::move(node)).second;
72 }
73 
74 bool
75 ValueNode::Remove(const ValueNode& node) const
76 {
77  const auto p_con(GetContainerPtr());
78 
79  return p_con ? p_con->erase({0, node.name}) != 0 : false;
80 }
81 
82 const ValueNode&
83 ValueNode::at(const string& name) const
84 {
85  return AccessNode(GetContainer(), name);
86 }
87 
88 
89 const ValueNode&
90 AccessNode(const ValueNode::Container* p_con, const string& name)
91 {
92  if(const auto p = AccessNodePtr(p_con, name))
93  return *p;
94  throw std::out_of_range("Wrong name found.");
95 }
96 
97 const ValueNode*
98 AccessNodePtr(const ValueNode::Container& con, const string& name)
99 {
100  const auto i(con.find(ValueNode(0, name)));
101 
102  return i != end(con) ? &*i : nullptr;
103 }
104 
105 
106 bool
107 IsPrefixedIndex(const string& name, char prefix)
108 {
109  if(name.length() > 1 && name[0] == prefix)
110  try
111  {
112  const string ss(&name[1]);
113 
114  return std::to_string(std::stoul(ss)) == ss;
115  }
116  catch(std::invalid_argument&)
117  {}
118  return false;
119 }
120 
121 } // namespace YSLib;
122 
set< ValueNode > Container
Definition: ValueNode.h:48
const ValueNode & at(const string &) const
Definition: ValueNode.cpp:83
YF_API bool IsPrefixedIndex(const string &, char= '$')
判断字符串是否是一个指定字符和非负整数的组合。
Definition: ValueNode.cpp:107
bool Add(const ValueNode &) const
Definition: ValueNode.cpp:63
ValueObject Value
Definition: ValueNode.h:59
YF_API const ValueNode & AccessNode(const ValueNode::Container *, const string &)
访问容器中的节点。
Definition: ValueNode.cpp:90
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
std::string to_string(unsigned char val)
转换为字符串。
Definition: string.hpp:353
Container Value const string name size_t GetSize() const ynothrow
Definition: ValueNode.cpp:47
const ValueNode & operator[](const ystdex::path< _tCon > &pth) const
Definition: ValueNode.h:157
Container & CheckNodes() const
Definition: ValueNode.cpp:55