github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/net/common/archive.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 26 #include "ff/net/common/common.h" 27 28 namespace ff { 29 namespace net { 30 31 //! Note, we can not use a base class and sub-classes to handle this, because we 32 // have to use template method archive for different types. Remind that a 33 // template method can't be a virtual method. Also, we can't make marshaler as 34 // a template class, because the calling point of Archive as parameter is a 35 // virtual method. So we can't involve any template parameter for Archive. 36 // Finally, our only choice is to use arch_type and switch-case to gain 37 // polymorphism. 38 39 class marshaler { 40 public: 41 enum marshaler_type { seralizer, deseralizer, length_retriver }; 42 43 marshaler(const char *buf, size_t len, marshaler_type at); 44 45 marshaler(char *buf, size_t len, marshaler_type at); 46 47 marshaler(marshaler_type at); 48 49 #include "ff/net/common/archive_data.h" 50 51 size_t get_length() { return m_iBase; } 52 53 marshaler_type get_marshaler_type() { return m_iAT; } 54 55 bool is_serializer() const { return m_iAT == seralizer; } 56 57 bool is_deserializer() const { return m_iAT == deseralizer; } 58 59 bool is_lengther() const { return m_iAT == length_retriver; } 60 61 size_t &internal_m_iBase() { return m_iBase; } 62 63 char *internal_m_pWriteBuf() { return m_pWriteBuf; } 64 65 const char *internal_m_pReadBuf() { return m_pReadBuf; } 66 67 protected: 68 marshaler_type m_iAT; 69 size_t m_iBase; 70 char *m_pWriteBuf; 71 const char *m_pReadBuf; 72 size_t m_iBufLen; 73 }; // end class marshaler 74 75 template <class Ty_> 76 typename std::enable_if<std::is_arithmetic<Ty_>::value, size_t>::type 77 seralize(const Ty_ &val, char *pBuf) { 78 std::memcpy(pBuf, (const char *)&val, sizeof(Ty_)); 79 return sizeof(Ty_); 80 } 81 82 template <class Ty_> 83 typename std::enable_if<std::is_arithmetic<Ty_>::value, size_t>::type 84 deseralize(const char *pBuf, Ty_ &val) { 85 std::memcpy((char *)&val, pBuf, sizeof(Ty_)); 86 return sizeof(Ty_); 87 } 88 89 template <class Ty_> 90 typename std::enable_if<std::is_arithmetic<Ty_>::value, size_t>::type 91 length(const Ty_ &) { 92 return sizeof(Ty_); 93 } 94 } // namespace net 95 } // namespace ff 96