diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-03 16:14:48 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-03 22:38:33 +0200 |
| commit | 13cfd05f9b2bb3c8d7d1bf47fcef037a745bacf8 (patch) | |
| tree | 98a325d0e547d5da0b0fd4b0b9ce2054306e87d0 /boost_any_qvariant_convert.hpp | |
| parent | 5bf919190e90b017ff00ab074bce68e90958fa3c (diff) | |
| download | generic-gui-13cfd05f9b2bb3c8d7d1bf47fcef037a745bacf8.tar.gz generic-gui-13cfd05f9b2bb3c8d7d1bf47fcef037a745bacf8.tar.bz2 generic-gui-13cfd05f9b2bb3c8d7d1bf47fcef037a745bacf8.zip | |
Adds editing functionality to the models.
no longer able to store QStrings in the model though, But when creating generic guis you should not use any Gui related types anyway.
Diffstat (limited to 'boost_any_qvariant_convert.hpp')
| -rw-r--r-- | boost_any_qvariant_convert.hpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/boost_any_qvariant_convert.hpp b/boost_any_qvariant_convert.hpp new file mode 100644 index 0000000..859623b --- /dev/null +++ b/boost_any_qvariant_convert.hpp @@ -0,0 +1,121 @@ +#pragma once + +#include "friendly_fusion.hpp" +#include "index_list.hpp" + +#include <QVariant> + +#include <boost/any.hpp> + +#include <string> + +template<typename value_type> +QVariant to_qvariant(boost::any const& x) +{ + return QVariant::fromValue<value_type>(boost::any_cast<value_type>(x)); +} + +template <typename T, int n> +QVariant to_qvariant(boost::any const& x) +{ + typedef friendly_fusion::result_of::begin<T> begin; + typedef friendly_fusion::result_of::advance_c<typename begin::type, n> adv_it; + typedef friendly_fusion::result_of::deref<typename adv_it::type> deref; + typedef typename std::decay<typename deref::type>::type value_type; + return to_qvariant<value_type>(x); +} + +#if defined( __GNUC__ ) && !defined( __clang__ ) +template <typename T, int index> +std::function<QVariant(boost::any const&)> to_qvariant_lambda() +{ + return [](boost::any const& any) + { + return convert<T, index>(any); + }; +} + +#endif //defined( __GNUC__ ) && !defined( __clang__ ) + +template<typename T, int... Indices> +QVariant to_qvariant(boost::any const& any, int index, indices<Indices...>) +{ + typedef std::function<QVariant(boost::any const&)> element_type; + static element_type table[] = + { +#if defined( __GNUC__ ) && !defined( __clang__ ) + //Workaround for gcc bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47226 + to_qvariant_lambda<T, Indices>() + ... +#else + [](boost::any const& any){return to_qvariant<T, Indices>(any);} + ... +#endif //defined( __GNUC__ ) && !defined( __clang__ ) + }; + + return table[index](any); +} + +template <typename T> +QVariant to_qvariant(boost::any const& x, int index) +{ + typedef typename friendly_fusion::result_of::size<T>::type seq_size; + typedef typename build_indices<seq_size::value>::type indices_type; + + return to_qvariant<T>(x, index, indices_type{}); +} + +template<typename value_type> +boost::any to_boost_any(QVariant const& x) +{ + return boost::any(x.value<value_type>()); +} + +template <typename T, int n> +boost::any to_boost_any(QVariant const& x) +{ + typedef friendly_fusion::result_of::begin<T> begin; + typedef friendly_fusion::result_of::advance_c<typename begin::type, n> adv_it; + typedef friendly_fusion::result_of::deref<typename adv_it::type> deref; + typedef typename std::decay<typename deref::type>::type value_type; + return to_boost_any<value_type>(x); +} + +#if defined( __GNUC__ ) && !defined( __clang__ ) +template <typename T, int index> +std::function<boost::any(QVariant const&)> to_qvariant_lambda() +{ + return [](QVariant const& value) + { + return to_boost_any<T, index>(value); + }; +} +#endif //defined( __GNUC__ ) && !defined( __clang__ ) + +template<typename T, int... Indices> +boost::any to_boost_any(QVariant const& value, int index, indices<Indices...>) +{ + typedef std::function<boost::any(QVariant const&)> element_type; + static element_type table[] = + { +#if defined( __GNUC__ ) && !defined( __clang__ ) + //Workaround for gcc bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47226 + to_boost_any_lambda<T, Indices>() + ... +#else + [](QVariant const& value){return to_boost_any<T, Indices>(value);} + ... +#endif //defined( __GNUC__ ) && !defined( __clang__ ) + }; + + return table[index](value); +} + +template <typename T> +boost::any to_boost_any(QVariant const& x, int index) +{ + typedef typename friendly_fusion::result_of::size<T>::type seq_size; + typedef typename build_indices<seq_size::value>::type indices_type; + + return to_boost_any<T>(x, index, indices_type{}); +}
\ No newline at end of file |
