Repository navigation
Expand file tree
/
Copy pathobject.hpp
More file actions
295 lines (216 loc) · 10.2 KB
/
Copy pathobject.hpp
File metadata and controls
295 lines (216 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
Copyright (C) 2015-present The DotCpp Authors.
This file is part of .C++, a native C++ implementation of
popular .NET class library APIs developed to facilitate
code reuse between C# and C++.
http://github.com/dotcpp/dotcpp (source)
http://dotcpp.org (documentation)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <dot/declare.hpp>
#include <dot/system/ptr.hpp>
#include <dot/system/object_impl.hpp>
#include <dot/system/exception.hpp>
#include <dot/system/string.hpp>
#include <dot/system/enum_impl.hpp>
namespace dot
{
template <class T> class nullable;
class local_minute;
class local_time;
class local_date;
class local_date_time;
template <class T> class enum_impl;
template <class T> class struct_wrapper_impl;
template <class T> using struct_wrapper = ptr<struct_wrapper_impl<T>>;
template <class T> type typeof();
namespace detail
{
template<class W, class T> class inherit_to_string;
template<class W, class T> class inherit_get_hashcode;
template<class W, class T> class inherit_equals;
}
/// Adds support for boxing value types to ptr(object_impl).
class DOT_CLASS object : public ptr<object_impl>
{
typedef ptr<object_impl> base;
public: // CONSTRUCTORS
/// Default constructor to create untyped object.
///
/// This constructor is used, among other things,
/// as argument to lock(...) to provide thread safety.
object() = default;
/// Construct object from nullptr.
object(nullptr_t);
/// Construct object from ptr(object_impl).
object(const ptr<object_impl>& p);
/// Construct object from ptr(T).
template <class T>
object(const ptr<T>& p) : base(p) {}
/// Construct object from object_impl pointer.
object(object_impl* value);
/// Construct object from string.
object(const string& value);
/// Construct object from const string.
object(const char* value);
/// Construct object from bool by boxing.
object(bool value);
/// Construct object from double by boxing.
object(double value);
/// Construct object from int by boxing.
object(int value);
/// Construct object from int by boxing.
object(int64_t value);
/// Construct object from char by boxing.
object(char value);
/// Construct object from nullable by boxing.
template <class T>
object(const nullable<T>& value) { if (value.has_value()) *this = value.value(); }
/// Construct object from local_minute by boxing.
object(const local_minute & value);
/// Construct object from local_time by boxing.
object(const local_time& value);
/// Construct object from local_date by boxing.
object(const local_date& value);
/// Construct object from local_date_time by boxing.
object(const local_date_time& value);
/// Construct object from enum by wrapping it in enum_impl(T).
template <class T>
object(T value, typename std::enable_if<std::is_enum<T>::value>::type* enableif = 0) : base(new enum_impl<T>(value)) {}
/// Construct object from struct wrapper, boxing the value if necessary.
template <typename T>
object(struct_wrapper<T> value) : base(value) {}
/// Construct object from tuple, boxing the value if necessary.
template <typename ... T>
object(const std::tuple<T...> & value) : object(new struct_wrapper_impl<std::tuple<T...>>(value)) {}
public: // METHODS
/// Convert object to enum. Error if object does is not a boxed T.
///
/// This method does not have a counterpart in C#. It provides a more
/// convenient alternative to unboxing than using cast to enum_impl(T).
template <class T>
T to_enum(typename std::enable_if<std::is_enum<T>::value>::type* enableif = 0) const { return ptr<enum_impl<T>>(*this)->value(); }
public: // OPERATORS
/// Forward to operator in type ptr(T).
bool operator==(nullptr_t) const;
/// Forward to operator in type ptr(T).
bool operator!=(nullptr_t) const;
/// Assign nullptr to object.
object& operator=(nullptr_t);
/// Assign ptr(T) to object.
object& operator=(const ptr<object_impl>& p);
/// Assign string to object by boxing.
object& operator=(const string& value);
/// Assign const string to object by boxing.
object& operator=(const char* value);
/// Assign bool to object by boxing.
object& operator=(bool value);
/// Assign double to object by boxing.
object& operator=(double value);
/// Assign int to object by boxing.
object& operator=(int value);
/// Assign long to object by boxing.
object& operator=(int64_t value);
/// Assign char to object by boxing.
object& operator=(char value);
/// Assign struct_wrapper to object by boxing.
template <class T>
object& operator=(const struct_wrapper<T>& value) { base::operator=(value); return *this; }
/// Assign tuple to object by boxing.
template <typename ... T>
object& operator=(const std::tuple<T...> & value) { base::operator=(new struct_wrapper_impl<std::tuple<T...>>(value)); return *this; }
/// Assign nullable to object by boxing.
template <class T>
object& operator=(const nullable<T>& value) { if (value.has_value()) *this = value.value(); else *this = nullptr; return *this; }
/// Assign local_minute to object by boxing.
object& operator=(const local_minute& value);
/// Assign local_time to object by boxing.
object& operator=(const local_time& value);
/// Assign local_date to object by boxing.
object& operator=(const local_date& value);
/// Assign local_date_time to object by boxing.
object& operator=(const local_date_time& value);
/// Convert object to bool by unboxing. Error if object does is not a boxed double.
operator bool() const;
/// Convert object to double by unboxing. Error if object does is not a boxed double.
operator double() const;
/// Convert object to int by unboxing. Error if object does is not a boxed int.
operator int() const;
/// Convert object to long by unboxing. Error if object does is not a boxed long.
operator int64_t() const;
/// Convert object to char by unboxing. Error if object does is not a boxed long.
operator char() const;
/// Convert object to local_minute by unboxing. Error if object does is not a boxed local_minute.
operator local_minute() const;
/// Convert object to local_time by unboxing. Error if object does is not a boxed local_time.
operator local_time() const;
/// Convert object to local_date by unboxing. Error if object does is not a boxed local_date.
operator local_date() const;
/// Convert object to local_date_time by unboxing. Error if object does is not a boxed local_date_time.
operator local_date_time() const;
/// Convert object to struct_wrapper by unboxing. Error if object does is not a boxed T.
template <class T>
operator struct_wrapper<T>() const { return this->as<struct_wrapper<T>>(); } // TODO - replace as by cast_to?
/// Convert object to tuple by unboxing. Error if object does is not a boxed T.
template <class ... T>
operator std::tuple<T...>() const { return *this->as<struct_wrapper<std::tuple<T...>>>(); } // TODO - replace as by cast_to?
template <class T, class enabled = typename std::enable_if<std::is_enum<T>::value>::type* >
operator T() const { return ptr<enum_impl<T>>(*this)->value(); }
/// Convert object to string by unboxing. Error if object does is not a boxed string.
operator string() const;
bool operator ==(object rhs) const { throw exception("Not implemented"); return false; }
public: // STATIC
/// Determines whether the specified System.object instances are the same instance.
static bool ReferenceEquals(object objA, object objB);
};
/// Initializes a new instance of object.
inline object make_object() { return object(new object_impl); }
}
#include <dot/detail/struct_wrapper.hpp>
namespace dot
{
/// Wraps struct into object.
template <class T>
class struct_wrapper_impl
: public virtual object_impl
, public T
, public detail::inherit_to_string<struct_wrapper_impl<T>, T>
, public detail::inherit_get_hashcode<struct_wrapper_impl<T>, T>
, public detail::inherit_equals<struct_wrapper_impl<T>, T>
{
public:
struct_wrapper_impl(const T& value) : T(value) {}
public:
static type typeof()
{
return ::dot::typeof<T>();
}
type get_type() override
{
return typeof();
}
virtual string to_string() override { return detail::inherit_to_string<struct_wrapper_impl<T>, T>::to_string(); }
virtual size_t hash_code() override { return detail::inherit_get_hashcode<struct_wrapper_impl<T>, T>::hash_code(); }
bool equals(object obj) override { return detail::inherit_equals<struct_wrapper_impl<T>, T>::equals(obj); }
};
}
namespace std
{
/// Implements hash struct used by STL unordered_map for object.
template <>
struct hash<dot::object> : public hash<dot::ptr<dot::object_impl>>
{};
/// Implements equal_to struct used by STL unordered_map for object.
template <>
struct equal_to<dot::object> : public equal_to<dot::ptr<dot::object_impl>>
{};
}