/
decorated_tuple.hpp
146 lines (114 loc) · 5.49 KB
/
decorated_tuple.hpp
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
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011, 2012 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef CPPA_DECORATED_TUPLE_HPP
#define CPPA_DECORATED_TUPLE_HPP
#include <vector>
#include <algorithm>
#include "cppa/config.hpp"
#include "cppa/cow_ptr.hpp"
#include "cppa/ref_counted.hpp"
#include "cppa/uniform_type_info.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/fixed_vector.hpp"
#include "cppa/detail/tuple_vals.hpp"
#include "cppa/detail/abstract_tuple.hpp"
#include "cppa/detail/serialize_tuple.hpp"
namespace cppa { namespace detail {
template<typename... Ts>
class decorated_tuple : public abstract_tuple {
typedef abstract_tuple super;
static_assert(sizeof...(Ts) > 0,
"decorated_tuple is not allowed to be empty");
public:
typedef util::fixed_vector<size_t, sizeof...(Ts)> vector_type;
typedef cow_ptr<abstract_tuple> cow_pointer_type;
static inline cow_pointer_type create(cow_pointer_type d,
const vector_type& v) {
return cow_pointer_type{new decorated_tuple(std::move(d), v)};
}
// creates a subtuple form @p d with an offset
static inline cow_pointer_type create(cow_pointer_type d, size_t offset) {
return cow_pointer_type{new decorated_tuple(std::move(d), offset)};
}
virtual void* mutable_at(size_t pos) {
CPPA_REQUIRE(pos < size());
return m_decorated->mutable_at(m_mapping[pos]);
}
virtual size_t size() const {
return sizeof...(Ts);
}
virtual decorated_tuple* copy() const {
return new decorated_tuple(*this);
}
virtual const void* at(size_t pos) const {
CPPA_REQUIRE(pos < size());
return m_decorated->at(m_mapping[pos]);
}
virtual const uniform_type_info* type_at(size_t pos) const {
CPPA_REQUIRE(pos < size());
return m_decorated->type_at(m_mapping[pos]);
}
const std::type_info* type_token() const {
return static_type_list<Ts...>::list;
}
private:
cow_pointer_type m_decorated;
vector_type m_mapping;
decorated_tuple(cow_pointer_type d, const vector_type& v)
: super(tuple_impl_info::statically_typed)
, m_decorated(std::move(d)), m_mapping(v) {
# ifdef CPPA_DEBUG
const cow_pointer_type& ptr = m_decorated; // prevent detaching
# endif
CPPA_REQUIRE(ptr->size() >= sizeof...(Ts));
CPPA_REQUIRE(v.size() == sizeof...(Ts));
CPPA_REQUIRE(*(std::max_element(v.begin(), v.end())) < ptr->size());
}
decorated_tuple(cow_pointer_type d, size_t offset)
: super(tuple_impl_info::statically_typed), m_decorated(std::move(d)) {
# ifdef CPPA_DEBUG
const cow_pointer_type& ptr = m_decorated; // prevent detaching
# endif
CPPA_REQUIRE((ptr->size() - offset) >= sizeof...(Ts));
CPPA_REQUIRE(offset > 0);
size_t i = offset;
m_mapping.resize(sizeof...(Ts));
std::generate(m_mapping.begin(), m_mapping.end(), [&]() {return i++;});
}
decorated_tuple(const decorated_tuple&) = default;
decorated_tuple& operator=(const decorated_tuple&) = delete;
};
template<typename TypeList>
struct decorated_cow_tuple_from_type_list;
template<typename... Ts>
struct decorated_cow_tuple_from_type_list< util::type_list<Ts...> > {
typedef decorated_tuple<Ts...> type;
};
} } // namespace cppa::detail
#endif // CPPA_DECORATED_TUPLE_HPP