저는 현재 제 자신의 엔진, 특히 그래픽 인터페이스에서 작업 중이며 DirectX와 OpenGL 렌더링 중 하나를 선택할 수 있도록 노력하고 있습니다. 이 목적을 위해 Texture Manager 싱글 톤을 원합니다 (저는이 표준 구현을 사용하는 데 익숙합니다). 그러나이 경우에는 기능을 추상화하는 동안 인스턴스의 동적 유형을 선택할 수 있기를 원합니다. 당신이 볼 수 있듯이, 나는 싱글의 RAII 보너스를 희생하고 null로 m_pInstance 포인터를 초기화 할 필요했습니다템플릿 유형이있는 동적 유형 Singleton입니다. 이것이 가능한 접근법입니까? [Solution provided]
template <typename T>
class TextureManagerType
{
public:
virtual ~TextureManagerType() = 0 {}
T* GetTexture(TEXTURE_ID texID)
{
std::map<TEXTURE_ID, T*>::const_iterator it = m_textures.find(texID);
if(it != m_textures.end())
{
return (*it)->second;
}
else
{
assert(0);
return 0;
}
}
bool Load(const std::string& texturePaths)
{
File f;
if(!f.Open(texturePaths))
{
ReportError("Fatal error! Could not open: " + texturePaths);
return false;
}
std::string str;
while(!f.EndOf())
{
if(!f.GetString(&str))
{
ReportError("Warning: Unexpected end of file in texturepaths file: " + texturePaths);
}
LoadTexture(str);
}
return true;
}
virtual T* LoadTexture(const std::string& texturePath) = 0;
protected:
std::map<TEXTURE_ID, T*> m_textures;
};
//Uncopyable is a base class with a disabled copy constructor
class TextureManager : public Uncopyable
{
private:
TheTextureManager();
public:
//T here is underlined in all three mentions as it is undefined
static TextureManagerType<T>* Instance()
{
return m_pInstance;
}
static void SetDynamicType(TextureManagerType<T>* type)
{
m_pInstance = type;
}
static TextureManagerType<T>* m_pInstance;
//static TexManagerType* Instance()
//{
// return m_pInstance;
//}
//static void SetDynamicType(TexManagerType* type)
//{
// m_pInstance = type;
//}
//static TexManagerType* m_pInstance;
};
//Named to be consistent with normal singletons
typedef TextureManager TheTextureManager;
: 여기에 지금까지 내 구현입니다. 전 세계 접근성을 유지하면서 각 파생 관리자의 수명을 제어 할 수 있도록하고 싶습니다으로
//I want the singleton once set, to be able to be accessed thusly:
TheTextureManager::Instance()->DoTextureFunction();
나는 TextureManager의 각 유형에 대한 싱글을 원하지 않는다. 현재 TextureManager :: Instance의 반환 값 유형은 액세스 할 수없는 템플릿 형식을 알아야하므로 내 시스템이 전혀 작동하지 않습니다.
TextureManager 내에서 TextureManagerType을 중첩하려고했으나 텍스처 관리자에 을 호출 할 때마다 필요한 유형이 템플릿 추출 매개 변수로 필요하다는 것을 의미했습니다.
여러분의 의견을지지 해 주시기 바랍니다. 중요/모든 세부 사항을 빠뜨린 경우 문의하십시오. 브레인은 지금도 생각하기에는 너무 지쳤습니다.
텍스처 관리자에서 설정 한 TexManagerType은 어떻게 결정합니까? –
내 Direct X Renderer 클래스에서 TextureManager :: SetDynamicType (new DXTextureManager)을 호출 할 것입니다 – Crackerjack55
한 번에 둘 이상의 유형을 사용할 수 있습니까? –