BrewSDKを用いて静的風なクラスを作るテンプレ
//============================================================================= // Hoeg.hの静的クラス作成のテンプレート //============================================================================= #ifndef __HOGE_H__//多重インクルード防止 #define __HOGE_H__ #include "source/core/BrewRefObject.h" #include "source/core/BrewSmartObject.h" class Hoge : public BrewSmartObject{ //============================================================================= // BrewSmartObject継承クラスのほぼ必須メンバおよびオーバーロード等 //============================================================================= public: class Data; friend class Data; public: Hoge(){} //コンストラクタ Hoge( const BrewRefObject& s ) : BrewSmartObject( s ){} //コピーコンストラクタ Hoge( long /* must be zero */) : BrewSmartObject( null ){} //null に対するコンストラクタ //代入演算子 Hoge& operator=( const BrewRefObject& s ){ return *(Hoge*)&BrewRefObject::operator=( s ); } //null に対する代入演算子 Hoge& operator=( long /* must be zero */){ return *(Hoge*)&BrewRefObject::operator=( null ); } private: static Hoge GetInstance(void); //静的な本体をBrewAppletから取得する Data* get(){ return (Data*)_pObj; } //実体を取得する const Data* get() const{ return (const Data*)_pObj; } //実体を取得する //============================================================================= // BrewSmartObject継承クラスのほぼ必須メンバおよびオーバーロード等 ここまで //============================================================================= public: static void bar(void); //サンプルメソッド static Hoge Create(void); friend class BrewApplet; }; #endif // __HOGE_H__//多重インクルード防止
//============================================================================= // Hoeg.cppの静的クラス作成のテンプレート //============================================================================= #include "source/stdafx.h" #include "Hoge.h" #include "HogeData.h" #include "source/applet/BrewApplet.h" Hoge Hoge::Create(){ Hoge hoge = BrewSmartCreator< Hoge , Hoge::Data >::Create(); if( hoge != null && hoge.get()->Init() == ER_OK ){ return hoge; }else{ return null; } }; //静的な本体をBrewAppletから取得する Hoge Hoge::GetInstance(){ return BrewApplet::Get()->GetKeyManager(); } //機能 void Hoge::bar(){ GetInstance().get()->bar(); }
//============================================================================= // HoegData.hの静的クラス作成のテンプレート //============================================================================= #ifndef __HOGEDATA_H__//多重インクルード防止 #define __HOGEDATA_H__ #include "Hoge.h" #include "source/core/BrewSmartObject.h" #include "source/core/BrewErrorEnums.h" class Hoge::Data : public BrewSmartObject::Data{ public: ~Data(){}; void bar(void); //サンプルメソッドの実装 virtual EResult Init(); //初期化 }; #endif // __HOGEDATA_H__//多重インクルード防止
//============================================================================= // HoegData.cppの静的クラス作成のテンプレート //============================================================================= #include "HogeData.h" //サンプルメソッドの実装 void Hoge::Data::bar(){ } //初期化 EResult Hoge::Data::Init(){ return ER_OK; }
//============================================================================= // BrewApplet.hの静的クラス作成のためのテンプレート //============================================================================= //〜前略〜 #include "Hoge.h" //〜中略〜 public: Hoge GetHoge(){ return _hoge; } //〜中略〜 private: Hoge _hoge; //〜後略〜
//============================================================================= // BrewApplet.cppの静的クラス作成のためのテンプレート //============================================================================= //〜中略〜 bool BrewApplet::Init(){ //〜中略〜 // これ以降、BrewSmartPtr が使える。 _pStaticString = new BrewStaticString(); if( _pStaticString == null ){ goto failed; } //〜中略〜 //Hogeクラスの生成 _hoge = Hoge::Create(); if( _hoge == null ){ goto failed; } //〜中略〜 } //〜中略〜 void BrewApplet::ReleaseApplet(){ //〜中略〜 _hoge = null; //〜中略〜 }
HogeをBrewAppletに作成してる。
BrewAppletはどこからでも取得できるから、Hogeの静的関数→BrewApplet取得→Hogeを取得でOK。
なのでHogeDataのインスタンスメソッドを使うときも傍目には
Hoge::bar();
で動く…と思う。
melt_slincさんの日記が削除されてるorz