Lumiera  0.pre.03
»edit your freedom«
type-handler.hpp
Go to the documentation of this file.
1 /*
2  TYPE-HANDLER.hpp - a functor pair for setup and destruction
3 
4  Copyright (C) Lumiera.org
5  2011, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (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
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
41 #ifndef STEAM_ENGINE_TYPE_HANDLER_H
42 #define STEAM_ENGINE_TYPE_HANDLER_H
43 
44 
45 #include "lib/error.hpp"
46 #include "lib/hash-value.h"
47 
48 #include <functional>
49 #include <boost/functional/hash.hpp>
50 
51 
52 namespace steam {
53 namespace engine {
54 
55  using lib::HashVal;
56  using std::bind;
57  using std::function;
58  using std::placeholders::_1;
59 
60  namespace error = lumiera::error;
61 
62 
63  namespace { // (optional) helpers to build an object embedded into a buffer...
64 
65  template<class X>
66  inline void
67  buildIntoBuffer (void* storageBuffer)
68  {
69  new(storageBuffer) X();
70  }
71 
72  template<class X, typename A1>
73  inline void
74  buildIntoBuffer_A1 (void* storageBuffer, A1 arg1)
75  {
76  new(storageBuffer) X(arg1);
77  }
78 
79  template<class X>
80  inline void
81  destroyInBuffer (void* storageBuffer)
82  {
83  X* embedded = static_cast<X*> (storageBuffer);
84  embedded->~X();
85  }
86 
87  template<typename CTOR, typename DTOR>
88  inline HashVal
89  deriveCombinedTypeIdenity()
90  {
91  HashVal hash{0};
92  boost::hash_combine (hash, typeid(CTOR).hash_code());
93  boost::hash_combine (hash, typeid(DTOR).hash_code());
94  return hash;
95  }
96  }//(End)placement-new helpers
97 
98 
99 
118  struct TypeHandler
119  {
120  typedef function<void(void*)> DoInBuffer;
121 
122  DoInBuffer createAttached;
123  DoInBuffer destroyAttached;
124  HashVal identity;
125 
128  : createAttached()
129  , destroyAttached()
130  , identity{0}
131  { }
132 
140  template<typename CTOR, typename DTOR>
141  TypeHandler(CTOR ctor, DTOR dtor)
142  : createAttached (ctor)
143  , destroyAttached (dtor)
144  , identity{deriveCombinedTypeIdenity<CTOR,DTOR>()}
145  { }
146 
150  template<class X>
151  static TypeHandler
153  {
154  return TypeHandler (buildIntoBuffer<X>, destroyInBuffer<X>);
155  }
156 
157  template<class X, typename A1>
158  static TypeHandler
159  create (A1 a1)
160  {
161  return TypeHandler ( bind (buildIntoBuffer_A1<X,A1>, _1, a1)
162  , destroyInBuffer<X>);
163  }
164 
165  bool
166  isValid() const
167  {
168  return bool(createAttached)
169  and bool(destroyAttached);
170  }
171 
172  friend HashVal
173  hash_value (TypeHandler const& handler)
174  {
175  return handler.identity;
176  }
177 
178  friend bool
179  operator== (TypeHandler const& left, TypeHandler const& right)
180  {
181  return (not left.isValid() and not right.isValid())
182  || (left.identity == right.identity);
183  }
184  friend bool
185  operator!= (TypeHandler const& left, TypeHandler const& right)
186  {
187  return not (left == right);
188  }
189  };
190 
191 
192 
193 }} // namespace steam::engine
194 #endif
static TypeHandler create()
builder function defining a TypeHandler to place a default-constructed object into the buffer...
Steam-Layer implementation namespace root.
TypeHandler()
build an invalid NIL TypeHandler
Lumiera error handling (C++ interface).
Hash value types and utilities.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
A pair of functors to maintain a datastructure within a buffer.
TypeHandler(CTOR ctor, DTOR dtor)
build a TypeHandler binding to arbitrary constructor and destructor functions.