C ++服务提供商 [英] C++ Service Providers

查看:109
本文介绍了C ++服务提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习C ++,来自C#,我习惯于使用服务提供商:基本上是一个Dictionary< Type,object>。不幸的是,我不知道如何在C ++中做到这一点。因此,问题基本上是:


  1. 如何在C ++中创建字典。


  2. 如果我知道C ++中没有Type,我该如何使用Type。


  3. 与上述相同,但使用'object'。


谢谢!

解决方案

我假设您正在尝试将类型映射到单个对象实例。您可以尝试以下行:

  #include< typeinfo> 
#include< map>
#include< string>
using namespace std;

class SomeClass
{
public:
virtual〜SomeClass(){} //获取v表的虚函数
};

struct type_info_less
{
bool operator()(const std :: type_info * lhs,const std :: type_info * rhs)const
{
return lhs-> before(* rhs)!= 0;
}
};

class TypeMap
{
typedef map< type_info *,void *,type_info_less> TypenameToObject;
TypenameToObject ObjectMap;

public:
template< typename T>
T * Get()const
{
TypenameToObject :: const_iterator iType = ObjectMap.find(& typeid(T));
if(iType == ObjectMap.end())
return NULL;
return reinterpret_cast< T *>(iType-> second);
}
template< typename T>
void Set(T * value)
{
ObjectMap [& typeid(T)] = reinterpret_cast< void *>(value);
}
};

int main()
{
TypeMap Services;
Services.Set< SomeClass>(new SomeClass());
SomeClass * x = Services.Get< SomeClass>();
}

在C ++中,类型不是第一类对象,最少的类型名将是唯一的,所以你可以通过键来。



编辑:名称实际上并不保证是唯一的,所以坚持type_info指针并使用before方法来比较它们。


I've been learning C++, coming from C#, where I've gotten used to using service providers: basically a Dictionary<Type, object>. Unfortunately, I can't figure out how to do this in C++. So the questions are basically:

  1. How would I make a dictionary in C++.

  2. How would I use 'Type' with it, as far as I know there is no 'Type' in C++.

  3. Same as above, but with 'object'.

Thanks!

解决方案

I'm assuming that you're trying to map a type to a single object instance. You could try something along these lines:

#include <typeinfo>
#include <map>
#include <string>
using namespace std;

class SomeClass
{
public:
    virtual ~SomeClass() {} // virtual function to get a v-table
};

struct type_info_less
{
    bool operator() (const std::type_info* lhs, const std::type_info* rhs) const
    {
        return lhs->before(*rhs) != 0;
    }
};

class TypeMap
{
    typedef map <type_info *, void *, type_info_less> TypenameToObject;
    TypenameToObject ObjectMap;

public:
    template <typename T> 
    T *Get () const
    {
    	TypenameToObject::const_iterator iType = ObjectMap.find(&typeid(T));
    	if (iType == ObjectMap.end())
    		return NULL;
    	return reinterpret_cast<T *>(iType->second);
    }
    template <typename T> 
    void Set(T *value) 
    {
    	ObjectMap[&typeid(T)] = reinterpret_cast<void *>(value);
    }
};

int main()
{
    TypeMap Services;
    Services.Set<SomeClass>(new SomeClass());
    SomeClass *x = Services.Get<SomeClass>();
}

In C++ types are not first-class objects in their own right, but at least the type-name is going to be unique, so you can key by that.

Edit: The names aren't actually guaranteed to be unique, so hold on to the type_info pointers and use the before method to compare them.

这篇关于C ++服务提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