如何解决带有自定义类的 std::map 具有不断变化的封装数据类型
我正在尝试编写一个通用的参数处理类,其中每个参数都有一个名称、描述和值,它们封装在一个派生自公共 ParameterInterface
的类中。下面是它的定义,然后是两个具体的参数实现
struct ParameterInterface {
virtual ~ParameterInterface() = default;
virtual std::string getName() const = 0;
virtual std::string getDescription() const = 0;
double getValue() { return _data; }
friend std::ostream& operator<<(std::ostream& os,const ParameterInterface& pInterface)
{
os << pInterface._data;
return os;
}
protected:
double _data;
};
struct Parameter1 : public ParameterInterface {
Parameter1(double data) {
this->_data = data;
}
virtual std::string getName() const final { return _name; }
virtual std::string getDescription() const final { return _description; }
private:
const std::string _name = "Parameter1";
const std::string _description = "Description of Parameter1";
};
struct Parameter2 : public ParameterInterface {
Parameter2(double data) {
this->_data = data;
}
virtual std::string getName() const final { return _name; }
virtual std::string getDescription() const final { return _description; }
private:
const std::string _name = "Parameter2";
const std::string _description = "Description of Parameter2";
};
目标是将所有这些参数存储在一个 ParameterManager
类中,每个参数在创建过程中都会注册到该类中。以下实现负责处理:
struct ParameterManager {
template <class T,class... Args>
void createParameter (Args &&... args) {
static_assert(std::is_base_of<ParameterInterface,T>::value,"T must derive from ParameterInterface");
auto obj = std::make_unique<T>(std::forward<Args>(args)...);
std::string name = obj->getName();
auto result = _parameters.emplace(std::move(name),std::move(obj));
assert(result.second == true);
}
void printAllParametersInList() {
for (const auto &entry : _parameters) {
std::cout << entry.first << " (" << entry.second->getDescription() << "): ";
std::cout << entry.second->getValue() << std::endl;
}
}
private:
std::map<std::string,std::unique_ptr<ParameterInterface>> _parameters;
};
(归功于 Darhuuk,他提供了对 ParameterManager
here 的稍微修改的实现)。
这很有效,我可以像这样调用我的主函数:
int main() {
ParameterManager manager;
manager.createParameter<Parameter1>(3.14);
manager.createParameter<Parameter2>(2.71);
manager.printAllParametersInList();
return 0;
}
每个参数都移动到 std::map
中的 ParameterManager
中,我可以通过 printAllParametersInList()
方法打印所有值。如果我只将 double
视为存储数据的类型,这很好用。我如何能够在同一个地图中存储不同的元素?即,以下显示了主函数所需的行为:
int main() {
ParameterManager manager;
manager.createParameter<Parameter1>(3.14); // type still double
manager.createParameter<Parameter2>(42); // now of type int
manager.createParameter<Parameter3>("string argument"); // class Parameter3 would need to be defined here first (similarily to the Parameter1 and Parameter2 class).
manager.printAllParametersInList();
// should print:
// Parameter1 (Description of Parameter1): 3.14
// Parameter2 (Description of Parameter2): 42
// Parameter3 (Description of Parameter3): string argument
return 0;
}
这是我尝试/想到的:
-
模板
ParameterInterface
。然后这需要ParameterManager
接受模板参数,因为它构成了std::map<std::string,std::unique_ptr<ParameterInterface<Type>>> _parameters
定义签名的一部分。我现在可以实例化ParameterManager
的不同实例,但每个实例仍然仅限于一种类型。我想要一个可以处理不同类型数据的ParameterManager
实例。 -
在
getValue()
内实现ParameterInterface
的策略模式,即将该责任委托给GetValueStrategy
超类并为double
、{{1} 实现具体策略} 和int
。这不起作用,因为std::string
方法签名的返回类型会发生变化。同样,实现一个getValue()
类,其中存在NodeType
、IntNode
和DoubleNode
的具体实现。为StringNode
返回一个NodeType
,但是,在不使用模板的情况下如何将数据存储在getValue()
中?似乎不可能。 -
最后,将
NodeType
方法的所有实现拉入参数类(Parameter1、Parameter2、...ParameterN)中。这会导致代码重复(因此不是理想的设计),但也不会开箱即用,因为getValue()
是std::map
类型,它现在对ParameterInterface
一无所知.这个问题可能可以通过向下转换来解决,但代码重复仍然存在。
这里肯定有一个干净的设计解决方案,任何想法如何实现上述目标?可以在 here
中找到代码的实时示例版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。