1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Result.h
4 
5   Copyright (C) 2009-2011 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   $Rev: 27772 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_UTIL_UTIL_RESULT_H_
17 #define NN_UTIL_UTIL_RESULT_H_
18 
19 #ifdef __cplusplus
20 
21 #include <nn/Result.h>
22 #include <nn/assert.h>
23 //#include <nn/Handle.h>
24 #include <nn/config.h>
25 #include <nn/dbg.h>
26 
27 #ifdef NN_SWITCH_PRINT_RESULT_IF_FAILED
28 #define NN_UTIL_RETURN_IF_FAILED_BASE(result, s1, s2, s3, s4, s5) \
29     do \
30     { \
31         ::nn::Result nn_util_return_if_failure_result = (result); \
32         if (nn_util_return_if_failure_result.IsFailure()) \
33         { \
34             s1; s2; s3; s4; s5; \
35             NN_LOG_FORCE("result failure:"); \
36             NN_DBG_PRINT_RESULT(nn_util_return_if_failure_result); \
37             return nn_util_return_if_failure_result; \
38         } \
39     } while(0)
40 #else
41 #define NN_UTIL_RETURN_IF_FAILED_BASE(result, s1, s2, s3, s4, s5) \
42     do \
43     { \
44         ::nn::Result nn_util_return_if_failure_result = (result); \
45         if (nn_util_return_if_failure_result.IsFailure()) \
46         { \
47             s1; s2; s3; s4; s5; \
48             return nn_util_return_if_failure_result; \
49         } \
50     } while(0)
51 #endif
52 
53 #define NN_UTIL_RETURN_IF_FAILED(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
54 
55 #define NN_UTIL_RETURN_IF_FAILED_0(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
56 #define NN_UTIL_RETURN_IF_FAILED_1(result, c1) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,,,,)
57 #define NN_UTIL_RETURN_IF_FAILED_2(result, c1, c2) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,,,)
58 #define NN_UTIL_RETURN_IF_FAILED_3(result, c1, c2, c3) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,,)
59 #define NN_UTIL_RETURN_IF_FAILED_4(result, c1, c2, c3, c4) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,c4,)
60 #define NN_UTIL_RETURN_IF_FAILED_5(result, c1, c2, c3, c4, c5) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,c4,c5)
61 
62 #define NN_UTIL_RETURN_IF_FAILED_EXCEPT(result, exceptResult) \
63     do \
64     { \
65         ::nn::Result nn_util_return_if_failure_result_except = (result); \
66         if (!(nn_util_return_if_failure_result_except <= (exceptResult))) \
67         { \
68             NN_UTIL_RETURN_IF_FAILED(nn_util_return_if_failure_result_except); \
69         } \
70     } while (0)
71 
72 #ifdef NN_SWITCH_DISABLE_DEBUG_PRINT
73     #define NN_UTIL_THROW_RESULT(result) return (result)
74 #else
75     #define NN_UTIL_THROW_RESULT(result) \
76     do \
77     { \
78         ::nn::Result nn_util_return_throw_result = (result); \
79         NN_TASSERT_(nn_util_return_throw_result.IsFailure()); \
80         NN_UTIL_RETURN_IF_FAILED_0(nn_util_return_throw_result); \
81         return nn_util_return_throw_result; \
82     } while(0)
83 #endif
84 
85 #define NN_UTIL_PANIC_IF_FAILED(result) NN_PANIC_IF_FAILED(result)
86 
87 #define NN_UTIL_PANIC_IF_FAILED_EXCEPT(result, exceptResult) \
88     do { \
89         ::nn::Result nn_util_panic_if_failed_except_result = (result); \
90         if ( !(nn_util_panic_if_failed_except_result <= (exceptResult)) ) \
91         { \
92             NN_PANIC_IF_FAILED(nn_util_panic_if_failed_except_result); \
93         } \
94     } while (0)
95 
96 
97 
98 namespace nn { namespace util {
99 
100     enum Description
101     {
102         DESCRIPTION_BUFFER_FULL = 1,
103         DESCRIPTION_BAD_DATA    = 2
104     };
105 
MakePermanentResult(Result::Summary summary,int description)106     inline Result MakePermanentResult(Result::Summary summary, int description)
107     {
108         return Result(Result::LEVEL_PERMANENT, summary, Result::MODULE_NN_UTIL, description);
109     }
110 
ResultAlreadyInitialized()111     inline Result ResultAlreadyInitialized()
112     {
113         return Result(Result::LEVEL_INFO, Result::SUMMARY_INVALID_STATE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_ALREADY_INITIALIZED);
114     }
115 
ResultOutOfMemory()116     inline Result ResultOutOfMemory()
117     {
118         return Result(Result::LEVEL_PERMANENT, Result::SUMMARY_OUT_OF_RESOURCE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_OUT_OF_MEMORY);
119     }
120 }}
121 
122 #endif // __cplusplus
123 
124 #endif
125