1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Result.h
4 
5   Copyright (C)2009-2012 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: 47344 $
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/dbg_SmallSet.h>
27 
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             return nn_util_return_if_failure_result; \
36         } \
37     } while(0)
38 
39 #define NN_UTIL_RETURN_IF_FAILED(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
40 
41 #define NN_UTIL_RETURN_IF_FAILED_0(result) NN_UTIL_RETURN_IF_FAILED_BASE(result,,,,,)
42 #define NN_UTIL_RETURN_IF_FAILED_1(result, c1) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,,,,)
43 #define NN_UTIL_RETURN_IF_FAILED_2(result, c1, c2) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,,,)
44 #define NN_UTIL_RETURN_IF_FAILED_3(result, c1, c2, c3) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,,)
45 #define NN_UTIL_RETURN_IF_FAILED_4(result, c1, c2, c3, c4) NN_UTIL_RETURN_IF_FAILED_BASE(result,c1,c2,c3,c4,)
46 #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)
47 
48 #define NN_UTIL_RETURN_IF_FAILED_EXCEPT(result, exceptResult) \
49     do \
50     { \
51         ::nn::Result nn_util_return_if_failure_result_except = (result); \
52         if (!(nn_util_return_if_failure_result_except <= (exceptResult))) \
53         { \
54             NN_UTIL_RETURN_IF_FAILED(nn_util_return_if_failure_result_except); \
55         } \
56     } while (0)
57 
58 #ifdef NN_SWITCH_DISABLE_DEBUG_PRINT
59     #define NN_UTIL_THROW_RESULT(result) return (result)
60 #else
61     #define NN_UTIL_THROW_RESULT(result) \
62     do \
63     { \
64         ::nn::Result nn_util_return_throw_result = (result); \
65         NN_TASSERT_(nn_util_return_throw_result.IsFailure()); \
66         NN_UTIL_RETURN_IF_FAILED_0(nn_util_return_throw_result); \
67         return nn_util_return_throw_result; \
68     } while(0)
69 #endif
70 
71 #define NN_UTIL_PANIC_IF_FAILED(result) NN_PANIC_IF_FAILED(result)
72 
73 
74 namespace nn { namespace util {
75 
76     enum Description
77     {
78         DESCRIPTION_BUFFER_FULL = 1,
79         DESCRIPTION_BAD_DATA    = 2
80     };
81 
MakePermanentResult(Result::Summary summary,int description)82     inline Result MakePermanentResult(Result::Summary summary, int description)
83     {
84         return Result(Result::LEVEL_PERMANENT, summary, Result::MODULE_NN_UTIL, description);
85     }
86 
ResultAlreadyInitialized()87     inline Result ResultAlreadyInitialized()
88     {
89         return Result(Result::LEVEL_INFO, Result::SUMMARY_INVALID_STATE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_ALREADY_INITIALIZED);
90     }
91 
ResultOutOfMemory()92     inline Result ResultOutOfMemory()
93     {
94         return Result(Result::LEVEL_PERMANENT, Result::SUMMARY_OUT_OF_RESOURCE, Result::MODULE_NN_UTIL, Result::DESCRIPTION_OUT_OF_MEMORY);
95     }
96 }}
97 
98 #endif // __cplusplus
99 
100 #endif
101