From a39339bd51fd0c5f2b3b7916030cba03f332bb8f Mon Sep 17 00:00:00 2001 From: Carson Lin <396098651@qq.com> Date: Tue, 26 Jul 2022 21:02:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0cpp=E7=9A=84=E8=81=94?= =?UTF-8?q?=E5=90=88=E5=A4=9A=E4=B8=BB=E9=94=AE=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Carson Lin <396098651@qq.com> --- .../Templates/config/cpp_bin/table.tpl | 144 ++++++++++++++---- 1 file changed, 115 insertions(+), 29 deletions(-) diff --git a/src/Luban.Server/Templates/config/cpp_bin/table.tpl b/src/Luban.Server/Templates/config/cpp_bin/table.tpl index 1d62615..0a109b4 100644 --- a/src/Luban.Server/Templates/config/cpp_bin/table.tpl +++ b/src/Luban.Server/Templates/config/cpp_bin/table.tpl @@ -13,6 +13,39 @@ * {{x.escape_comment}} */ {{~end~}} + +{{~if x.is_list_table ~}} +{{~if x.is_union_index~}} +typedef struct t_{{name}}_MultiKey +{ +{{~for idx in x.index_list~}} + {{cpp_define_type idx.type}} {{idx.index_field.name}}; +{{~end~}} + bool operator==(const t_{{name}}_MultiKey& stOther) const + { + bool bEqual = true; + {{~for idx in x.index_list~}} + bEqual = bEqual && (stOther.{{idx.index_field.name}} == {{idx.index_field.name}}); + {{~end~}} + return bEqual; + } +}t_{{name}}_MultiKey; + +typedef struct t_{{name}}_MultiKey_HashFunc +{ + std::size_t operator()(const t_{{name}}_MultiKey& stMK) const + { + std::size_t sHash = 0; + {{~for idx in x.index_list~}} + sHash ^= std::hash<{{cpp_define_type idx.type}}>()(stMK.{{idx.index_field.name}}); + {{~end~}} + return sHash; + } +}t_{{name}}_MultiKey_HashFunc; +{{~end~}} +{{~end~}} + + class {{name}} { {{~if x.is_map_table ~}} @@ -60,41 +93,94 @@ class {{name}} {{~else if x.is_list_table~}} private: - ::bright::Vector<{{cpp_define_type value_type}}> _dataList; + ::bright::Vector<{{cpp_define_type value_type}}> _dataList; + {{~if x.is_union_index~}} + ::bright::HashMapMultiKey _dataMap; + {{~else if !x.index_list.empty?~}} + {{~for idx in x.index_list~}} + ::bright::HashMap<{{cpp_define_type idx.type}}, {{cpp_define_type value_type}}> _dataMap_{{idx.index_field.name}}; + {{~end~}} + {{~else~}} + {{~end~}} - public: - bool load(ByteBuf& _buf) - { - int n; - if (!_buf.readSize(n)) return false; - for(; n > 0 ; --n) - { - {{cpp_define_type value_type}} _v; - {{cpp_deserialize '_buf' '_v' value_type}} - _dataList.push_back(_v); + public: + bool load(ByteBuf& _buf) + { + int n; + if (!_buf.readSize(n)) return false; + for(; n > 0 ; --n) + { + {{cpp_define_type value_type}} _v; + {{cpp_deserialize '_buf' '_v' value_type}} + _dataList.push_back(_v); + {{~if x.is_union_index~}} + t_{{name}}_MultiKey stKey; + {{~for idx in x.index_list~}} + stKey.{{idx.index_field.name}} = _v->{{idx.index_field.name}}; + {{~end~}} + _dataMap[stKey] = _v; + {{~else if !x.index_list.empty?~}} + {{~for idx in x.index_list~}} + _dataMap_{{idx.index_field.name}}[_v->{{idx.index_field.name}}] = _v; + {{~end~}} + {{~else~}} + {{~end~}} + } + return true; } - return true; - } - const ::bright::Vector<{{cpp_define_type value_type}}>& getDataList() const { return _dataList; } + const ::bright::Vector<{{cpp_define_type value_type}}>& getDataList() const { return _dataList; } - {{value_type.bean.cpp_full_name}}* getRaw(size_t index) const - { - return _dataList[index].get(); - } - - {{cpp_define_type value_type}} get(size_t index) const - { - return _dataList[index]; - } - - void resolve(::bright::HashMap<::bright::String, void*>& _tables) - { - for(auto v : _dataList) + {{~if x.is_union_index~}} + ::bright::HashMapMultiKey& getDataMap() { - v->resolve(_tables); + return _dataMap; } - } + {{value_type.bean.cpp_full_name}}* getRaw(t_{{name}}_MultiKey& key) + { + auto it = _dataMap.find(key); + return it != _dataMap.end() ? it->second.get() : nullptr; + } + {{cpp_define_type value_type}} get(t_{{name}}_MultiKey& key) + { + auto it = _dataMap.find(key); + return it != _dataMap.end() ? it->second : nullptr; + } + {{~else if !x.index_list.empty?~}} + {{~for idx in x.index_list~}} + ::bright::HashMap<{{cpp_define_type idx.type}}, {{cpp_define_type value_type}}>& getDataMapBy{{idx.index_field.name}}() + { + return _dataMap_{{idx.index_field.name}}; + } + {{value_type.bean.cpp_full_name}}* getRawBy{{idx.index_field.name}}({{cpp_define_type idx.type}} key) + { + auto it = _dataMap_{{idx.index_field.name}}.find(key); + return it != _dataMap_{{idx.index_field.name}}.end() ? it->second.get() : nullptr; + } + {{cpp_define_type value_type}} getBy{{idx.index_field.name}}({{cpp_define_type idx.type}} key) + { + auto it = _dataMap_{{idx.index_field.name}}.find(key); + return it != _dataMap_{{idx.index_field.name}}.end() ? it->second : nullptr; + } + {{~end~}} + {{~else~}} + {{value_type.bean.cpp_full_name}}* getRaw(size_t index) const + { + return _dataList[index].get(); + } + + {{cpp_define_type value_type}} get(size_t index) const + { + return _dataList[index]; + } + {{~end~}} + void resolve(::bright::HashMap<::bright::String, void*>& _tables) + { + for(auto v : _dataList) + { + v->resolve(_tables); + } + } {{~else~}} private: {{cpp_define_type value_type}} _data;