Parameter/Value lists when values can have different types?

Started by
1 comment, last by SeanMiddleditch 9 years, 9 months ago

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

"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Advertisement

If you need to deal with a limited, known number of types, maybe boost::variant or something inspired by it is an option?

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.

Sean Middleditch – Game Systems Engineer – Join my team!

This topic is closed to new replies.

Advertisement