9
9
#include " ./traits_scalar.h"
10
10
#include " ./traits_str.h"
11
11
#include < cstring>
12
- #include < vector>
13
-
14
- /* ********** AnyView ***********/
15
12
16
13
namespace mlc {
14
+
15
+ /* ********** Section 1. Any <=> Any View ***********/
17
16
MLC_INLINE AnyView::AnyView (const Any &src) : MLCAny(static_cast <const MLCAny &>(src)) {}
18
17
MLC_INLINE AnyView::AnyView (Any &&src) : MLCAny(static_cast <const MLCAny &>(src)) {}
18
+ MLC_INLINE Any::Any (const Any &src) : MLCAny(static_cast <const MLCAny &>(src)) { this ->IncRef (); }
19
+ MLC_INLINE Any::Any (Any &&src) : MLCAny(static_cast <const MLCAny &>(src)) { *static_cast <MLCAny *>(&src) = MLCAny (); }
20
+ MLC_INLINE Any::Any (const AnyView &src) : MLCAny(static_cast <const MLCAny &>(src)) {
21
+ this ->SwitchFromRawStr ();
22
+ this ->IncRef ();
23
+ }
24
+ MLC_INLINE Any::Any (AnyView &&src) : MLCAny(static_cast <const MLCAny &>(src)) {
25
+ *static_cast <MLCAny *>(&src) = MLCAny ();
26
+ this ->SwitchFromRawStr ();
27
+ this ->IncRef ();
28
+ }
19
29
MLC_INLINE AnyView &AnyView::operator =(const Any &src) {
20
30
*static_cast <MLCAny *>(this ) = static_cast <const MLCAny &>(src);
21
31
return *this ;
@@ -24,149 +34,109 @@ MLC_INLINE AnyView &AnyView::operator=(Any &&src) {
24
34
*static_cast <MLCAny *>(this ) = static_cast <const MLCAny &>(src);
25
35
return *this ;
26
36
}
27
- MLC_INLINE AnyView::AnyView (::mlc::base::tag::ObjPtr, const MLCObjPtr &src) : MLCAny() {
28
- if (src.ptr != nullptr ) {
29
- this ->type_index = src.ptr ->type_index ;
30
- this ->v_obj = src.ptr ;
31
- }
32
- }
33
- MLC_INLINE AnyView::AnyView (::mlc::base::tag::ObjPtr, MLCObjPtr &&src) : MLCAny() {
34
- if (src.ptr != nullptr ) {
35
- this ->type_index = src.ptr ->type_index ;
36
- this ->v_obj = src.ptr ;
37
- }
37
+ MLC_INLINE Any &Any::operator =(const Any &src) {
38
+ Any (src).Swap (*this );
39
+ return *this ;
38
40
}
39
- template <typename T> MLC_INLINE AnyView::AnyView (::mlc::base::tag::POD, const T &src) : MLCAny() {
40
- ::mlc::base::PODTraits<::mlc::base::RemoveCR<T>>::TypeCopyToAny (src, this );
41
+ MLC_INLINE Any &Any::operator =(Any &&src) {
42
+ Any (std::move (src)).Swap (*this );
43
+ return *this ;
41
44
}
42
- template <typename T> MLC_INLINE AnyView::AnyView (::mlc::base::tag::POD, T &&src) : MLCAny() {
43
- ::mlc::base::PODTraits<::mlc::base::RemoveCR<T>>::TypeCopyToAny (src, this );
45
+ MLC_INLINE Any &Any::operator =(const AnyView &src) {
46
+ Any (src).Swap (*this );
47
+ return *this ;
44
48
}
45
- template <typename T> MLC_INLINE AnyView::AnyView (::mlc::base::tag::RawObjPtr, T *src) : MLCAny() {
46
- ::mlc::base::PtrToAnyView<T>(src, this );
49
+ MLC_INLINE Any &Any::operator =(AnyView &&src) {
50
+ Any (std::move (src)).Swap (*this );
51
+ return *this ;
47
52
}
48
- template <typename T> MLC_INLINE T AnyView::Cast (::mlc::base::tag::ObjPtr) const {
49
- if constexpr (::mlc::base::IsObjRef<T>) {
50
- if (this ->type_index == static_cast <int32_t >(MLCTypeIndex::kMLCNone )) {
51
- using RefT = Ref<typename T::TObj>;
52
- MLC_THROW (TypeError) << " Cannot convert from type `None` to non-nullable `" << ::mlc::base::Type2Str<RefT>::Run ()
53
- << " `" ;
54
- }
55
- }
56
- using TObj = typename T::TObj;
57
- return T ([this ]() -> TObj * {
58
- MLC_TRY_CONVERT (::mlc::base::ObjPtrTraits<TObj>::AnyToOwnedPtr (this ), this ->type_index ,
59
- ::mlc::base::Type2Str<TObj *>::Run ());
60
- }());
61
- }
62
- template <typename T> MLC_INLINE T AnyView::Cast () const {
63
- if constexpr (std::is_same_v<T, Any> || std::is_same_v<T, AnyView>) {
64
- return *this ;
53
+
54
+ /* ********** Section 2. Conversion between Any/AnyView <=> POD ***********/
55
+
56
+ template <typename T> MLC_INLINE AnyView::AnyView (const T &src) : MLCAny() {
57
+ using namespace ::mlc::base;
58
+ if constexpr (HasTypeTraits<T>) {
59
+ TypeTraits<T>::TypeToAny (src, this );
65
60
} else {
66
- return this -> operator T ( );
61
+ (src. operator AnyView ()). Swap (* this );
67
62
}
68
63
}
69
- template <typename T> MLC_INLINE_NO_MSVC T AnyView::Cast (::mlc::base::tag::POD) const {
70
- MLC_TRY_CONVERT (::mlc::base::PODTraits<::mlc::base::RemoveCR<T>>::AnyCopyToType (this ), this ->type_index ,
71
- ::mlc::base::Type2Str<T>::Run ());
72
- }
73
- template <typename _T> MLC_INLINE_NO_MSVC _T AnyView::Cast (::mlc::base::tag::RawObjPtr) const {
74
- using T = std::remove_pointer_t <_T>;
75
- MLC_TRY_CONVERT (::mlc::base::ObjPtrTraits<::mlc::base::RemoveCR<T>>::AnyToUnownedPtr (this ), this ->type_index ,
76
- ::mlc::base::Type2Str<T *>::Run ());
77
- }
78
- template <typename T> MLC_INLINE_NO_MSVC T *AnyView::CastWithStorage (Any *storage) const {
79
- MLC_TRY_CONVERT (::mlc::base::ObjPtrTraits<T>::AnyToOwnedPtrWithStorage (this , storage), this ->type_index ,
80
- ::mlc::base::Type2Str<T *>::Run ());
81
- }
82
- } // namespace mlc
83
64
84
- /* ********** Any ***********/
85
-
86
- namespace mlc {
87
- MLC_INLINE Any::Any (::mlc::base::tag::ObjPtr, const MLCObjPtr &src) : MLCAny() {
88
- if (src.ptr != nullptr ) {
89
- this ->type_index = src.ptr ->type_index ;
90
- this ->v_obj = src.ptr ;
65
+ template <typename T> MLC_INLINE Any::Any (const T &src) : MLCAny() {
66
+ using namespace ::mlc::base;
67
+ if constexpr (HasTypeTraits<RemoveCR<T>>) {
68
+ TypeTraits<RemoveCR<T>>::TypeToAny (src, this );
69
+ this ->SwitchFromRawStr ();
91
70
this ->IncRef ();
71
+ } else {
72
+ (src.operator Any ()).Swap (*this );
92
73
}
93
74
}
94
- MLC_INLINE Any::Any (::mlc::base::tag::ObjPtr, MLCObjPtr &&src) : MLCAny() {
95
- if (src.ptr != nullptr ) {
96
- this ->type_index = src.ptr ->type_index ;
97
- this ->v_obj = src.ptr ;
98
- src.ptr = nullptr ;
99
- }
100
- }
101
- template <typename T> MLC_INLINE Any::Any (::mlc::base::tag::RawObjPtr, T *src) : MLCAny() {
102
- ::mlc::base::PtrToAnyView<T>(src, this );
103
- this ->IncRef ();
104
- }
105
- template <typename T> MLC_INLINE T Any::Cast () const {
106
- if constexpr (std::is_same_v<T, Any> || std::is_same_v<T, AnyView>) {
107
- return *this ;
75
+
76
+ template <typename T> MLC_INLINE_NO_MSVC T AnyView::Cast () const {
77
+ using namespace ::mlc::base;
78
+ if constexpr (HasTypeTraits<T>) {
79
+ MLC_TRY_CONVERT (TypeTraits<T>::AnyToTypeUnowned (this ), this ->type_index , Type2Str<T>::Run ());
108
80
} else {
109
- return this -> operator T ( );
81
+ return T (* this );
110
82
}
111
83
}
112
- template < typename T> MLC_INLINE T Any::Cast (::mlc::base::tag::ObjPtr) const {
113
- if constexpr (::mlc::base::IsObjRef<T>) {
114
- if ( this -> type_index == static_cast < int32_t >(MLCTypeIndex:: kMLCNone )) {
115
- using RefT = Ref< typename T::TObj>;
116
- MLC_THROW (TypeError) << " Cannot convert from type `None` to non-nullable ` " << ::mlc::base:: Type2Str<RefT >::Run ()
117
- << " ` " ;
118
- }
84
+
85
+ template < typename T> MLC_INLINE_NO_MSVC T Any::Cast () const {
86
+ using namespace ::mlc::base ;
87
+ if constexpr (HasTypeTraits<T>) {
88
+ MLC_TRY_CONVERT (TypeTraits<T>:: AnyToTypeUnowned ( this ), this -> type_index , Type2Str<T >::Run ());
89
+ } else {
90
+ return T (* this );
119
91
}
120
- using TObj = typename T::TObj;
121
- return T ([this ]() -> TObj * {
122
- MLC_TRY_CONVERT (::mlc::base::ObjPtrTraits<TObj>::AnyToOwnedPtr (this ), this ->type_index ,
123
- ::mlc::base::Type2Str<TObj *>::Run ());
124
- }());
125
92
}
126
- template <typename T> MLC_INLINE_NO_MSVC T Any::Cast (::mlc::base::tag::POD) const {
127
- MLC_TRY_CONVERT (::mlc::base::PODTraits<::mlc::base::RemoveCR<T>>::AnyCopyToType (this ), this ->type_index ,
128
- ::mlc::base::Type2Str<T>::Run ());
129
- }
130
- template <typename _T> MLC_INLINE_NO_MSVC _T Any::Cast (::mlc::base::tag::RawObjPtr) const {
131
- using T = std::remove_pointer_t <_T>;
132
- MLC_TRY_CONVERT (::mlc::base::ObjPtrTraits<::mlc::base::RemoveCR<T>>::AnyToUnownedPtr (this ), this ->type_index ,
133
- ::mlc::base::Type2Str<T *>::Run ());
93
+
94
+ template <typename T> MLC_INLINE_NO_MSVC T AnyView::CastWithStorage (Any *storage) const {
95
+ using namespace ::mlc::base;
96
+ MLC_TRY_CONVERT (TypeTraits<T>::AnyToTypeWithStorage (this , storage), this ->type_index , Type2Str<T>::Run ());
134
97
}
135
- template <typename T> MLC_INLINE_NO_MSVC T *Any::CastWithStorage (Any *storage) const {
136
- MLC_TRY_CONVERT (::mlc::base::ObjPtrTraits<T>::AnyToOwnedPtrWithStorage (this , storage), this ->type_index ,
137
- ::mlc::base::Type2Str<T *>::Run ());
98
+
99
+ template <typename T> MLC_INLINE_NO_MSVC T Any::CastWithStorage (Any *storage) const {
100
+ using namespace ::mlc::base;
101
+ MLC_TRY_CONVERT (TypeTraits<T>::AnyToTypeWithStorage (this , storage), this ->type_index , Type2Str<T>::Run ());
138
102
}
139
- } // namespace mlc
140
103
141
- namespace mlc {
142
- namespace base {
143
- struct ReflectionHelper {
144
- explicit ReflectionHelper (int32_t type_index);
145
- template <typename Super, typename FieldType>
146
- ReflectionHelper &FieldReadOnly (const char *name, FieldType Super::*field);
147
- template <typename Super, typename FieldType> ReflectionHelper &Field (const char *name, FieldType Super::*field);
148
- template <typename Callable> ReflectionHelper &MemFn (const char *name, Callable &&method);
149
- template <typename Callable> ReflectionHelper &StaticFn (const char *name, Callable &&method);
150
- operator int32_t ();
151
- static std::string DefaultStrMethod (AnyView any);
104
+ /* ********** Section 3. Conversion between Any/AnyView <=> Ref ***********/
152
105
153
- private:
154
- template <typename Cls, typename FieldType> constexpr std::ptrdiff_t ReflectOffset (FieldType Cls::*member) {
155
- return reinterpret_cast <std::ptrdiff_t >(&((Cls *)(nullptr )->*member));
106
+ template <typename T> MLC_INLINE Ref<T>::Ref(const AnyView &src) : TBase() { TBase::_Init<T>(src); }
107
+ template <typename T> MLC_INLINE Ref<T>::Ref(const Any &src) : TBase() { TBase::_Init<T>(src); }
108
+ template <typename T> MLC_INLINE Ref<T>::operator AnyView () const {
109
+ if (this ->ptr != nullptr ) {
110
+ AnyView ret;
111
+ ret.type_index = this ->ptr ->type_index ;
112
+ ret.v_obj = this ->ptr ;
113
+ return ret;
156
114
}
157
- template <typename Super, typename FieldType> MLCTypeField PrepareField (const char *name, FieldType Super::*field);
158
- template <typename Callable> MLCTypeMethod PrepareMethod (const char *name, Callable &&method);
115
+ return AnyView ();
116
+ }
117
+ template <typename T> MLC_INLINE Ref<T>::operator Any () const & {
118
+ if (this ->ptr != nullptr ) {
119
+ Any ret;
120
+ ret.type_index = this ->ptr ->type_index ;
121
+ ret.v_obj = this ->ptr ;
122
+ ::mlc::base::IncRef (this ->ptr);
123
+ return ret;
124
+ }
125
+ return Any ();
126
+ }
127
+ template <typename T> MLC_INLINE Ref<T>::operator Any () && {
128
+ if (this ->ptr != nullptr ) {
129
+ Any ret;
130
+ ret.type_index = this ->ptr ->type_index ;
131
+ ret.v_obj = this ->ptr ;
132
+ this ->ptr = nullptr ;
133
+ return ret;
134
+ }
135
+ return Any ();
136
+ }
159
137
160
- int32_t type_index;
161
- std::vector<MLCTypeField> fields;
162
- std::vector<MLCTypeMethod> methods;
163
- std::vector<Any> method_pool;
164
- std::vector<std::vector<MLCTypeInfo *>> type_annotation_pool;
165
- };
166
- } // namespace base
167
- } // namespace mlc
138
+ /* ********** Section 4. AnyViewArray ***********/
168
139
169
- namespace mlc {
170
140
template <std::size_t N> template <typename ... Args> MLC_INLINE void AnyViewArray<N>::Fill(Args &&...args) {
171
141
static_assert (sizeof ...(args) == N, " Invalid number of arguments" );
172
142
if constexpr (N > 0 ) {
0 commit comments