Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Parameter/Value lists when values can have different types?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Waaayoff   Members   


Posted 19 July 2014 - 06:01 AM

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, 19 July 2014 - 06:09 AM.

"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. "

#2 BitMaster   Members   


Posted 19 July 2014 - 06:25 AM

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

#3 SeanMiddleditch   Members   


Posted 19 July 2014 - 09:27 PM

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;

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;

  void SetInt(std::string name, int value) {
    _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 {
  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;

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

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

  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.

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.