source: rrlib_rtti/detail/tDataType.hpp @ 77:bfa8833413d2

Last change on this file since 77:bfa8833413d2 was 77:bfa8833413d2, checked in by Max Reichardt <mreichardt@…>, 4 years ago

Fix in last commit

File size: 5.0 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.hpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2012-02-05
27 *
28 */
29//----------------------------------------------------------------------
30
31//----------------------------------------------------------------------
32// External includes (system with <>, local with "")
33//----------------------------------------------------------------------
34#include "rrlib/logging/messages.h"
35
36//----------------------------------------------------------------------
37// Internal includes with ""
38//----------------------------------------------------------------------
39#include "rrlib/rtti/tGenericObjectInstance.h"
40#include "rrlib/rtti/tGenericObjectWrapper.h"
41#include "rrlib/rtti/detail/tGenericObjectInstanceEmplaced.h"
42
43//----------------------------------------------------------------------
44// Debugging
45//----------------------------------------------------------------------
46#include <cassert>
47
48//----------------------------------------------------------------------
49// Namespace declaration
50//----------------------------------------------------------------------
51namespace rrlib
52{
53namespace rtti
54{
55
56//----------------------------------------------------------------------
57// Forward declarations / typedefs / enums
58//----------------------------------------------------------------------
59std::string GetBinaryCurrentlyPerformingStaticInitialization();
60
61namespace detail
62{
63
64//----------------------------------------------------------------------
65// Const values
66//----------------------------------------------------------------------
67
68//----------------------------------------------------------------------
69// Implementation
70//----------------------------------------------------------------------
71
72template<typename T>
73tDataType<T>::tDataTypeInfoBase::tDataTypeInfoBase(tType::tClassification classification, const std::string& name) :
74  tInfo(classification, typeid(T).name(), name)
75{
76  size = sizeof(T);
77  generic_object_size = sizeof(tGenericObjectInstance<T>);
78  type_traits = tTypeTraitsVector<T>::value;
79  binary = GetBinaryCurrentlyPerformingStaticInitialization();
80#if RRLIB_RTTI_BINARY_DETECTION_ENABLED
81  if (binary.length() > 0)
82  {
83    RRLIB_LOG_PRINT_STATIC(DEBUG_VERBOSE_1, "Data type ", name, " is statically loaded in '", binary, "'.");
84  }
85  else
86  {
87    RRLIB_LOG_PRINT_STATIC(DEBUG_VERBOSE_1, "Data type ", name, " is dynamically loaded.");
88  }
89#endif
90}
91
92template<typename T>
93tGenericObject* tDataType<T>::tDataTypeInfoBase::CreateInstanceGeneric(void* placement, bool emplace_generic_object) const
94{
95  if (placement == NULL)
96  {
97    placement = operator new(emplace_generic_object ? sizeof(tGenericObjectInstance<T>) : sizeof(T));
98  }
99  memset(placement, 0, emplace_generic_object ? sizeof(tGenericObjectInstance<T>) : sizeof(T)); // set memory to 0 so that memcmp on class T can be performed cleanly for certain types
100  if (emplace_generic_object)
101  {
102    return new(placement) tGenericObjectInstance<T>();
103  }
104  else
105  {
106    return new detail::tGenericObjectInstanceEmplaced<T>(placement);
107  }
108}
109
110template<typename T>
111void tDataType<T>::tDataTypeInfoBase::DeepCopy(const void* src, void* dest, tFactory* f) const
112{
113  const T* s = static_cast<const T*>(src);
114  T* d = static_cast<T*>(dest);
115
116  if (std::has_virtual_destructor<T>::value)
117  {
118    assert(typeid(*s).name() == typeid(T).name());
119    assert(typeid(*d).name() == typeid(T).name());
120  }
121
122  GenericOperations<T>::DeepCopy(*s, *d);
123}
124
125template<typename T>
126void tDataType<T>::tDataTypeInfoBase::Deserialize(serialization::tInputStream& is, void* obj) const
127{
128  T* s = static_cast<T*>(obj);
129  if (std::has_virtual_destructor<T>::value)
130  {
131    assert(typeid(*s).name() == typeid(T).name());
132  }
133  serialization::Deserialize(is, *s);
134}
135
136template<typename T>
137void tDataType<T>::tDataTypeInfoBase::Serialize(serialization::tOutputStream& os, const void* obj) const
138{
139  const T* s = static_cast<const T*>(obj);
140  if (std::has_virtual_destructor<T>::value)
141  {
142    assert(typeid(*s).name() == typeid(T).name());
143  }
144  serialization::Serialize(os, *s);
145}
146
147//----------------------------------------------------------------------
148// End of namespace declaration
149//----------------------------------------------------------------------
150}
151}
152}
Note: See TracBrowser for help on using the repository browser.