From 2ea7c50caaf8c2df8bf9c4bc57ce32e2b4b710c9 Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 16 Dec 2017 11:00:12 +0000 Subject: [PATCH] Core: Improved Create for InArray, One, Any, ArrayMap, FixedArrayMap, SortedArrayMap git-svn-id: svn://ultimatepp.org/upp/trunk@11558 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/FixedMap.h | 2 +- uppsrc/Core/InVector.h | 6 +++--- uppsrc/Core/Map.h | 2 +- uppsrc/Core/Other.h | 8 ++++---- uppsrc/Core/src.tpp/Any_en-us.tpp | 6 +++--- uppsrc/Core/src.tpp/ArrayMap_en-us.tpp | 7 ++++--- uppsrc/Core/src.tpp/InArray_en-us.tpp | 12 ++++++------ uppsrc/Core/src.tpp/One_en-us.tpp | 6 +++--- uppsrc/Core/src.tpp/SortedArrayMap_en-us.tpp | 7 ++++--- 9 files changed, 29 insertions(+), 27 deletions(-) diff --git a/uppsrc/Core/FixedMap.h b/uppsrc/Core/FixedMap.h index 7cc3ae573..a5cb38990 100644 --- a/uppsrc/Core/FixedMap.h +++ b/uppsrc/Core/FixedMap.h @@ -104,7 +104,7 @@ public: T& Add(const K& k) { return B::Add(k); } T& Add(const K& k, T *newt) { B::key.Add(k); return B::value.Add(newt); } template - TT& Create(const K& k, Args... args) { TT *q = new TT(args...); B::key.Add(k); return static_cast(B::value.Add(q)); } + TT& Create(const K& k, Args&&... args) { TT *q = new TT(std::forward(args)...); B::key.Add(k); return static_cast(B::value.Add(q)); } FixedArrayMap(const FixedArrayMap& s, int) : FixedAMap, Less>(s, 1) {} FixedArrayMap(Vector&& ndx, Array&& val) : FixedAMap, Less>(pick(ndx), pick(val)) {} diff --git a/uppsrc/Core/InVector.h b/uppsrc/Core/InVector.h index b1380c4bf..270d1ff22 100644 --- a/uppsrc/Core/InVector.h +++ b/uppsrc/Core/InVector.h @@ -301,7 +301,7 @@ public: T& Insert(int i) { return Insert(i, new T); } T& Insert(int i, const T& x) { return Insert(i, new T(x)); } template - TT& InsertCreate(int i, Args... args) { TT *q = new TT(args...); Insert(i, q); return *q; } + TT& InsertCreate(int i, Args&&... args) { TT *q = new TT(std::forward(args)...); Insert(i, q); return *q; } void InsertN(int i, int count); template @@ -319,7 +319,7 @@ public: void AddN(int n) { InsertN(GetCount(), n); } T& Add(T *newt) { Insert(GetCount(), newt); return *newt; } template - TT& Create(Args... args) { TT *q = new TT(args...); Add(q); return *q; } + TT& Create(Args&&... args) { TT *q = new TT(std::forward(args)...); Add(q); return *q; } int GetCount() const { return iv.GetCount(); } bool IsEmpty() const { return GetCount() == 0; } @@ -727,7 +727,7 @@ public: T& Add(const K& k) { B::value.res = NULL; B::key.Add(k); return *(T*)B::value.res; } T& Add(const K& k, T *newt) { B::value.res = newt; B::key.Add(k); return *newt; } template - TT& Create(const K& k, Args... args) { TT *q = new TT(args...); B::value.res = q; B::key.Add(k); return *q; } + TT& Create(const K& k, Args&&... args) { TT *q = new TT(std::forward(args)...); B::value.res = q; B::key.Add(k); return *q; } int FindAdd(const K& k) { B::value.res = NULL; return B::key.FindAdd(k); } int FindAdd(const K& k, const T& init); diff --git a/uppsrc/Core/Map.h b/uppsrc/Core/Map.h index 1af71ecc2..490eba3c2 100644 --- a/uppsrc/Core/Map.h +++ b/uppsrc/Core/Map.h @@ -222,7 +222,7 @@ public: T& Insert(int i, const K& k, T *newt) { B::key.Insert(i, k); return B::value.Insert(i, newt); } using B::Insert; template - TT& Create(const K& k, Args... args) { TT *q = new TT(args...); B::key.Add(k); return static_cast(B::value.Add(q)); } + TT& Create(const K& k, Args&&... args) { TT *q = new TT(std::forward(args)...); B::key.Add(k); return static_cast(B::value.Add(q)); } T& Set(int i, T *ptr) { return B::value.Set(i, ptr); } T *PopDetach() { B::key.Drop(); return B::value.PopDetach(); } diff --git a/uppsrc/Core/Other.h b/uppsrc/Core/Other.h index 3e142d228..bd16b0375 100644 --- a/uppsrc/Core/Other.h +++ b/uppsrc/Core/Other.h @@ -46,11 +46,11 @@ public: T& operator*() { ASSERT(ptr); return *ptr; } template - TT& Create(Args... args) { TT *q = new TT(args...); Attach(q); return *q; } + TT& Create(Args&&... args) { TT *q = new TT(std::forward(args)...); Attach(q); return *q; } template // with C++ conforming compiler, this would not be needed - GCC bug workaround TT& Create() { TT *q = new TT; Attach(q); return *q; } template - T& Create(Args... args) { T *q = new T(args...); Attach(q); return *q; } + T& Create(Args&&... args) { T *q = new T(std::forward(args)...); Attach(q); return *q; } T& Create() { T *q = new T; Attach(q); return *q; } template @@ -90,7 +90,7 @@ class Any : Moveable { T data; template - Data(Args... args) : data(args...) { typeno = StaticTypeNo(); } + Data(Args&&... args) : data(std::forward(args)...) { typeno = StaticTypeNo(); } }; BaseData *ptr; @@ -99,7 +99,7 @@ class Any : Moveable { void Pick(Any&& s) { ptr = s.ptr; const_cast(s).ptr = NULL; } public: - template T& Create(Args... args) { Clear(); Data *x = new Data(args...); ptr = x; return x->data; } + template T& Create(Args&&... args) { Clear(); Data *x = new Data(std::forward(args)...); ptr = x; return x->data; } template bool Is() const { return ptr && ptr->typeno == StaticTypeNo(); } template T& Get() { ASSERT(Is()); Chk(); return ((Data*)ptr)->data; } template const T& Get() const { ASSERT(Is()); Chk(); return ((Data*)ptr)->data; } diff --git a/uppsrc/Core/src.tpp/Any_en-us.tpp b/uppsrc/Core/src.tpp/Any_en-us.tpp index d43bf67d3..5d080dc86 100644 --- a/uppsrc/Core/src.tpp/Any_en-us.tpp +++ b/uppsrc/Core/src.tpp/Any_en-us.tpp @@ -39,9 +39,9 @@ while destroying its content by picking..&] [s0; &] [ {{10000F(128)G(128)@1 [s0; [* Public Method List]]}}&] [s3;%- &] -[s5;:Upp`:`:Any`:`:Create`(Args`.`.`.args`):%- [@(0.0.255) template]_<[@(0.0.255) class]_ -[*@4 T], [@(0.0.255) class...]_[*@4 Args]>_[*@4 T][@(0.0.255) `&]_[* Create]([*@4 Args][@(0.0.255) . -..]_args)&] +[s5;:Upp`:`:Any`:`:Create`(Args`&`&`.`.`.args`):%- [@(0.0.255) template]_<[@(0.0.255) cla +ss]_[*@4 T], [@(0.0.255) class...]_[*@4 Args]>_[*@4 T][@(0.0.255) `&]_[* Create]([*@4 Args][@(0.0.255) `& +`&...]_args)&] [s2; Creates content of type T inside Any. Additional parameters to constructor can be specified.&] [s3;%- &] diff --git a/uppsrc/Core/src.tpp/ArrayMap_en-us.tpp b/uppsrc/Core/src.tpp/ArrayMap_en-us.tpp index ade9bad9d..e23a8ff67 100644 --- a/uppsrc/Core/src.tpp/ArrayMap_en-us.tpp +++ b/uppsrc/Core/src.tpp/ArrayMap_en-us.tpp @@ -137,9 +137,10 @@ from T as well. No constructor is applied.&] [s7; [%-*@3 newt]-|Value.&] [s3; &] [s4;%- &] -[s5;:Upp`:`:ArrayMap`:`:Create`(const K`&`,Args`.`.`.args`):%- [@(0.0.255) template]_<[@(0.0.255) c -lass]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* Create]([@(0.0.255) c -onst]_[*@4 K][@(0.0.255) `&]_[*@3 k], [*@4 Args][@(0.0.255) ...]_args)&] +[s5;:Upp`:`:ArrayMap`:`:Create`(const K`&`,Args`&`&`.`.`.args`):%- [@(0.0.255) template +]_<[@(0.0.255) class]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* Cr +eate]([@(0.0.255) const]_[*@4 K][@(0.0.255) `&]_[*@3 k], [*@4 Args][@(0.0.255) `&`&...]_args) +&] [s2;%- [%% Adds a new key ][*@3 k][%% with value element of type ][*@4 TT][%% (must be derived from ][*@4 T]). Value is constructd in`-place, with args as constructor parameters.&] diff --git a/uppsrc/Core/src.tpp/InArray_en-us.tpp b/uppsrc/Core/src.tpp/InArray_en-us.tpp index 3f49e58fe..a5fd67c2e 100644 --- a/uppsrc/Core/src.tpp/InArray_en-us.tpp +++ b/uppsrc/Core/src.tpp/InArray_en-us.tpp @@ -47,9 +47,9 @@ iterators.&] [s2; Inserts a copy of [%-*@3 x] at [%-*@3 i]. Invalidates iterators.&] [s3; &] [s4;%- &] -[s5;:Upp`:`:InArray`:`:InsertCreate`(int`,Args`.`.`.args`):%- [@(0.0.255) template]_<[@(0.0.255) c -lass]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* InsertCreate]([@(0.0.255) i -nt]_[*@3 i], [*@4 Args][@(0.0.255) ...]_args)&] +[s5;:Upp`:`:InArray`:`:InsertCreate`(int`,Args`&`&`.`.`.args`):%- [@(0.0.255) template]_ +<[@(0.0.255) class]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* Inse +rtCreate]([@(0.0.255) int]_[*@3 i], [*@4 Args][@(0.0.255) `&`&...]_args)&] [s2; Creates a in`-place constructed element (with args as constructor parameters) of type [%-*@4 TT] and inserts it at [%-*@3 i]. Invalidates iterators.&] @@ -91,9 +91,9 @@ t]_[*@3 i])&] [s2; Same as Insert(GetCount(), [%-*@3 newt]). Invalidates iterators.&] [s3; &] [s4;%- &] -[s5;:Upp`:`:InArray`:`:Create`(Args`.`.`.args`):%- [@(0.0.255) template]_<[@(0.0.255) cla -ss]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* Create]([*@4 Args][@(0.0.255) . -..]_args)&] +[s5;:Upp`:`:InArray`:`:Create`(Args`&`&`.`.`.args`):%- [@(0.0.255) template]_<[@(0.0.255) c +lass]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* Create]([*@4 Args][@(0.0.255) `& +`&...]_args)&] [s2; Same as InsertCreate<[%-*@4 TT]>(GetCount(), args...). Invalidates iterators.&] [s3; &] diff --git a/uppsrc/Core/src.tpp/One_en-us.tpp b/uppsrc/Core/src.tpp/One_en-us.tpp index 26411ab7c..70a126cc5 100644 --- a/uppsrc/Core/src.tpp/One_en-us.tpp +++ b/uppsrc/Core/src.tpp/One_en-us.tpp @@ -125,9 +125,9 @@ content. Returns constant reference to content.&] Returns reference to content.&] [s3; &] [s4; &] -[s5;:Upp`:`:One`:`:Create`(Args`.`.`.args`): [@(0.0.255) template]_<[@(0.0.255) class]_[*@4 T -T], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* Create]([*@4 Args][@(0.0.255) . -..]_args)&] +[s5;:Upp`:`:One`:`:Create`(Args`&`&`.`.`.args`): [@(0.0.255) template]_<[@(0.0.255) class +]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* Create]([*@4 Args][@(0.0.255) `& +`&...]_args)&] [s2;%% Creates [%-*@4 TT] content in One. If there is content in One while calling this function, it is destroyed. Additional arguments can be specified for [%-*@4 TT ]constructor.&] diff --git a/uppsrc/Core/src.tpp/SortedArrayMap_en-us.tpp b/uppsrc/Core/src.tpp/SortedArrayMap_en-us.tpp index 08556cb41..5a3b8bb8a 100644 --- a/uppsrc/Core/src.tpp/SortedArrayMap_en-us.tpp +++ b/uppsrc/Core/src.tpp/SortedArrayMap_en-us.tpp @@ -46,9 +46,10 @@ nst]_[*@4 K][@(0.0.255) `&]_[*@3 k], [*@4 T]_`*[*@3 newt])&] [%-*@3 newt] `- SortedArrayMap takes ownership. Returns [%- `*][%-*@3 newt].&] [s3; &] [s4;%- &] -[s5;:Upp`:`:SortedArrayMap`:`:Create`(const K`&`,Args`.`.`.args`):%- [@(0.0.255) templa -te]_<[@(0.0.255) class]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `&]_[* C -reate]([@(0.0.255) const]_[*@4 K][@(0.0.255) `&]_[*@3 k], [*@4 Args][@(0.0.255) ...]_args)&] +[s5;:Upp`:`:SortedArrayMap`:`:Create`(const K`&`,Args`&`&`.`.`.args`):%- [@(0.0.255) te +mplate]_<[@(0.0.255) class]_[*@4 TT], [@(0.0.255) class...]_[*@4 Args]>_[*@4 TT][@(0.0.255) `& +]_[* Create]([@(0.0.255) const]_[*@4 K][@(0.0.255) `&]_[*@3 k], [*@4 Args][@(0.0.255) `&`&...]_ +args)&] [s2; Inserts the key at upper bound key position and creates a new value of type [%-*@4 TT] using args as constructor parameters. [%-*@4 TT] has to be derived from [%-*@4 T]. Returns a reference