Reputation: 2815
I have an a simple Registry pattern:
class Object
{
//some secondary simple methods
};
#define OBJECT_DEF(Name) \
public: \
static const char* Name() { return Name; } \
private:
class Registry
{
struct string_less {
bool operator() (const std::string& str1, const std::string& str2) {
for(int i = 0; i < str1.size() && i < str2.size(); ++i) {
if(str1[i] != str2[i])
return str1[i] < str2[i];
}
return str1.size() < str2.size();
}
};
typedef boost::shared_ptr < Object> ptrInterface;
typedef std::map < std::string, ptrInterface, string_less > container;
container registry;
static Registry _instance;
ptrInterface find(std::string name);
protected:
Registry () {}
~Registry () {}
public:
template < class T >
static T* Get(); // I want to write so !!!
template < class T >
static bool Set(T* instance, bool reSet = false);
};
And if i have a class that exteds Object:
class Foo : public Object {
OBJECT_DEF("foo");
//smth
};
I want to use Registry such way:
Registry::Set(new Foo());
or
Registry::Get<Foo>();
How can I emulate static templates with external implementation?
Upvotes: 0
Views: 380
Reputation: 264391
I would not use the hack with OBJECT_DEF()
Each class has a unique name (defined by the compiler) that can by accessed via type_info
I would then use the boost any object to store this information in a map nicely.
I have not checked this.
But register should now work with any type.
#include <map>
#include <boost/any.hpp>
#include <typeinfo>
#include <string>
#include <iostream>
class Register
{
static std::map<std::string, boost::any> data;
public:
// If you just want to store pointers then:
// alter the input parameter to Set() to be T*
// alter the output of Get() to be T* and modify the any_cast.
//
// If you are just storing pointers I would also modify the map
// to be a boost::pointer_map so that ownership is transfered
// and memory is cleaned up.
template<typename T>
static void Set(T const& value)
{
data[std::string(typeid(T).name())] = boost::any(value);
}
template<typename T>
static T Get()
{
return boost::any_cast<T>(data[std::string(typeid(T).name())]);
}
};
std::map<std::string, boost::any> Register::data;
int main()
{
// Currently this line will leak as register does not take ownership.
Register::Set(new int(1));
// This line works fine.
Register::Set(int(5));
std::cout << *(Register::Get<int*>()) << "\n";
std::cout << Register::Get<int>() << "\n";
}
Upvotes: 1