source: rrlib_rtti/detail/tDataType.h @ 87:2defcae2db7f

Last change on this file since 87:2defcae2db7f was 87:2defcae2db7f, checked in by Max Reichardt <mreichardt@…>, 3 years ago

Merged with 14.08

File size: 6.5 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/rtti/detail/tDataType.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2012-02-05
27 *
28 * \brief   Contains tDataType
29 *
30 * \b tDataType
31 *
32 * Objects of this class contain and register runtime information about the
33 * data type T.
34 *
35 */
36//----------------------------------------------------------------------
37#ifndef __rrlib__rtti__detail__tDataType_h__
38#define __rrlib__rtti__detail__tDataType_h__
39
40//----------------------------------------------------------------------
41// External includes (system with <>, local with "")
42//----------------------------------------------------------------------
43#include <string>
44#include <typeinfo>
45
46//----------------------------------------------------------------------
47// Internal includes with ""
48//----------------------------------------------------------------------
49#include "rrlib/rtti/rtti.h"
50#include "rrlib/rtti/type_traits.h"
51#include "rrlib/rtti/tType.h"
52
53//----------------------------------------------------------------------
54// Namespace declaration
55//----------------------------------------------------------------------
56namespace rrlib
57{
58
59//----------------------------------------------------------------------
60// Forward declarations / typedefs / enums
61//----------------------------------------------------------------------
62namespace serialization
63{
64class tInputStream;
65class tOutputStream;
66}
67namespace rtti
68{
69class tGenericObject;
70
71namespace detail
72{
73
74//----------------------------------------------------------------------
75// Class declaration
76//----------------------------------------------------------------------
77//! Runtime type information for T
78/*!
79 * Objects of this class contain and register runtime information about the
80 * data type T.
81 */
82template<typename T>
83class tDataType : public tType
84{
85  template <typename U, bool Enum>
86  class tDataTypeInfo;
87
88//----------------------------------------------------------------------
89// Public methods and typedefs
90//----------------------------------------------------------------------
91public:
92
93  tDataType() : tType(GetDataTypeInfo())
94  {
95  }
96
97  /*!
98   * Custom name may only be specified on first instantiation of a tDataType<T> for each type T
99   *
100   * \param name Name data type should get (if different from default)
101   */
102  tDataType(const std::string& name) : tType(GetDataTypeInfo(name.c_str()))
103  {
104  }
105
106  /*!
107   * Lookup data type by rtti name
108   *
109   * Tries T first
110   * \param rtti_name rtti name
111   * \return Data type with specified name (== NULL if it could not be found)
112   */
113  static tType FindTypeByRtti(const char* rtti_name)
114  {
115    if (rtti_name == GetDataTypeInfo()->rtti_name)
116    {
117      return tDataType();
118    }
119    return tType::FindTypeByRtti(rtti_name);
120  }
121
122  /*!
123   * \param name Custom name for data type (may only be specified on first call/instantiation)
124   * \return DataTypeInfo for this type T
125   */
126  inline static tInfo* GetDataTypeInfo(const char* name = nullptr)
127  {
128    static tDataTypeInfo<T, std::is_enum<T>::value> info(name);
129    if (name && info.name != name) // hopefully, compiler optimizes this away for all calls with name == nullptr
130    {
131      RRLIB_LOG_PRINT_STATIC(ERROR, "Type name '", info.name, "' can only be changed on initial instantiation of tDataType<T>.");
132    }
133    assert(((!name) || info.name == name) && "Type name may not be changed later"); // assertion for efficiency reasons
134    return &info;
135  }
136
137//----------------------------------------------------------------------
138// Private fields and methods
139//----------------------------------------------------------------------
140private:
141
142  /*!
143   * Data type info with factory functions
144   */
145  class tDataTypeInfoBase : public tType::tInfo
146  {
147  public:
148
149    tDataTypeInfoBase(tType::tClassification classification, const std::string& name);
150
151    virtual tGenericObject* CreateInstanceGeneric(void* placement, bool emplace_generic_object) const override;
152
153    virtual void DeepCopy(const void* src, void* dest, tFactory* f) const override;
154
155    virtual void Deserialize(serialization::tInputStream& is, void* obj) const override;
156
157    virtual void Serialize(serialization::tOutputStream& os, const void* obj) const override;
158
159    virtual void Init() override
160    {
161      AutoRegisterRelatedTypes<T>::Register();
162    }
163  };
164
165  template <typename U, bool Enum>
166  class tDataTypeInfo : public tDataTypeInfoBase
167  {
168  public:
169    tDataTypeInfo(const char* name) : tDataTypeInfoBase(tClassification::PLAIN, name ? name : TypeName<T>::Get().c_str()) {}
170  };
171
172  template <typename U>
173  class tDataTypeInfo<std::vector<U>, false> : public tDataTypeInfoBase
174  {
175  public:
176    tDataTypeInfo(const char* name) : tDataTypeInfoBase(tClassification::LIST, name ? name : TypeName<T>::Get().c_str())
177    {
178      this->element_type = tDataType<U>::GetDataTypeInfo();
179      this->element_type->list_type = this;
180    }
181  };
182
183  template <typename U>
184  class tDataTypeInfo<U, true> : public tDataTypeInfoBase
185  {
186  public:
187    tDataTypeInfo(const char* name) : tDataTypeInfoBase(tClassification::PLAIN, name ? name : TypeName<T>::Get().c_str())
188    {
189      this->enum_strings = &make_builder::internal::GetEnumStrings<T>();
190      if (this->enum_strings->non_standard_values)
191      {
192        for (size_t i = 0; i < this->enum_strings->size; i++)
193        {
194          this->non_standard_enum_value_strings.emplace_back(std::to_string(static_cast<typename std::underlying_type<T>::type>(static_cast<const T*>(this->enum_strings->non_standard_values)[i])));
195        }
196      }
197    }
198  };
199};
200
201//----------------------------------------------------------------------
202// End of namespace declaration
203//----------------------------------------------------------------------
204}
205}
206}
207
208#include "rrlib/rtti/detail/tDataType.hpp"
209
210#endif
Note: See TracBrowser for help on using the repository browser.