Created
February 20, 2015 16:02
-
-
Save Antoine-Lassauzay/6ed5b76d0a14ab493253 to your computer and use it in GitHub Desktop.
Attempt to write a templated locator for Model (as in MVC). Views and controller can receive this object to use what they need.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#pragma once | |
using namespace std; | |
class ModelLocator | |
{ | |
map<string, AGObject*> m_models; | |
public: | |
ModelLocator() {} | |
~ModelLocator() {} | |
/* find a model based on a type */ | |
template<class ModelClass> | |
AGIntrusivePtr<ModelClass> findModel() const | |
{ | |
map<string, AGObject*>::const_iterator it = m_models.find(typeid(ModelClass).name()); | |
AGASSERT(it != m_models.end(), "Invalid model %s", typeid(ModelClass).name()); | |
return dynamic_cast<ModelClass*>(it->second); | |
} | |
/* register a model, any existing instance will be erased */ | |
template<class ModelClass> | |
void registerModel(AGIntrusivePtr<ModelClass> model) | |
{ | |
m_models[typeid(ModelClass).name()] = model.get(); | |
} | |
/* to register a model with a different typename (should check if types are compatible) */ | |
template<class ModelClass, class ModelClassID> | |
void registerModel(AGIntrusivePtr<ModelClass> model) | |
{ | |
m_models[typeid(ModelClassID).name()] = model.get(); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment