Boost GIL


variant.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_VARIANT_HPP
9 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_VARIANT_HPP
10 
11 // TODO: Replace with C++17 std::variant?
12 
13 #include <boost/gil/utilities.hpp>
14 
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>
24 
25 #include <algorithm>
26 #include <cassert>
27 #include <cstddef>
28 #include <typeinfo>
29 
30 namespace boost { namespace gil {
31 
32 // Support for run-time instantiated types
33 
34 namespace detail {
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(); }
40  };
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;
44 }
79 template <typename Types> // models MPL Random Access Container
80 class variant {
81  // size in bytes of the largest type in 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;
84 public:
85  typedef Types types_t;
86 
87  typedef struct { char data[MAX_SIZE]; } base_t; // empty space equal to the size of the largest type in Types
88 
89  // Default constructor - default construct the first type
90  variant() : _index(0) { new(&_bits) typename mpl::at_c<Types,0>::type(); }
91  virtual ~variant() { apply_operation(*this, detail::destructor_op()); }
92 
93  // Throws std::bad_cast if T is not in Types
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); }
95 
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));
99  }
100 
101  // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
102  template <typename T> explicit variant(T& obj, bool do_swap);
103 
104  template <typename T> variant& operator=(const T& obj) { variant tmp(obj); swap(*this,tmp); return *this; }
105  variant& operator=(const variant& v) { variant tmp(v ); swap(*this,tmp); return *this; }
106 
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); }
109 
110  template <typename TS> friend bool operator==(const variant<TS>& x, const variant<TS>& y);
111  template <typename TS> friend bool operator!=(const variant<TS>& x, const variant<TS>& y);
112 
113  template <typename T> static bool has_type() { return type_id<T>()!=NUM_TYPES; }
114 
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); }
117 
118  template <typename T> bool current_type_is() const { return type_id<T>()==_index; }
119 
120  base_t bits() const { return _bits; }
121  std::size_t index() const { return _index; }
122 
123 private:
124  template <typename T> static std::size_t type_id() { return detail::type_to_index<Types,T>::value; }
125 
126  template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
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);
130 
131  base_t _bits;
132  std::size_t _index;
133 };
134 
135 namespace detail {
136 
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);
140  new(&b)T(t); // default-construct
141  }
142 
143  template <typename Bits>
144  struct copy_construct_in_place_fn {
145  typedef void result_type;
146  Bits& _dst;
147  copy_construct_in_place_fn(Bits& dst) : _dst(dst) {}
148 
149  template <typename T> void operator()(const T& src) const { copy_construct_in_place(src,_dst); }
150  };
151 
152  template <typename Bits>
153  struct equal_to_fn {
154  const Bits& _dst;
155  equal_to_fn(const Bits& dst) : _dst(dst) {}
156 
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);
160  }
161  };
162 
163  template <typename Types>
164  struct type_to_index_fn {
165  typedef std::size_t result_type;
166 
167  template <typename T> result_type operator()(const T&) const { return detail::type_to_index<Types,T>::value; }
168  };
169 }
170 
171 // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
172 template <typename Types>
173 template <typename T> variant<Types>::variant(T& obj, bool do_swap) {
174  _index=type_id<T>();
175  if (_index==NUM_TYPES) throw std::bad_cast();
176 
177  if (do_swap) {
178  new(&_bits) T(); // default construct
179  swap(obj, *gil_reinterpret_cast<T*>(&_bits));
180  } else
181  detail::copy_construct_in_place(const_cast<const T&>(obj), _bits);
182 }
183 
184 template <typename Types>
185 void swap(variant<Types>& x, variant<Types>& y) {
186  std::swap(x._bits,y._bits);
187  std::swap(x._index, y._index);
188 }
189 
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));
193 }
194 
195 template <typename C>
196 inline bool operator!=(const variant<C>& x, const variant<C>& y) {
197  return !(x==y);
198 }
199 
200 } } // namespace boost::gil
201 
202 #endif
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