gpt4 book ai didi

c++ - 将类型安全代码与运行时决策相结合

转载 作者:行者123 更新时间:2023-11-28 05:09:15 24 4
gpt4 key购买 nike

我正在重写一些现有代码 - 以前,所有答案信息都存储在内存中的字符串数组中。根据数据类型,数据在各个地方进行了转换。下面是我的目标设置的快速模型。本质上你有一些问题——存储在数据库中的答案的结构取决于数据类型。通常我会避免处理 void*,并将它们转换为适当的类型——但我找不到更好的解决方案来运行通用代码(通过 lambda 表达式),或者在数据类型已知的情况下进行具体说明。模板类在这种情况下无济于事,因为所有答案都需要存储在同一个 vector 中(因为一些算法会根据预定义规则应用于所有答案)。

如有任何建议,我们将不胜感激。

#include <vector>
#include <memory>


struct AddressData
{
wchar_t Line1[50];
wchar_t Line2[50];
long CountrySeqNo;

AddressData()
{
memset(this, 0, sizeof(*this));
};
};

struct GenericData
{
wchar_t value[200];

GenericData()
{
memset(this, 0, sizeof(*this));
};
};

enum class DataType
: short
{
GENERIC,
ADDRESS
};

class AnswerBase
{
protected:
const void* const data;
const DataType dataType;

protected:
AnswerBase(const DataType datatype, const void* const _data)
: dataType(datatype), data(data)
{
if (data == nullptr)
throw std::exception("Data may not be initialized as NULL");
};

public:
/*
Some generic methods here that would apply logic by means of lambdas etc - these would be overwritten in the derived classes
*/

template<typename T> const T& GetData() { static_assert(false, "The given type is not supported"); };

template<>
const GenericData& GetData()
{
if (DataType::GENERIC != dataType)
throw std::exception("The requested type does not match the value that initialised data");

return *static_cast<const GenericData* const>(data);
};
template<>
const AddressData& GetData()
{
if (DataType::ADDRESS != dataType)
throw std::exception("The requested type does not match the value that initialised data");

return *static_cast<const AddressData* const>(data);
};


};


class AddressAnswer
: public AnswerBase
{
public:
AddressAnswer()
: AnswerBase(DataType::ADDRESS, &answer)
{
};

protected:
AddressData answer;
};


class GenericAnswer
: public AnswerBase
{
public:
GenericAnswer()
: AnswerBase(DataType::GENERIC, &answer)
{
};

protected:
GenericData answer;
};


int main()
{
std::vector<std::shared_ptr<AnswerBase>> answers;
answers.push_back(std::make_shared<GenericAnswer>());
answers.push_back(std::make_shared<AddressAnswer>());


// In some parts of code - interact with generic methods without needing to check the underlying data type
// ....
// ....

// In parts of code where we know we are dealing with a given type - like saving to a DB


auto val1 = answers[0]->GetData<GenericData>().value;
auto val2 = answers[1]->GetData<AddressData>().Line1;

// this will give a runtime failure
//auto val3 = answers[0]->GetData<AddressData>().Line1;


return 0;
}

最佳答案

variant是执行此操作的干净方法。将其存储在父级中。

或者,提供一个 variant<A,B> GetData()在 parent 。现在访问被封装在返回的变体中。父级存储数据。

或者,提供一个 virtual variant<A,B> GetData() = 0 .子类型返回数据,AB , 在有问题的变体中。

或者,写virtual A* GetA() = 0; virtual B* GetB() = 0; .然后也许写一个名为 GetData<T> 的模板方法这样 GetData<A>()电话 GetA

或者,写virtual A* Get(tag_t<A>) = 0; virtual B* Get(tag_t<B>)=0; , 其中

template<class T>
struct tag_t {
using type=T;
constexpr tag_t(){}
};
template<class T>
constexpr tag_t<T> tag{};

是用于调度的标签。现在您可以通过执行 Get(tag<AddressData>) 来调用正确的虚拟接口(interface).

在这些虚拟案例中,数据存储在派生类型中。

关于c++ - 将类型安全代码与运行时决策相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43853620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com