【发布时间】:2012-11-23 16:48:06
【问题描述】:
在我工作的公司,我们创建了一个名为“RestrictedMap”的类。这提供了与常规 std::map 相同的接口,但不允许您使用 [] 运算符。还提供了一些其他功能以舒适地使用该类。该类在内部包装了一个 std::map。
我现在正在尝试创建一个类似的类,它对 boost::ptr_map 执行相同的操作,称为“RestrictedPointerMap”。为此,我创建了 RestrictedMapBase,它接受作为模板参数的映射类型,它应该包装并包含大部分实现。两个类派生它并指定要包装的地图类型:
- RestrictedMap 与我们以前拥有的相同并包装 std::map
- 和 RestrictedPointerMap 是新的并包装了 boost::ptr_map。
这是代码,为了完整起见,我没有简化类,但我稍后会命名相关函数。
RestrictedMap.h
#pragma once
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/static_assert.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/foreach.hpp>
#include <map>
/**
* Class that has the benefits of a map, but does not add an entry if it does not exists.
* Do not use RestrictedMapBase directly but use one of the derived classes (RestrictedMap or RestrictedPointerMap).
*/
template <typename MAP>
class RestrictedMapBase
{
public:
RestrictedMapBase(const MAP& map):
m_map(map)
{}
template<class InputIterator>
RestrictedMapBase(InputIterator first, InputIterator last):
m_map(first, last)
{}
RestrictedMapBase()
{}
/************************************************************************/
/* std::map interface */
/************************************************************************/
typedef typename MAP::iterator iterator;
typedef typename MAP::const_iterator const_iterator;
typedef typename MAP::value_type value_type;
typedef typename MAP::key_type key_type;
typedef typename MAP::mapped_type mapped_type;
typedef typename MAP::size_type size_type;
iterator begin() { return m_map.begin(); }
iterator end() { return m_map.end(); }
const_iterator begin() const { return m_map.begin(); }
const_iterator end() const { return m_map.end(); }
bool empty() const { return m_map.empty(); }
size_type size() const { return m_map.size(); }
iterator find(const key_type& key) { return m_map.find(key); }
const_iterator find(const key_type& key) const { return m_map.find(key); }
void clear() { m_map.clear(); }
void erase(iterator where) { m_map.erase(where); }
bool operator==(const typename RestrictedMapBase<MAP>& other) const { return m_map == other.m_map; }
bool operator!=(const typename RestrictedMapBase<MAP>& other) const { return m_map != other.m_map; }
bool operator<(const typename RestrictedMapBase<MAP>& other) const { return m_map < other.m_map; }
/************************************************************************/
/* extra */
/************************************************************************/
void erase(const key_type& key)
{
iterator iter(find(key));
assert(found(iter));
erase(iter);
}
void eraseIfExists(const key_type& key)
{
m_map.erase(key);
}
bool exists(const key_type& key) const
{
return found(find(key));
}
mapped_type& getValue(const key_type& key)
{
return const_cast<mapped_type&>(static_cast<const RestrictedMapBase<MAP>&> (*this).getValue(key));
}
const mapped_type& getValue(const key_type& key) const
{
const_iterator iter(find(key));
assert(found(iter));
return getData(iter);
}
mapped_type getValueIfExists(const key_type& key) const
{
BOOST_STATIC_ASSERT(boost::is_pointer<mapped_type>::value);
const_iterator iter(find(key));
if (found(iter)) {
return getData(iter);
} else {
return 0;
}
}
void setValue(const key_type& key, const mapped_type& value)
{
iterator iter(find(key));
assert(found(iter));
setData(iter, value);
}
void add(const key_type& key, const mapped_type& value)
{
assert(!exists(key));
insert(key, value);
}
void add(const RestrictedMapBase<MAP>& mapToAdd)
{
BOOST_FOREACH(value_type element, mapToAdd.m_map)
{
add(element.first, element.second);
}
}
void addOrReplace(const key_type& key, const mapped_type& value)
{
iterator iter(find(key));
if (found(iter)) {
setData(iter, value);
} else {
insert(key, value);
}
}
mapped_type* addDefaultConstructed(const key_type& key)
{
assert(!exists(key));
return &m_map[key];
}
private:
bool found(const const_iterator& iter) const
{
return iter != end();
}
const mapped_type& getData(const const_iterator& iter) const
{
return const_cast<const mapped_type&>(iter->second);
}
mapped_type& getData(const iterator& iter)
{
return const_cast<mapped_type&>(static_cast<const RestrictedMapBase<MAP>&>(*this).getData(iter));
}
void setData(const iterator& iter, const mapped_type& value)
{
getData(iter) = value;
}
virtual void insert(const key_type& key, const mapped_type& value) = 0;
protected:
MAP& getMap()
{
return m_map;
}
private:
MAP m_map;
};
template <typename KEYTYPE, typename DATATYPE>
class RestrictedMap: public RestrictedMapBase<std::map<KEYTYPE, DATATYPE> >
{
public:
RestrictedMap(const std::map<typename KEYTYPE, typename DATATYPE>& map): RestrictedMapBase(map)
{}
template<class InputIterator>
RestrictedMap(InputIterator first, InputIterator last): RestrictedMapBase(first, last)
{}
RestrictedMap()
{}
virtual void insert(const KEYTYPE& key, const DATATYPE& value)
{
getMap().insert(std::make_pair(key, value));
}
};
template <typename KEYTYPE, typename DATATYPE>
class RestrictedPointerMap: public RestrictedMapBase<boost::ptr_map<KEYTYPE, DATATYPE> >
{
public:
RestrictedPointerMap(const boost::ptr_map<typename KEYTYPE, typename DATATYPE>& map): RestrictedMapBase(map)
{}
template<class InputIterator>
RestrictedPointerMap(InputIterator first, InputIterator last): RestrictedMapBase(first, last)
{}
RestrictedPointerMap()
{}
virtual void insert(const KEYTYPE& key, DATATYPE* const& value)
{
/* boost::ptr_map::mapped_type does not equal the DATATYPE template parameter passed to it. Therefore this
* functions signature *looks* different from the RestrictedMapBase::insert signature */
getMap().insert(key, std::auto_ptr<DATATYPE>(value));
}
};
这很有效,除非我想在 RestrictedPointerMap 上调用 getValue。 getData 函数返回正确的值,但之后在 getValue 函数中出错。它返回一个错误的指针(如指针错误)。
下面是一些重现问题的代码:
TestClass.h
#pragma once
class SomeClass
{
public:
SomeClass();
virtual ~SomeClass();
};
TestClass.cpp
#include "stdafx.h"
#include "TestClass.h"
#include <iostream>
SomeClass::SomeClass()
{
std::cout << "TestClass[" << this << "] created." << std::endl;
}
SomeClass::~SomeClass()
{
std::cout << "TestClass[" << this << "] deleted." << std::endl;
}
TestRestrictedPtrMap.cpp(主)
#include "stdafx.h"
#include "RestrictedMap.h"
#include "TestClass.h"
#include <boost/foreach.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
typedef RestrictedPointerMap<int, SomeClass> MapType;
MapType theMap;
theMap.add(1, new SomeClass());
theMap.add(2, new SomeClass());
BOOST_FOREACH(MapType::value_type mapEntry, theMap) {
std::cout << mapEntry.first << " = " << mapEntry.second << std::endl;
}
SomeClass* oneClass = theMap.getValue(1);
std::cout << oneClass << std::endl;
SomeClass* twoClass = theMap.getValue(2);
std::cout << twoClass << std::endl;
std::cin.get();
return 0;
}
这个的输出是:
TestClass[0078A318] created.
TestClass[0078A448] created.
1 = 0078A318
2 = 0078A448
0018FBD4
0018FBD4
TestClass[0078A318] deleted.
TestClass[0078A448] deleted.
我不知道为什么会出错。据我所知,返回值变坏了。
提前感谢您的帮助,
汤姆
【问题讨论】:
-
return const_cast<mapped_type&>(static_cast<const RestrictedMapBase<MAP>&> (*this).getValue(key));中的static_cast是怎么回事?这在我看来非常可疑。 -
@DavidSchwartz:这是避免在
const和非const版本之间重复工作的典型策略。您可以采用任何一种方式,但是让非const版本调用const版本(如此处所示)会更好,因为const版本不会意外修改对象。我认为它甚至出现在 Meyers 的 Efficient C++ 中。 -
这段代码在我看来就像法语:) 看起来生锈了。无论如何,为了您的目标,为什么不模板
class RestrictedMapBase : public MAP 然后以某种方式隐藏 [] 运算符。如果可能的话,也许可以将其设为私有。 -
@kellogs:代码确实很奇怪,但在可能的情况下,组合应该优先于继承,这就是这种情况。
-
@Tekar:不幸的是,您的代码是错误的(可能是由于困扰它的 microsoftism 的数量),因此很难在本地尝试和重现。模板和虚方法的奇怪组合让我觉得原作者不太懂 C++(你的代码对对象切片很开放,基类的析构函数没有受到保护);不幸的是,这并不能很好地表明您面临的错误。
标签: c++ stdmap boost-ptr-container