variant.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_DYNAMICIMAGE_VARIANT_HPP
  10. #define GIL_DYNAMICIMAGE_VARIANT_HPP
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief Support for run-time instantiated types
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated on September 18, 2007
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include "../../gil_config.hpp"
  20. #include "../../utilities.hpp"
  21. #include <cstddef>
  22. #include <cassert>
  23. #include <algorithm>
  24. #include <typeinfo>
  25. #include <boost/bind.hpp>
  26. #include <boost/mpl/transform.hpp>
  27. #include <boost/mpl/size.hpp>
  28. #include <boost/mpl/sizeof.hpp>
  29. #include <boost/mpl/max.hpp>
  30. #include <boost/mpl/at.hpp>
  31. #include <boost/mpl/fold.hpp>
  32. namespace boost { namespace gil {
  33. namespace detail {
  34. template <typename Types, typename T> struct type_to_index;
  35. template <typename Op, typename T> struct reduce;
  36. struct destructor_op {
  37. typedef void result_type;
  38. template <typename T> result_type operator()(const T& t) const { t.~T(); }
  39. };
  40. template <typename T, typename Bits> void copy_construct_in_place(const T& t, Bits& bits);
  41. template <typename Bits> struct copy_construct_in_place_fn;
  42. }
  43. /**
  44. \brief Represents a concrete instance of a run-time specified type from a set of types
  45. \class variant
  46. \ingroup Variant
  47. A concept is typically modeled by a collection of different types. They may be instantiations
  48. of a templated type with different template parameters or even completely unrelated types.
  49. We call the type with which the concept is instantiated in a given place in the code "the concrete type".
  50. The concrete type must be chosen at compile time, which sometimes is a severe limitation.
  51. Consider, for example, having an image concept modeled by an image class templated over the color space.
  52. It would be difficult to write a function that reads an image from file preserving its native color space, since the
  53. type of the return value is only available at run time. It would be difficult to store images of different color
  54. spaces in the same container or apply operations on them uniformly.
  55. The variant class addresses this deficiency. It allows for run-time instantiation of a class from a given set of allowed classes
  56. 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
  57. can be constructed with rgb8_image_t and then assigned a cmyk16_image_t.
  58. The variant has a templated constructor, which allows us to construct it with any concrete type instantiation. It can also perform a generic
  59. operation on the concrete type via a call to apply_operation. The operation must be provided as a function object whose application
  60. operator has a single parameter which can be instantiated with any of the allowed types of the variant.
  61. variant breaks down the instantiated type into a non-templated underlying base type and a unique instantiation
  62. type identifier. In the most common implementation the concrete instantiation in stored 'in-place' - in 'bits_t'.
  63. bits_t contains sufficient space to fit the largest of the instantiated objects.
  64. 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
  65. implementation. Most notably, it does not take a variable number of template parameters but a single parameter defining the type enumeration. As
  66. such it can be used more effectively in generic code.
  67. The Types parameter specifies the set of allowable types. It models MPL Random Access Container
  68. */
  69. template <typename Types> // models MPL Random Access Container
  70. class variant {
  71. // size in bytes of the largest type in Types
  72. static const std::size_t MAX_SIZE = mpl::fold<Types, mpl::size_t<0>, mpl::max<mpl::_1, mpl::sizeof_<mpl::_2> > >::type::value;
  73. static const std::size_t NUM_TYPES = mpl::size<Types>::value;
  74. public:
  75. typedef Types types_t;
  76. typedef struct { char data[MAX_SIZE]; } base_t; // empty space equal to the size of the largest type in Types
  77. // Default constructor - default construct the first type
  78. variant() : _index(0) { new(&_bits) typename mpl::at_c<Types,0>::type(); }
  79. virtual ~variant() { apply_operation(*this, detail::destructor_op()); }
  80. // Throws std::bad_cast if T is not in Types
  81. 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); }
  82. // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
  83. template <typename T> explicit variant(T& obj, bool do_swap);
  84. template <typename T> variant& operator=(const T& obj) { variant tmp(obj); swap(*this,tmp); return *this; }
  85. variant& operator=(const variant& v) { variant tmp(v ); swap(*this,tmp); return *this; }
  86. variant(const variant& v) : _index(v._index) { apply_operation(v, detail::copy_construct_in_place_fn<base_t>(_bits)); }
  87. template <typename T> void move_in(T& obj) { variant tmp(obj, true); swap(*this,tmp); }
  88. template <typename TS> friend bool operator==(const variant<TS>& x, const variant<TS>& y);
  89. template <typename TS> friend bool operator!=(const variant<TS>& x, const variant<TS>& y);
  90. template <typename T> static bool has_type() { return type_id<T>()!=NUM_TYPES; }
  91. 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); }
  92. template <typename T> T& _dynamic_cast() { if (!current_type_is<T>()) throw std::bad_cast(); return *gil_reinterpret_cast < T*>(&_bits); }
  93. template <typename T> bool current_type_is() const { return type_id<T>()==_index; }
  94. base_t bits() const { return _bits; }
  95. std::size_t index() const { return _index; }
  96. private:
  97. template <typename T> static std::size_t type_id() { return detail::type_to_index<Types,T>::value; }
  98. template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
  99. template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(variant<Types2>& var, UnaryOp op);
  100. template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(const variant<Types2>& var, UnaryOp op);
  101. template <typename Types1, typename Types2, typename BinaryOp> friend typename BinaryOp::result_type apply_operation(const variant<Types1>& arg1, const variant<Types2>& arg2, BinaryOp op);
  102. base_t _bits;
  103. std::size_t _index;
  104. };
  105. namespace detail {
  106. template <typename T, typename Bits>
  107. void copy_construct_in_place(const T& t, Bits& bits) {
  108. T& b=*gil_reinterpret_cast<T*>(&bits);
  109. new(&b)T(t); // default-construct
  110. }
  111. template <typename Bits>
  112. struct copy_construct_in_place_fn {
  113. typedef void result_type;
  114. Bits& _dst;
  115. copy_construct_in_place_fn(Bits& dst) : _dst(dst) {}
  116. template <typename T> void operator()(const T& src) const { copy_construct_in_place(src,_dst); }
  117. };
  118. template <typename Bits>
  119. struct equal_to_fn {
  120. const Bits& _dst;
  121. equal_to_fn(const Bits& dst) : _dst(dst) {}
  122. typedef bool result_type;
  123. template <typename T> result_type operator()(const T& x) const {
  124. return x==*gil_reinterpret_cast_c<const T*>(&_dst);
  125. }
  126. };
  127. }
  128. // When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
  129. template <typename Types>
  130. template <typename T> variant<Types>::variant(T& obj, bool do_swap) {
  131. _index=type_id<T>();
  132. if (_index==NUM_TYPES) throw std::bad_cast();
  133. if (do_swap) {
  134. new(&_bits) T(); // default construct
  135. swap(obj, *gil_reinterpret_cast<T*>(&_bits));
  136. } else
  137. detail::copy_construct_in_place(const_cast<const T&>(obj), _bits);
  138. }
  139. template <typename Types>
  140. void swap(variant<Types>& x, variant<Types>& y) {
  141. std::swap(x._bits,y._bits);
  142. std::swap(x._index, y._index);
  143. }
  144. template <typename Types>
  145. inline bool operator==(const variant<Types>& x, const variant<Types>& y) {
  146. return x._index==y._index && apply_operation(x,detail::equal_to_fn<typename variant<Types>::base_t>(y._bits));
  147. }
  148. template <typename C>
  149. inline bool operator!=(const variant<C>& x, const variant<C>& y) {
  150. return !(x==y);
  151. }
  152. } } // namespace boost::gil
  153. #endif