1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_GfxObject.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NW_GFX_GFXOBJECT_H_
19 #define NW_GFX_GFXOBJECT_H_
20 
21 #include <nw/types.h>
22 #include <nw/os/os_Memory.h>
23 #include <nw/ut/ut_MovePtr.h>
24 #include <nw/ut/ut_Inlines.h>
25 
26 namespace nw {
27 namespace gfx {
28 
29 class GfxObject;
30 
31 //---------------------------------------------------------------------------
32 //! @brief        グラフィックス関連の各種オブジェクトの基底クラスです。
33 //!
34 //! アロケータからメモリを確保、解放するしくみを提供します。
35 //! このクラスを継承したクラスを破棄するには、Destroy メソッドを用いて
36 //! 破棄してください。
37 //---------------------------------------------------------------------------
38 class GfxObject
39 {
40 public:
41     //----------------------------------------
42     //! @name 作成/破棄
43     //@{
44 
45     //! オブジェクトを破棄します。
Destroy()46     void Destroy() { this->~GfxObject(); m_Allocator.Free( this ); }
47 
48     //@}
49 
50 protected:
51     //----------------------------------------
52     //! @name 作成/破棄
53     //@{
54 
55     //! new 演算子のオーバーロードです。
56     //!
57     //! :private
new(size_t,void * buf)58     static void*    operator new( size_t, void* buf ) NW_NO_THROW { return buf; }
59 
60     //! delete 演算子のオーバーロードです。
61     //!
62     //! :private
delete(void *)63     static void     operator delete( void* ) NW_NO_THROW {}
64 
65 #ifdef NW_COMPILER_MSVC
66     //! delete 演算子のオーバーロードです。
67     //!
68     //! :private
delete(void *,void *)69     static void     operator delete( void*, void* ) NW_NO_THROW {}
70 #endif
71     //@}
72 
73     //----------------------------------------
74     //! @name コンストラクタ/デストラクタ
75     //@{
76 
77     //! コンストラクタです。
78     //!
79     //! :private
GfxObject(nw::os::IAllocator * allocator)80     GfxObject( nw::os::IAllocator* allocator )
81     : m_Allocator( *allocator )
82     {
83         NW_NULL_ASSERT( allocator );
84     }
85 
86     //! デストラクタです。
87     //!
88     //! :private
~GfxObject()89     virtual  ~GfxObject() {}
90 
91     //@}
92 
93     //----------------------------------------
94     //! @name アロケータ
95     //@{
96 
97     //! アロケータを取得します。
98     //!
99     //! :private
GetAllocator()100     nw::os::IAllocator& GetAllocator() { return m_Allocator; }
101 
102     //@}
103 
104 private:
105     nw::os::IAllocator&  m_Allocator;
106 };
107 
108 using ut::SafeDestroy;
109 using ut::SafeDestroyer;
110 using ut::SafeDestroyAll;
111 
112 //! @name GfxObject関連
113 //@{
114 
115 //---------------------------------------------------------------------------
116 //! @brief        MovePtr の GfxObject 用デリータークラスです。
117 //---------------------------------------------------------------------------
118 class GfxDeleter
119 {
120 public:
121     //! @brief コンストラクタです。
GfxDeleter()122     GfxDeleter()
123     : m_HasOwnership(true)
124     {}
125 
126     //! @brief コンストラクタです。
127     //!
128     //! @param[in] hasOwnership false を指定するとこのデリーターは GfxObject を破棄しません。
GfxDeleter(bool hasOwnership)129     GfxDeleter(bool hasOwnership)
130     : m_HasOwnership(hasOwnership)
131     {}
132 
133     //! GfxObject を破棄します。
operator()134     void operator() (GfxObject* object)
135     {
136         if (m_HasOwnership)
137         {
138             object->Destroy();
139         }
140     }
141 
142 private:
143     bool m_HasOwnership;
144 };
145 
146 //@}
147 
148 //---------------------------------------------------------------------------
149 //! @brief        GfxObject 用の MovePtr ラップクラスです。
150 //---------------------------------------------------------------------------
151 template<typename TObject>
152 class GfxPtr
153 {
154 public:
155     typedef ut::MovePtr<TObject, GfxDeleter> GfxMovePtr;
156     typedef typename GfxMovePtr::element_type element_type;
157     typedef typename GfxMovePtr::SafeBool SafeBool;
158 
159     //----------------------------------------
160     //! @name コンストラクタ/デストラクタ
161     //@{
162 
163     //! コンストラクタです。
GfxPtr()164     GfxPtr() : m_MovePtr() {}
165 
166     //! コピーコンストラクタです。コピー元から要素を移動します。
GfxPtr(const GfxPtr & pointer)167     GfxPtr(const GfxPtr& pointer)
168     : m_MovePtr(const_cast<GfxPtr*>(&pointer)->m_MovePtr) {}
169 
170     //! コンストラクタです。
171     template<typename TTObject>
GfxPtr(TTObject * pointer)172     explicit GfxPtr(TTObject* pointer)
173     : m_MovePtr(pointer) {}
174 
175     //! コンストラクタです。
176     //!
177     //!             hasOwnership に false を指定すると、この GfxPtr インスタンスを破棄するときに
178     //!             object の破棄を行いません。
179     //!
180     //! @param[in]  pointer 設定するオブジェクトです。
181     //! @param[in]  hasOwnership 所有権を持つかを指定します。
182     template<typename TTObject>
GfxPtr(TTObject * pointer,bool hasOwnership)183     GfxPtr(TTObject* pointer, bool hasOwnership)
184     : m_MovePtr(pointer, GfxDeleter(hasOwnership)) {}
185 
186     //! コンストラクタです。
187     template<typename TTObject, typename TTDeleter>
GfxPtr(ut::internal::MoveSource<ut::MovePtr<TTObject,TTDeleter>> source)188     GfxPtr(ut::internal::MoveSource< ut::MovePtr<TTObject, TTDeleter> > source)
189     : m_MovePtr(source) {}
190 
191 
192     //! @brief デストラクタです。
~GfxPtr()193     ~GfxPtr() {}
194 
195     GfxPtr& operator=(GfxPtr<TObject> rhs)
196     {
197         m_MovePtr.operator=(rhs.m_MovePtr);
198         return *this;
199     }
200 
Get()201     element_type* Get() const { return m_MovePtr.Get(); }
202     element_type& operator*() const { return *m_MovePtr; }
203     element_type* operator->() const { return m_MovePtr.operator->(); }
204     element_type& operator[](std::size_t i) const { return m_MovePtr[i]; }
Release()205     element_type* Release() { return m_MovePtr.Release(); }
206 
Reset()207     void Reset() { m_MovePtr.Reset(); }
208 
209     template<typename TTObject>
Reset(TTObject * object)210     void Reset(TTObject* object) { GfxPtr(object).Swap(*this); }
211 
212     //! @brief      オブジェクトを設定します。
213     //!
214     //!             hasOwnership に false を指定すると、この GfxPtr インスタンスを破棄するときに
215     //!             object の破棄を行いません。
216     //!
217     //! @param[in]  object 設定するオブジェクトです。
218     //! @param[in]  hasOwnership 所有権を持つかを指定します。
219     template<typename TTObject>
Reset(TTObject * object,bool hasOwnership)220     void Reset(TTObject* object, bool hasOwnership)
221     {
222         GfxPtr(object, hasOwnership).Swap(*this);
223     }
224 
SafeBool()225     operator SafeBool() const { return m_MovePtr.operator SafeBool(); }
226 
Swap(GfxPtr & pointer)227     void Swap(GfxPtr& pointer) { m_MovePtr.Swap(pointer.m_MovePtr); }
228 
GetDeleter()229     typename GfxMovePtr::deleter_reference GetDeleter() { return m_MovePtr.GetDeleter(); }
GetDeleter()230     typename GfxMovePtr::deleter_const_reference GetDeleter() const { return m_MovePtr.GetDeleter(); }
231 
232 private:
233     GfxMovePtr m_MovePtr;
234     //@}
235 };
236 
237 } // namespace gfx
238 } // namespace nw
239 
240 #endif // NW_GFX_GFXOBJECT_H_
241