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: 30058 $
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         NN_LOG_FORCE("Failure Return: %x L=%d S=%d M=%d D=%d", \
77             nn_util_return_if_failure_result.GetValue(), \
78             (int)nn_util_return_if_failure_result.GetLevel(), \
79             (int)nn_util_return_if_failure_result.GetSummary(), \
80             (int)nn_util_return_if_failure_result.GetModule(), \
81             (int)nn_util_return_if_failure_result.GetDescription() \
82         ); \
83         return nn_util_return_if_failure_result; \
84     } \
85 } while(0)
86 
87 #define NN_UTIL_RETURN_IF_FAILED(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
88 
89 #define NN_UTIL_RETURN_IF_FAILED_0(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
90 #define NN_UTIL_RETURN_IF_FAILED_1(result, c1) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,,,,)
91 #define NN_UTIL_RETURN_IF_FAILED_2(result, c1, c2) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,,,)
92 #define NN_UTIL_RETURN_IF_FAILED_3(result, c1, c2, c3) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,,)
93 #define NN_UTIL_RETURN_IF_FAILED_4(result, c1, c2, c3, c4) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,c4,)
94 #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)
95 
96 #define NN_UTIL_RETURN_IF_FAILED_EXCEPT(result, exceptResult) \
97 do \
98 { \
99     ::nn::Result nn_util_return_if_failure_result_except = (result); \
100     if (!(nn_util_return_if_failure_result_except <= (exceptResult))) \
101     { \
102         NN_UTIL_RETURN_IF_FAILED(nn_util_return_if_failure_result_except); \
103     } \
104 } while (0)
105 
106 #ifdef NN_SWITCH_DISABLE_DEBUG_PRINT
107     #define NN_UTIL_THROW_RESULT(result) return (result)
108 #else
109     #define NN_UTIL_THROW_RESULT(result) \
110     do \
111     { \
112         ::nn::Result nn_util_return_throw_result = (result); \
113         NN_TASSERT_(nn_util_return_throw_result.IsFailure()); \
114         NN_UTIL_RETURN_IF_FAILED_0(nn_util_return_throw_result); \
115         return nn_util_return_throw_result; \
116     } while(0)
117 #endif
118 
119 #define NN_UTIL_PANIC_IF_FAILED(result)                     \
120     do {                                                    \
121         NN_UTIL_BEGIN_CHECK_RESULT(result)                  \
122         NN_TLOG_("RESULT FAILURE: result = %s\n", #result);   \
123         NN_UTIL_END_CHECK_RESULT                            \
124     } while (0)
125 
126 namespace nn { namespace util {
127 
MakePermanentResult(Result::Summary summary,int description)128     inline Result MakePermanentResult(Result::Summary summary, int description)
129     {
130         return Result(Result::LEVEL_PERMANENT, summary, Result::MODULE_NN_UTIL, description);
131     }
132 
ResultAlreadyInitialized()133     inline Result ResultAlreadyInitialized()
134     {
135         return Result(Result::LEVEL_INFO, Result::SUMMARY_INVALID_STATE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_ALREADY_INITIALIZED);
136     }
137 
ResultOutOfMemory()138     inline Result ResultOutOfMemory()
139     {
140         return Result(Result::LEVEL_PERMANENT, Result::SUMMARY_OUT_OF_RESOURCE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_OUT_OF_MEMORY);
141     }
142 }}
143 
144 #endif // __cplusplus
145 
146 #endif
147