1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_CLibImpl.h
4 
5   Copyright (C) 2009-2012 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 /*! @file
17     @brief      Utility header for implementing the C interface.
18 
19 */
20 
21 #ifndef NN_UTIL_DETAIL_UTIL_CLIBIMPL_H_
22 #define NN_UTIL_DETAIL_UTIL_CLIBIMPL_H_
23 
24 #include <nn/types.h>
25 #include <nn/util/util_StaticAssert.h>
26 
27 #ifdef __cplusplus
28 #include <nn/util/util_TypeTraits.h>
29 #endif
30 
31 #ifdef __cplusplus
32     /*! @def        NN_EXTERN_C
33         @brief      Declares external linkage using C.
34     */
35     #define NN_EXTERN_C extern "C"
36     #define NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(cname, cppname, size, alignment_holder_type) \
37         typedef union cname \
38         { \
39             char buf[size]; \
40             alignment_holder_type alignment_holder; \
41             NN_STATIC_ASSERT((size) == sizeof(cppname) && ::nn::util::alignment_of<alignment_holder_type>::value == ::nn::util::alignment_of<cppname>::value); \
42         } cname
43 
44 #else
45     #define NN_EXTERN_C extern
46     #define NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(cname, cppname, size, alignment_holder_type) \
47         typedef union cname \
48         { \
49             char buf[size]; \
50             alignment_holder_type alignment_holder; \
51         } cname
52 #endif
53 
54 #define NN_UTIL_DETAIL_CLIBIMPL_DEFINE_ABSTRACT_BUFFER_CLASS(name) typedef struct { bit8 dummy; } name;
55 
56 #define NN_UTIL_DETAIL_CLIBIMPL_DECLARE_CONVERSION(name, fromC, toC) \
57     NN_EXTERN_C toC* name(fromC*); \
58     NN_EXTERN_C const toC* name##Const(const fromC*)
59 
60 #define NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(name, fromC, toC, fromCpp, toCpp) \
61     NN_EXTERN_C toC* name(fromC* p) { return reinterpret_cast<toC*>(static_cast<toCpp*>(reinterpret_cast<fromCpp*>(p))); } \
62     NN_EXTERN_C const toC* name##Const(const fromC* p) { return reinterpret_cast<const toC*>(static_cast<const toCpp*>(reinterpret_cast<const fromCpp*>(p))); }
63 
64 #endif
65 
66 /* @defgroup       page_for_c_users    About C Language Wrappers
67     @brief         C language wrappers.
68 
69     @{
70 
71 The CTR-SDK APIs are mostly written in C++.
72 C++ APIs cannot be called directly from C language files.
73 
74 The CTR-SDK provides wrappers that enable you to call the C++ APIs from C source files.
75 
76     Mutex Example
77 
78 In C++, you would use the <tt>@ref nn::os::Mutex</tt> class to use mutexes. The mutex class maintains mutex objects internally.
79 
80 The names of the wrappers that enable this class to be used in C begin with <tt>nnosMutex</tt>, as shown in the following examples.
81 These functions have a 1:1 correspondence with the C++ methods.
82 
83 @li <tt>@ref nnosMutexInitialize</tt> → <tt>@ref nn::os::Mutex::Initialize</tt> <br>
84 @li <tt>@ref nnosMutexLock</tt>       → <tt>@ref nn::os::Mutex::Lock</tt> <br>
85 @li <tt>@ref nnosMutexTryLock</tt>    → <tt>@ref nn::os::Mutex::TryLock</tt> <br>
86 @li <tt>@ref nnosMutexUnlock</tt>     → <tt>@ref nn::os::Mutex::Unlock</tt> <br>
87 @li <tt>@ref nnosMutexInitialize</tt> → <tt>@ref nn::os::Mutex::Initialize</tt> <br>
88 
89 For more information about a C wrapper function, see the documentation for the corresponding class method.
90 
91 The <tt>@ref nnosMutex</tt> structure, the data type that stores mutex objects in C, is an exception.
92 This structure is actually a class instance under the hood, so it cannot be referenced directly from the C language.
93 
94 
95     List
96 
97 For a list of modules that can be used in C and C++ code, see the "Modules" page.
98 
99 <!-- For a list of C functions, see <A HREF="globals.html">File Members</A>.-->
100 
101     @}
102 */
103