13 #ifndef GIL_DYNAMICIMAGE_VARIANT_HPP
14 #define GIL_DYNAMICIMAGE_VARIANT_HPP
25 #include "../../utilities.hpp"
30 #include <boost/bind.hpp>
31 #include <boost/utility/enable_if.hpp>
32 #include <boost/mpl/bool.hpp>
33 #include <boost/mpl/transform.hpp>
34 #include <boost/mpl/size.hpp>
35 #include <boost/mpl/sizeof.hpp>
36 #include <boost/mpl/max.hpp>
37 #include <boost/mpl/at.hpp>
38 #include <boost/mpl/fold.hpp>
40 namespace boost {
namespace gil {
43 template <
typename Types,
typename T>
struct type_to_index;
44 template <
typename Op,
typename T>
struct reduce;
45 struct destructor_op {
46 typedef void result_type;
47 template <
typename T> result_type operator()(
const T& t)
const { t.~T(); }
49 template <
typename T,
typename Bits>
void copy_construct_in_place(
const T& t, Bits& bits);
50 template <
typename Bits>
struct copy_construct_in_place_fn;
51 template <
typename Types>
struct type_to_index_fn;
87 template <
typename Types>
90 static const std::size_t MAX_SIZE = mpl::fold<Types, mpl::size_t<0>, mpl::max<mpl::_1, mpl::sizeof_<mpl::_2> > >::type::value;
91 static const std::size_t NUM_TYPES = mpl::size<Types>::value;
93 typedef Types types_t;
95 typedef struct {
char data[MAX_SIZE]; } base_t;
98 variant() : _index(0) {
new(&_bits)
typename mpl::at_c<Types,0>::type(); }
99 virtual ~
variant() { apply_operation(*
this, detail::destructor_op()); }
102 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); }
104 template <
typename Types2>
explicit variant(
const variant<Types2>& obj) : _index(apply_operation(obj,detail::type_to_index_fn<Types>())) {
105 if (_index==NUM_TYPES)
throw std::bad_cast();
106 apply_operation(obj, detail::copy_construct_in_place_fn<base_t>(_bits));
110 template <
typename T>
explicit variant(T& obj,
bool do_swap);
112 template <
typename T>
variant& operator=(
const T& obj) {
variant tmp(obj); swap(*
this,tmp);
return *
this; }
115 variant(
const variant& v) : _index(v._index) { apply_operation(v, detail::copy_construct_in_place_fn<base_t>(_bits)); }
116 template <
typename T>
void move_in(T& obj) {
variant tmp(obj,
true); swap(*
this,tmp); }
121 template <
typename T>
static bool has_type() {
return type_id<T>()!=NUM_TYPES; }
123 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); }
124 template <
typename T> T& _dynamic_cast() {
if (!current_type_is<T>())
throw std::bad_cast();
return *gil_reinterpret_cast < T*>(&_bits); }
126 template <
typename T>
bool current_type_is()
const {
return type_id<T>()==_index; }
128 base_t bits()
const {
return _bits; }
129 std::size_t index()
const {
return _index; }
135 template <
typename Types2,
typename UnaryOp>
friend typename UnaryOp::result_type apply_operation(
variant<Types2>& var, UnaryOp op);
136 template <
typename Types2,
typename UnaryOp>
friend typename UnaryOp::result_type apply_operation(
const variant<Types2>& var, UnaryOp op);
137 template <
typename Types1,
typename Types2,
typename BinaryOp>
friend typename BinaryOp::result_type apply_operation(
const variant<Types1>& arg1,
const variant<Types2>& arg2, BinaryOp op);
145 template <
typename T,
typename Bits>
146 void copy_construct_in_place(
const T& t, Bits& bits) {
147 T& b=*gil_reinterpret_cast<T*>(&bits);
151 template <
typename Bits>
152 struct copy_construct_in_place_fn {
153 typedef void result_type;
155 copy_construct_in_place_fn(Bits& dst) : _dst(dst) {}
157 template <
typename T>
void operator()(
const T& src)
const { copy_construct_in_place(src,_dst); }
160 template <
typename Bits>
163 equal_to_fn(
const Bits& dst) : _dst(dst) {}
165 typedef bool result_type;
166 template <
typename T> result_type operator()(
const T& x)
const {
167 return x==*gil_reinterpret_cast_c<const T*>(&_dst);
171 template <
typename Types>
172 struct type_to_index_fn {
173 typedef std::size_t result_type;
175 template <
typename T> result_type operator()(
const T&)
const {
return detail::type_to_index<Types,T>::value; }
180 template <
typename Types>
181 template <
typename T> variant<Types>::variant(T& obj,
bool do_swap) {
183 if (_index==NUM_TYPES)
throw std::bad_cast();
187 swap(obj, *gil_reinterpret_cast<T*>(&_bits));
189 detail::copy_construct_in_place(const_cast<const T&>(obj), _bits);
192 template <
typename Types>
193 void swap(variant<Types>& x, variant<Types>& y) {
198 template <
typename Types>
199 inline bool operator==(
const variant<Types>& x,
const variant<Types>& y) {
200 return x._index==y._index &&
apply_operation(x,detail::equal_to_fn<
typename variant<Types>::base_t>(y._bits));
203 template <
typename C>
204 inline bool operator!=(
const variant<C>& x,
const variant<C>& y) {
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:35
Represents a concrete instance of a run-time specified type from a set of typesA concept is typically...
Definition: variant.hpp:88
void swap(const boost::gil::planar_pixel_reference< CR, CS > x, const boost::gil::planar_pixel_reference< CR, CS > y)
swap for planar_pixel_reference
Definition: planar_pixel_reference.hpp:192
Returns the index corresponding to the first occurrance of a given given type in. ...
Definition: utilities.hpp:306