8 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_VARIANT_HPP
9 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_VARIANT_HPP
13 #include <boost/gil/utilities.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/mpl/at.hpp>
17 #include <boost/mpl/bool.hpp>
18 #include <boost/mpl/fold.hpp>
19 #include <boost/mpl/max.hpp>
20 #include <boost/mpl/size.hpp>
21 #include <boost/mpl/sizeof.hpp>
22 #include <boost/mpl/transform.hpp>
23 #include <boost/utility/enable_if.hpp>
30 namespace boost {
namespace gil {
35 template <
typename Types,
typename T>
struct type_to_index;
36 template <
typename Op,
typename T>
struct reduce;
37 struct destructor_op {
38 typedef void result_type;
39 template <
typename T> result_type operator()(
const T& t)
const { t.~T(); }
41 template <
typename T,
typename Bits>
void copy_construct_in_place(
const T& t, Bits& bits);
42 template <
typename Bits>
struct copy_construct_in_place_fn;
43 template <
typename Types>
struct type_to_index_fn;
79 template <
typename Types>
82 static const std::size_t MAX_SIZE = mpl::fold<Types, mpl::size_t<0>, mpl::max<mpl::_1, mpl::sizeof_<mpl::_2> > >::type::value;
83 static const std::size_t NUM_TYPES = mpl::size<Types>::value;
85 typedef Types types_t;
87 typedef struct {
char data[MAX_SIZE]; } base_t;
90 variant() : _index(0) {
new(&_bits)
typename mpl::at_c<Types,0>::type(); }
91 virtual ~
variant() { apply_operation(*
this, detail::destructor_op()); }
94 template <
typename T>
explicit variant(
const T& obj){ _index=type_id<T>();
if (_index==NUM_TYPES)
throw std::bad_cast(); detail::copy_construct_in_place(obj, _bits); }
96 template <
typename Types2>
explicit variant(
const variant<Types2>& obj) : _index(apply_operation(obj,detail::type_to_index_fn<Types>())) {
97 if (_index==NUM_TYPES)
throw std::bad_cast();
98 apply_operation(obj, detail::copy_construct_in_place_fn<base_t>(_bits));
102 template <
typename T>
explicit variant(T& obj,
bool do_swap);
104 template <
typename T>
variant& operator=(
const T& obj) {
variant tmp(obj); swap(*
this,tmp);
return *
this; }
107 variant(
const variant& v) : _index(v._index) { apply_operation(v, detail::copy_construct_in_place_fn<base_t>(_bits)); }
108 template <
typename T>
void move_in(T& obj) {
variant tmp(obj,
true); swap(*
this,tmp); }
113 template <
typename T>
static bool has_type() {
return type_id<T>()!=NUM_TYPES; }
115 template <
typename T>
const T& _dynamic_cast()
const {
if (!current_type_is<T>())
throw std::bad_cast();
return *gil_reinterpret_cast_c<const T*>(&_bits); }
116 template <
typename T> T& _dynamic_cast() {
if (!current_type_is<T>())
throw std::bad_cast();
return *gil_reinterpret_cast < T*>(&_bits); }
118 template <
typename T>
bool current_type_is()
const {
return type_id<T>()==_index; }
120 base_t bits()
const {
return _bits; }
121 std::size_t index()
const {
return _index; }
127 template <
typename Types2,
typename UnaryOp>
friend typename UnaryOp::result_type apply_operation(
variant<Types2>& var, UnaryOp op);
128 template <
typename Types2,
typename UnaryOp>
friend typename UnaryOp::result_type apply_operation(
const variant<Types2>& var, UnaryOp op);
129 template <
typename Types1,
typename Types2,
typename BinaryOp>
friend typename BinaryOp::result_type apply_operation(
const variant<Types1>& arg1,
const variant<Types2>& arg2, BinaryOp op);
137 template <
typename T,
typename Bits>
138 void copy_construct_in_place(
const T& t, Bits& bits) {
139 T& b=*gil_reinterpret_cast<T*>(&bits);
143 template <
typename Bits>
144 struct copy_construct_in_place_fn {
145 typedef void result_type;
147 copy_construct_in_place_fn(Bits& dst) : _dst(dst) {}
149 template <
typename T>
void operator()(
const T& src)
const { copy_construct_in_place(src,_dst); }
152 template <
typename Bits>
155 equal_to_fn(
const Bits& dst) : _dst(dst) {}
157 typedef bool result_type;
158 template <
typename T> result_type operator()(
const T& x)
const {
159 return x==*gil_reinterpret_cast_c<const T*>(&_dst);
163 template <
typename Types>
164 struct type_to_index_fn {
165 typedef std::size_t result_type;
167 template <
typename T> result_type operator()(
const T&)
const {
return detail::type_to_index<Types,T>::value; }
172 template <
typename Types>
173 template <
typename T> variant<Types>::variant(T& obj,
bool do_swap) {
175 if (_index==NUM_TYPES)
throw std::bad_cast();
179 swap(obj, *gil_reinterpret_cast<T*>(&_bits));
181 detail::copy_construct_in_place(const_cast<const T&>(obj), _bits);
184 template <
typename Types>
185 void swap(variant<Types>& x, variant<Types>& y) {
190 template <
typename Types>
191 inline bool operator==(
const variant<Types>& x,
const variant<Types>& y) {
192 return x._index==y._index &&
apply_operation(x,detail::equal_to_fn<
typename variant<Types>::base_t>(y._bits));
195 template <
typename C>
196 inline bool operator!=(
const variant<C>& x,
const variant<C>& y) {
void swap(const boost::gil::packed_channel_reference< BF, FB, NB, M > x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:480
BOOST_FORCEINLINE UnaryOp::result_type apply_operation(variant< Types > &arg, UnaryOp op)
Invokes a generic mutable operation (represented as a unary function object) on a variant...
Definition: apply_operation.hpp:31
Represents a concrete instance of a run-time specified type from a set of typesA concept is typically...
Definition: variant.hpp:80
Returns the index corresponding to the first occurrance of a given given type in. ...
Definition: utilities.hpp:233