c++ - 从基类指针获取模板派生类的值
问题描述
我有一个允许用户编辑 glsl 的统一变量值的工具,我需要将所有数据存储在 std::vector 中。
由于所有变量的值都有不同的变量类型(vec2、vec3、vec4、mat2...等),因此将它们存储在单个容器中是一个挑战。我决定采用这种方法
class BaseData
{
public:
};
template <typename T>
class Data: public BaseData
{
public:
Data(TypeEnum enumType, T valueIN): value(valueIN), type(enumType)
{
}
T GetValue()
{
return value;
}
TypeEnum GetType()
{
return type;
}
private:
T value;
TypeEnum type;
};
class Material
{
Material(std::vector<Base*> valueVec)
{
for(auto i : valueVec)
{
switch(i->GetType())
{
case BaseColor:
SetBaseColor(i->GetValue());//need something like this
break;
case BaseTexture:
SetBaseTexture(i->GetValue());//need something like this
break;
case EmissionColor:
SetEmissionFactor(i->GetValue());//need something like this
break;
case EmissionTexture:
SetEmissionTexture(i->GetValue());//need something like this
break;
case Material_nProps_NormalTexture:
SetNormalMap(i->GetValue());//need something like this
}
}
}
}
int main()
{
std::vector<BaseData*> uniformValue;
uniformValue.push_back(new Data(BaseColor, glm::vec4(1,2,3,4)));
uniformValue.push_back(new Data(BaseTexture, 0));
uniformValue.push_back(new Data(EmissionColor, glm::vec3(1,1,1)));
uniformValue.push_back(new Data(BaseTexture, 1));
Material PBR(uniformValue);
}
但问题是,现在如何从基指针获取值而不将其转换为正确的派生类型指针?
解决方案
如果您不想强制转换,可以使用命令模式,其中每个 GLSL 属性类接收指向 Material 对象的指针并执行适当的操作。每个命令都需要访问 Material 的一些内部结构。下面的代码说明了这一点。(请注意,为了简单起见,所有内容都是公开的)。
struct Material; // forward declaration
struct GlslPropertyBase {
virtual ~GlslPropertyBase() {}
virtual void Execute(Material* m) = 0;
};
struct Material {
void SetBaseColor(Vec3 col) { /* Do something */ }
void SetBaseTexture(GLuint uid) { /* Do something */ }
Material(std::vector<GlslPropertyBase*> properties) {
for (GlslPropertyBase* property : properties)
property->Execute(this);
}
};
struct GlSlBaseColor : GlslPropertyBase {
Vec3 color;
GlSlBaseColor(float r, float g, float b) : color(r, g, b) {}
void Execute(Material* m) { m->SetBaseColor(color); }
};
struct GlSlBaseTexture : GlslPropertyBase {
GLuint uid;
GlSlBaseTexture(GLuint uid) : uid(uid) {}
void Execute(Material* m) { m->SetBaseTexture(uid); }
};
int main() {
std::vector<GlslPropertyBase*> properties;
properties.push_back(new GlSlBaseColor(1, 2, 3));
properties.push_back(new GlSlBaseTexture(1));
Material PBR(properties);
// delete heap objects...
}
这只是一种简单的方法(和实现)来处理存储在单个向量中的异构 glsl 属性,无需强制转换,可能是@francesco 在他的答案末尾所建议的。
推荐阅读
- c# - 客户端使用 SOAP 请求发送文件附件
- php - 如何根据用户选择功能更改在 echo 脚本中使用的给定 $variable 值
- sql - ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter#quote 是否可以防止 SQL 注入?
- svn - svn diff 失败并显示“无法显示:文件标记为二进制类型。” 即使文件的 svn:mime-type 属性设置为“text/plain”
- java - 是每个 JVM 的 Java WatchService 还是一个应用程序可以启动多个?
- java - 如何使用底页从片段回调到活动
- java - Hibernate 在尝试检索数据时抛出错误(分离的实体)
- javascript - 在 React 中不会触发 play() 方法(除非有点击事件)
- angular - 根据条件添加角度模块
- java - Xpath 在 Chrome 浏览器中工作,但相同的 Xpath 在 Firefox 浏览器中不工作