Repository navigation
Expand file tree
/
Copy pathtype.hpp
More file actions
641 lines (527 loc) · 20 KB
/
Copy pathtype.hpp
File metadata and controls
641 lines (527 loc) · 20 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
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/detail/traits.hpp>
#include <dot/detail/reflection_macro.hpp>
#include <dot/system/object.hpp>
#include <dot/system/string.hpp>
#include <dot/system/nullable.hpp>
#include <dot/system/collections/generic/list.hpp>
#include <dot/system/reflection/constructor_info.hpp>
#include <dot/system/reflection/method_info.hpp>
#include <dot/system/reflection/parameter_info.hpp>
#include <dot/system/reflection/field_info.hpp>
#include <dot/noda_time/local_date.hpp>
#include <dot/noda_time/local_time.hpp>
#include <dot/noda_time/local_minute.hpp>
#include <dot/noda_time/local_date_time.hpp>
namespace dot
{
class string_impl; class string;
class type_impl; using type = ptr<type_impl>;
class type_builder_impl; using type_builder = ptr<type_builder_impl>;
class string_impl; class string;
class method_info_impl; using method_info = ptr<method_info_impl>;
class constructor_info_impl; using constqructor_info = ptr<constructor_info_impl>;
template <class T> class list_impl; template <class T> using list = ptr<list_impl<T>>;
template <class class_t, class ... Args> class member_constructor_info_impl;
template <class T> type typeof();
/// builder for Type.
class DOT_CLASS type_builder_impl final : public virtual object_impl
{
template <class>
friend type_builder make_type_builder(string nspace, string name);
friend class type_impl;
private:
string full_name_;
list<method_info> methods_;
list<constructor_info> ctors_;
list<field_info> fields_;
type type_;
type base_;
list<type> interfaces_;
list<type> generic_args_;
bool is_class_;
bool is_enum_ = false;
public: // METHODS
/// Add public field of the current Type.
template <class class_t, class fld>
type_builder with_field(string name, fld class_t::*prop)
{
if (fields_.is_empty())
{
fields_ = make_list<field_info>();
}
fields_->add(make_field_info<fld, class_t>(name, type_, dot::typeof<fld>(), prop));
return this;
}
/// Add public member method of the current Type.
template <class class_t, class return_t, class ... args>
type_builder with_method(string name, return_t(class_t::*mth) (args ...), std::vector<string> const& names) // TODO Change to List? Make overload?
{
const int args_count = sizeof...(args);
if (args_count != names.size())
throw exception("Wrong number of parameters for method " + full_name_);
if (methods_.is_empty())
{
methods_ = make_list<method_info>();
}
list<parameter_info> parameters = make_list<parameter_info>(sizeof...(args));
std::vector<type> param_types = { dot::typeof<args>()... };
for (int i = 0; i < args_count; ++i)
{
parameters[i] = make_parameter_info(names[i], param_types[i], i);
}
method_info method_info = new member_method_info_impl<class_t, return_t, args...>(name, type_, dot::typeof<return_t>(), mth);
method_info->parameters = parameters;
methods_->add(method_info);
return this;
}
/// Add public static method of the current Type.
template <class return_t, class ... args>
type_builder with_method(string name, return_t(*mth) (args ...), std::vector<string> const& names) // TODO Change to List? Make overload?
{
const int args_count = sizeof...(args);
if (args_count != names.size())
throw exception("Wrong number of parameters for method " + full_name_);
if (methods_.is_empty())
{
methods_ = make_list<method_info>();
}
list<parameter_info> parameters = make_list<parameter_info>(sizeof...(args));
std::vector<type> param_types = { dot::typeof<args>()... };
for (int i = 0; i < args_count; ++i)
{
parameters[i] = make_parameter_info(names[i], param_types[i], i);
}
method_info method_info = new static_method_info_impl<return_t, args...>(name, type_, dot::typeof<return_t>(), mth);
method_info->parameters = parameters;
methods_->add(method_info);
return this;
}
/// Add public constructor of the current Type.
template <class class_t, class ... args>
type_builder with_constructor(class_t(*ctor)(args...), std::vector<string> const& names) // TODO Change to List? Make overload?
{
const int args_count = sizeof...(args);
if (args_count != names.size())
throw exception("Wrong number of parameters for method " + full_name_);
if (ctors_.is_empty())
{
ctors_ = make_list<constructor_info>();
}
list<parameter_info> parameters = make_list<parameter_info>(sizeof...(args));
std::vector<type> param_types = { dot::typeof<args>()... };
for (int i = 0; i < args_count; ++i)
{
parameters[i] = make_parameter_info(names[i], param_types[i], i);
}
constructor_info ctor_info = new member_constructor_info_impl<class_t, args...>(type_, ctor);
ctor_info->parameters = parameters;
ctors_->add(ctor_info);
return this;
}
/// Set IsEnum to true.
type_builder is_enum()
{
this->is_class_ = false;
this->is_enum_ = true;
return this;
}
/// Add base type of the current Type.
template <class class_t>
type_builder with_base()
{
if (!(this->base_.is_empty()))
throw exception("Base already defined in class " + full_name_);
this->base_ = dot::typeof<class_t>();
return this;
}
/// Add interface type of the current Type.
template <class class_t>
type_builder with_interface()
{
if (this->interfaces_.is_empty())
this->interfaces_ = make_list<type>();
this->interfaces_->add(dot::typeof<class_t>());
return this;
}
/// Add generic argument type of the current Type.
template <class class_t>
type_builder with_generic_argument()
{
if (this->generic_args_.is_empty())
this->generic_args_ = make_list<type>();
this->generic_args_->add(dot::typeof<class_t>());
return this;
}
/// Build type object from this type builder.
type build();
private: // CONSTRUCTORS
/// Create an empty instance of type_builder.
///
/// This constructor is private. Use make_type_builder() function instead.
type_builder_impl(string nspace, string name, string cppname);
};
/// Create an empty instance of type_builder.
template <class T>
inline type_builder make_type_builder(string nspace, string name)
{
type_builder td = new type_builder_impl(nspace, name, typeid(T).name());
td->is_class_ = std::is_base_of<object_impl, T>::value;
return td;
}
/// Represents type declarations: class types, interface types, array types, value types, enumeration types,
/// type parameters, generic type definitions, and open or closed constructed generic types.
///
/// type_t is the root of the System.Reflection functionality and is the primary way to access metadata.
/// Use the members of type_t to get information about a type declaration, about the members of a type
/// (such as the constructors, methods, fields, properties, and events of a class), as well as the module
/// and the assembly in which the class is deployed.
///
/// The type_t object associated with a particular type can be obtained in the following ways:
///
/// \begin{itemize}
///
/// \item The instance object.Gettype_t method returns a type_t object that represents the type of an instance.
/// Because all managed types derive from object, the Gettype_t method can be called on an instance of any type.
///
/// \item The typeof method obtains the type_t object for the argument type.
///
/// \end{itemize}
class DOT_CLASS type_impl final : public virtual object_impl
{
friend class type_builder_impl;
template <class T>
friend struct type_traits;
typedef type_impl self;
private: // FIELDS
list<method_info> methods_;
list<constructor_info> ctors_;
list<type> interfaces_;
list<type> generic_args_;
type base_;
list<field_info> fields_;
public: // PROPERTIES
/// Gets the name of the current type, excluding namespace.
string name; // TODO - replace by method
/// Gets the fully qualified name of the type, including its namespace but not its assembly.
string name_space; // TODO - replace by method
/// Gets the fully qualified name of the type, including its namespace but not its assembly.
string full_name() const { return string::format("{0}.{1}", this->name_space, this->name); } // TODO - replace by string::Join
/// Gets the base type if current type.
type get_base_type() { return base_; }
/// Gets a value indicating whether the System.type_t is a class or a delegate; that is, not a value type or interface.
bool is_class; // TODO - replace by method
/// Gets a value indicating whether the current System.type_t represents an enumeration.
bool is_enum; // TODO - replace by method
public: // METHODS
/// Returns methods of the current type.
list<method_info> get_methods() { return methods_; }
/// Returns constructors of the current type.
list<constructor_info> get_constructors() { return ctors_; }
/// Returns fields of the current type.
list<field_info> get_fields() { return fields_; }
/// Returns interfaces of the current type.
list<type> get_interfaces() { return interfaces_; }
/// Returns interfaces of the current type.
list<type> get_generic_arguments() { return generic_args_; }
/// Searches for the public method with the specified name.
method_info get_method(string name);
/// Searches for the interface with the specified name.
type get_interface(string name);
/// Searches for the field with the specified name.
field_info get_field(string name);
/// A string representing the name of the current type.
virtual string to_string() override { return full_name(); }
/// Get type_t object for the name.
static type get_type_of(string name) { return get_type_map()[name]; }
/// Get derived types list for the name.
static list<type> get_derived_types(string name) { return get_derived_types_map()[name]; }
/// Get derived types list for the type.
static list<type> get_derived_types(type t) { return get_derived_types_map()[t->full_name()]; }
bool equals(object obj) override;
virtual size_t hash_code() override;
private: // METHODS
/// Fill data from builder.
void fill(const type_builder& data);
static std::map<string, type>& get_type_map()
{
static std::map<string, type> map_;
return map_;
}
static std::map<string, list<type>>& get_derived_types_map()
{
static std::map<string, list<type>> map_;
return map_;
}
private: // CONSTRUCTORS
/// Create from builder.
///
/// This constructor is private. Use type_builder->build() method instead.
type_impl(string nspace, string name);
};
/// Initializes a new instance of the type_t class for untyped instance of object.
inline type object_impl::get_type()
{
return typeof();
}
inline type object_impl::typeof()
{
static type type_ = make_type_builder<object_impl>("System", "object")->build();
return type_;
}
template <class T> type list_impl<T>::typeof()
{
static type type_ = make_type_builder<list_impl<T>>("System.Collections.Generic", "List`1")
//DOT_TYPE_CTOR(make_list<T>)
->with_constructor(static_cast<list<T>(*)()>(&make_list<T>), { })
DOT_TYPE_GENERIC_ARGUMENT(T)
->template with_interface<dot::list_base>()
->build();
return type_;
}
template <class T>
struct type_traits
{
static type typeof()
{
string cppname = typeid(typename T::element_type).name(); // TODO - is it faster to use typeid rather than string as key?
auto p = type_impl::get_type_map().find(cppname);
if (p == type_impl::get_type_map().end())
{
return T::element_type::typeof();
}
return p->second;
}
};
template <>
struct type_traits<double>
{
static type typeof()
{
static type type_ = make_type_builder<double>("System", "Double")->build();
return type_;
}
};
template <>
struct type_traits<int64_t>
{
static type typeof()
{
static type type_ = make_type_builder<int64_t>("System", "Int64")->build();
return type_;
}
};
template <>
struct type_traits<int>
{
static type typeof()
{
static type type_ = make_type_builder<int>("System", "Int32")->build();
return type_;
}
};
template <>
struct type_traits<void>
{
static type typeof()
{
static type type_ = make_type_builder<void>("System", "Void")->build();
return type_;
}
};
template <>
struct type_traits<bool>
{
static type typeof()
{
static type type_ = make_type_builder<bool>("System", "Bool")->build();
return type_;
}
};
template <>
struct type_traits<char>
{
static type typeof()
{
static type type_ = make_type_builder<char>("System", "Char")->build();
return type_;
}
};
template <>
struct type_traits<local_date>
{
static type typeof()
{
static type type_ = make_type_builder<local_date>("System", "local_date")->build();
return type_;
}
};
template <>
struct type_traits<local_time>
{
static type typeof()
{
static type type_ = make_type_builder<local_time>("System", "local_time")->build();
return type_;
}
};
template <>
struct type_traits<local_minute>
{
static type typeof()
{
static type type_ = make_type_builder<local_time>("System", "local_minute")->build();
return type_;
}
};
template <>
struct type_traits<local_date_time>
{
static type typeof()
{
static type type_ = make_type_builder<local_date_time>("System", "local_date_time")->build();
return type_;
}
};
template <>
struct type_traits<period>
{
static type typeof()
{
static type type_ = make_type_builder<local_date_time>("System", "Period")->build();
return type_;
}
};
template <class T>
struct type_traits<nullable<T>>
{
static type typeof()
{
static type type_ = make_type_builder<nullable<T>>("System", "nullable<" + dot::typeof<T>()->name + ">")
->template with_generic_argument<T>()
->build();
return type_;
}
};
template <>
struct type_traits<std::tuple<>>
{
static type typeof()
{
static type type_ = make_type_builder<std::tuple<>>("System", "Tuple<>")
->build();
return type_;
}
};
template <class ... T>
struct type_traits<std::tuple<T...>>
{
static type typeof()
{
static type_builder builder =
[]()
{
type_builder type_builder = make_type_builder<std::tuple<T...>>("System", "Tuple<" + get_name<T...>() + ">");
set_generic_args<T ...>(type_builder);
type_builder->with_method("GetItem", &get_item, { "tuple", "index" })
->with_method("SetItem", &set_item, { "tuple", "index", "value" })
->with_constructor(&contructor, {})
;
return type_builder;
}();
static type type_ = builder->build();
return type_;
}
private:
static object contructor()
{
return object(std::tuple<T...>());
}
struct dummy{};
template <int I, typename Dummy = dummy>
struct get_item_impl
{
static object impl(object tuple, int index)
{
if (I == index) return std::get<I>(*(struct_wrapper<std::tuple<T...>>)tuple);
else return get_item_impl<I + 1>::Impl(tuple, index);
}
};
template <typename Dummy>
struct get_item_impl<sizeof...(T), Dummy>
{
static object impl(object tuple, int index)
{
throw exception("Tuple index out of bounds");
}
};
static object get_item(object tuple, int index)
{
return get_item_impl<0>::impl(tuple, index);
}
template <int I, typename Dummy = dummy>
struct set_item_impl
{
static void impl(object tuple, int index, object value)
{
if (I == index) std::get<I>(*(struct_wrapper<std::tuple<T...>>)tuple) = (std::tuple_element_t<I, std::tuple<T...>>)value;
else set_item_impl<I + 1>::impl(tuple, index, value);
}
};
template <typename Dummy>
struct set_item_impl<sizeof...(T), Dummy>
{
static void impl(object tuple, int index, object value)
{
throw exception("Tuple index out of bounds");
}
};
static void set_item(object tuple, int index, object value)
{
set_item_impl<0>::impl(tuple, index, value);
}
template <class Head, class Second, class ... Tail>
static string get_name()
{
return dot::typeof<Head>()->name + get_name<Second, Tail...>();
}
template <class Head>
static string get_name()
{
return dot::typeof<Head>()->name;
}
template <class Head, class Second, class ... Tail>
static type_builder set_generic_args(type_builder tb)
{
return set_generic_args<Second, Tail ... >(tb->with_generic_argument<Head>());
}
template <class Head>
static type_builder set_generic_args(type_builder tb)
{
return tb->with_generic_argument<Head>();
}
};
/// Get type object for the argument.
template <class T>
type typeof()
{
return type_traits<T>::typeof();
}
}