YSTest: ValueNode.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

ValueNode.cpp
浏览该文件的文档.
1 /*
2 © 2012-2017 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 // for ystdex::call_value_or, ystdex::addrof,
30 // ystdex::compose, std::mem_fn, ystdex::bind1;
31 #include <cstdio> // for std::snprintf;
32
33 namespace YSLib
34 {
35
36 ValueNode&
37 ValueNode::operator%=(const ValueNode& node)
38 {
39 return Deref(insert_or_assign(node.name, node).first);
40 }
41 ValueNode&
42 ValueNode::operator%=(ValueNode&& node)
43 {
44 return Deref(insert_or_assign(node.name, std::move(node)).first);
45 }
46
47 ValueNode::Container
48 ValueNode::CreateRecursively(const Container& con, IValueHolder::Creation c)
49 {
50 return CreateRecursively(con, ystdex::bind1(&ValueObject::Create, c));
51 }
52
53 void
54 ValueNode::SwapContent(ValueNode& node) ynothrow
55 {
56 SwapContainer(node),
57 swap(Value, node.Value);
58 }
59
60 void
61 ValueNode::ThrowIndexOutOfRange()
62 {
63 throw std::out_of_range("Index is out of range.");
64 }
65
66 void
67 ValueNode::ThrowWrongNameFound()
68 {
69 throw std::out_of_range("Wrong name found.");
70 }
71
72 void
73 swap(ValueNode& x, ValueNode& y) ynothrow
74 {
75 std::swap(x.name, y.name),
76 x.SwapContent(y);
77 }
78
79 ValueNode&
80 AccessNode(ValueNode::Container* p_con, const string& name)
81 {
82 if(const auto p = AccessNodePtr(p_con, name))
83 return *p;
84 ValueNode::ThrowWrongNameFound();
85 }
86 const ValueNode&
87 AccessNode(const ValueNode::Container* p_con, const string& name)
88 {
89 if(const auto p = AccessNodePtr(p_con, name))
90 return *p;
91 ValueNode::ThrowWrongNameFound();
92 }
93 ValueNode&
94 AccessNode(ValueNode& node, size_t n)
95 {
96 const auto p(AccessNodePtr(node, n));
97
98 if(p)
99 return *p;
100 ValueNode::ThrowIndexOutOfRange();
101 }
102 const ValueNode&
103 AccessNode(const ValueNode& node, size_t n)
104 {
105 const auto p(AccessNodePtr(node, n));
106
107 if(p)
108 return *p;
109 ValueNode::ThrowIndexOutOfRange();
110 }
111
112 observer_ptr<ValueNode>
113 AccessNodePtr(ValueNode::Container& con, const string& name) ynothrow
114 {
115 return make_observer(ystdex::call_value_or<ValueNode*>(ystdex::addrof<>(),
116 con.find(name), {}, end(con)));
117 }
118 observer_ptr<const ValueNode>
119 AccessNodePtr(const ValueNode::Container& con, const string& name) ynothrow
120 {
121 return make_observer(ystdex::call_value_or<const ValueNode*>(
122 ystdex::addrof<>(), con.find(name), {}, end(con)));
123 }
124 observer_ptr<ValueNode>
125 AccessNodePtr(ValueNode& node, size_t n)
126 {
127 auto& con(node.GetContainerRef());
128
129 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
130 return n < con.size()
131 ? make_observer(&*std::next(con.begin(), ptrdiff_t(n))) : nullptr;
132 }
133 observer_ptr<const ValueNode>
134 AccessNodePtr(const ValueNode& node, size_t n)
135 {
136 auto& con(node.GetContainer());
137
138 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
139 return n < con.size()
140 ? make_observer(&*std::next(con.cbegin(), ptrdiff_t(n))) : nullptr;
141 }
142
143 ValueObject
144 GetValueOf(observer_ptr<const ValueNode> p_node)
145 {
146 return ystdex::call_value_or(std::mem_fn(&ValueNode::Value), p_node);
147 }
148
149 observer_ptr<const ValueObject>
150 GetValuePtrOf(observer_ptr<const ValueNode> p_node)
151 {
152 return ystdex::call_value_or(ystdex::compose(make_observer<const ValueObject
153 >, ystdex::addrof<>(), std::mem_fn(&ValueNode::Value)), p_node);
154 }
155
156
157 void
158 RemoveEmptyChildren(ValueNode::Container& con) ynothrow
159 {
160 ystdex::erase_all_if(con, std::mem_fn(&ValueNode::operator!));
161 }
162
163 void
164 RemoveHead(ValueNode::Container& con) ynothrowv
165 {
166 YAssert(!con.empty(), "Empty node container found.");
167 con.erase(con.cbegin());
168 }
169
170
171 bool
172 IsPrefixedIndex(string_view name, char prefix)
173 {
174 YAssertNonnull(name.data());
175 if(name.length() > 1 && name.front() == prefix)
176 try
177 {
178 const auto ss(name.substr(1));
179
180 return MakeIndex(std::stoul(string(ss))) == ss;
181 }
182 CatchIgnore(std::invalid_argument&)
183 return {};
184 }
185
186 string
187 MakeIndex(size_t n)
188 {
189 char str[5]{};
190
191 if(n < 10000)
192 std::snprintf(str, 5, "%04u", unsigned(n));
193 else
194 throw std::invalid_argument("Argument is too large.");
195 return str;
196 }
197
198 size_t
199 GetLastIndexOf(const ValueNode::Container& con)
200 {
201 return con.empty() ? size_t(-1)
202 : size_t(std::stoul(con.rbegin()->GetName()));
203 }
204
205 } // namespace YSLib;
206
ystdex::mapped_set::empty
bool empty() const
Definition: set.hpp:358
ystdex::bind1
auto bind1(_func &&f, _tParams &&...args) -> decltype(std::bind(std::forward< decltype(f)>(f), std::placeholders::_1, std::forward< decltype(args)>(args)...))
复合调用 std::bind 和 std::placeholders::_1 。
Definition: functional.hpp:623
YSLib::GetLastIndexOf
size_t GetLastIndexOf(const ValueNode::Container &con)
Definition: ValueNode.cpp:199
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:326
YSLib::MakeIndex
MakeIndex
Definition: ValueNode.h:1202
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:451
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
NPL::x
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term ValueNode &node const ValueNode &node const _type & x
Definition: SContext.h:94
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
ystdex::call_value_or
auto call_value_or(_func f, _type &&p) -> decay_t< decltype(f(*std::forward< decltype(p)>(p)))>
调用非空值或取默认值。
Definition: deref_op.hpp:140
ystdex::compose
composed< _func, _func2 > compose(_func f, _func2 g)
函数复合。
Definition: functional.hpp:686
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:120
YSLib::GetValuePtrOf
observer_ptr< const ValueObject > GetValuePtrOf(observer_ptr< const ValueNode > p_node)
Definition: ValueNode.cpp:150
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
YSLib::AccessNode
const ValueNode & AccessNode(const ValueNode &node, const _tKey &name)
Definition: ValueNode.h:866
ystdex::cpp2011::end
_type * end(_type(&&array)[_vN])
Definition: range.hpp:54
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
YSLib::ValueNode::ThrowWrongNameFound
static YB_NORETURN void ThrowWrongNameFound()
抛出名称错误异常。
Definition: ValueNode.cpp:67
YSLib::ValueNode::Value
ValueObject Value
Definition: ValueNode.h:84
YSLib::ValueObject
值类型对象类。
Definition: YObject.h:539
ystdex::erase_all_if
void erase_all_if(_tCon &con, _tIter first, _tIter last, _fPred pred)
删除指定容器中指定区间中的或全部满足谓词的元素。
Definition: container.hpp:727
YSLib::ValueNode::insert_or_assign
iterator insert_or_assign(const_iterator hint, _tKey &&k, _tParam &&arg)
Definition: ValueNode.h:659
YSLib::ValueNode::ThrowIndexOutOfRange
static YB_NORETURN void ThrowIndexOutOfRange()
抛出索引越界异常。
Definition: ValueNode.cpp:61
YSLib::AccessNodePtr
YF_API observer_ptr< ValueNode > AccessNodePtr(ValueNode::Container &, const string &) ynothrow
访问节点指针。
Definition: ValueNode.cpp:113
YSLib::RemoveEmptyChildren
YF_API void RemoveEmptyChildren(ValueNode::Container &) ynothrow
移除空子节点。
Definition: ValueNode.cpp:158
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
YSLib::ValueNode::name
string name
Definition: ValueNode.h:75
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
YSLib::RemoveHead
YF_API void RemoveHead(ValueNode::Container &) ynothrowv
移除首个子节点。
Definition: ValueNode.cpp:164
YSLib::ValueNode
值类型节点。
Definition: ValueNode.h:58
YSLib::ValueNode::CreateRecursively
static Container CreateRecursively(Container &con, _fCallable f)
Definition: ValueNode.h:453
YSLib::GetValueOf
ValueObject GetValueOf(observer_ptr< const ValueNode > p_node)
Definition: ValueNode.cpp:144
ystdex::mapped_set
允许指定不同于值类型的键的集合。
Definition: set.hpp:160
YModules.h
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:130
YSLib::Text::str
const char const char * str
Definition: TextFile.h:81
CHRLib::c
c
Definition: CharacterProcessing.h:81
ystdex::addrof
一元 & 操作。
Definition: functor.hpp:437
ystdex::mapped_set::rbegin
reverse_iterator rbegin()
Definition: set.hpp:312
YSLib::ValueNode::node
node Value const ValueNode &node ValueNode &&node const ValueNode & node
Definition: ValueNode.h:348
YSLib::ValueNode::operator%=
const ValueNode &node const ValueNode &node ValueNode &&node const ValueNode &node const string &str const ValueNode &node ImplRet this *this ValueNode & operator%=(const ValueNode &)
替换同名子节点。
Definition: ValueNode.cpp:37
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
CHRLib::p
const auto p(d)
YSLib::IsPrefixedIndex
YF_API bool IsPrefixedIndex(string_view, char= '$')
判断字符串是否是前缀索引：一个指定字符和非负整数的组合。
Definition: ValueNode.cpp:172
YSLib::swap
void swap(ValueNode &x, ValueNode &y) ynothrow
Definition: ValueNode.cpp:73