Skip to content

Commit 94dad95

Browse files
Handle polymorphic classes properly in RESTProcess.
1 parent c2be4af commit 94dad95

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

RESTProcess_base.h

+19-5
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,14 @@ namespace classdesc
539539
is_class<T>,
540540
Not<is_container<T>>,
541541
Not<is_smart_ptr<T>>,
542-
Not<is_string<T>>
542+
Not<is_string<T>>,
543+
Not<is_excluded<T>>
543544
> {};
544545

545546

546547
template <class T>
547-
void RESTProcessp(RESTProcess_t& repo, const string& d, Exclude<T>& a)
548+
typename enable_if<is_excluded<T>,void>::T
549+
RESTProcessp(RESTProcess_t& repo, const string& d, T& a)
548550
{}
549551

550552
template <class T>
@@ -658,7 +660,8 @@ namespace classdesc
658660
erase(U& o,typename U::iterator i) {o.erase(i);}
659661

660662
template <class U>
661-
void erase(U& o,typename U::const_iterator i) {}
663+
typename enable_if<Not<is_same<typename U::iterator,typename U::const_iterator>>,void>::T
664+
erase(U& o,typename U::const_iterator i) {}
662665

663666
template <class U>
664667
typename enable_if<Not<has_member_erase<U,typename U::iterator(U::*)(typename U::const_iterator)>>,void>::T
@@ -714,6 +717,12 @@ namespace classdesc
714717
RESTProcessValueSequence(Args&&... args): RESTProcessSequence<T>(actual), actual(std::forward<Args>(args)...) {}
715718
};
716719

720+
template <class T, int R> RPPtr copyMultiArrayIterator(MultiArray<T,R>& x);
721+
template <class T, int R> RPPtr copyMultiArrayIterator(const MultiArray<T,R>& x);
722+
723+
template <class T> RPPtr copyMultiArrayIterator(T& x)
724+
{return std::make_shared<RESTProcessObject<T>>(x);}
725+
717726
template <class T> struct RESTProcessMultiArray: public RESTProcessBase
718727
{
719728
T actual;
@@ -732,7 +741,7 @@ namespace classdesc
732741
RPPtr getElem(const REST_PROCESS_BUFFER& index) override {
733742
size_t idx; index>>idx;
734743
if (idx<actual.size())
735-
return std::make_shared<RESTProcessMultiArray<typename T::value_type>>(actual[idx]);
744+
return copyMultiArrayIterator(actual[idx]);
736745
return std::make_shared<RESTProcessVoid>();
737746
}
738747
RPPtr setElem(const REST_PROCESS_BUFFER& index, const REST_PROCESS_BUFFER& value) override {
@@ -741,13 +750,18 @@ namespace classdesc
741750
{
742751
auto elem=actual[idx];
743752
convert(elem,value);
744-
return std::make_shared<RESTProcessMultiArray<typename T::value_type>>(elem);
753+
return copyMultiArrayIterator(elem);
745754
}
746755
return std::make_shared<RESTProcessVoid>();
747756
}
748757
size_t size() const override {return actual.size();}
749758
};
750759

760+
template <class T, int R> RPPtr copyMultiArrayIterator(MultiArray<T,R>& x)
761+
{return std::make_shared<RESTProcessMultiArray<MultiArray<T,R>>>(x);}
762+
template <class T, int R> RPPtr copyMultiArrayIterator(const MultiArray<T,R>& x)
763+
{return std::make_shared<RESTProcessMultiArray<MultiArray<T,R>>>(x);}
764+
751765
template <class T> struct RESTProcessMultiArray<classdesc::MultiArray<T,1>>:
752766
public RESTProcessSequence<classdesc::MultiArray<T,1>>
753767
{

RESTProcess_epilogue.h

+20-7
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,28 @@ namespace classdesc
102102
return {{"vector<string>",{}}};
103103
}
104104

105-
template <class T>
106-
RESTProcess_t RESTProcessObject<T>::list() const
105+
template <class T>
106+
typename enable_if<is_base_of<PolyRESTProcessBase, T>, RESTProcess_t>::T
107+
rlist(T& obj)
108+
{
109+
RESTProcess_t map;
110+
obj.RESTProcess(map,"");
111+
return map;
112+
}
113+
114+
template <class T>
115+
typename enable_if<Not<is_base_of<PolyRESTProcessBase, T>>, RESTProcess_t>::T
116+
rlist(T& obj)
107117
{
108118
RESTProcess_t map;
109119
RESTProcess(map,"",obj);
110120
return map;
111121
}
112122

123+
template <class T>
124+
RESTProcess_t RESTProcessObject<T>::list() const
125+
{return rlist(obj);}
126+
113127
inline RESTProcess_t RESTProcessOverloadedFunction::list() const {return {};}
114128

115129
template <class T>
@@ -167,7 +181,7 @@ namespace classdesc
167181
RESTProcessp(RESTProcess_t& repo, string d, T& obj)
168182
{
169183
classdesc_access::access_RESTProcess<typename remove_const<T>::type,void>()(repo,d,obj);
170-
repo.add(d, new RESTProcessObject<T>(obj));
184+
if (!is_excluded<T>::value) repo.add(d, new RESTProcessObject<T>(obj));
171185
}
172186

173187
inline RPPtr RESTProcessOverloadedFunction::process(const string& remainder, const REST_PROCESS_BUFFER& arguments)
@@ -285,7 +299,7 @@ namespace classdesc
285299
auto r=mapAndProcess(remainder.substr(idxEnd), arguments, *i);
286300
if (idxEnd==remainder.length() && T::rank>1)
287301
// create a copy of the MultiArray iterator before it goes out of scope
288-
return std::make_shared<RESTProcessMultiArray<typename T::iterator::value_type>>(*i);
302+
return copyMultiArrayIterator(*i);
289303
return r;
290304
}
291305
return sequenceProcess(actual,remainder,arguments);
@@ -489,11 +503,10 @@ namespace classdesc
489503
}
490504
}
491505

492-
template <class T>
506+
template <class T>
493507
RPPtr mapAndProcess(const string& query, const REST_PROCESS_BUFFER& arguments, T& a)
494508
{
495-
RESTProcess_t map;
496-
RESTProcess(map,"",a);
509+
RESTProcess_t map=RESTProcessObject<T>(a).list();
497510
if (query.empty())
498511
{
499512
auto i=map.find("");

classdesc.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ namespace classdesc
428428
///@}
429429

430430
/// transfer the constness property of T to U
431-
template <class T, class U, bool c=is_const<T>::value> struct transfer_const;
431+
template <class T, class U, bool c=Or<is_const<T>,is_const<U>>::value> struct transfer_const;
432432
template <class T, class U> struct transfer_const<T,U,true>
433433
{
434434
typedef typename add_const<U>::type type;
@@ -1065,6 +1065,7 @@ namespace classdesc
10651065

10661066
template <class T> struct is_excluded: public false_type {};
10671067
template <class T> struct is_excluded<Exclude<T> >: public true_type {};
1068+
template <class T> struct is_excluded<const Exclude<T> >: public true_type {};
10681069

10691070

10701071
/// @}

0 commit comments

Comments
 (0)