mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-07-11 22:03:01 -06:00
tutorial: Value related examples modernized
git-svn-id: svn://ultimatepp.org/upp/trunk@5158 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
52329bf08f
commit
07371c1d34
1 changed files with 24 additions and 25 deletions
|
|
@ -465,50 +465,49 @@ to any value type that can contain Null:&]
|
|||
has little requirements for the type used `- only copy constructor
|
||||
and assignment operator are required:&]
|
||||
[s7; &]
|
||||
[s7; struct Foo `{&]
|
||||
[s7; -|int x;&]
|
||||
[s7; -|&]
|
||||
[s7; -|Foo(int x) : x(x) `{`}&]
|
||||
[s7; -|Foo() `{`}&]
|
||||
[s7; struct RawFoo `{&]
|
||||
[s7; -|String x;&]
|
||||
[s7; `};&]
|
||||
[s7; &]
|
||||
[s7; &]
|
||||
[s0; (int this case, default copy constructor and assignment operator
|
||||
are provided by compiler).&]
|
||||
[s7; &]
|
||||
[s7; -|Value v `= RawToValue(Foo(12345));&]
|
||||
[s7; -|RawFoo h;&]
|
||||
[s7; -|h.x `= `"hello`";&]
|
||||
[s7; -|Value q `= RawToValue(h);&]
|
||||
[s7; &]
|
||||
[s16; v.Is<Foo>() `= true&]
|
||||
[s16; ValueTo<Foo>(v).x `= 12345&]
|
||||
[s16; q.Is<Foo>() `= true&]
|
||||
[s16; q.To<Foo>().x `= `"hello`"&]
|
||||
[s7; &]
|
||||
[s0; [*/ RichValue] level Values provide more operations for Value
|
||||
`- equality test, IsNull test, hashing, conversion to text and
|
||||
serialization. In order to make serialization work, type must
|
||||
also have assigned an integer id (client types should use ids
|
||||
in range 10000..20000). Type can provide the support for these
|
||||
operations via templete function specializations or (perhaps
|
||||
more convenient) using defined methods:&]
|
||||
operations via template function specializations or (perhaps
|
||||
more convenient) using defined methods and inheriting from ValueType
|
||||
base class template:&]
|
||||
[s7; &]
|
||||
[s7; struct Foo : [* AssignValueTypeNo]<Foo, 10010> `{&]
|
||||
[s7; struct Foo : ValueType<Foo, 10010> `{&]
|
||||
[s7; -|int x;&]
|
||||
[s7; -|&]
|
||||
[s7; -|Foo(const Nuller`&) `{ x `= Null; `}&]
|
||||
[s7; -|Foo(int x) : x(x) `{`}&]
|
||||
[s7; -|Foo() `{`}&]
|
||||
[s7; &]
|
||||
[s7; -|String [* ToString]() const `{ return AsString(x);
|
||||
[s7; -|String ToString() const `{ return AsString(x);
|
||||
`}&]
|
||||
[s7; -|unsigned [* GetHashValue]() const `{ return x; `}&]
|
||||
[s7; -|void [* Serialize](Stream`& s) `{ s % x; `}&]
|
||||
[s7; -|bool [* operator`=`=](const Foo`& b) const `{ return x `=`= b.x;
|
||||
`}&]
|
||||
[s7; -|bool [* IsNullInstance]() const `{ return IsNull(x);
|
||||
[s7; -|unsigned GetHashValue() const `{ return x; `}&]
|
||||
[s7; -|void Serialize(Stream`& s) `{ s % x; `}&]
|
||||
[s7; -|bool operator`=`=(const Foo`& b) const `{ return x `=`= b.x;
|
||||
`}&]
|
||||
[s7; -|bool IsNullInstance() const `{ return IsNull(x); `}-|&]
|
||||
[s7; `};&]
|
||||
[s7; &]
|
||||
[s7; &]
|
||||
[s7; INITBLOCK `{&]
|
||||
[s7; -|RichValue<Foo>`::[* Register](); // registers the type for serialization&]
|
||||
[s7; -|Value`::Register<Foo>();&]
|
||||
[s7; `}&]
|
||||
[s7; &]
|
||||
[s7; .......&]
|
||||
[s7; &]
|
||||
[s7; -|Value a `= [* RichToValue](Foo(54321));&]
|
||||
|
|
@ -516,23 +515,23 @@ more convenient) using defined methods:&]
|
|||
[s7; &]
|
||||
[s16; (a `=`= b) `= true&]
|
||||
[s16; IsNull(a) `= false&]
|
||||
[s16; ValueTo<Foo>(v) `= 54321&]
|
||||
[s16; v.Get<Foo>() `= 54321&]
|
||||
[s7; &]
|
||||
[s7; -|String s `= StoreAsString(a);&]
|
||||
[s7; -|LoadFromString(v, s);&]
|
||||
[s7; &]
|
||||
[s16; ValueTo<Foo>(v) `= 54321&]
|
||||
[s16; v.Get<Foo>() `= 54321&]
|
||||
[s0; &]
|
||||
[s0; To avoid RichToValue and ValueTo calls, the client type can
|
||||
also provide constructor from Value and cast operator to Value:&]
|
||||
[s0; &]
|
||||
[s7; struct Foo : AssignValueTypeNo<Foo, 10010> `{&]
|
||||
[s7; struct Foo : ValueType<Foo, 10010> `{&]
|
||||
[s7; -|int x;&]
|
||||
[s7; -|&]
|
||||
[s7; ......&]
|
||||
[s7; -|&]
|
||||
[s7; -|operator Value() `{ return RichToValue(`*this); `}&]
|
||||
[s7; -|Foo(const Value`& v) `{ `*this `= ValueTo<Foo>(v); `}&]
|
||||
[s7; -|Foo(const Value`& v) `{ `*this `= v.Get<Foo>(); `}&]
|
||||
[s7; `};&]
|
||||
[s7; &]
|
||||
[s7; ......&]
|
||||
|
|
@ -573,4 +572,4 @@ GetHashValue method defines GetHashValue function too.&]
|
|||
[s0; &]
|
||||
[s0; &]
|
||||
[s0; &]
|
||||
[s0; [/ (to be continued)]]
|
||||
[s0; [/ (to be continued)]]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue