A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove
Like the ETL? Become a patron!
or
Join the ETL community
Successor

Added successor traits to a derived class.
The template adds the ability for a class to store a successor to itself which allows the 'Chain of Responsibility' design
pattern to be implemented in a consistent fashion.

etl::successor<typename T>
T   The successor type
____________________________________________________________________________________________________

Member types


successor_type T
____________________________________________________________________________________________________

Constructor


successor()
Constructs a default successor.
has_successor() will return false.

successor(successor_type& s)
Constructs a successor from the supplied parameter s.
has_successor() will return true.
____________________________________________________________________________________________________

Member functions


void set_successor(successor_type& s)
Sets the successor to s.
has_successor() will return true.

successor_type& get_successor() const
Gets a reference to the successor.
Undefined behaviour if a successor has not been set.

bool has_successor() const
Returns true if a successor has been set.
____________________________________________________________________________________________________

Example


class Processor : public etl::successor<Processor>
{
  public:

  bool Process(const Data& data)
  {
    bool handled = CheckData(data);

    // Were we unable to handle the data?
    if (handled == false)
    {
      // Do we have a successor?
      if (has_successor())
      {
        // Call the successor.
        return get_successor().Process(data);
      }
    }

    return handled;
  }

  bool CheckData(const Data& data)
  {
    bool did_we_handle_it = false;
   
    // Check the data and set the flag to true if we handled it.
   
    return did_we_handle_it;
  }
};

Processor processor1;
Processor processor2;

proccessor1.set_successor(processor2);

successor.h