/*****************************************************************************

    source::worx libPiGPio
    Copyright © 2022 c.holzheuer
    chris@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Uses:

    pigpiod_if2 by joan2937, pigpio @ abyz.me.uk
    https://abyz.me.uk/rpi/pigpio

    sigslot  by 2017 Pierre-Antoine Lacaze
    https://github.com/palacaze/sigslot

***************************************************************************/


#ifndef PIGSTATE_H
#define PIGSTATE_H

#include <libpigpio.h>
#include <pigvalue.h>


/**
 * @brief The pigState class
 *  set_state ist passiv; change_state kann von 'innen' (per Gui) _oder_ von 'aussen'
 * (elektrisch) kommen, je nach Zweck der implementierung ...
 */

class LIBPIGPIO_EXPORT pigState
{

public:

    signalBool sigState;

    pigState() = default;
    explicit pigState( bool astate );
    virtual ~pigState() = default;

    bool state();
    virtual void set_state( bool newState );
    void toggle_state();

    // passiv (empfänger) oder aktiv (sender):
    // die implemetierung entscheidet
    virtual void change_state( bool newState ) = 0;

    template< typename T >
    void connect_state( T& listener )
    {
        //sigState.connect( [&](bool state) { T::change_state( state ); } );
        sigState.connect( &T::change_state, &listener );
    }

protected:

    bool _state = false;

};

template< typename V>
class LIBPIGPIO_EXPORT pigRec
{

public:

    virtual void change_x( V newValue )
    {
         std::cout << "pigRec:" << newValue << std::endl;
    }

};

template< typename V>
class LIBPIGPIO_EXPORT pigFykz
{


public:

    sigslot::signal<V> sigFykz;

    virtual void change_x( V newValue )
    {
        std::cout << "pigFykz: " << newValue << std::endl;
    }

    template< typename T >
    void connect_x( T& listener )
    {
        //sigState.connect( [&](bool state) { T::change_state( state ); } );
        sigFykz.connect( &T::change_x, &listener );
    }

};




#endif // PIGSTATE_H
