Boost GIL


variant.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3 
4  Use, modification and distribution are subject to the Boost Software License,
5  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6  http://www.boost.org/LICENSE_1_0.txt).
7 
8  See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 
11 /*************************************************************************************************/
12 
13 #ifndef GIL_DYNAMICIMAGE_VARIANT_HPP
14 #define GIL_DYNAMICIMAGE_VARIANT_HPP
15 
24 
25 #include "../../utilities.hpp"
26 #include <cstddef>
27 #include <cassert>
28 #include <algorithm>
29 #include <typeinfo>
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>
39 
40 namespace boost { namespace gil {
41 
42 namespace detail {
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(); }
48  };
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;
52 }
87 template <typename Types> // models MPL Random Access Container
88 class variant {
89  // size in bytes of the largest type in 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;
92 public:
93  typedef Types types_t;
94 
95  typedef struct { char data[MAX_SIZE]; } base_t; // empty space equal to the size of the largest type in Types
96 
97  // Default constructor - default construct the first type
98  variant() : _index(0) { new(&_bits) typename mpl::at_c<Types,0>::type(); }
99  virtual ~variant() { apply_operation(*this, detail::destructor_op()); }
100 
101  // Throws std::bad_cast if T is not in Types
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); }
103 
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));
107  }
108 
109  // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
110  template <typename T> explicit variant(T& obj, bool do_swap);
111 
112  template <typename T> variant& operator=(const T& obj) { variant tmp(obj); swap(*this,tmp); return *this; }
113  variant& operator=(const variant& v) { variant tmp(v ); swap(*this,tmp); return *this; }
114 
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); }
117 
118  template <typename TS> friend bool operator==(const variant<TS>& x, const variant<TS>& y);
119  template <typename TS> friend bool operator!=(const variant<TS>& x, const variant<TS>& y);
120 
121  template <typename T> static bool has_type() { return type_id<T>()!=NUM_TYPES; }
122 
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); }
125 
126  template <typename T> bool current_type_is() const { return type_id<T>()==_index; }
127 
128  base_t bits() const { return _bits; }
129  std::size_t index() const { return _index; }
130 
131 private:
132  template <typename T> static std::size_t type_id() { return detail::type_to_index<Types,T>::value; }
133 
134  template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
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);
138 
139  base_t _bits;
140  std::size_t _index;
141 };
142 
143 namespace detail {
144 
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);
148  new(&b)T(t); // default-construct
149  }
150 
151  template <typename Bits>
152  struct copy_construct_in_place_fn {
153  typedef void result_type;
154  Bits& _dst;
155  copy_construct_in_place_fn(Bits& dst) : _dst(dst) {}
156 
157  template <typename T> void operator()(const T& src) const { copy_construct_in_place(src,_dst); }
158  };
159 
160  template <typename Bits>
161  struct equal_to_fn {
162  const Bits& _dst;
163  equal_to_fn(const Bits& dst) : _dst(dst) {}
164 
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);
168  }
169  };
170 
171  template <typename Types>
172  struct type_to_index_fn {
173  typedef std::size_t result_type;
174 
175  template <typename T> result_type operator()(const T&) const { return detail::type_to_index<Types,T>::value; }
176  };
177 }
178 
179 // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
180 template <typename Types>
181 template <typename T> variant<Types>::variant(T& obj, bool do_swap) {
182  _index=type_id<T>();
183  if (_index==NUM_TYPES) throw std::bad_cast();
184 
185  if (do_swap) {
186  new(&_bits) T(); // default construct
187  swap(obj, *gil_reinterpret_cast<T*>(&_bits));
188  } else
189  detail::copy_construct_in_place(const_cast<const T&>(obj), _bits);
190 }
191 
192 template <typename Types>
193 void swap(variant<Types>& x, variant<Types>& y) {
194  std::swap(x._bits,y._bits);
195  std::swap(x._index, y._index);
196 }
197 
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));
201 }
202 
203 template <typename C>
204 inline bool operator!=(const variant<C>& x, const variant<C>& y) {
205  return !(x==y);
206 }
207 
208 } } // namespace boost::gil
209 
210 #endif
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