1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_HandleObject.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: 35549 $
14  *---------------------------------------------------------------------------*/
15 
16 /* Please see man pages for details
17 
18 
19 
20 */
21 
22 #ifndef NN_OS_OS_HANDLEOBJECT_H_
23 #define NN_OS_OS_HANDLEOBJECT_H_
24 
25 #include <nn/Result.h>
26 #include <nn/Handle.h>
27 #include <nn/svc.h>
28 
29 #include <nn/assert.h>
30 #include <nn/util/util_NonCopyable.h>
31 
32 #ifdef __cplusplus
33 
34 namespace nn {
35 namespace os {
36 
37 class HandleManager;
38 
39 /* Please see man pages for details
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 */
50 class HandleObject : private nn::util::NonCopyable<HandleObject>
51 {
52 public:
53 
54     /* Please see man pages for details
55 
56 
57 
58     */
GetHandle()59     nn::Handle GetHandle() const { return m_Handle; }
60 
61     /* Please see man pages for details
62 
63 
64 
65     */
IsValid()66     bool IsValid() const { return m_Handle.IsValid(); }
67 
68 protected:
69 
70     /* Please see man pages for details
71 
72 
73 
74     */
75 #pragma push
76 #pragma diag_suppress 2530
HandleObject()77     HandleObject() {}
78 #pragma pop
79 
80     /* Please see man pages for details
81 
82 
83 
84     */
85     ~HandleObject();
86 
87     /* Please see man pages for details
88 
89 
90 
91 
92 
93     */
94     void SetHandle(nn::Handle handle);
95 
96     /* Please see man pages for details
97 
98 
99 
100     */
101     void Close();
102 
103     void Finalize();
104 
105     void ClearHandle();
106 
107 private:
108 
109     nn::Handle m_Handle;
110     friend class HandleManager;
111 
112 };
113 
114 // In-line implementation
115 
Close()116 inline void HandleObject::Close()
117 {
118     if (IsValid())
119     {
120         nn::svc::CloseHandle(m_Handle);
121         m_Handle = Handle();
122     }
123 }
124 
~HandleObject()125 inline HandleObject::~HandleObject()
126 {
127     Close();
128 }
129 
Finalize()130 inline void HandleObject::Finalize()
131 {
132     Close();
133 }
134 
SetHandle(nn::Handle handle)135 inline void HandleObject::SetHandle(nn::Handle handle)
136 {
137     NN_TASSERTMSG_(!IsValid(), "current handle(=%08X) is active\n", m_Handle.GetPrintableBits());
138     NN_TASSERT_(handle.IsValid());
139 
140     m_Handle = handle;
141 }
142 
ClearHandle()143 inline void HandleObject::ClearHandle()
144 {
145     m_Handle = Handle();
146 }
147 
148 }   // end of namespace os
149 }   // end of namespace nn
150 
151 #endif // __cplusplus
152 
153 #endif  // ifndef NN_OS_OS_HANDLEOBJECT_H_
154