source: rrlib_rtti/detail/tGenericObjectInstanceEmplaced.h @ 57:26e5d627582d

Last change on this file since 57:26e5d627582d was 57:26e5d627582d, checked in by Max Reichardt <mreichardt@…>, 5 years ago

Small extension: tType can now create objects at specific addresses and wrap them as a tGenericObject

File size: 3.8 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) AG Robotersysteme TU Kaiserslautern
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/tGenericObjectInstanceEmplaced.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2014-06-23
27 *
28 * \brief   Contains tGenericObjectInstanceEmplaced
29 *
30 * \b tGenericObjectInstanceEmplaced
31 *
32 * A generic object that receives memory to initialize an object T in - and wrap it
33 *
34 */
35//----------------------------------------------------------------------
36#ifndef __rrlib__rtti__detail__tGenericObjectInstanceEmplaced_h__
37#define __rrlib__rtti__detail__tGenericObjectInstanceEmplaced_h__
38
39//----------------------------------------------------------------------
40// External includes (system with <>, local with "")
41//----------------------------------------------------------------------
42
43//----------------------------------------------------------------------
44// Internal includes with ""
45//----------------------------------------------------------------------
46
47//----------------------------------------------------------------------
48// Namespace declaration
49//----------------------------------------------------------------------
50namespace rrlib
51{
52namespace rtti
53{
54namespace detail
55{
56
57//----------------------------------------------------------------------
58// Forward declarations / typedefs / enums
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Class declaration
63//----------------------------------------------------------------------
64//! Generic object using specified memory address
65/*!
66 * A generic object that receives memory to initialize an object T in - and wrap it.
67 * Memory block needs to have size sizeof(T)
68 */
69template<typename T, bool NO_ARG_CONSTRUCTOR = std::is_base_of<serialization::DefaultImplementation, serialization::DefaultInstantiation<T>>::value>
70class tGenericObjectInstanceEmplaced : public detail::tGenericObjectBaseImpl<T>
71{
72
73//----------------------------------------------------------------------
74// Public methods and typedefs
75//----------------------------------------------------------------------
76public:
77
78  tGenericObjectInstanceEmplaced(void* address) :
79    detail::tGenericObjectBaseImpl<T>()
80  {
81    new(address) T(serialization::DefaultInstantiation<T>::Create());
82    tGenericObject::wrapped = address;
83  }
84
85};
86
87// Specialization for when default constructor is available
88template<typename T>
89class tGenericObjectInstanceEmplaced<T, true> : public detail::tGenericObjectBaseImpl<T>
90{
91
92//----------------------------------------------------------------------
93// Public methods and typedefs
94//----------------------------------------------------------------------
95public:
96
97  tGenericObjectInstanceEmplaced(void* address) :
98    detail::tGenericObjectBaseImpl<T>()
99  {
100    new(address) T();
101    tGenericObject::wrapped = address;
102  }
103
104};
105
106//----------------------------------------------------------------------
107// End of namespace declaration
108//----------------------------------------------------------------------
109}
110}
111}
112
113
114#endif
Note: See TracBrowser for help on using the repository browser.