Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Parameter/Value lists when values can have different types?


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   -  Reputation: 783

Like
0Likes
Like

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

Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4437

Like
0Likes
Like

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   -  Reputation: 7257

Like
2Likes
Like

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




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.



PARTNERS