Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace col_enum {
- template<class enum_t>
- inline auto operator+(enum_t e) noexcept
- {
- static_assert(std::is_enum_v<enum_t>, "Only for enums!!");
- return std::underlying_type<enum_t>(e);
- }
- template<class enum_t>
- inline std::string title(enum_t e); // Header title / string version of the enum
- template<class enum_t>
- inline std::enable_if_t<std::is_enum_v<enum_t>::value, std::ostream&>
- operator<<(std::ostream& os, enum_t e)
- { return os << title(e); }
- template<class enum_t>
- inline auto& operator++(enum_t& e) noexcept
- { return e = enum_t(+e + 1); }
- // "Table size"
- template<class enum_t>
- constexpr std::size_t size() noexcept;
- // We assume one is an enumerator and the other
- // is an integral type, or two enumerators of same type.
- // For simplicity, no further check is done (it should though).
- template<class a_t, class b_t>
- inline bool operator==(a_t const& a, b_t const& b)
- {
- using cmp_t = std::common_type_t<decltype(+a), decltype(+b)>;
- return static_cast<cmp_t>(a) == static_cast<cmp_t>(b);
- }
- template<class a_t, class b_t>
- inline bool operator!=(a_t a, b_t b) { return !(a == b); }
- // Any other "enumerator-member" or operator, if needed.
- }
- // User-code
- namespace col_enum {
- enum class table1_col { id, name };
- template<>
- constexpr std::size_t size<table1_col>() noexcept { return 2; }
- template<>
- inline std::string title<table1_col>(table1_col c)
- {
- switch(c) {
- case table1_col::id:
- return "ID";
- case table1_col::name:
- return "name";
- default:
- throw std::logic_error("invalid col");
- }
- }
- }
- namespace col_enum {
- // Other enumerator for other table,
- // with their corresponding methods.
- }
- class my_table1_model
- {
- public:
- using col_t = col_enum::table1_col;
- // ...
- void fun(int c)
- {
- if (c >= col_enum::size<col_t>())
- throw std::logic_error("Whaaat?");
- if (c == col_t::id)
- third_party_fun(this, +col_t::name,
- boost::lexical_cast<std::string>(col_t::name));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement