summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/framing/Blob.h
blob: cf81f693b06a0b3685522be8f494d31c7e58b148 (plain)
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
#ifndef QPID_FRAMING_BLOB_H
#define QPID_FRAMING_BLOB_H

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include <boost/static_assert.hpp>
#include <boost/aligned_storage.hpp>
#include <boost/checked_delete.hpp>
#include <boost/utility/typed_in_place_factory.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>
#include <boost/utility/enable_if.hpp>

#include <new>

#include <assert.h>


namespace qpid {
namespace framing {

using boost::in_place;          
using boost::typed_in_place_factory_base;

/** 0-arg typed_in_place_factory, missing in boost. */
template <class T>
struct typed_in_place_factory0 : public typed_in_place_factory_base {
    typedef T value_type ; 
    void apply ( void* address ) const { new (address) T(); }
};

/** 0-arg in_place<T>() function, missing from boost. */
template<class T>
typed_in_place_factory0<T> in_place() { return typed_in_place_factory0<T>(); }

template <class T, class R=void>
struct EnableInPlace
    : public boost::enable_if<boost::is_base_and_derived<
                                  typed_in_place_factory_base, T>,
                              R>
{};
       
template <class T, class R=void>
struct DisableInPlace
    : public boost::disable_if<boost::is_base_and_derived<
                                   typed_in_place_factory_base, T>,
                               R>
{};
       
template <class T> struct BlobHelper {
    static void destroy(void* ptr) { static_cast<T*>(ptr)->~T(); }
    static void copy(void* dest, const void* src) {
        new (dest) T(*static_cast<const T*>(src));
    }
};

template <> struct BlobHelper<void> {
    static void destroy(void*);
    static void copy(void* to, const void* from);
};

/**
 * A "blob" is a chunk of memory which can contain a single object at
 * a time arbitrary type, provided sizeof(T)<=blob.size(). Blob
 * ensures proper construction and destruction of its contents,
 * and proper copying between Blobs, but nothing else.
 * 
 * In particular the user must ensure the blob is big enough for its
 * contents and must know the type of object in the blob to cast get().
 *
 * If BaseType is specified then only object that can be
 * safely static_cast to BaseType may be stored in the Blob.
 */
template <size_t Size, class BaseType=void>
class Blob
{
    boost::aligned_storage<Size> store;
    BaseType* basePtr;
    
    void (*destroy)(void*);
    void (*copy)(void*, const void*);

    template <class T>void setType() {
        BOOST_STATIC_ASSERT(sizeof(T) <= Size);
        destroy=&BlobHelper<T>::destroy;
        copy=&BlobHelper<T>::copy;
        // Base pointer may be offeset from store.address()
        basePtr = reinterpret_cast<T*>(store.address());
    }
        
    void initialize() {
        destroy=&BlobHelper<void>::destroy;
        copy=&BlobHelper<void>::copy;
        basePtr=0;
    }

    template<class Factory>
    typename EnableInPlace<Factory>::type apply(const Factory& factory)
    {
        typedef typename Factory::value_type T;
        assert(empty());
        factory.apply(store.address());
        setType<T>();
    }

    void assign(const Blob& b) {
        assert(empty());
        b.copy(this->store.address(), b.store.address());
        copy = b.copy;
        destroy = b.destroy;
        basePtr = reinterpret_cast<BaseType*>(
            ((char*)this)+ ((char*)(b.basePtr) - (char*)(&b)));
    }

  public:
    /** Construct an empty blob. */
    Blob() { initialize(); }

    /** Copy a blob. */
    Blob(const Blob& b) { initialize(); assign(b); }

    /** Construct from in_place constructor */
    template<class InPlace>
    Blob(const InPlace & expr, typename EnableInPlace<InPlace>::type* =0) {
        initialize(); apply(expr);
    }

    /** Construct by copying an objecct constructor */
    template<class T>
    Blob(const T & t, typename DisableInPlace<T>::type* =0) {
        initialize(); apply(in_place<T>(t));
    }

    ~Blob() { clear(); }

    /** Assign from another blob. */
    Blob& operator=(const Blob& b) {
        clear();
        assign(b);
        return *this;
    }

    /** Assign from an in_place constructor expression. */
    template<class InPlace>
    typename EnableInPlace<InPlace,Blob&>::type operator=(const InPlace& expr) {
        clear(); apply(expr); return *this;
    }

    /** Assign from an object of type T. */
    template <class T>
    typename DisableInPlace<T, Blob&>::type operator=(const T& x) {
        clear(); apply(in_place<T>(x)); return *this;
    }

    /** Get pointer to blob contents, returns 0 if empty. */
    BaseType* get() { return  basePtr; }

    /** Get pointer to blob contents, returns 0 if empty. */
    const BaseType* get() const { return basePtr; }

    /** Destroy the object in the blob making it empty. */
    void clear() {
        void (*oldDestroy)(void*) = destroy; 
        initialize();
        oldDestroy(store.address());
    }

    bool empty() const { return destroy==BlobHelper<void>::destroy; }
    
    static size_t size() { return Size; }
};

}} // namespace qpid::framing


#endif  /*!QPID_FRAMING_BLOB_H*/