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: 26367 $
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_THROW_RESULT(result) \
90 do \
91 { \
92     ::nn::Result nn_util_return_if_failure_result = (result); \
93     NN_TASSERT_(nn_util_return_if_failure_result.IsFailure()); \
94     return nn_util_return_if_failure_result; \
95 } while(0)
96 
97 #define NN_UTIL_PANIC_IF_FAILED(result)                     \
98     do {                                                    \
99         NN_UTIL_BEGIN_CHECK_RESULT(result)                  \
100         NN_TLOG_("RESULT FAILURE: result = %s\n", #result);   \
101         NN_UTIL_END_CHECK_RESULT                            \
102     } while (0)
103 
104 namespace nn { namespace util {
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