github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/net/common/archive_data.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  
    25  //! This file is to implement archive for different types.
    26  
    27  template <class T>
    28  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
    29  archive(T &data) {
    30    switch (m_iAT) {
    31    case seralizer:
    32      std::memcpy(m_pWriteBuf + m_iBase, (const char *)&data, sizeof(T));
    33      m_iBase += sizeof(T);
    34      break;
    35    case deseralizer:
    36      std::memcpy((char *)&data, m_pReadBuf + m_iBase, sizeof(T));
    37      m_iBase += sizeof(T);
    38      break;
    39    case length_retriver:
    40      m_iBase += sizeof(T);
    41      break;
    42    }
    43  }
    44  
    45  void archive(std::string &s);
    46  
    47  template <class T, size_t N>
    48  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
    49  archive(T (&data)[N]) {
    50    switch (m_iAT) {
    51    case seralizer:
    52      std::memcpy(m_pWriteBuf + m_iBase, (const char *)&data, sizeof(T) * N);
    53      m_iBase += sizeof(T) * N;
    54      break;
    55    case deseralizer:
    56      std::memcpy((char *)&data, m_pReadBuf + m_iBase, sizeof(T) * N);
    57      m_iBase += sizeof(T) * N;
    58      break;
    59    case length_retriver:
    60      m_iBase += sizeof(T) * N;
    61      break;
    62    }
    63  }
    64  
    65  template <class T, size_t N>
    66  typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
    67  archive(T (&data)[N]) {
    68    for (size_t i = 0; i < N; i++) {
    69      archive(data[i]);
    70    }
    71  }
    72  
    73  template <class T>
    74  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
    75  archive(T *&data, size_t count) {
    76    switch (m_iAT) {
    77    case seralizer:
    78      std::memcpy(m_pWriteBuf + m_iBase, (const char *)data, sizeof(T) * count);
    79      m_iBase += sizeof(T) * count;
    80      break;
    81    case deseralizer:
    82      std::memcpy((char *)data, m_pReadBuf + m_iBase, sizeof(T) * count);
    83      m_iBase += sizeof(T) * count;
    84      break;
    85    case length_retriver:
    86      m_iBase += sizeof(T) * count;
    87      break;
    88    }
    89  }
    90  
    91  template <class T>
    92  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
    93  archive(std::vector<T> &data) {
    94    size_t count = data.size();
    95    archive(count);
    96    switch (m_iAT) {
    97    case seralizer:
    98      std::memcpy(m_pWriteBuf + m_iBase, (const char *)&data.front(),
    99                  sizeof(T) * count);
   100      m_iBase += count * sizeof(T);
   101      break;
   102    case deseralizer:
   103      data.clear();
   104      for (int i = 0; i < count; ++i) {
   105        T d;
   106        archive(d);
   107        data.push_back(d);
   108      }
   109      break;
   110    case length_retriver:
   111      m_iBase += sizeof(T) * count;
   112      break;
   113    }
   114  }
   115  
   116  template <class T>
   117  typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
   118  archive(std::vector<T> &data) {
   119    size_t count = data.size();
   120    archive(count);
   121    switch (m_iAT) {
   122    case seralizer:
   123      for (size_t i = 0; i < data.size(); ++i) {
   124        archive(data[i]);
   125      }
   126      break;
   127    case deseralizer:
   128      data.clear();
   129      for (int i = 0; i < count; ++i) {
   130        T d;
   131        archive(d);
   132        data.push_back(d);
   133      }
   134      break;
   135    case length_retriver:
   136      for (size_t i = 0; i < data.size(); ++i) {
   137        archive(data[i]);
   138      }
   139      break;
   140    }
   141  }
   142