rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
type.h
Go to the documentation of this file.
1 /************************************************************************************
2 * *
3 * Copyright (c) 2014 Axel Menzel <info@axelmenzel.de> *
4 * *
5 * This file is part of RTTR (Run Time Type Reflection) *
6 * License: MIT License *
7 * *
8 * Permission is hereby granted, free of charge, to any person obtaining *
9 * a copy of this software and associated documentation files (the "Software"), *
10 * to deal in the Software without restriction, including without limitation *
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
12 * and/or sell copies of the Software, and to permit persons to whom the *
13 * Software is furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included in *
16 * all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
24 * SOFTWARE. *
25 * *
26 *************************************************************************************/
27 
28 #ifndef __RTTR_TYPE_H__
29 #define __RTTR_TYPE_H__
30 
32 
33 #include <type_traits>
34 #include <vector>
35 #include <string>
36 #include <memory>
37 
38 namespace rttr
39 {
40 class variant;
41 class constructor;
42 class destructor;
43 class method;
44 class property;
45 class enumeration;
46 class type;
47 
48 template<typename TargetType, typename SourceType>
49 TargetType rttr_cast(SourceType object);
50 
51 namespace detail
52 {
53 class instance;
54 class argument;
55 struct derived_info;
56 struct base_class_info;
57 struct type_converter_base;
58 } // end namespace detail
59 
60 namespace impl
61 {
62 template<typename T>
63 struct MetaTypeInfo;
64 static type get_invalid_type();
65 } // end namespace impl
66 
166 {
167  public:
168  typedef uint16 type_id;
169 
174  type(const type& other);
175 
181  type& operator=(const type& other);
182 
188  bool operator<(const type& other) const;
189 
195  bool operator>(const type& other) const;
196 
202  bool operator>=(const type& other) const;
203 
209  bool operator<=(const type& other) const;
210 
217  bool operator==(const type& other) const;
218 
225  bool operator!=(const type& other) const;
226 
235  type_id get_id() const;
236 
242  std::string get_name() const;
243 
249  bool is_valid() const;
250 
256  operator bool() const;
257 
266  type get_raw_type() const;
267 
273  template<typename T>
274  static type get();
275 
286  template<typename T>
287  static type get(T&& object);
288 
297  static type get(const char* name);
298 
306  static std::vector<type> get_types();
307 
313  bool is_class() const;
314 
320  bool is_enumeration() const;
321 
330  enumeration get_enumeration() const;
331 
337  bool is_array() const;
338 
344  bool is_pointer() const;
345 
351  bool is_primitive() const;
352 
358  bool is_derived_from(const type& other) const;
359 
365  template<typename T>
366  bool is_derived_from() const;
367 
373  std::vector<type> get_base_classes() const;
374 
380  std::vector<type> get_derived_classes() const;
381 
385 
394  constructor get_constructor(const std::vector<type>& params = std::vector<type>() ) const;
395 
401  std::vector<constructor> get_constructors() const;
402 
410  variant create(std::vector<detail::argument> args) const;
411 
420  destructor get_destructor() const;
421 
428  void destroy(variant& obj) const;
429 
430 
438  property get_property(const std::string& name) const;
439 
446  std::vector<property> get_properties() const;
447 
455  static property get_global_property(const std::string& name);
456 
462  static std::vector<property> get_global_properties();
463 
464 
472  variant get_property_value(const std::string& name, detail::instance obj) const;
473 
479  static variant get_property_value(const std::string& name);
480 
488  bool set_property_value(const std::string& name, detail::instance obj, detail::argument arg) const;
489 
495  static bool set_property_value(const std::string& name, detail::argument arg);
496 
497 
505  method get_method(const std::string& name) const;
506 
515  method get_method(const std::string& name, const std::vector<type>& params) const;
516 
523  std::vector<method> get_methods() const;
524 
532  static method get_global_method(const std::string& name);
533 
542  static method get_global_method(const std::string& name, const std::vector<type>& params);
543 
549  static std::vector<method> get_global_methods();
550 
551 
560  variant invoke(const std::string& name, detail::instance obj, std::vector<detail::argument> args) const;
561 
568  static variant invoke(const std::string& name, std::vector<detail::argument> args);
569 
586  template<typename F>
587  static void register_converter_func(F func);
588 
589  private:
590 
594  type();
595 
601  type(type_id id);
602 
611  static void* apply_offset(void* ptr, const type& source_type, const type& target_type);
612 
619  detail::type_converter_base* get_type_converter(const type& target_type) const;
620 
622  // now comes the register functions
623 
632  static type register_type(const char* name,
633  const type& raw_type,
634  std::vector<detail::base_class_info> base_classes,
635  detail::derived_info(*get_derived_func)(void*),
636  variant(*variant_create_func)(void*),
637  bool is_class,
638  bool is_enum,
639  bool is_array,
640  bool is_pointer,
641  bool is_primitive);
642 
643  void register_type_converter(std::unique_ptr<detail::type_converter_base> converter) const;
644 
645  variant create_from_ptr(void* ptr) const;
646 
647  template<typename T>
648  friend struct impl::MetaTypeInfo;
649 
650  template<typename T>
651  friend class class_;
652 
653  friend type impl::get_invalid_type();
654  friend class variant;
655 
656  friend class detail::instance;
657 
658  template<typename TargetType, typename SourceType>
659  friend TargetType rttr_cast(SourceType object);
660 
661  private:
662  type_id m_id;
663 };
664 
665 #ifdef DOXYGEN
666 
696 #define RTTR_DECLARE_TYPE(Type)
697 
703 #define RTTR_DECLARE_STANDARD_TYPE_VARIANTS(Type)
704 
705 
722 #define RTTR_DEFINE_TYPE(Type)
723 
724 #endif
725 
726 } // end namespace rttr
727 
728 #include "rttr/impl/type_impl.h"
729 
730 #endif // __RTTR_TYPE_H__
This class holds the type information for any arbitrary object.
Definition: type.h:165
The destructor class provides a destructor for registered types.
Definition: destructor.h:72
uint16 type_id
Definition: type.h:168
The enumeration class provides several meta information about an enum.
Definition: enumeration.h:96
The method class provides several meta information about a method and can be invoked.
Definition: method.h:112
TargetType rttr_cast(SourceType object)
Casts the given object of type SourceType to an object of type TargetType.
The variant class allows to store data of any type and convert between these types transparently...
Definition: variant.h:125
This class is used to register the constructors, properties, methods and enumeration for a certain cl...
Definition: register_reflection.h:129
unsigned short uint16
Definition: core_prerequisites.h:132
The constructor class provides several meta information about a constructor and can be invoked...
Definition: constructor.h:90
#define RTTR_API
Definition: core_prerequisites.h:124