aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/serialization/src/void_cast.cpp
blob: 261e4bcc949fcacad8aa4099d4b35e8a9da3d601 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// void_cast.cpp: implementation of run-time casting of void pointers

// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// <gennadiy.rozental@tfn.com>

//  See http://www.boost.org for updates, documentation, and revision history.

#if (defined _MSC_VER) && (_MSC_VER == 1200)
# pragma warning (disable : 4786) // too long name, harmless warning
#endif

// STL
#include <set>
#include <functional>
#include <algorithm>
#include <cstddef> // NULL
#ifdef BOOST_SERIALIZATION_LOG
#include <iostream>
#endif

// BOOST
#include <boost/config.hpp>
#include <boost/assert.hpp>

#define BOOST_SERIALIZATION_SOURCE
#include <boost/serialization/config.hpp>
// it marks our code with proper attributes as being exported when
// we're compiling it while marking it import when just the headers
// is being included.
#include <boost/serialization/singleton.hpp>
#include <boost/serialization/extended_type_info.hpp>
#include <boost/serialization/void_cast.hpp>

namespace boost { 
namespace serialization {
namespace void_cast_detail {

// note that void_casters are keyed on value of
// member extended type info records - NOT their
// addresses.  This is necessary in order for the
// void cast operations to work across dll and exe
// module boundaries.
bool void_caster::operator<(const void_caster & rhs) const {
    // include short cut to save time and eliminate
    // problems when when base class aren't virtual
    if(m_derived != rhs.m_derived){
        if(*m_derived < *rhs.m_derived)
            return true;
        if(*rhs.m_derived < *m_derived)
            return false;
    }
    // m_derived == rhs.m_derived
    if(m_base != rhs.m_base)
        return *m_base < *rhs.m_base;
    else
        return false;
}

struct void_caster_compare {
    bool operator()(const void_caster * lhs, const void_caster * rhs) const {
        return *lhs < *rhs;
    }
};

typedef std::set<const void_caster *, void_caster_compare> set_type;
typedef boost::serialization::singleton<set_type> void_caster_registry;

#ifdef BOOST_MSVC
#  pragma warning(push)
#  pragma warning(disable : 4511 4512)
#endif

// implementation of shortcut void caster
class void_caster_shortcut : public void_caster
{
    bool m_includes_virtual_base;

