diff --git a/tutorial/CoreTutorial/CapturingContainers.cpp b/tutorial/CoreTutorial/CapturingContainers.cpp new file mode 100644 index 000000000..7e63bc1fa --- /dev/null +++ b/tutorial/CoreTutorial/CapturingContainers.cpp @@ -0,0 +1,22 @@ +#include "Tutorial.h" + +void CapturingContainers() +{ + /// .Capturing U++ containers into lambdas + + /// Capturing objects with pick/clone semantics can be achieved using %capture with an + /// initializer%: + + Vector x{ 1, 2 }; + Array y{ "one", "two" }; + Event<> ev = [x = pick(x), y = clone(y)] { DUMP(x); DUMP(y); }; + + DUMP(x); // x is picked, so empty + DUMP(y); // y was cloned, so it retains original value + + LOG("About to invoke event"); + + ev(); + + /// +} diff --git a/tutorial/CoreTutorial/CoreTutorial.upp b/tutorial/CoreTutorial/CoreTutorial.upp index 0f89cf439..a2b47aa0a 100644 --- a/tutorial/CoreTutorial/CoreTutorial.upp +++ b/tutorial/CoreTutorial/CoreTutorial.upp @@ -17,6 +17,7 @@ file Value.cpp, Null.cpp, Value2.cpp, + ValueArrayMap.cpp, CombineHash.cpp, Compare.cpp, Vector.cpp, @@ -37,6 +38,7 @@ file Ranges.cpp, Sort.cpp, Function.cpp, + CapturingContainers.cpp, tutorial2.cpp, help.qtf; diff --git a/tutorial/CoreTutorial/ToDo.cpp b/tutorial/CoreTutorial/ToDo.cpp index 598e34eae..86d2b482e 100644 --- a/tutorial/CoreTutorial/ToDo.cpp +++ b/tutorial/CoreTutorial/ToDo.cpp @@ -1,25 +1,11 @@ #include "Tutorial.h" #if 0 - return CombineCompare(a.NDX, b.NDX)(a.V_TEXT, b.V_TEXT)(a.V_NUMBER)(b.V_NUMBER); - - Vector id = clone(_id); - SplitTo(ln.Mid(4), '@', path, ln); CONSOLE_APP_MAIN { - Event&> h = [](const Vector& x) { DUMP(x); }; - - Event<> ev; - - { - Vector x = { 1, 2 }; - ev = [=, x = pick(x)] { h(x); }; - } - - ev(); } #endif diff --git a/tutorial/CoreTutorial/ValueArrayMap.cpp b/tutorial/CoreTutorial/ValueArrayMap.cpp new file mode 100644 index 000000000..0ab0a6982 --- /dev/null +++ b/tutorial/CoreTutorial/ValueArrayMap.cpp @@ -0,0 +1,118 @@ +#include "Tutorial.h" + +void ValueArrayMap() +{ + /// .`ValueArray` and `ValueMap` + + /// `ValueArray` is a type that represents an array of `Value`s: + + ValueArray va{1, 2, 3}; + + DUMP(va); + + /// ValueArray can be assigned to Value (and back): + + Value v = va; + + DUMP(v); + DUMP(v.Is()); // must be exactly ValueArray + DUMP(IsValueArray(v)); // is ValueArray or ValueMap (which is convertible to ValueArray) + + ValueArray va2 = v; + + DUMP(va2); + + /// Elements can be appended using `Add` method or `operator<<`, element at + /// index can be changed with `Set`: + + va.Add(10); + va << 20 << 21; + va.Set(0, 999); + + DUMP(va); + + /// Elements can be removed: + + va.Remove(0, 2); + + DUMP(va); + + /// and inserted: + + va.Insert(1, v); + + DUMP(va); + + /// It is possible to get a reference to element at index, however note that some + /// ^topic://Core/srcdoc/ValueReference$en-us:special rules^ apply here: + + va.At(0) = 222; + + DUMP(va); + + /// If `Value` contains `ValueArray`, `Value::GetCount` method returns the number of + /// elements in the array (if there is no `ValueArray` in `Value`, it returns zero). You + /// can use `Value::operator[](int)` to get constant reference to `ValueArray` elements: + + for(int i = 0; i < v.GetCount(); i++) + LOG(v[i]); + + /// It is even possible to directly add element to `Value` if it contains `ValueArray`: + + v.Add(4); + + DUMP(v); + + /// Or even get a reference to element at some index (with + /// ^topic://Core/srcdoc/ValueReference$en-us:special rules^): + + v.At(0) = 111; + + DUMP(v); + + /// `ValueMap` can store key - value pairs and retrieve value for key quickly. Note that + /// keys are not limited to `String`, but can be any `Value` with `operator==` and hash + /// code defined. + + /// `Add` method or `operator()` add data to `ValueMap`: + + ValueMap m; + + m.Add("one", 1); + m("two", 2)("three", 3); + + DUMP(m); + + /// `operator[]` retrieves the value at the key: + + DUMP(m["two"]); + + /// Just like `VectorMap`, `ValueMap` is ordered, so the order of adding pairs to it + /// matters: + + ValueMap m2; + + m2.Add("two", 2); + m2("one", 1)("three", 3); + + DUMP(m2); + DUMP(m == m2); // different order of adding means they are not equal + + /// 'Unordered' equality test can be done using `IsSame`: + + DUMP(m.IsSame(m2)); + + /// + + Value j = ParseJSON("{ \"array\" : [ 1, 2, 3 ] }"); + + DUMP(j); + + /// + + j("value") = m; + + DUMP(AsJSON(j)); + + /// +} diff --git a/tutorial/CoreTutorial/tutorial2.cpp b/tutorial/CoreTutorial/tutorial2.cpp index 42b150518..4e9f2790f 100644 --- a/tutorial/CoreTutorial/tutorial2.cpp +++ b/tutorial/CoreTutorial/tutorial2.cpp @@ -8,9 +8,6 @@ GUI_APP_MAIN DO(WStringTutorial); DO(DateTime); DO(AsStringTutorial); - DO(ValueTutorial); - DO(NullTutorial); - DO(Value2Tutorial); DO(CombineHashTutorial); DO(ComparableTutorial); @@ -36,6 +33,12 @@ GUI_APP_MAIN DO(Sorting); DO(FunctionTutorial); + DO(CapturingContainers); + + DO(ValueTutorial); + DO(NullTutorial); + DO(Value2Tutorial); + DO(ValueArrayMap); MakeTutorial(); }