【C++】:允许用户在类中添加额外内容
类似IParameter
,添加模板类型转换as
,这可以让用户自行添加额外内容。
class VFLOW_ABI IParameter {
public:
virtual ~IParameter() = default;
NO_DISCARD virtual std::shared_ptr<IParameter> clone() const = 0;
NO_DISCARD virtual Buffer to_json() const = 0;
NO_DISCARD virtual Buffer dump() const = 0;
virtual IParameter &from_json(const Buffer &buf) = 0;
virtual IParameter &load(const Buffer &buf) = 0;
template <typename ParameterType>
inline const ParameterType &as() const {
static_assert(std::is_base_of_v<IParameter, ParameterType>,
"ParameterType must be a subtype of IParameter");
return dynamic_cast<const ParameterType &>(*this);
}
template <typename ParameterType>
inline ParameterType &as() {
static_assert(std::is_base_of_v<IParameter, ParameterType>,
"ParameterType must be a subtype of IParameter");
return dynamic_cast<ParameterType &>(*this);
}
private:
friend cereal::access;
template <typename Archive>
void serialize(Archive &ar, uint32_t version) {}
};