我几乎没有使用C ++移动功能,所以我不确定我在做什么是正确的。我很感激任何人查看我的代码并指出我所犯的任何错误。
我们的想法是创建一个按键存储的资源图。资源可能是不可复制的和不可移动的。
另外,我的类需要构造函数和析构函数定义吗?
感谢。
#define TYPE(x) std::identity<decltype(x)>::type
namespace General
{
template<class T>
std::string ToString(const T& x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}
}
namespace General
{
template<class T, class KEY = std::string>
class ResourceManager
{
public:
typedef T ResourceType;
typedef KEY KeyType;
void Load(const KeyType& key, std::unique_ptr<ResourceType>&& resource);
const ResourceType& Read(const KeyType& key) const;
ResourceType& Modify(const KeyType& key);
void Unload(const KeyType& key);
std::unique_ptr<ResourceType>&& Release(const KeyType& key);
void UnloadAll();
private:
std::map<KeyType, std::unique_ptr<ResourceType>> data;
};
}
template<class T, class KEY>
void General::ResourceManager<T, KEY>::Load(const KeyType& key, std::unique_ptr<ResourceType>&& resource)
{
auto find_it = data.lower_bound(key);
if (find_it != data.end() && ! (data.key_comp()(key, find_it->first)))
{
throw std::runtime_error(General::ToString(key) + " already exists!");
}
else
{
data.insert(find_it, TYPE(data)::value_type(key, std::move(resource)));
}
}
template<class T, class KEY>
const typename General::ResourceManager<T, KEY>::ResourceType& General::ResourceManager<T, KEY>::Read(const KeyType& key) const
{
auto find_it = data.find(key);
if (find_it == data.end())
{
throw std::runtime_error(General::ToString(key) + " could not be found!");
}
else
{
return *find_it->second;
}
}
template<class T, class KEY>
typename General::ResourceManager<T, KEY>::ResourceType& General::ResourceManager<T, KEY>::Modify(const KeyType& key)
{
auto find_it = data.find(key);
if (find_it == data.end())
{
throw std::runtime_error(General::ToString(key) + " could not be found!");
}
else
{
return *find_it->second;
}
}
template<class T, class KEY>
void General::ResourceManager<T, KEY>::Unload(const KeyType& key)
{
auto find_it = data.find(key);
if (find_it == data.end())
{
throw std::runtime_error(General::ToString(key) + " could not be found!");
}
else
{
data.erase(find_it);
}
}
template<class T, class KEY>
std::unique_ptr<typename General::ResourceManager<T, KEY>::ResourceType>&& General::ResourceManager<T, KEY>::Release(const KeyType& key)
{
auto find_it = data.find(key);
if (find_it == data.end())
{
throw std::runtime_error(General::ToString(key) + " could not be found!");
}
else
{
auto resource = std::move(find_it->second);
data.erase(find_it);
return std::move(resource);
}
}
template<class T, class KEY>
void General::ResourceManager<T, KEY>::UnloadAll()
{
data.clear();
}
答案 0 :(得分:3)
这是一段简化的代码,演示了您的情况的关键以及如何用惯用语编写代码:
std::map<int, std::unique_ptr<Foo>> m;
void add_to_map(int key, std::unique_ptr<Foo> val)
{
m[key] = std::move(val);
}
用法:
add_to_map(1, std::unique_ptr<Foo>(new Foo(1, 2, 3)));
std::unique_ptr<Foo> p(new Foo(true, 'x'));
p->mutate();
add_to_map(std::move(p));
基本上,通过 value 传递唯一指针(或任何其他仅可移动的类型)并从中移动。
我遇到的一个特殊情况是你想要有条件地拥有对象 。在这种情况下,通过引用传递唯一指针并在之后检查它:
void add_maybe(std::unique_ptr<Foo> & val)
{
if (rand() % 2 == 0)
{
m[12] = std::move(val);
}
}
用法:
std::unique_ptr<Foo> p(new Foo(true, 'x'));
add_maybe(p);
if (p) { /* we still own the resource */ }
else { /* the resource is now owned by the map */ }
更新:要从地图中释放对象,请按值返回:
std::unique_ptr<Foo> release(int key)
{
auto it = m.find(key);
return it == m.end() ? { } : std::move(it->second);
}