FixupInfo.h
1/*
2 * Copyright (C) 2005-2013 by Dr. Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_FixupInfo_h
30#define Pt_FixupInfo_h
31
32#include <Pt/Api.h>
33#include <Pt/SerializationError.h>
34#include <typeinfo>
35
36namespace Pt {
37
43{
44 public:
46 typedef void (*FixupHandler)(void* fixme,
47 void* target,
48 const std::type_info& targetType,
49 unsigned m);
50
51 public:
53 FixupInfo(void* target, const std::type_info& targetType, unsigned mid)
54 : _target(target)
55 , _type(&targetType)
56 , _mid(mid)
57 {}
58
61 {}
62
64 void* target() const
65 { return _target; }
66
68 const std::type_info& targetType() const
69 { return *_type; }
70
72 unsigned memberId() const
73 { return _mid; }
74
76 bool isNull() const
77 { return _target == 0; }
78
80 template <typename T>
81 T* getTarget() const
82 {
83 if( _target == 0 || typeid(T) != *_type )
84 {
85 throw SerializationError("fixup type mismatch");
86 }
87
88 return static_cast<T*>( _target );
89 }
90
96 template <typename T>
97 friend T getTarget(FixupInfo* fi);
98
99 private:
100 void* _target;
101 const std::type_info* _type;
102 unsigned _mid;
103};
104
106template <typename T>
107struct FixupThunk
108{
109 static void fixupPointer(void* fixme,
110 void* target,
111 const std::type_info& targetType,
112 unsigned mid)
113 {
114 T** from = static_cast<T**>(fixme);
115 FixupInfo fi(target, targetType, mid);
116 fixup(fi, *from);
117 }
118
119 static void fixupReference(void* fixme,
120 void* target,
121 const std::type_info& targetType,
122 unsigned mid)
123 {
124 T* from = static_cast<T*>(fixme);
125 FixupInfo fi(target, targetType, mid);
126 fixup(fi, *from);
127 }
128};
129
135template <typename T>
136inline void fixup(const FixupInfo& fixup, T*& fixme)
137{
138 fixme = 0;
139
140 if( ! fixup.isNull() )
141 {
142 fixme = fixup.getTarget<T>();
143 }
144}
145
151template <typename T>
152inline void fixup(const FixupInfo& fixup, T& fixme)
153{
154 T* to = fixup.getTarget<T>();
155 fixme = *to;
156}
157
158} // namespace Pt
159
160#endif
Fixup of references during serialization.
Definition FixupInfo.h:43
FixupInfo(void *target, const std::type_info &targetType, unsigned mid)
Construct from referenced type.
Definition FixupInfo.h:53
const std::type_info & targetType() const
Returns the referenced type.
Definition FixupInfo.h:68
~FixupInfo()
Destructor.
Definition FixupInfo.h:60
T * getTarget() const
Returns a pointer to the refernced type.
Definition FixupInfo.h:81
void * target() const
Returns a pointer to the refernced type.
Definition FixupInfo.h:64
bool isNull() const
Returns true if null.
Definition FixupInfo.h:76
unsigned memberId() const
Returns the member ID.
Definition FixupInfo.h:72
SerializationError(const std::string &msg)
Construct with message.
void fixup(const FixupInfo &fixup, T &fixme)
Fixup references during serialization.
Definition FixupInfo.h:152
void fixup(const FixupInfo &fixup, T *&fixme)
Fixup references during serialization.
Definition FixupInfo.h:136
Core module.
Definition Allocator.h:33