CTR Pia  4.11.3
Game Communication Engine
clone_ReverseSerializePolicy.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: clone_ReverseSerializePolicy.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 #include <pia/clone/clone_definitions.h>
18 #include <pia/common/common_ByteOrder.h>
19 
20 
21 namespace nn
22 {
23 namespace pia
24 {
25 namespace clone
26 {
27 
28 
29 /*!
30 @brief Reverses the byte order of the source data and then serializes it.
31 
32 @tparam Type_ Specifies the type of the data to be serialized.
33 
34 @details This class is only implemented for the following types: <tt>u8</tt>, <tt>s8</tt>, <tt>u16</tt>, <tt>s16</tt>, <tt>u32</tt>, <tt>s32</tt>, <tt>u64</tt>, <tt>s64</tt>, <tt>f32</tt>, and <tt>f64</tt>.
35 To use this class with a type, you must implement a <tt>Reverse</tt> function corresponding to that type.
36 
37 For example:
38 @code
39 struct Val
40  {
41 u32 a;
42 f32 b;
43  };
44 @endcode
45 You would implement this type as follows.
46 @code
47 template<> void ReverseSerializePolicy<Val>::Reverse(Val* pTo, const Val* cpFrom)
48  {
49 ReverseValue(&pTo->a, &cpFrom->a);
50 ReverseValue(&pTo->b, &cpFrom->b);
51  }
52 @endcode
53 
54 
55 @date 2013-07-18 Initial version.
56 */
57 template <typename Type_>
59 {
60 public:
61 /*!
62 @brief Defines the type of the data to be serialized.
63 */
64  typedef Type_ Type;
65 
66 
67 /*!
68 @cond PRIVATE
69 @brief Serializes data.
70 @param[out] pBuffer Specifies the output buffer.
71 @param[in] value Specifies the data to serialize.
72 */
73  static void Serialize(void* pBuffer, const Type& value)
74  {
75  Reverse(reinterpret_cast<Type*>(pBuffer), &value);
76  }
77  //! @endcond
78 
79 
80 /*!
81 @cond PRIVATE
82 @brief Deserializes data.
83 @param[out] pValue Specifies the place to save the deserialized data.
84 @param[in] cpData Specifies the byte array to deserialize.
85 */
86  static void Deserialize(Type* pValue, const void* cpData)
87  {
88  Reverse(pValue, reinterpret_cast<const Type*>(cpData));
89  }
90  //! @endcond
91 
92 
93 /*!
94 @cond PRIVATE
95 @brief Gets the size of the serialized data.
96 @details Cannot use a value larger than <tt>@ref nn::pia::clone::CloneProtocol::GetElementSizeMax() "CloneProtocol::GetElementSizeMax"</tt>.
97 @return Returns the size of the serialized data.
98 */
99  static size_t GetSize()
100  {
101  return sizeof(Type);
102  }
103  //! @endcond
104 
105 
106 /*!
107 @cond PRIVATE
108 @brief Returns the size of the serialized data.
109 @details This value, and not the value of <tt>@ref GetSize</tt>, is used when using <tt>@ref nn::pia::clone::ReliableLargeCloneElement</tt>.
110 */
111  static const size_t SIZE = sizeof(Type);
112  //! @endcond
113 
114 private:
115 /*!
116 @brief Reverses the byte order.
117 
118 @details This function is only implemented for the following types: <tt>u8</tt>, <tt>s8</tt>, <tt>u16</tt>, <tt>s16</tt>, <tt>u32</tt>, <tt>s32</tt>, <tt>u64</tt>, <tt>s64</tt>, <tt>f32</tt>, and <tt>f64</tt>.
119 To use this class with a type, you must implement a <tt>Reverse</tt> function corresponding to that type.
120 @attention In some cases, the alignment for <span class="argument">pTo</span> or <span class="argument">cpFrom</span> is invalid.
121 
122 @param[out] pTo Specifies the place to output the reversed data.
123 @param[in] cpFrom Specifies the source data.
124 */
125  static void Reverse(Type* pTo, const Type* cpFrom);
126 
127 
128 /*!
129 @brief Reverses the byte order of a basic type value.
130 
131 @tparam T Specifies the type of value to reverse.
132 
133 @param[out] pTo Specifies the place to output the reversed value.
134 @param[in] cpFrom Specifies the source data.
135 */
136  template <typename T>
137  static void ReverseValue(T* pTo, const T* cpFrom)
138  {
139  common::ByteOrder::Swap<sizeof(T)>(pTo, cpFrom);
140  }
141 };
142 
143 //! @cond
144 
145 template <>
146 inline void ReverseSerializePolicy<u8>::Reverse(Type* pTo, const Type* pFrom)
147 {
148  ReverseValue(pTo, pFrom);
149 }
150 template <>
151 inline void ReverseSerializePolicy<s8>::Reverse(Type* pTo, const Type* pFrom)
152 {
153  ReverseValue(pTo, pFrom);
154 }
155 template <>
156 inline void ReverseSerializePolicy<u16>::Reverse(Type* pTo, const Type* pFrom)
157 {
158  ReverseValue(pTo, pFrom);
159 }
160 template <>
161 inline void ReverseSerializePolicy<s16>::Reverse(Type* pTo, const Type* pFrom)
162 {
163  ReverseValue(pTo, pFrom);
164 }
165 template <>
166 inline void ReverseSerializePolicy<u32>::Reverse(Type* pTo, const Type* pFrom)
167 {
168  ReverseValue(pTo, pFrom);
169 }
170 template <>
171 inline void ReverseSerializePolicy<s32>::Reverse(Type* pTo, const Type* pFrom)
172 {
173  ReverseValue(pTo, pFrom);
174 }
175 template <>
176 inline void ReverseSerializePolicy<u64>::Reverse(Type* pTo, const Type* pFrom)
177 {
178  ReverseValue(pTo, pFrom);
179 }
180 template <>
181 inline void ReverseSerializePolicy<s64>::Reverse(Type* pTo, const Type* pFrom)
182 {
183  ReverseValue(pTo, pFrom);
184 }
185 template <>
186 inline void ReverseSerializePolicy<f32>::Reverse(Type* pTo, const Type* pFrom)
187 {
188  ReverseValue(pTo, pFrom);
189 }
190 template <>
191 inline void ReverseSerializePolicy<f64>::Reverse(Type* pTo, const Type* pFrom)
192 {
193  ReverseValue(pTo, pFrom);
194 }
195 
196 //! @endcond
197 }
198 }
199 } // end of namespace nn::pia::clone
Definition: assert.h:115
Type_ Type
Defines the type of the data to be serialized.
Definition: clone_ReverseSerializePolicy.h:64
Reverses the byte order of the source data and then serializes it.
Definition: clone_ReverseSerializePolicy.h:58