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: 20014 $
14  *---------------------------------------------------------------------------*/
15 
16 /*! @file
17   @brief    Handle に関するAPI の宣言
18 
19   :include nn/os.h
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 /*!
40     @brief OS が管理するオブジェクトを表す抽象基底クラスです。
41 
42     実際のオブジェクトの生成は、継承されたクラスのコンストラクタか、
43     クラス固有の初期化関数によって行われます。
44     オブジェクトを破棄するには、継承されたクラス固有の破棄関数を呼びます。
45     明示的に破棄関数が呼ばれなかった場合は、多くのクラスではデストラクタによって自動的に破棄されます。
46 
47     このクラスを直接インスタンス化することはできません。
48 
49 */
50 class HandleObject : private nn::util::NonCopyable<HandleObject>
51 {
52 public:
53 
54     /*!
55     @brief        ハンドルを取得します。
56 
57     @return       ハンドルを返します。
58     */
GetHandle()59     nn::Handle GetHandle() const { return m_Handle; }
60 
61     /*!
62     @brief        オブジェクトが有効かどうかを返します。
63 
64     @return       オブジェクトが有効ならtrue、無効ならfalseを返します。
65     */
IsValid()66     bool IsValid() const { return m_Handle.IsValid(); }
67 
68 protected:
69 
70     /*!
71     @brief        コンストラクタです。
72 
73                   ハンドルオブジェクトを構築します。
74     */
HandleObject()75     HandleObject() {}
76 
77     /*!
78     @brief        デストラクタです。
79 
80                   内部で @ref Close を呼びます。
81     */
82     ~HandleObject();
83 
84     /*!
85     @brief        ハンドルをセットします。
86 
87     @param[in]    handle    ハンドルを指定します。
88 
89     @return       無し。
90     */
91     void SetHandle(nn::Handle handle);
92 
93     /*!
94     @brief        ハンドルを閉じます。
95 
96     @return       無し。
97     */
98     void Close();
99 
100     void Finalize();
101 
102     void ClearHandle();
103 
104 private:
105 
106     nn::Handle m_Handle;
107     friend class HandleManager;
108 
109 };
110 
111 // インライン実装
112 
Close()113 inline void HandleObject::Close()
114 {
115     if (IsValid())
116     {
117         nn::svc::CloseHandle(m_Handle);
118         m_Handle = Handle();
119     }
120 }
121 
~HandleObject()122 inline HandleObject::~HandleObject()
123 {
124     Close();
125 }
126 
Finalize()127 inline void HandleObject::Finalize()
128 {
129     Close();
130 }
131 
SetHandle(nn::Handle handle)132 inline void HandleObject::SetHandle(nn::Handle handle)
133 {
134     NN_TASSERTMSG_(!IsValid(), "current handle(=%08X) is active\n", m_Handle.GetPrintableBits());
135     NN_TASSERT_(handle.IsValid());
136 
137     m_Handle = handle;
138 }
139 
ClearHandle()140 inline void HandleObject::ClearHandle()
141 {
142     m_Handle = Handle();
143 }
144 
145 }   // end of namespace os
146 }   // end of namespace nn
147 
148 #endif // __cplusplus
149 
150 #endif  // ifndef NN_OS_OS_HANDLEOBJECT_H_
151