    void const * 
    vbc_upcast(
        void const * const t
    ) const;
    void const *
    vbc_downcast(
        void const * const t
    ) const;
    void const *
    upcast(void const * const t) const BOOST_OVERRIDE {
        if(m_includes_virtual_base)
            return vbc_upcast(t);
        return static_cast<const char *> ( t ) - m_difference;
    }
    void const *
    downcast(void const * const t) const BOOST_OVERRIDE {
        if(m_includes_virtual_base)
            return vbc_downcast(t);
        return static_cast<const char *> ( t ) + m_difference;
    }
    virtual bool is_shortcut() const {
        return true;
    }
    bool has_virtual_base() const BOOST_OVERRIDE {
        return m_includes_virtual_base;
    }
public:
    void_caster_shortcut(
        extended_type_info const * derived,
        extended_type_info const * base,
        std::ptrdiff_t difference,
        bool includes_virtual_base,
        void_caster const * const parent
    ) :
        void_caster(derived, base, difference, parent),
        m_includes_virtual_base(includes_virtual_base)
    {
        recursive_register(includes_virtual_base);
    }
    ~void_caster_shortcut() BOOST_OVERRIDE {
        recursive_unregister();
    }
};

#ifdef BOOST_MSVC
#  pragma warning(pop)
#endif

void const * 
void_caster_shortcut::vbc_downcast(
    void const * const t
) const {
    // try to find a chain that gives us what we want
    const void_cast_detail::set_type & s
        = void_cast_detail::void_caster_registry::get_const_instance();
    void_cast_detail::set_type::const_iterator it;
    for(it = s.begin(); it != s.end(); ++it){
        // if the current candidate casts to the desired target type
        if ((*it)->m_derived == m_derived){
            // and if it's not us
            if ((*it)->m_base != m_base){
                // try to cast from the candidate base to our base
                const void * t_new;
                t_new = void_downcast(*(*it)->m_base, *m_base, t);
                // if we were successful
                if(NULL != t_new){
                    // recast to our derived
                    const void_caster * vc = *it;
                    return vc->downcast(t_new);
                }
            }
        }
    }
    return NULL;
}

void const * 
void_caster_shortcut::vbc_upcast(
    void const * const t
) const {
    // try to find a chain that gives us what we want
    const void_cast_detail::set_type & s
        = void_cast_detail::void_caster_registry::get_const_instance();
    void_cast_detail::set_type::const_iterator it;
    for(it = s.begin(); it != s.end(); ++it){
        // if the current candidate casts from the desired base type
        if((*it)->m_base == m_base){
            // and if it's not us
            if ((*it)->m_derived != m_derived){
                // try to cast from the candidate derived to our our derived
                const void * t_new;
                t_new = void_upcast(*m_derived, *(*it)->m_derived, t);
                if(NULL != t_new)
                    return (*it)->upcast(t_new);
            }
        }
    }
    return NULL;
}

#ifdef BOOST_MSVC
#  pragma warning(push)
#  pragma warning(disable : 4511 4512)
#endif

// just used as a search key
class void_caster_argument : public void_caster
{
    void const *
    upcast(void const * const /*t*/) const BOOST_OVERRIDE {
        BOOST_ASSERT(false);
        return NULL;
    }
    void const *
    downcast( void const * const /*t*/) const BOOST_OVERRIDE {
        BOOST_ASSERT(false);
        return NULL;
    }
    bool has_virtual_base() const BOOST_OVERRIDE {
        BOOST_ASSERT(false);
        return false;
    }
public:
    void_caster_argument(
        extended_type_info const * derived,
        extended_type_info const * base
    ) :
        void_caster(derived, base)
    {}
    ~void_caster_argument() BOOST_OVERRIDE {}
};

#ifdef BOOST_MSVC
#  pragma warning(pop)
#endif

// implementation of void caster base class
BOOST_SERIALIZATION_DECL void
void_caster::recursive_register(bool includes_virtual_base) const {
    void_cast_detail::set_type & s
        = void_cast_detail::void_caster_registry::get_mutable_instance();

    #ifdef BOOST_SERIALIZATION_LOG
    std::clog << "recursive_register\n";
    std::clog << m_derived->get_debug_info();
    std::clog << "<-";
    std::clog << m_base->get_debug_info();
    std::clog << "\n";
    #endif

    std::pair<void_cast_detail::set_type::const_iterator, bool> result;
    // comment this out for now.  
    result = s.insert(this);
    //assert(result.second);

    // generate all implied void_casts.
    void_cast_detail::set_type::const_iterator it;
    for(it = s.begin(); it != s.end(); ++it){
        if(* m_derived == * (*it)->m_base){
            const void_caster_argument vca(
                (*it)->m_derived, 
                m_base
            );
            void_cast_detail::set_type::const_iterator i;
            i = s.find(& vca);
            if(i == s.end()){
                new void_caster_shortcut(
                    (*it)->m_derived, 
                    m_base,
                    m_difference + (*it)->m_difference,
                    (*it)->has_virtual_base() || includes_virtual_base,
                    this
                );
            }
        }
        if(* (*it)->m_derived == * m_base){
            const void_caster_argument vca(
                m_derived, 
                (*it)->m_base
            );
            void_cast_detail::set_type::const_iterator i;
            i = s.find(& vca);
            if(i == s.end()){
                new void_caster_shortcut(
                    m_derived, 
                    (*it)->m_base, 
                    m_difference + (*it)->m_difference,
                    (*it)->has_virtual_base() || includes_virtual_base,
                    this
                );
            }
        }
    }
}

BOOST_SERIALIZATION_DECL void
void_caster::recursive_unregister() const {
    // note: it's been discovered that at least one platform is not guaranteed
    // to destroy singletons reverse order of construction.  So we can't
    // use a runtime assert here.  Leave this in a reminder not to do this!
    // BOOST_ASSERT(! void_caster_registry::is_destroyed());
    if(void_caster_registry::is_destroyed())
        return;

    #ifdef BOOST_SERIALIZATION_LOG
    std::clog << "recursive_unregister\n";
    std::clog << m_derived->get_debug_info();
    std::clog << "<-";
    std::clog << m_base->get_debug_info();
    std::clog << "\n";
    #endif

    void_cast_detail::set_type & s 
        = void_caster_registry::get_mutable_instance();

    // delete all shortcuts which use this primitive
    void_cast_detail::set_type::iterator it;
    for(it = s.begin(); it != s.end();){
        const void_caster * vc = *it;
        if(vc == this){
            s.erase(it++);
        }
        else
        if(vc->m_parent == this){
            s.erase(it);
            delete vc;
            it = s.begin();
        }
        else
            it++;
    }
}

} // namespace void_cast_detail

BOOST_SYMBOL_VISIBLE void const *
void_upcast(
    extended_type_info const & derived,
    extended_type_info const & base,
    void const * const t
);

// Given a void *, assume that it really points to an instance of one type
// and alter it so that it would point to an instance of a related type.
// Return the altered pointer. If there exists no sequence of casts that
// can transform from_type to to_type, return a NULL.  
BOOST_SERIALIZATION_DECL void const *
void_upcast(
    extended_type_info const & derived,
    extended_type_info const & base,
    void const * const t
){
    // same types - trivial case
    if (derived == base)
        return t;

    // check to see if base/derived pair is found in the registry
    const void_cast_detail::set_type & s
        = void_cast_detail::void_caster_registry::get_const_instance();
    const void_cast_detail::void_caster_argument ca(& derived, & base);

    void_cast_detail::set_type::const_iterator it;
    it = s.find(& ca);
    if (s.end() != it)
        return (*it)->upcast(t);

    return NULL;
}

BOOST_SYMBOL_VISIBLE void const *
void_downcast(
    extended_type_info const & derived,
    extended_type_info const & base,
    void const * const t
);

BOOST_SERIALIZATION_DECL void const *
void_downcast(
    extended_type_info const & derived,
    extended_type_info const & base,
    void const * const t
){
    // same types - trivial case
    if (derived == base)
        return t;

    // check to see if base/derived pair is found in the registry
    const void_cast_detail::set_type & s
        = void_cast_detail::void_caster_registry::get_const_instance();
    const void_cast_detail::void_caster_argument ca(& derived, & base);

    void_cast_detail::set_type::const_iterator it;
    it = s.find(&ca);
    if (s.end() != it)
        return(*it)->downcast(t);

    return NULL;
}

} // namespace serialization
} // namespace boost