Sign in to follow this  
Waaayoff

Parameter/Value lists when values can have different types?

Recommended Posts

Waaayoff    952

Right now in my editor i have a map of parameters that the user can set. A parameter is a simple structure containing the data type and a void* pointing to the data. I inherit different types from it like FloatParam, VectorParam, etc..

 

When i need to set/retrieve the data, i always have to cast it. What would be a better way of doing this?

 

 

Another way that i can think of is to just hardcode all the parameters as class members and i would have a setParam function that consists of a giant switch statement to set the (cast) value to the correct class member

Edited by Waaayoff

Share this post


Link to post
Share on other sites
BitMaster    8651

If you need to deal with a limited, known number of types, maybe [url=http://www.boost.org/doc/libs/1_55_0/doc/html/variant/tutorial.html#variant.tutorial.basic]boost::variant[/url] or something inspired by it is an option?

Share this post


Link to post
Share on other sites
SeanMiddleditch    17565
A slightly more organized approach to the giant switch statement is pretty common. If you have a class, implementing it as a bag of random Value objects is pretty inefficient, difficult to use safely, can't be verified or analyzed properly, and is just all-around a bad idea. Use a bag of values when you need heavy dynamism, not just to make a UI editor for an existing type. There are plenty of articles, blog posts, and forum comments about how to add reflection/introspection to C++ to make it easier to build editor UIs automatically out of marked-up classes.

If you really do need the dynamism, one option is to have a separate data structure for each type. e.g., instead of:
 
class FloatValue : IValue;
class IntValue : IValue;
etc.

std::map<std::string, std::unique_ptr<IValue>> values;
use something like this instead:
 
class NaivePropertyBag final {
  std::map<std::string, float> _floatValues;
  std::map<std::string, int> _intValues;

public:
  void SetInt(std::string name, int value) {
    _floatValues.erase(name);
    _intValues[move(name)] = value;
  }

  bool GetInt(std::string const& name, int& out_value) const {
    auto iter = _intValues.find(name);
    if (iter == _intValues.end()) {
      return false;
    } else {
      out_value = 
    return iter == _intValues.end() ? 0 : iter->second;
  }

  // same for float, etc.
};
Note that that's just a general approach, not code I would in any way recommend using as a model. You can also continue using IntValue, FloatValue, etc., but just wrap the data structure using them into a safe container that does any casting or checks internally rather than exposing those to the user.

In place of the one you have, you can use a visitor pattern or a type-aware value interface, something like:
 
class ValueBase {
public:
  virtual ~ValueBase() = default;

  virtual bool IsInt() const { return false; }
  virtual bool IsFloat() const { return false; }

  virtual int GetInt() const { return 0; }
  virtual float GetFloat() const { return 0.f; }
};

class FloatValue final : public ValueBase {
  float _value;

public:
  bool IsFloat() const override { return true; }
  float GetFloat() const override { return _value; }
};

class PropertyBag final {
  std::map<std::string, std::unique_ptr<ValueBase>> _values;

public:
  void SetInt(std::string name, int value) {
    _values[std::move(name)] = std::make_unique<IntValue>(value);
  }

  void SetFloat(std::string name, float value) {
    _values[std::move(name)] = std::make_unique<FloatValue>(value);
  }

  bool GetFloat(std::string const& name) {
    auto iter = _values.find(name);
    if (iter == _values.end())
      return false;
    if (!iter->second->IsFloat())
      return false;
    return iter->second->GetFloat();
  }
};
And you can simplify that further with an enumations, allow type conversions where appropriate, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this