-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from narizhny/Casts
Add variant_static_cast, variant_dynamic_cast
- Loading branch information
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#ifndef VARIANT_CAST_HPP | ||
#define VARIANT_CAST_HPP | ||
|
||
#include <type_traits> | ||
|
||
namespace mapbox { | ||
namespace util { | ||
|
||
namespace detail { | ||
|
||
template <class T> | ||
class static_caster | ||
{ | ||
public: | ||
template <class V> | ||
T& operator()(V& v) const | ||
{ | ||
return static_cast<T&>(v); | ||
} | ||
}; | ||
|
||
template <class T> | ||
class dynamic_caster | ||
{ | ||
public: | ||
using result_type = T&; | ||
template <class V> | ||
T& operator()(V& v, typename std::enable_if<!std::is_polymorphic<V>::value>::type* = nullptr) const | ||
{ | ||
throw std::bad_cast(); | ||
} | ||
template <class V> | ||
T& operator()(V& v, typename std::enable_if<std::is_polymorphic<V>::value>::type* = nullptr) const | ||
{ | ||
return dynamic_cast<T&>(v); | ||
} | ||
}; | ||
|
||
template <class T> | ||
class dynamic_caster<T*> | ||
{ | ||
public: | ||
using result_type = T*; | ||
template <class V> | ||
T* operator()(V& v, typename std::enable_if<!std::is_polymorphic<V>::value>::type* = nullptr) const | ||
{ | ||
return nullptr; | ||
} | ||
template <class V> | ||
T* operator()(V& v, typename std::enable_if<std::is_polymorphic<V>::value>::type* = nullptr) const | ||
{ | ||
return dynamic_cast<T*>(&v); | ||
} | ||
}; | ||
} | ||
|
||
template <class T, class V> | ||
typename detail::dynamic_caster<T>::result_type | ||
dynamic_variant_cast(V& v) | ||
{ | ||
return mapbox::util::apply_visitor(detail::dynamic_caster<T>(), v); | ||
} | ||
|
||
template <class T, class V> | ||
typename detail::dynamic_caster<const T>::result_type | ||
dynamic_variant_cast(const V& v) | ||
{ | ||
return mapbox::util::apply_visitor(detail::dynamic_caster<const T>(), v); | ||
} | ||
|
||
template <class T, class V> | ||
T& static_variant_cast(V& v) | ||
{ | ||
return mapbox::util::apply_visitor(detail::static_caster<T>(), v); | ||
} | ||
|
||
template <class T, class V> | ||
const T& static_variant_cast(const V& v) | ||
{ | ||
return mapbox::util::apply_visitor(detail::static_caster<const T>(), v); | ||
} | ||
} | ||
} | ||
|
||
#endif // VARIANT_CAST_HPP |