インターフェイスの動作パターンを見つけようとしています。
私がしていることのいくつかの情報。dx11 でレンダリング エンジンを実装しています。私の目標は、クライアントが dx11 や高度なレンダリング技術の知識を必要としない、簡単で高度にインターフェース化されたエンジンを提供することです。
私は自分のエンジンを持っており、そのエンジンは、オブジェクト、地形、太陽、照明、水、地平線などを設定および作成する機能を提供します。
今、私は私の質問に来ます。エンジンのいわゆる MeshController は、次の 3 つのメソッドを提供します (コードは簡略化されています)。
//check if that description already exists (if not create it) and return that id
UINT addDesc(const MESH_ENTITY_DESC& desc);
//create an instance of the given object
MeshEntity* createInstance(const UINT descID);
//destroy the instance
void destroyInstance(MeshEntity* entity);
クライアントがポインターを処理するのを避けるために、それをラッパー クラスにパックして、nullptr チェックを実行し、エンティティが既に破棄されているか既に作成されているかなどを確認します。
ラッパー:
class MeshObject
{
private:
UINT g_Desc;
MeshEntity* g_Entity;
}
今、私はオブジェクトが常に有効な状態であることを望んでいます。インスタンスがラッパー オブジェクトを失う前に破棄されない場合、メモリ リークが発生します (実際にはそうではありませんが、コントローラーはシーンが閉じられるまでレンダリングするか、インスタンス スロットをブロックします。全体として、クライアントは制御を失います)。インスタンス)。だから私の最初の考えはこれでした:
//constructor
MeshObject(const MESH_ENTITY_DESC& desc)
{
g_Desc = Controller::addDesc(desc);
g_Entity = Controller::createInstance(g_Desc);
}
//copy constructor
MeshObject(const MeshObject& mo)
{
g_Desc = mo.g_Desc; //no need of adding desc, already exists
g_Entity = g_Entity; // no need of recreation? or should i also create a copy of the rendered object in the controller, so duplicate it?
}
MeshObject& operator=(const MeshObject& mo)
{
g_Desc = mo.g_Desc; //no need of adding desc, already exists
g_Entity = g_Entity; // definitly no need of recreation
}
~MeshObject()
{
Controller::destroyInstance(g_Entity);
}
3 つのルールにより、コピー演算子と代入演算子が必要です。しかし、問題があります。createInstance および destroyInstance メソッドには、シーンへの追加/シーンからの削除、おそらくレンダー バッファーの再作成のため、多くのオーバーヘッドがあります ...
そのため、以前にコピーされた場合、またはオブジェクトへの有効な参照(ポインター)がある場合、オブジェクトが破棄されるのを防ぐ方法を見つけようとしています。これは典型的な動的メモリ割り当てではなく、コントローラーでのある種の割り当てです。
クライアントの典型的なユースケース:
std::vector<MeshObject> moVector;
for(int i = 0; i < blabla; i++)
{
MeshObject mo(someDescription); // created object in controller
//do lot of init stuff, like position, rotation, ...
mo.pushback(mo); // here mo will be copied, try to avoid double creation
} // here mo will lose scope and will be destroyed, stop destroying because there is a valid wrapper object
助けてくれてありがとう!