Fixes circa safe and static_variant

This commit is contained in:
Nathan Hourt 2015-03-02 18:24:37 -05:00
parent b393ddc1da
commit d0803ec9cd
2 changed files with 61 additions and 11 deletions

View file

@ -31,7 +31,8 @@ namespace fc {
if( b.value < 0 && a.value < std::numeric_limits<T>::min() - b.value ) FC_CAPTURE_AND_THROW( underflow_exception, (a)(b) );
return safe(a.value+b.value);
}
safe& operator *= ( T v ) { value *= v; return *this; }
safe& operator *= ( safe v ) { value *= v.value; return *this; }
safe& operator /= ( safe v ) { FC_ASSERT(v.value != 0); value /= v.value; return *this; }
safe& operator -= ( const safe& b ) { return *this += safe(-b.value); }
safe operator -()const
{

View file

@ -29,6 +29,7 @@ struct type_info;
template<typename StaticVariant>
struct copy_construct
{
typedef void result_type;
StaticVariant& sv;
copy_construct( StaticVariant& s ):sv(s){}
template<typename T>
@ -38,6 +39,19 @@ struct copy_construct
}
};
template<typename StaticVariant>
struct move_construct
{
typedef void result_type;
StaticVariant& sv;
move_construct( StaticVariant& s ):sv(s){}
template<typename T>
void operator()( T& v )const
{
sv.init( std::move(v) );
}
};
template<int N, typename T, typename... Ts>
struct storage_ops<N, T&, Ts...> {
static void del(int n, void *data) {}
@ -183,8 +197,16 @@ class static_variant {
new(storage) X(x);
}
template<typename X>
void init(X&& x) {
_tag = impl::position<X, Types...>::pos;
new(storage) X( std::move(x) );
}
template<typename StaticVariant>
friend struct copy_construct;
friend struct impl::copy_construct;
template<typename StaticVariant>
friend struct impl::move_construct;
public:
template<typename X>
struct tag
@ -204,7 +226,16 @@ public:
template<typename... Other>
static_variant( const static_variant<Other...>& cpy )
{
cpy.apply( impl::copy_construct<static_variant<Types...>>(*this) );
cpy.visit( impl::copy_construct<static_variant>(*this) );
}
static_variant( const static_variant& cpy )
{
cpy.visit( impl::copy_construct<static_variant>(*this) );
}
static_variant( static_variant&& mv )
{
mv.visit( impl::move_construct<static_variant>(*this) );
}
template<typename X>
@ -218,15 +249,33 @@ public:
~static_variant() {
impl::storage_ops<0, Types...>::del(_tag, storage);
}
template<typename X>
void operator=(const X& v) {
static_variant& operator=(const X& v) {
static_assert(
impl::position<X, Types...>::pos != -1,
"Type not in static_variant."
);
this->~static_variant();
init(v);
return *this;
}
static_variant& operator=( const static_variant& v )
{
if( this == &v ) return *this;
this->~static_variant();
v.visit( impl::copy_construct<static_variant>(*this) );
return *this;
}
static_variant& operator=( static_variant&& v )
{
if( this == &v ) return *this;
this->~static_variant();
v.visit( impl::move_construct<static_variant>(*this) );
return *this;
}
template<typename X>
X& get() {
static_assert(