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)
5  2011, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
32 #ifndef STEAM_ENGINE_TYPE_HANDLER_H
33 #define STEAM_ENGINE_TYPE_HANDLER_H
34 
35 
36 #include "lib/error.hpp"
37 #include "lib/hash-value.h"
38 
39 #include <utility>
40 #include <functional>
41 #include <boost/functional/hash.hpp>
42 
43 
44 namespace steam {
45 namespace engine {
46 
47  using lib::HashVal;
48  using std::bind;
49  using std::forward;
50  using std::function;
51  using std::placeholders::_1;
52 
53  namespace error = lumiera::error;
54 
55 
56  namespace { // (optional) helpers to build an object embedded into a buffer...
57 
58  template<class X, typename...ARGS>
59  inline void
60  buildIntoBuffer (void* storageBuffer, ARGS&& ...args)
61  {
62  new(storageBuffer) X(forward<ARGS> (args)...);
63  }
64 
65  template<class X>
66  inline void
67  destroyInBuffer (void* storageBuffer)
68  {
69  X* embedded = static_cast<X*> (storageBuffer);
70  embedded->~X();
71  }
72 
73  template<typename CTOR, typename DTOR>
74  inline HashVal
75  deriveCombinedTypeIdenity()
76  {
77  HashVal hash{0};
78  boost::hash_combine (hash, typeid(CTOR).hash_code());
79  boost::hash_combine (hash, typeid(DTOR).hash_code());
80  return hash;
81  }
82  }//(End)placement-new helpers
83 
84 
85 
104  struct TypeHandler
105  {
106  typedef function<void(void*)> DoInBuffer;
107 
108  DoInBuffer createAttached;
109  DoInBuffer destroyAttached;
110  HashVal identity;
111 
113  static const TypeHandler RAW;
114 
117  : createAttached()
118  , destroyAttached()
119  , identity{0}
120  { }
121 
129  template<typename CTOR, typename DTOR>
130  TypeHandler(CTOR ctor, DTOR dtor)
131  : createAttached (ctor)
132  , destroyAttached (dtor)
133  , identity{deriveCombinedTypeIdenity<CTOR,DTOR>()}
134  { }
135 
139  template<class X, typename...ARGS>
140  static TypeHandler
141  create (ARGS&& ...args)
142  {
143  return TypeHandler ( bind (buildIntoBuffer<X,ARGS...>, _1, forward<ARGS> (args)...)
144  , destroyInBuffer<X>);
145  }
146 
147  bool
148  isValid() const
149  {
150  return bool(createAttached)
151  and bool(destroyAttached);
152  }
153 
154  friend HashVal
155  hash_value (TypeHandler const& handler)
156  {
157  return handler.identity;
158  }
159 
160  friend bool
161  operator== (TypeHandler const& left, TypeHandler const& right)
162  {
163  return (not left.isValid() and not right.isValid())
164  || (left.identity == right.identity);
165  }
166  friend bool
167  operator!= (TypeHandler const& left, TypeHandler const& right)
168  {
169  return not (left == right);
170  }
171  };
172 
173 
174 
175 }} // namespace steam::engine
176 #endif
Steam-Layer implementation namespace root.
TypeHandler()
build an invalid NIL TypeHandler
static const TypeHandler RAW
Marker for the default case: raw buffer without type handling.
Lumiera error handling (C++ interface).
Hash value types and utilities.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:52
A pair of functors to maintain a datastructure within a buffer.
HashVal hash_value(ProcID const &procID)
generate registry hash value based on the distinct data in ProcID.
Definition: proc-node.cpp:105
TypeHandler(CTOR ctor, DTOR dtor)
build a TypeHandler binding to arbitrary constructor and destructor functions.
static TypeHandler create(ARGS &&...args)
builder function defining a TypeHandler to place an object into the buffer, possibly with given ctor ...