|
Boost Users : |
Subject: Re: [Boost-users] Boost version of Loki StaticDispatcher
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-12-15 19:05:03
Have you looked at boost::variant<>? You would need to make a few code
changes:
typedef boost::variant<double, long, int, float> ParamValue;
After doing this, you no longer need a BaseParamValue or any inheritance at
all for that matter.
class Param
{
public:
Param() : mParamValue(0) {}
Param(const string& type)
: mParamValue(create_param(type))
{
}
private:
ParamValue create_param(const string& type)
{
if (type == "LONG") return ParamValue(0L);
else if (type == "DOUBLE") return ParamValue(0.0);
//etc
}
ParamValue mParamValue;
};
class TestVisit : public boost::static_visitor<>
{
void operator()(double d) const
{
}
void operator()(long l) const
{
}
void operator(float f) const
{
}
};
class TestVisitReturn : public boost::static_visitor<bool>
{
bool operator()(double d) const
{
return true;
}
bool operator()(long l) const
{
return true;
}
bool operator(float f) const
{
return true;
}
};
Alternatively, you could templatize the operator() to accept any of the
types.
int main(int argc, char** argv)
{
ParamValue value("LONG");
boost::apply_visitor(TestVisit(), value);
bool result = boost::apply_visitor(TestVisitReturn(), value);
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net