Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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
44namespace steam {
45namespace 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
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
105 {
106 using DoInBuffer = function<void(void*)>;
107
110 HashVal identity;
111
113 static const TypeHandler RAW;
114
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
144 template<class X, typename...ARGS>
145 static TypeHandler
146 create (ARGS&& ...args)
147 {
148 return TypeHandler ( bind (buildIntoBuffer<X,ARGS&...>, _1, forward<ARGS> (args)...)
149 , destroyInBuffer<X>);
150 }
151
152 bool
153 isValid() const
154 {
155 return bool(createAttached)
156 and bool(destroyAttached);
157 }
158
159 friend HashVal
160 hash_value (TypeHandler const& handler)
161 {
162 return handler.identity;
163 }
164
165 friend bool
166 operator== (TypeHandler const& left, TypeHandler const& right)
167 {
168 return (not left.isValid() and not right.isValid())
169 or (left.identity == right.identity);
170 }
171 friend bool
172 operator!= (TypeHandler const& left, TypeHandler const& right)
173 {
174 return not (left == right);
175 }
176 };
177
178
179
180}} // namespace steam::engine
181#endif
Lumiera error handling (C++ interface).
#define hash
Hash value types and utilities.
size_t HashVal
a STL compatible hash value
Definition hash-value.h:52
void buildIntoBuffer(void *storageBuffer, ARGS &&...args)
Steam-Layer implementation namespace root.
A pair of functors to maintain a datastructure within a buffer.
function< void(void *)> DoInBuffer
friend bool operator==(TypeHandler const &left, TypeHandler const &right)
friend HashVal hash_value(TypeHandler const &handler)
static TypeHandler create(ARGS &&...args)
builder function for a pre-configured TypeHandler to place a new instance into the buffer,...
TypeHandler()
build an invalid NIL TypeHandler
static const TypeHandler RAW
Marker for the default case: raw buffer without type handling.
TypeHandler(CTOR ctor, DTOR dtor)
build a TypeHandler binding to arbitrary constructor and destructor functions.
friend bool operator!=(TypeHandler const &left, TypeHandler const &right)