Nintendo 3DS Miiverse Library  1.3.2
(OLV/Olive)
olv_Result.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*
2 Project: OLV
3 File: olv_Result.h
4 
5 Copyright (C) 2009-2013 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 #ifndef NN_OLV_OLV_RESULT_H_
15 #define NN_OLV_OLV_RESULT_H_
16 
17 /*! @file
18 @brief These are result declarations for the OLV library.
19 */
20 
21 #include <nn/Result.h>
22 
23 #ifdef __cplusplus
24 
25 namespace nn {
26 namespace olv {
27 
28 /// @cond
29 class Result : public nn::Result
30 {
31 private:
32  static const bit32 MASK_FAIL_BIT = 0x80000000u; // Most significant bit.
33 
34  static const s32 SIZE_DESCRIPTION = 20;
35  static const s32 SIZE_MODULE = 9;
36  static const s32 SIZE_LEVEL = 3;
37 
38  static const s32 SHIFTS_DESCRIPTION = 0;
39  static const s32 SHIFTS_MODULE = SHIFTS_DESCRIPTION + SIZE_DESCRIPTION;
40  static const s32 SHIFTS_LEVEL = SHIFTS_MODULE + SIZE_MODULE;
41  //NN_STATIC_ASSERT( SHIFTS_LEVEL + SIZE_LEVEL == 32 );
42 
43  static const u32 MASK_NEGATIVE_LEVEL = (~0u) << SIZE_LEVEL;
44 
45 #define NN_RESULT_H_MAKE_MASK(size, shift) (((~0u) >> (32 - (size))) << (shift))
46 #define NN_RESULT_H_MAKE_MASK_HELPER(c) \
47  static const u32 MASK_ ## c = NN_RESULT_H_MAKE_MASK(SIZE_ ## c, SHIFTS_ ## c)
48  NN_RESULT_H_MAKE_MASK_HELPER(DESCRIPTION);
49  NN_RESULT_H_MAKE_MASK_HELPER(MODULE);
50  NN_RESULT_H_MAKE_MASK_HELPER(LEVEL);
51 #undef NN_RESULT_H_MAKE_MASK_HELPER
52 #undef NN_RESULT_H_MAKE_MASK
53 
54 #define NN_RESULT_H_MAKE_MAX(size) ((~0u) >> (32 - (size)))
55 #define NN_RESULT_H_MAKE_MAX_HELPER(c) \
56  static const s32 MAX_ ## c = NN_RESULT_H_MAKE_MAX(SIZE_ ## c)
57  NN_RESULT_H_MAKE_MAX_HELPER(DESCRIPTION);
58  NN_RESULT_H_MAKE_MAX_HELPER(MODULE);
59  NN_RESULT_H_MAKE_MAX_HELPER(LEVEL);
60 #undef NN_RESULT_H_MAKE_MAX_HELPER
61 #undef NN_RESULT_H_MAKE_MAX
62 
63 public:
64  /*
65 * @brief Enumerated type indicating the severity of an error. (Do not use this type for error handling.)
66  */
67  enum LevelImpl
68  {
69  LEVEL_SUCCESS = 0,
70  LEVEL_FATAL = -1,
71  LEVEL_USAGE = -2,
72  LEVEL_STATUS = -3,
73 
74  LEVEL_END = -7
75  };
76  typedef int Level;
77 
78  /*
79 * @brief Enumerated type that indicates the details of errors. (Do not use this type for error handling.)
80  */
81  enum
82  {
83  DESCRIPTION_SUCCESS = 0, // Succeeded.
84  DESCRIPTION_INVALID_RESULT_VALUE = MAX_DESCRIPTION - 0 // These values are not used.
85  };
86 
87 private:
88 
89  template <nn::Result::Level TLevel, int TSummary, int TModule, int TDescription, int TDescriptionEx> friend struct ConstRangeBase;
90 
91 public:
92  Result()
93  : nn::Result(),
94  m_CodeEx( static_cast<u32>(
95  ((static_cast<u32>(MAX_LEVEL) << SHIFTS_LEVEL) & MASK_LEVEL) |
96  ((static_cast<u32>(MAX_MODULE) << SHIFTS_MODULE) & MASK_MODULE) |
97  ((static_cast<u32>(MAX_DESCRIPTION) << SHIFTS_DESCRIPTION) & MASK_DESCRIPTION) ))
98  {
99  }
100  Result(nn::Result::Level level, nn::Result::Summary summary, nn::Result::Module module, int description, int descriptionEx)
101  : nn::Result(level, summary, module, description),
102  m_CodeEx( static_cast<u32>(
103  ((static_cast<u32>(ConvertLevel(level)) << SHIFTS_LEVEL) & MASK_LEVEL) |
104  ((static_cast<u32>(module) << SHIFTS_MODULE) & MASK_MODULE) |
105  ((static_cast<u32>(descriptionEx) << SHIFTS_DESCRIPTION) & MASK_DESCRIPTION) ))
106  {
107  }
108 
109  /*
110 * @brief Returns a <tt>Description</tt> enumerated type indicating the details of the error.
111  *
112 * Enumerated type definitions will change in the future. Do not use them for any purpose other than checking the content of errors during development.
113  */
114  int GetDescription() const
115  {
116  return static_cast<int>( GetCodeBitsEx(MASK_DESCRIPTION, SHIFTS_DESCRIPTION) );
117  }
118 
119  /*!
120 * @brief Converts a <tt>Result</tt> code into a 32-bit array.
121  *
122 * Do not use them for any purpose other than checking the content of errors during development.
123  */
124  u32 GetPrintableBits() const { return m_CodeEx; }
125 
126  /*
127 * @brief Checks for equality.
128  *
129 * For information about handling errors, see the OLV library reference.
130  *
131 * @param[in] rhs Specifies the right-hand operand.
132 * @return Returns <tt>true</tt> if the operands are equal, and <tt>false</tt> otherwise.
133  */
134  bool operator ==(const Result& rhs) const { return this->m_CodeEx == rhs.m_CodeEx; }
135 
136  /*
137 * @brief Checks for inequality.
138  *
139 * For information about handling errors, see the OLV library reference.
140  *
141 * @param[in] rhs Specifies the right-hand operand.
142 * @return Returns <tt>true</tt> if the operands are not equal, and <tt>false</tt> otherwise.
143  */
144  bool operator !=(const Result& rhs) const { return this->m_CodeEx != rhs.m_CodeEx; }
145 
146  template <nn::Result::Level TLevel, nn::Result::Summary TSummary, Result::Module TModule, int TDescription, int TDescriptionEx> struct ConstRangeBase;
147  template <nn::Result::Level TLevel, nn::Result::Summary TSummary, nn::Result::Module TModule, int TDescription, int TDescriptionMin, int TDescriptionMax, int TDescriptionEx, int TDescriptionExStart, int TDescriptionExEnd> struct ConstRange;
148 
149 private:
150  nn::olv::Result::Level ConvertLevel(nn::Result::Level level) const
151  {
152  switch (level)
153  {
154  case nn::Result::LEVEL_SUCCESS:
155  return nn::olv::Result::LEVEL_SUCCESS;
156  case nn::Result::LEVEL_FATAL:
157  return nn::olv::Result::LEVEL_FATAL;
158  case nn::Result::LEVEL_USAGE:
159  return nn::olv::Result::LEVEL_USAGE;
160  default:
161  return nn::olv::Result::LEVEL_STATUS;
162  }
163  }
164 
165  u32 GetCodeBitsEx(u32 mask, s32 shift) const
166  {
167  return ((m_CodeEx & mask) >> shift);
168  }
169 
170 private:
171  u32 m_CodeEx;
172 };
173 
174 struct ResultSuccess
175 {
176  operator Result() const { return Result((nn::Result::Level)0, (nn::Result::Summary)0, (nn::Result::Module)0, 0, 0); }
177 };
178 
179 template <nn::Result::Level TLevel, nn::Result::Summary TSummary, nn::Result::Module TModule, int TDescription, int TDescriptionEx>
180 struct Result::ConstRangeBase
181 {
182  static const nn::Result::Level Level = TLevel;
183  static const nn::Result::Summary Summary = TSummary;
184  static const nn::Result::Module Module = TModule;
185  static const int Description = TDescription;
186  static const int DescriptionEx = TDescriptionEx;
187 
188  operator Result() const { return Result(Level, Summary, Module, Description, DescriptionEx); }
189 };
190 
191 template <nn::Result::Level TLevel, nn::Result::Summary TSummary, nn::Result::Module TModule, int TDescription, int TDescriptionMin, int TDescriptionMax, int TDescriptionEx, int TDescriptionExStart, int TDescriptionExEnd>
192 struct Result::ConstRange : public Result::ConstRangeBase<TLevel, TSummary, TModule, TDescription, TDescriptionEx>
193 {
194  static const int DescriptionMin = TDescriptionMin;
195  static const int DescriptionMax = TDescriptionMax;
196  static const int DescriptionExStart = TDescriptionExStart;
197  static const int DescriptionExEnd = TDescriptionExEnd;
198 
199  static bool Includes(nn::Result result)
200  {
201  return result.GetModule() == TModule && TDescriptionMin <= result.GetDescription() && result.GetDescription() < TDescriptionMax;
202  }
203  static bool IncludesEx(Result result)
204  {
205  return result.GetModule() == TModule && (TDescriptionExStart <= result.GetDescription() && result.GetDescription() < TDescriptionExEnd);
206  }
207 
208  friend bool operator<=(nn::Result lhs, const ConstRange&) { return ConstRange::Includes(lhs); }
209  friend bool operator>=(const ConstRange&, nn::Result rhs) { return ConstRange::Includes(rhs); }
210  friend bool operator<=(Result lhs, const ConstRange&) { return ConstRange::IncludesEx(lhs); }
211  friend bool operator>=(const ConstRange&, Result rhs) { return ConstRange::IncludesEx(rhs); }
212 };
213 
214 #define NN_OLV_DEFINE_RESULT_CONST_RANGE(name, level, summary, module, errorCode, errorCodeMin, errorCodeMax) \
215  typedef ::nn::olv::Result::ConstRange<(level), (summary), (module), (errorCode / 100), (errorCodeMin / 100), (errorCodeMax / 100), (errorCode * 128), (errorCodeMin * 128), (errorCodeMax * 128)> name
216 /// @endcond
217 
218 /** @addtogroup nn_olv_result Return Values
219  * @{
220  */
221 
222 /*!
223 @class nn::olv::ResultFatalError
224 @brief Fatal error.
225 */
226 NN_OLV_DEFINE_RESULT_CONST_RANGE(
227  ResultFatalError,
228  nn::Result::LEVEL_FATAL, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 100, 100, 200
229 );
230 
231 /*!
232 @class nn::olv::ResultUsageError
233 @brief Usage error.
234 */
235 NN_OLV_DEFINE_RESULT_CONST_RANGE(
236  ResultUsageError,
237  nn::Result::LEVEL_USAGE, nn::Result::SUMMARY_INVALID_ARGUMENT, nn::Result::MODULE_NN_OLV, 200, 200, 300
238 );
239 
240 /*!
241 @class nn::olv::ResultCanceled
242 @brief Canceled.
243 */
244 NN_OLV_DEFINE_RESULT_CONST_RANGE(
245  ResultCanceled,
246  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_CANCELLED, nn::Result::MODULE_NN_OLV, 300, 300, 400
247 );
248 
249 /*!
250 @class nn::olv::ResultLibraryError
251 @brief Library error.
252 */
253 NN_OLV_DEFINE_RESULT_CONST_RANGE(
254  ResultLibraryError,
255  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 1000, 1000, 2000
256 );
257 
258 /*!
259 @class nn::olv::ResultLibraryPermanentError
260 @brief Permanent library error.
261 */
262 NN_OLV_DEFINE_RESULT_CONST_RANGE(
263  ResultLibraryPermanentError,
264  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 1000, 1000, 1100
265 );
266 
267 /*!
268 @class nn::olv::ResultLibraryTemporaryError
269 @brief Temporary library error.
270 */
271 NN_OLV_DEFINE_RESULT_CONST_RANGE(
272  ResultLibraryTemporaryError,
273  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 1100, 1100, 2000
274 );
275 
276 /*!
277 @class nn::olv::ResultNetworkError
278 @brief Network error.
279 */
280 NN_OLV_DEFINE_RESULT_CONST_RANGE(
281  ResultNetworkError,
282  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 2000, 2000, 5000
283 );
284 
285 /*!
286 @class nn::olv::ResultNetworkDataError
287 @brief Network data error.
288 */
289 NN_OLV_DEFINE_RESULT_CONST_RANGE(
290  ResultNetworkDataError,
291  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_WRONG_ARGUMENT, nn::Result::MODULE_NN_OLV, 2000, 2000, 3000
292 );
293 
294 /*!
295 @class nn::olv::ResultNetworkLibraryError
296 @brief Network library error.
297 */
298 NN_OLV_DEFINE_RESULT_CONST_RANGE(
299  ResultNetworkLibraryError,
300  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 3000, 3000, 4000
301 );
302 
303 /*!
304 @class nn::olv::ResultNetworkHttpError
305 @brief HTTP error.
306 */
307 NN_OLV_DEFINE_RESULT_CONST_RANGE(
308  ResultNetworkHttpError,
309  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 4000, 4000, 5000
310 );
311 
312 /*!
313 @class nn::olv::ResultServerError
314 @brief Server error.
315 */
316 NN_OLV_DEFINE_RESULT_CONST_RANGE(
317  ResultServerError,
318  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 5000, 5000, 6000
319 );
320 
321 /*!
322 @class nn::olv::ResultServerPermanentError
323 @brief Permanent server error.
324 */
325 NN_OLV_DEFINE_RESULT_CONST_RANGE(
326  ResultServerPermanentError,
327  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 5000, 5000, 5100
328 );
329 
330 /*!
331 @class nn::olv::ResultServerTemporaryError
332 @brief Temporary server error.
333 */
334 NN_OLV_DEFINE_RESULT_CONST_RANGE(
335  ResultServerTemporaryError,
336  nn::Result::LEVEL_STATUS, nn::Result::SUMMARY_INVALID_STATE, nn::Result::MODULE_NN_OLV, 5100, 5100, 6000
337 );
338 
339 /** @} */
340 
341 } // namespace olv
342 } // namespace nn
343 
344 
345 #endif // __cplusplus
346 
347 #endif // NN_OLV_OLV_RESULT_H_