github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/util/ntobject.h (about) 1 /*********************************************** 2 The MIT License (MIT) 3 4 Copyright (c) 2012 Athrun Arthur <athrunarthur@gmail.com> 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 *************************************************/ 24 #pragma once 25 #include "ff/util/internal/user_new_type.h" 26 #include "ff/util/preprocessor.h" 27 #include "ff/util/tuple_type.h" 28 #include "ff/util/type_list.h" 29 #include <memory> 30 31 namespace ff { 32 namespace util { 33 34 /* 35 * ntobject is used to define a struct without writng your own class. 36 * To use it, you need to declare a bunch of types first, like this 37 * 38 * define_nt(email, std::string, "email"); 39 * define_nt(uid, uint64_t, "uid"); 40 * define_nt(_name, std::string, "name"); 41 * 42 * typedef ntobject<email, uid, name> myobject_t; 43 * 44 * Now you have your own structure -- myobject_t, you can set and get field in 45 * it like 46 * 47 * myobject_t obj; 48 * obj.set<_name>("xuepeng"); 49 * obj.set<email>("xp@example.com"); 50 * obj.set<uid>(128); 51 * 52 * std::string val_name = obj.get<name>(); 53 * 54 * Further more, you can set multiple values with any order if you want, 55 * 56 * obj.set<name, email>("xuepeng", "xp@example.com"); 57 * obj.set<email, uid, name> ("xp@example.com", 128, "xuepeng"); 58 * 59 * 60 * */ 61 template <typename... ARGS> class ntobject { 62 public: 63 typedef typename util::type_list<ARGS...> type_list; 64 65 ntobject() : m_content(new content_type()) {} 66 67 ntobject<ARGS...> make_copy() const { 68 ntobject<ARGS...> rt; 69 *rt.m_content = *m_content; 70 return rt; 71 } 72 73 template <typename CT> 74 void set(const typename internal::nt_traits<CT>::type &val) { 75 static_assert(is_type_in_type_list<CT, util::type_list<ARGS...>>::value, 76 "Cannot set a value that's not in the ntobject/row!"); 77 const static int index = 78 get_index_of_type_in_typelist<CT, util::type_list<ARGS...>>::value; 79 std::get<index>(*m_content) = val; 80 } 81 82 template <typename CT, typename CT1, typename... CARGS, typename... PARGS> 83 void set(const typename internal::nt_traits<CT>::type &val, 84 const typename internal::nt_traits<CT1>::type &val1, 85 PARGS... params) { 86 static_assert(is_type_in_type_list<CT, util::type_list<ARGS...>>::value, 87 "Cannot set a value that's not in the row!"); 88 static_assert(is_type_in_type_list<CT1, util::type_list<ARGS...>>::value, 89 "Cannot set a value that's not in the row!"); 90 const static int index = 91 get_index_of_type_in_typelist<CT, util::type_list<ARGS...>>::value; 92 std::get<index>(*m_content) = val; 93 94 set<CT1, CARGS...>(val1, params...); 95 } 96 97 template <typename CT> typename internal::nt_traits<CT>::type get() const { 98 static_assert(is_type_in_type_list<CT, util::type_list<ARGS...>>::value, 99 "Cannot get a value that's not in the ntobject/row!"); 100 const static int index = 101 get_index_of_type_in_typelist<CT, util::type_list<ARGS...>>::value; 102 return std::get<index>(*m_content); 103 } 104 105 protected: 106 typedef 107 typename convert_type_list_to_tuple<typename nt_extract_content_type_list< 108 util::type_list<ARGS...>>::type>::type content_type; 109 std::unique_ptr<content_type> m_content; 110 }; 111 112 template <typename... ARGS> class ntarray { 113 public: 114 typedef ntobject<ARGS...> row_type; 115 116 void push_back(row_type &&row) { m_collection.push_back(std::move(row)); } 117 118 void clear() { m_collection.clear(); } 119 120 size_t size() const { return m_collection.size(); } 121 122 bool empty() const { return m_collection.empty(); } 123 124 row_type &operator[](size_t index) { return m_collection[index]; } 125 126 const row_type &operator[](size_t index) const { return m_collection[index]; } 127 128 protected: 129 std::vector<row_type> m_collection; 130 }; 131 132 template <typename T> struct is_ntobject { const static bool value = false; }; 133 134 template <typename... ARGS> struct is_ntobject<ntobject<ARGS...>> { 135 const static bool value = true; 136 }; 137 138 template <typename T> struct is_ntarray { const static bool value = false; }; 139 140 template <typename... ARGS> struct is_ntarray<ntarray<ARGS...>> { 141 const static bool value = true; 142 }; 143 144 } // namespace util 145 } // namespace ff 146