YSTest: ValueNode.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

ValueNode.cpp
浏览该文件的文档.
1 /*
2 © 2012-2016 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 #include <cstdio> // for std::snprintf;
31
32 namespace YSLib
33 {
34
35 const ValueNode&
36 ValueNode::operator%=(const ValueNode& node)
37 {
38 auto& n((*this)[node.name]);
39
40 n.Value = node.Value;
41 return n;
42 }
43 const ValueNode&
44 ValueNode::operator%=(const ValueNode&& node)
45 {
46 auto& n((*this)[node.name]);
47
48 n.Value = std::move(node.Value);
49 return n;
50 }
51
52 void
53 ValueNode::SwapContent(ValueNode& node) ynothrow
54 {
55 SwapContainer(node),
56 Value.swap(node.Value);
57 }
58
59 void
60 ValueNode::swap(ValueNode& node) ynothrow
61 {
62 std::swap(name, node.name),
63 SwapContent(node);
64 }
65
66 ValueNode&
67 AccessNode(ValueNode::Container* p_con, const string& name)
68 {
69 if(const auto p = AccessNodePtr(p_con, name))
70 return *p;
71 throw std::out_of_range("Wrong name found.");
72 }
73 const ValueNode&
74 AccessNode(const ValueNode::Container* p_con, const string& name)
75 {
76 if(const auto p = AccessNodePtr(p_con, name))
77 return *p;
78 throw std::out_of_range("Wrong name found.");
79 }
80 ValueNode&
81 AccessNode(ValueNode& node, size_t n)
82 {
83 const auto p(AccessNodePtr(node, n));
84
85 if(p)
86 return *p;
87 throw std::out_of_range("Index is out of range.");
88 }
89 const ValueNode&
90 AccessNode(const ValueNode& node, size_t n)
91 {
92 const auto p(AccessNodePtr(node, n));
93
94 if(p)
95 return *p;
96 throw std::out_of_range("Index is out of range.");
97 }
98
99 observer_ptr<ValueNode>
100 AccessNodePtr(ValueNode::Container& con, const string& name) ynothrow
101 {
102 return make_observer(ystdex::call_value_or<ValueNode*>(ystdex::addrof<>(),
103 con.find(name), {}, end(con)));
104 }
105 observer_ptr<const ValueNode>
106 AccessNodePtr(const ValueNode::Container& con, const string& name) ynothrow
107 {
108 return make_observer(ystdex::call_value_or<const ValueNode*>(
109 ystdex::addrof<>(), con.find(name), {}, end(con)));
110 }
111 observer_ptr<ValueNode>
112 AccessNodePtr(ValueNode& node, size_t n)
113 {
114 auto& con(node.GetContainerRef());
115
116 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
117 return n < con.size() ? make_observer(&*std::next(con.begin(), ptrdiff_t(n)))
118 : nullptr;
119 }
120 observer_ptr<const ValueNode>
121 AccessNodePtr(const ValueNode& node, size_t n)
122 {
123 auto& con(node.GetContainer());
124
125 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
126 return n < con.size() ? make_observer(&*std::next(con.cbegin(), ptrdiff_t(n)))
127 : nullptr;
128 }
129
130
131 void
132 RemoveEmptyChildren(ValueNode::Container& con) ynothrow
133 {
134 ystdex::erase_all_if(con, [](const ValueNode& term) ynothrow{
135 return !term;
136 });
137 }
138
139 void
140 RemoveHead(ValueNode::Container& con) ynothrowv
141 {
142 YAssert(!con.empty(), "Empty node container found.");
143 con.erase(con.cbegin());
144 }
145
146
147 bool
148 IsPrefixedIndex(string_view name, char prefix)
149 {
150 YAssertNonnull(name.data());
151 if(name.length() > 1 && name.front() == prefix)
152 try
153 {
154 const auto ss(name.substr(1));
155
156 return MakeIndex(std::stoul(string(ss))) == ss;
157 }
158 CatchIgnore(std::invalid_argument&)
159 return {};
160 }
161
162 string
163 MakeIndex(size_t n)
164 {
165 char str[5]{};
166
167 if(n < 10000)
168 std::snprintf(str, 5, "%04u", unsigned(n));
169 else
170 throw std::invalid_argument("Argument is too large.");
171 return str;
172 }
173
174 } // namespace YSLib;
175
YSLib::RemoveEmptyChildren
YF_API void RemoveEmptyChildren(ValueNode::Container &) ynothrow
移除空子节点。
Definition: ValueNode.cpp:132
ystdex::erase_all_if
void erase_all_if(_tCon &con, _tIter first, _tIter last, _fPred pred)
删除指定容器中指定区间中的或全部满足谓词的元素。
Definition: container.hpp:723
NPL::term
const TermNode &term ValueNode &node const ValueNode &node const TermNode & term
Definition: SContext.h:72
platform::p
while p p
Definition: YCLib/FileSystem.h:225
YSLib::RemoveHead
YF_API void RemoveHead(ValueNode::Container &) ynothrowv
移除首个子节点。
Definition: ValueNode.cpp:140
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
YModules.h
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
YSLib::MakeIndex
MakeIndex
Definition: ValueNode.h:865
NPL::A1::name
ctx const string & name
Definition: NPLA1.h:248
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
YSLib::AccessNode
name name name name YF_API ValueNode & AccessNode(ValueNode &, size_t)
Definition: ValueNode.cpp:81
ystdex::mapped_set
允许指定不同于值类型的键的集合。
Definition: set.hpp:160
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
YSLib::ValueNode::name
string name
Definition: ValueNode.h:74
YSLib::AccessNodePtr
name AccessNodePtr
Definition: ValueNode.h:627
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:114
YSLib::ValueNode::operator%=
ValueNode node ynothrow const ValueNode &node ValueNode &&node const ValueNode &node const string &str const ValueNode &node ImplRet this *this const ValueNode & operator%=(const ValueNode &)
替换同名子节点。
Definition: ValueNode.cpp:36
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
YSLib::IsPrefixedIndex
bool IsPrefixedIndex(string_view name, char prefix)
Definition: ValueNode.cpp:148
YSLib::Text::str
const char const char * str
Definition: TextFile.h:77
YSLib::ValueNode::Value
ValueObject Value
Definition: ValueNode.h:83
YSLib::p_con
name const ValueNode::Container * p_con
Definition: ValueNode.h:627
ystdex::addrof
一元 & 操作。
Definition: functor.hpp:416
YSLib::ValueNode
值类型节点。
Definition: ValueNode.h:57
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: memory.hpp:1042
ystdex::cpp2011::end
_type * end(_type(&&array)[_vN])
Definition: range.hpp:54
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321