mirror of
https://github.com/boostorg/gil.git
synced 2026-02-18 14:12:10 +00:00
GIL: Added support for copying between variants of different types
[SVN r61899]
This commit is contained in:
committed by
Stefan Seefeld
parent
33d94c303e
commit
e548b4477d
@@ -84,9 +84,11 @@ public:
|
||||
template <typename T> explicit any_image(const T& obj) : parent_t(obj) {}
|
||||
template <typename T> explicit any_image(T& obj, bool do_swap) : parent_t(obj,do_swap) {}
|
||||
any_image(const any_image& v) : parent_t((const parent_t&)v) {}
|
||||
template <typename Types> any_image(const any_image<Types>& v) : parent_t((const variant<Types>&)v) {}
|
||||
|
||||
template <typename T> any_image& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
|
||||
any_image& operator=(const any_image& v) { parent_t::operator=((const parent_t&)v); return *this;}
|
||||
template <typename T> any_image& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
|
||||
any_image& operator=(const any_image& v) { parent_t::operator=((const parent_t&)v); return *this;}
|
||||
template <typename Types> any_image& operator=(const any_image<Types>& v) { parent_t::operator=((const variant<Types>&)v); return *this;}
|
||||
|
||||
void recreate(const point_t& dims, unsigned alignment=1) { apply_operation(*this,detail::recreate_image_fnobj(dims,alignment)); }
|
||||
void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1) { recreate(point2<std::ptrdiff_t>(width,height),alignment); }
|
||||
|
||||
@@ -72,9 +72,11 @@ public:
|
||||
any_image_view() : parent_t() {}
|
||||
template <typename T> explicit any_image_view(const T& obj) : parent_t(obj) {}
|
||||
any_image_view(const any_image_view& v) : parent_t((const parent_t&)v) {}
|
||||
template <typename Types> any_image_view(const any_image_view<Types>& v) : parent_t((const variant<Types>&)v) {}
|
||||
|
||||
template <typename T> any_image_view& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
|
||||
any_image_view& operator=(const any_image_view& v) { parent_t::operator=((const parent_t&)v); return *this;}
|
||||
template <typename T> any_image_view& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
|
||||
any_image_view& operator=(const any_image_view& v) { parent_t::operator=((const parent_t&)v); return *this;}
|
||||
template <typename Types> any_image_view& operator=(const any_image_view<Types>& v) { parent_t::operator=((const variant<Types>&)v); return *this;}
|
||||
|
||||
std::size_t num_channels() const { return apply_operation(*this, detail::any_type_get_num_channels()); }
|
||||
point_t dimensions() const { return apply_operation(*this, detail::any_type_get_dimensions()); }
|
||||
|
||||
@@ -29,7 +29,8 @@
|
||||
#include <algorithm>
|
||||
#include <typeinfo>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/mpl/transform.hpp>
|
||||
#include <boost/mpl/size.hpp>
|
||||
#include <boost/mpl/sizeof.hpp>
|
||||
@@ -48,6 +49,7 @@ namespace detail {
|
||||
};
|
||||
template <typename T, typename Bits> void copy_construct_in_place(const T& t, Bits& bits);
|
||||
template <typename Bits> struct copy_construct_in_place_fn;
|
||||
template <typename Types> struct type_to_index_fn;
|
||||
}
|
||||
/**
|
||||
\brief Represents a concrete instance of a run-time specified type from a set of types
|
||||
@@ -98,7 +100,12 @@ public:
|
||||
virtual ~variant() { apply_operation(*this, detail::destructor_op()); }
|
||||
|
||||
// Throws std::bad_cast if T is not in Types
|
||||
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); }
|
||||
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); }
|
||||
|
||||
template <typename Types2> explicit variant(const variant<Types2>& obj) : _index(apply_operation(obj,detail::type_to_index_fn<Types>())) {
|
||||
if (_index==NUM_TYPES) throw std::bad_cast();
|
||||
apply_operation(obj, detail::copy_construct_in_place_fn<base_t>(_bits));
|
||||
}
|
||||
|
||||
// When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
|
||||
template <typename T> explicit variant(T& obj, bool do_swap);
|
||||
@@ -125,7 +132,7 @@ public:
|
||||
private:
|
||||
template <typename T> static std::size_t type_id() { return detail::type_to_index<Types,T>::value; }
|
||||
|
||||
template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
|
||||
template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
|
||||
template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(variant<Types2>& var, UnaryOp op);
|
||||
template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(const variant<Types2>& var, UnaryOp op);
|
||||
template <typename Types1, typename Types2, typename BinaryOp> friend typename BinaryOp::result_type apply_operation(const variant<Types1>& arg1, const variant<Types2>& arg2, BinaryOp op);
|
||||
@@ -161,6 +168,13 @@ namespace detail {
|
||||
return x==*gil_reinterpret_cast_c<const T*>(&_dst);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Types>
|
||||
struct type_to_index_fn {
|
||||
typedef std::size_t result_type;
|
||||
|
||||
template <typename T> result_type operator()(const T&) const { return detail::type_to_index<Types,T>::value; }
|
||||
};
|
||||
}
|
||||
|
||||
// When doSwap is true, swaps obj with the contents of the variant. obj will contain default-constructed instance after the call
|
||||
|
||||
Reference in New Issue
Block a user