Less insane interface design for a trigger component for T types? Disclaimer: I just asked this on the regular SE, but was suggested to post it in here since it's kind of a code review.

I've recently been playing around with component frameworks, and I'm trying to create a basic trigger component. The component should be able to take one (or more) pair of values that can be compared against each other, triggering upon certain conditions.

The value pair should be of a generic type, I suggest an interface like IComparable. The TriggerComponent itself cannot be templated (i think?), as the component system must register it and would not be able to do so without supplying type information via angle brackets. Here's my current prototype version. It works, but some awkward issues that I'll explain below:

  /* interface IComparable.h */
  class IComparable
  {
  public:
    virtual ~IComparable() {}

    virtual bool lessOrEqual(const IComparable&amp; other) const = 0;
  };

  /* Derived type container, intended for primitive values like int, float etc */
  template&lt;class T&gt;
  class NumberType : public IComparable
  {
  public:
    NumberType(const T&amp; number) : m_number(number) {}
    bool lessOrEqual(const IComparable&amp; other) const
    {
      NumberType&amp; nt = (NumberType&amp;)other;
      return m_number &lt;= nt.m_number;
    }

    const T&amp; m_number;
  };

  /* Component with threshold/trigger values made up by number types, but also objects of IComparable */
  class TriggerComponent : public artemis::Component
  {
  public:
    TriggerComponent(const IComparable&amp; threshold, const IComparable&amp; triggerValue)
      : m_thresholdValue(threshold), m_triggerValue(triggerValue) {}


    const IComparable&amp; threshold() const { return m_thresholdValue; }
    const IComparable&amp; triggerValue() const { return m_triggerValue; }

    void execute() const { qDebug() &lt;&lt; "execute!"; }

  private:
    const IComparable&amp; m_thresholdValue;
    const IComparable&amp; m_triggerValue;
  };


The triggerValue parameter should be a reference as it is supposed to be monitored and checked against the threshold. Threshold should ideally be a copy, and not refer to anything. I would ideally like to have IComparable/NumberType as structs as it would make it easy to reference or copy as needed, but I don't think it is possible due to the pure interface.. As they're both reference values, I'm in a lot of cases forced to allocate heap memory, which i'd like to avoid for simple types.

If I had a cube in a 3D world with an ongoing scaling reduction, here's how I'd use the TriggerComponent to detect when it's X component reaches 1.0f:

    Entity* cubeEntity = /* omitted */
    ScaleComponent* scaleComp = /* omitted*/
    float* cubeScaleMinThreshold = new float(1.0f);
    NumberType&lt;float&gt;* threshold = new NumberType&lt;float&gt;(*cubeScaleMinThreshold);
    NumberType&lt;float&gt;* currScale = new NumberType&lt;float&gt;(scaleComp-&gt;getx());  
    cubeEntity .addComponent(new TriggerComponent(*threshold, *currScale));


I need the flexibility to create components on the fly, and I'd like to pass stack-created values as copies rather than passing raw pointer refs, but I'm not exactly sure how I can make it work in my current interface setup. I don't see how I can make generic comparisons between values (of same type) without one, but that seems to also require members to not be abstract value types.

I would be very appreciative on possible changes to make the design less insane. Maybe I'm just overlooking an obvious simple approach? 

PS. I know raw pointers set up like I just did, leak like a 2 year old. It's to show why the design is terrible! :)
Having thought a little bit on the problem, I've come up with a solution I think satisfy my goal without being too awkward or introducing gotchas with raw pointers and ownership. Below is my updated code and as always, I would appreciate any feedback or tips on improvement.

  class IComparable
  {
  public:
    virtual ~IComparable() {}
    virtual bool lessOrEqual() const = 0;
    virtual bool greaterOrEqual() const = 0;
    virtual bool equal() const = 0;
    virtual bool lessThan() const = 0;
    virtual bool greaterThan() const = 0;
  };

  /* trigger/threshold set of any type supporting the comparison operators. Can create new set by adhering to the interface*/
  template&lt;class T&gt;
  class ComparableTriggerSet : public IComparable
  {
  public:
    ComparableTriggerSet(std::shared_ptr&lt;T&gt; threshold, const T&amp; trigger) : m_threshold(threshold), m_trigger(trigger) {}
    bool lessOrEqual() const { return m_trigger &lt;= *m_threshold; }
    bool greaterOrEqual() const { return m_trigger &gt;= *m_threshold; }
    bool equal() const { return m_trigger == *m_threshold; }
    bool lessThan() const { return m_trigger &lt; *m_threshold; }
    bool greaterThan() const { return m_trigger &gt; *m_threshold; }

  private:
    std::shared_ptr&lt;T&gt; m_threshold;
    const T&amp; m_trigger;
  };

  /* supported operators */
  enum CompareOperator { LessOrEqual, GreaterOrEqual, Equal, LessThan, GreaterThan };

  /* TriggerComponent - getting checked at regular intervals by the TriggerSystem */
  class TriggerComponent : public artemis::Component
  {
  public:
    TriggerComponent(std::shared_ptr&lt;IComparable&gt; comparableTriggerSet, CompareOperator compareOperator)
      : m_comparableTriggerSet(comparableTriggerSet), m_compareOperator(compareOperator) {}


    void check() const
    {
      bool condition = false;
      switch(m_compareOperator)
      {
        case LessOrEqual:
          condition = m_comparableTriggerSet-&gt;lessOrEqual();
          break;
        case GreaterOrEqual:
          condition = m_comparableTriggerSet-&gt;greaterOrEqual();
          break;
        case Equal:
          condition = m_comparableTriggerSet-&gt;equal();
          break;
        case LessThan:
          condition = m_comparableTriggerSet-&gt;lessThan();
          break;
        case GreaterThan:
          condition = m_comparableTriggerSet-&gt;greaterThan();
          break;
        default:
          assert(false &amp;&amp; "unsupported operator enum");
          break;
      }

      if(condition)
        execute();
    }

    /* supposed to invoke an event or maybe call a script or something similar */
    void execute() const
    {
      qDebug() &lt;&lt; "execute!";
    }

  private:
    std::shared_ptr&lt;IComparable&gt; m_comparableTriggerSet;
    CompareOperator m_compareOperator;
  };


I would then add the component in my executable like so:

  artemis::Entity&amp; ent = /* omitted */
  ScaleComponent* scaleComp = /* omitted */
  std::shared_ptr&lt;float&gt; threshold = std::make_shared&lt;float&gt;(1.0f);
  std::shared_ptr&lt;ComparableTriggerSet&lt;float&gt;&gt; triggerSet = std::make_shared&lt;ComparableTriggerSet&lt;float&gt;&gt;(threshold, scaleComp-&gt;getx());
  ent.addComponent(new TriggerComponent(triggerSet , CompareOperator::LessThan));


I think the updated code solves my original issues fairly well. I have thus far not encountered any problems with it, and I can add TriggerComponents without worry of whether threshold value is getting off the stack, cleaned up properly etc.