1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Result.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  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: 36122 $
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/svc/svc_Stub.h>
25 #include <nn/config.h>
26 #include <nn/dbg.h>
27 
28 #ifdef NN_SWITCH_DISABLE_DEBUG_PRINT
29 
30 #define NN_UTIL_BEGIN_CHECK_RESULT(result) \
31     { \
32         ::nn::Result nn_util_result_try_result = (result); \
33         if (nn_util_result_try_result.IsFailure()) \
34         {
35 
36 #define NN_UTIL_ADD_RESULT_MESSAGE(expected, ...)
37 
38 #define NN_UTIL_END_CHECK_RESULT \
39             ::nnResultTFailureHandler(nn_util_result_try_result, "", 0, ""); \
40             ::nn::dbg::Break(::nn::dbg::BREAK_REASON_PANIC); \
41         } \
42     }
43 
44 #else
45 
46 #define NN_UTIL_BEGIN_CHECK_RESULT(result) \
47     { \
48         ::nn::Result nn_util_result_try_result = (result); \
49         if (nn_util_result_try_result.IsFailure()) \
50         { \
51             do {
52 
53 #define NN_UTIL_ADD_RESULT_MESSAGE(expected, ...) \
54                 if (nn_util_result_try_result == (expected)) \
55                 { \
56                     ::nnResultFailureHandler(nn_util_result_try_result, NN_FILE_NAME, __LINE__, __VA_ARGS__); \
57                     break; \
58                 }
59 
60 #define NN_UTIL_END_CHECK_RESULT \
61                 ::nnResultTFailureHandler(nn_util_result_try_result, NN_FILE_NAME, __LINE__, "Unexpected Result Failure."); \
62             } while (0); \
63             ::nn::dbg::Break(::nn::dbg::BREAK_REASON_PANIC); \
64         } \
65     }
66 
67 #endif // NN_SWITCH_DISABLE_DEBUG_PRINT
68 
69 #define NN_UTIL_RETURN_IF_FAILED_BASE(result, s1, s2, s3, s4, s5) \
70 do \
71 { \
72     ::nn::Result nn_util_return_if_failure_result = (result); \
73     if (nn_util_return_if_failure_result.IsFailure()) \
74     { \
75         s1; s2; s3; s4; s5; \
76         return nn_util_return_if_failure_result; \
77     } \
78 } while(0)
79 
80 #define NN_UTIL_RETURN_IF_FAILED(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
81 
82 #define NN_UTIL_RETURN_IF_FAILED_0(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
83 #define NN_UTIL_RETURN_IF_FAILED_1(result, c1) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,,,,)
84 #define NN_UTIL_RETURN_IF_FAILED_2(result, c1, c2) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,,,)
85 #define NN_UTIL_RETURN_IF_FAILED_3(result, c1, c2, c3) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,,)
86 #define NN_UTIL_RETURN_IF_FAILED_4(result, c1, c2, c3, c4) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,c4,)
87 #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)
88 
89 #define NN_UTIL_RETURN_IF_FAILED_EXCEPT(result, exceptResult) \
90 do \
91 { \
92     ::nn::Result nn_util_return_if_failure_result_except = (result); \
93     if (!(nn_util_return_if_failure_result_except <= (exceptResult))) \
94     { \
95         NN_UTIL_RETURN_IF_FAILED(nn_util_return_if_failure_result_except); \
96     } \
97 } while (0)
98 
99 #ifdef NN_SWITCH_DISABLE_DEBUG_PRINT
100     #define NN_UTIL_THROW_RESULT(result) return (result)
101 #else
102     #define NN_UTIL_THROW_RESULT(result) \
103     do \
104     { \
105         ::nn::Result nn_util_return_throw_result = (result); \
106         NN_TASSERT_(nn_util_return_throw_result.IsFailure()); \
107         NN_UTIL_RETURN_IF_FAILED_0(nn_util_return_throw_result); \
108         return nn_util_return_throw_result; \
109     } while(0)
110 #endif
111 
112 #define NN_UTIL_PANIC_IF_FAILED(result)                     \
113     do {                                                    \
114         NN_UTIL_BEGIN_CHECK_RESULT(result)                  \
115         NN_TLOG_("RESULT FAILURE: result = %s\n", #result);   \
116         NN_UTIL_END_CHECK_RESULT                            \
117     } while (0)
118 
119 namespace nn { namespace util {
120 
MakePermanentResult(Result::Summary summary,int description)121     inline Result MakePermanentResult(Result::Summary summary, int description)
122     {
123         return Result(Result::LEVEL_PERMANENT, summary, Result::MODULE_NN_UTIL, description);
124     }
125 
ResultAlreadyInitialized()126     inline Result ResultAlreadyInitialized()
127     {
128         return Result(Result::LEVEL_INFO, Result::SUMMARY_INVALID_STATE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_ALREADY_INITIALIZED);
129     }
130 
ResultOutOfMemory()131     inline Result ResultOutOfMemory()
132     {
133         return Result(Result::LEVEL_PERMANENT, Result::SUMMARY_OUT_OF_RESOURCE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_OUT_OF_MEMORY);
134     }
135 }}
136 
137 #endif // __cplusplus
138 
139 #endif
140