diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2014-04-28 15:25:20 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2014-04-28 16:04:45 +0200 |
| commit | 42442fd5025b69c4b7800c71d6f568ae255e7b00 (patch) | |
| tree | 8e35f36410e9b9cb7d7e5367a62d24b5c5f181c1 /fusion_model.hpp | |
| parent | d834eba65c2c65c1540d13c9c39ced51bd87cf83 (diff) | |
| download | generic-gui-42442fd5025b69c4b7800c71d6f568ae255e7b00.tar.gz generic-gui-42442fd5025b69c4b7800c71d6f568ae255e7b00.tar.bz2 generic-gui-42442fd5025b69c4b7800c71d6f568ae255e7b00.zip | |
Adds support for a std::map<std::string, T> model.
Diffstat (limited to 'fusion_model.hpp')
| -rw-r--r-- | fusion_model.hpp | 71 |
1 files changed, 61 insertions, 10 deletions
diff --git a/fusion_model.hpp b/fusion_model.hpp index ace8f51..f40ac33 100644 --- a/fusion_model.hpp +++ b/fusion_model.hpp @@ -9,44 +9,95 @@ #include <vector> #include <map> +#include <iostream> template <typename T> struct fusion_model { - +private: + fusion_model(T); +}; + +struct FusionModelInterface { + virtual size_t row_count() const {throw std::runtime_error("\"row_count()\" not implemented for this model");} + virtual size_t column_count() const {throw std::runtime_error("\"column_count()\" not implemented for this model");} + virtual std::string field_name(size_t section) const {throw std::runtime_error("\"field_name(size_t)\" not implemented for this model");} + virtual std::string key(size_t section) const {throw std::runtime_error("\"key(size_t)\" not implemented for this model");} + virtual boost::any get_cell(size_t row, size_t column) const {throw std::runtime_error("\"get_cell(size_t, size_t)\" not implemented for this model");} }; template <typename T> -struct fusion_model<std::vector<T>> +struct fusion_model<std::vector<T>> : public FusionModelInterface { - static_assert(boost::fusion::traits::is_sequence<T>::type::value, "T is not a boost fusion sequence"); + static_assert(friendly_fusion::traits::is_sequence<T>::type::value, "T is not a boost fusion sequence"); typedef std::vector<T> data_type; + typedef T row_type; + + static constexpr bool has_header_h = true; + static constexpr bool has_header_v = false; std::vector<T> data; - size_t row_count() const + virtual size_t row_count() const override final { return data.size(); } - size_t column_count() const + virtual size_t column_count() const override final { return boost::fusion::result_of::size<T>::type::value; } - std::string horizontal_header_data(size_t section) const + virtual std::string field_name(size_t section) const override final { return get_nth_name<T>(section); } - boost::any get_cell(size_t row, size_t column) const + virtual boost::any get_cell(size_t row, size_t column) const override final { return get_nth(data[row], column); } }; -template <typename T, typename U> -struct fusion_model<std::map<T, U>> +template <typename T> +struct fusion_model<std::map<std::string, T>> : public FusionModelInterface { - std::map<T, U> data; + static_assert(boost::fusion::traits::is_sequence<T>::type::value, "T is not a boost fusion sequence"); + + typedef std::map<std::string, T> data_type; + typedef T row_type; + + static constexpr bool has_header_h = true; + static constexpr bool has_header_v = true; + + std::map<std::string, T> data; + + virtual size_t row_count() const override final + { + return data.size(); + } + + virtual size_t column_count() const override final + { + return boost::fusion::result_of::size<T>::type::value; + } + + virtual std::string field_name(size_t section) const override final + { + return get_nth_name<T>(section); + } + + virtual std::string key(size_t section) const override final + { + auto cit = data.cbegin(); + std::advance(cit, section); + return cit->first; + } + + virtual boost::any get_cell(size_t row, size_t column) const override final + { + auto cit = data.cbegin(); + std::advance(cit, row); + return get_nth(cit->second, column); + } };
\ No newline at end of file |
