@@ -9,22 +9,60 @@ namespace jinja2
99template <typename T>
1010Value Reflect (T&& val);
1111
12- #if 0
12+ template <typename T, bool val>
13+ struct TypeReflectedImpl : std::integral_constant<bool , val>
14+ {
15+ };
16+
17+ template <typename T>
18+ struct TypeReflected : TypeReflectedImpl<T, true >
19+ {
20+ using FieldAccessor = std::function<Value (const T& value)>;
21+ };
22+
23+
24+
25+ template <typename T>
26+ struct TypeReflection : TypeReflectedImpl<T, false >
27+ {
28+ };
29+
1330template <typename Derived>
14- class ReflectedMapImplBase : public ReflectedMap::ItemAccessor
31+ class ReflectedMapImplBase : public MapItemAccessor
1532{
1633public:
1734 bool HasValue (const std::string& name) const override
1835 {
1936 return Derived::GetAccessors ().count (name) != 0 ;
2037 }
21- Value GetValue (const std::string& name) const override
38+ Value GetValueByName (const std::string& name) const override
2239 {
2340 auto & accessors = Derived::GetAccessors ();
2441 auto p = accessors.find (name);
2542 if (p == accessors.end ())
2643 throw std::runtime_error (" Invalid field access" );
2744
45+ return static_cast <const Derived*>(this )->GetField (p->second );
46+ }
47+ std::vector<std::string> GetKeys () const
48+ {
49+ std::vector<std::string> result;
50+ auto & accessors = Derived::GetAccessors ();
51+ for (auto & i : accessors)
52+ result.push_back (i.first );
53+
54+ return result;
55+ }
56+ size_t GetSize () const override
57+ {
58+ return Derived::GetAccessors ().size ();
59+ }
60+ virtual Value GetValueByIndex (int64_t idx) const
61+ {
62+ auto & accessors = Derived::GetAccessors ();
63+ auto p = accessors.begin ();
64+ std::advance (p, idx);
65+
2866 return static_cast <const Derived*>(this )->GetField (p->second );
2967 }
3068};
@@ -34,30 +72,31 @@ class ReflectedMapImpl : public ReflectedMapImplBase<ReflectedMapImpl<T>>
3472{
3573public:
3674 ReflectedMapImpl (T val) : m_value(val) {}
37- using FieldAccessor = std::function<Value (const T& value)>;
75+ ReflectedMapImpl ( const T* val) : m_valuePtr(val) {}
3876
39- static auto& GetAccessors();
40- static std::string ToString(const T& value);
77+ static auto GetAccessors () {return TypeReflection<T>::GetAccessors ();}
4178 template <typename Fn>
4279 Value GetField (Fn&& accessor) const
4380 {
44- return accessor(m_value);
81+ return accessor (m_valuePtr ? *m_valuePtr : m_value);
4582 }
4683
47- std::string ToString() const override
48- {
49- return ToString(m_value);
50- }
5184private:
5285 T m_value;
86+ const T* m_valuePtr = nullptr ;
5387};
54- #endif
5588
5689namespace detail
5790{
5891template <typename T, typename Tag = void >
5992struct Reflector ;
6093
94+ template <typename T>
95+ using IsReflectedType = std::enable_if_t <TypeReflection<T>::value>;
96+
97+ // using IsReflectedType = std::enable_if_t<std::is_same<decltype(ReflectedMapImpl<T>::GetAccessors())::key_type, std::string>::value>;
98+ // using IsReflectedType = typename Type2Void<typename Type2TypeT<decltype(TypeReflection<T>::GetAccessors())>::key_type>::type;
99+
61100struct ContainerReflector
62101{
63102 template <typename T>
@@ -136,9 +175,29 @@ struct Reflector<std::vector<T>>
136175 {
137176 return ContainerReflector::CreateFromValue (std::move (val));
138177 }
139- static auto CreateFromPtr (const std::vector<T>* val)
178+ template <typename U>
179+ static auto CreateFromPtr (U&& val)
140180 {
141- return ContainerReflector::CreateFromPtr (val);
181+ return ContainerReflector::CreateFromPtr (std::forward<U>(val));
182+ }
183+ };
184+
185+ template <typename T>
186+ struct Reflector <T, IsReflectedType<T>>
187+ {
188+ static auto Create (const T& val)
189+ {
190+ return GenericMap ([accessor = ReflectedMapImpl<T>(val)]() {return &accessor;});
191+ }
192+
193+ static auto CreateFromPtr (const T* val)
194+ {
195+ return GenericMap ([accessor = ReflectedMapImpl<T>(static_cast <const T*>(val))]() {return &accessor;});
196+ }
197+
198+ static auto CreateFromPtr (std::shared_ptr<T> val)
199+ {
200+ return GenericMap ([accessor = ReflectedMapImpl<T>(val.get ())]() {return &accessor;});
142201 }
143202};
144203
@@ -151,6 +210,15 @@ struct Reflector<const T&>
151210 }
152211};
153212
213+ template <typename T>
214+ struct Reflector <T&>
215+ {
216+ static auto Create (T& val)
217+ {
218+ return Reflector<T>::Create (val);
219+ }
220+ };
221+
154222template <typename T>
155223struct Reflector <const T*>
156224{
@@ -169,6 +237,15 @@ struct Reflector<T*>
169237 }
170238};
171239
240+ template <typename T>
241+ struct Reflector <std::shared_ptr<T>>
242+ {
243+ static auto Create (std::shared_ptr<T> val)
244+ {
245+ return Reflector<T>::CreateFromPtr (val);
246+ }
247+ };
248+
172249template <>
173250struct Reflector <std::string>
174251{
0 commit comments