2 Copyright 2005-2007 Adobe Systems Incorporated
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).
8 See http://opensource.adobe.com/gil for most recent version including documentation.
11 /*************************************************************************************************/
13 #ifndef GIL_DYNAMICIMAGE_VARIANT_HPP
14 #define GIL_DYNAMICIMAGE_VARIANT_HPP
16 ////////////////////////////////////////////////////////////////////////////////////////
18 /// \brief Support for run-time instantiated types
19 /// \author Lubomir Bourdev and Hailin Jin \n
20 /// Adobe Systems Incorporated
21 /// \date 2005-2007 \n Last updated on September 18, 2007
23 ////////////////////////////////////////////////////////////////////////////////////////
25 #include "../../gil_config.hpp"
26 #include "../../utilities.hpp"
31 #include <boost/bind.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;
53 \brief Represents a concrete instance of a run-time specified type from a set of types
57 A concept is typically modeled by a collection of different types. They may be instantiations
58 of a templated type with different template parameters or even completely unrelated types.
60 We call the type with which the concept is instantiated in a given place in the code "the concrete type".
61 The concrete type must be chosen at compile time, which sometimes is a severe limitation.
62 Consider, for example, having an image concept modeled by an image class templated over the color space.
63 It would be difficult to write a function that reads an image from file preserving its native color space, since the
64 type of the return value is only available at run time. It would be difficult to store images of different color
65 spaces in the same container or apply operations on them uniformly.
67 The variant class addresses this deficiency. It allows for run-time instantiation of a class from a given set of allowed classes
68 specified at compile time. For example, the set of allowed classes may include 8-bit and 16-bit RGB and CMYK images. Such a variant
69 can be constructed with rgb8_image_t and then assigned a cmyk16_image_t.
71 The variant has a templated constructor, which allows us to construct it with any concrete type instantiation. It can also perform a generic
72 operation on the concrete type via a call to apply_operation. The operation must be provided as a function object whose application
73 operator has a single parameter which can be instantiated with any of the allowed types of the variant.
75 variant breaks down the instantiated type into a non-templated underlying base type and a unique instantiation
76 type identifier. In the most common implementation the concrete instantiation in stored 'in-place' - in 'bits_t'.
77 bits_t contains sufficient space to fit the largest of the instantiated objects.
79 GIL's variant is similar to boost::variant in spirit (hence we borrow the name from there) but it differs in several ways from the current boost
80 implementation. Most notably, it does not take a variable number of template parameters but a single parameter defining the type enumeration. As
81 such it can be used more effectively in generic code.
83 The Types parameter specifies the set of allowable types. It models MPL Random Access Container
86 template <typename Types> // models MPL Random Access Container
88 // size in bytes of the largest type in Types
89 static const std::size_t MAX_SIZE = mpl::fold<Types, mpl::size_t<0>, mpl::max<mpl::_1, mpl::sizeof_<mpl::_2> > >::type::value;
90 static const std::size_t NUM_TYPES = mpl::size<Types>::value;
92 typedef Types types_t;
94 typedef struct { char data[MAX_SIZE]; } base_t; // empty space equal to the size of the largest type in Types
96 // Default constructor - default construct the first type
97 variant() : _index(0) { new(&_bits) typename mpl::at_c<Types,0>::type(); }
98 virtual ~variant() { apply_operation(*this, detail::destructor_op()); }
100 // Throws std::bad_cast if T is not in Types
101 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 // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
104 template <typename T> explicit variant(T& obj, bool do_swap);
106 template <typename T> variant& operator=(const T& obj) { variant tmp(obj); swap(*this,tmp); return *this; }
107 variant& operator=(const variant& v) { variant tmp(v ); swap(*this,tmp); return *this; }
109 variant(const variant& v) : _index(v._index) { apply_operation(v, detail::copy_construct_in_place_fn<base_t>(_bits)); }
110 template <typename T> void move_in(T& obj) { variant tmp(obj, true); swap(*this,tmp); }
112 template <typename TS> friend bool operator==(const variant<TS>& x, const variant<TS>& y);
113 template <typename TS> friend bool operator!=(const variant<TS>& x, const variant<TS>& y);
115 template <typename T> static bool has_type() { return type_id<T>()!=NUM_TYPES; }
117 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); }
118 template <typename T> T& _dynamic_cast() { if (!current_type_is<T>()) throw std::bad_cast(); return *gil_reinterpret_cast < T*>(&_bits); }
120 template <typename T> bool current_type_is() const { return type_id<T>()==_index; }
122 base_t bits() const { return _bits; }
123 std::size_t index() const { return _index; }
126 template <typename T> static std::size_t type_id() { return detail::type_to_index<Types,T>::value; }
128 template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
129 template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(variant<Types2>& var, UnaryOp op);
130 template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(const variant<Types2>& var, UnaryOp op);
131 template <typename Types1, typename Types2, typename BinaryOp> friend typename BinaryOp::result_type apply_operation(const variant<Types1>& arg1, const variant<Types2>& arg2, BinaryOp op);
139 template <typename T, typename Bits>
140 void copy_construct_in_place(const T& t, Bits& bits) {
141 T& b=*gil_reinterpret_cast<T*>(&bits);
142 new(&b)T(t); // default-construct
145 template <typename Bits>
146 struct copy_construct_in_place_fn {
147 typedef void result_type;
149 copy_construct_in_place_fn(Bits& dst) : _dst(dst) {}
151 template <typename T> void operator()(const T& src) const { copy_construct_in_place(src,_dst); }
154 template <typename Bits>
157 equal_to_fn(const Bits& dst) : _dst(dst) {}
159 typedef bool result_type;
160 template <typename T> result_type operator()(const T& x) const {
161 return x==*gil_reinterpret_cast_c<const T*>(&_dst);
166 // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
167 template <typename Types>
168 template <typename T> variant<Types>::variant(T& obj, bool do_swap) {
170 if (_index==NUM_TYPES) throw std::bad_cast();
173 new(&_bits) T(); // default construct
174 swap(obj, *gil_reinterpret_cast<T*>(&_bits));
176 detail::copy_construct_in_place(const_cast<const T&>(obj), _bits);
179 template <typename Types>
180 void swap(variant<Types>& x, variant<Types>& y) {
181 std::swap(x._bits,y._bits);
182 std::swap(x._index, y._index);
185 template <typename Types>
186 inline bool operator==(const variant<Types>& x, const variant<Types>& y) {
187 return x._index==y._index && apply_operation(x,detail::equal_to_fn<typename variant<Types>::base_t>(y._bits));
190 template <typename C>
191 inline bool operator!=(const variant<C>& x, const variant<C>& y) {
195 } } // namespace boost::gil