Changeset 269 in pacpusframework for trunk


Ignore:
Timestamp:
Mar 13, 2014, 10:41:25 AM (11 years ago)
Author:
Marek Kurdej
Message:

Major: InputOutputInterface callback method with 1 or 2 arguments: 1st can be const or non-const, second (PacpusEvent) can be optional.

Location:
trunk/include/Pacpus/kernel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/Pacpus/kernel/InputOutputInterface.h

    r258 r269  
    1818    typedef T data_type;
    1919    typedef C component_type;
    20     typedef void (C::*process_data_function_type)(const T &);
    21 
    22     InputInterface(QString name, C * component, process_data_function_type processMethod)
    23         : InputInterfaceBase(name, component, component)
    24         , method(processMethod)
    25     {
     20    typedef void (C::*process_data_function_type_with_event)(T&, PacpusEvent);
     21    typedef void (C::*process_data_function_type)(T&);
     22    typedef void (C::*process_const_data_function_type_with_event)(T const&, PacpusEvent);
     23    typedef void (C::*process_const_data_function_type)(T const&);
     24
     25    InputInterface(QString name, C* component, process_data_function_type_with_event processingMethod)
     26        : InputInterfaceBase(name, component, component)
     27    {
     28        mProcessingMethod.fe = processingMethod;
     29    }
     30   
     31    InputInterface(QString name, C* component, process_data_function_type processingMethod)
     32        : InputInterfaceBase(name, component, component)
     33    {
     34        mProcessingMethod.f = processingMethod;
     35    }
     36   
     37    InputInterface(QString name, C* component, process_const_data_function_type_with_event processingMethod)
     38        : InputInterfaceBase(name, component, component)
     39    {
     40        mProcessingMethod.cfe = processingMethod;
     41    }
     42   
     43    InputInterface(QString name, C* component, process_const_data_function_type processingMethod)
     44        : InputInterfaceBase(name, component, component)
     45    {
     46        mProcessingMethod.cf = processingMethod;
    2647    }
    2748
     
    3556    }
    3657
    37     const std::type_info & getDataType() const
     58    std::type_info const& getDataType() const
    3859    {
    3960        return typeid(T);
     
    4768
    4869    // FIXME: what's the purpose of this function?
    49     void customEvent(QEvent * event)
    50     {
     70    void customEvent(QEvent* event)
     71    {
     72        static road_timerange_t const kMaximumBoundedTimeRange = 500;
     73
     74        if (!event) {
     75            LOG_WARN("null event");
     76            return;
     77        }
     78       
    5179        // check that component has been started
    5280        if ((NULL == getComponent()) || (!getComponent()->isActive())) {
     
    5785        LOG_TRACE("Receiver: " << getSignature());
    5886
    59         //PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *>(event);
    60         PacpusEvent * pacpusEvent = dynamic_cast<PacpusEvent *>(event);
     87        //PacpusTypedEvent<T>* typedEvent = dynamic_cast<PacpusTypedEvent<T> *>(event);
     88        PacpusEvent* pacpusEvent = dynamic_cast<PacpusEvent*>(event);
    6189        if (!pacpusEvent) {
    6290            LOG_WARN("dynamic_cast failed: not a PacpusEvent");
    6391            return;
    6492        }
    65         PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *>(pacpusEvent);
     93        PacpusTypedEvent<T>* typedEvent = dynamic_cast<PacpusTypedEvent<T>*>(pacpusEvent);
    6694        if (!typedEvent) {
    6795            LOG_WARN("dynamic_cast failed: incompatible event types");
     
    7199        switch (event->type()) {
    72100        case TYPED_EVENT:
    73             if (TimeBounded == readingMode() && typedEvent->timerange() < 500) {
    74                 LOG_WARN("Incorrect TimeRange (0), switch to NeverSkip");
     101            if (TimeBounded == readingMode() && typedEvent->timerange() < kMaximumBoundedTimeRange) {
     102                LOG_WARN("Incorrect TimeRange (< " << kMaximumBoundedTimeRange << "), switching to NeverSkip mode");
    75103                readingMode() = NeverSkip;
    76104            }
     
    83111                }
    84112
    85                 (dynamic_cast<C*>(component())->*method)(typedEvent->data());
     113                mProcessingMethod.invoke(component(), typedEvent->data(), *typedEvent);
    86114                break;
    87115
    88116            case GetLast:
    89                 (dynamic_cast<C*>(component())->*method)(typedEvent->data());
     117                mProcessingMethod.invoke(component(), typedEvent->data(), *typedEvent);
     118
    90119                // delete all remaining events
    91120                QCoreApplication::removePostedEvents(this, TYPED_EVENT);
     
    93122
    94123            case NeverSkip:
    95                 (dynamic_cast<C*>(component())->*method)(typedEvent->data());
     124                mProcessingMethod.invoke(component(), typedEvent->data(), *typedEvent);
    96125                break;
    97126
     
    111140    }
    112141
    113     // TODO for Pulling mode (not yet implemented !!!)
    114     T & getData()
     142    // TODO: pull mode (NOT YET IMPLEMENTED!!!)
     143    T& getData()
    115144    {
    116145        T data;
    117         // TODO ask output data;
     146        // TODO: ask the output data;
    118147        return data;
    119148    }
    120149
    121150protected:
    122     process_data_function_type method;
     151    struct ProcessingMethod
     152    {
     153        ProcessingMethod()
     154        {
     155            fe = NULL;
     156            f = NULL;
     157            cfe = NULL;
     158            cf = NULL;
     159        }
     160
     161        void invoke(ComponentBase* component, T& data, PacpusEvent event)
     162        {
     163            C* comp = dynamic_cast<C*>(component);
     164            if (NULL == comp) {
     165                LOG_WARN("NULL component");
     166                return;
     167            }
     168            if (fe) {
     169                (comp->*fe)(data, event);
     170            } else if (f) {
     171                (comp->*f)(data);
     172            } else if (cfe) {
     173                (comp->*cfe)(data, event);
     174            } else if (cf) {
     175                (comp->*cf)(data);
     176            } else {
     177                LOG_WARN("no method to invoke");
     178            }
     179        }
     180           
     181        process_data_function_type_with_event fe;
     182        process_data_function_type f;
     183        process_const_data_function_type_with_event cfe;
     184        process_const_data_function_type cf;
     185    };
     186
     187    ProcessingMethod mProcessingMethod;
    123188};
    124189
     
    131196    typedef C component_type;
    132197
    133     OutputInterface(QString name, C * component)
     198    OutputInterface(QString name, C* component)
    134199        : OutputInterfaceBase(name, component, component)
    135200    {}
     
    139204
    140205    /// Send data through a typed output
    141     void send(const T & data, road_time_t t = road_time(), road_timerange_t tr = 0);
     206    void send(T const& data, road_time_t t = road_time(), road_timerange_t tr = 0);
    142207   
    143208    std::size_t getDataSize() const
     
    146211    }
    147212
    148     const std::type_info & getDataType() const
     213    std::type_info const& getDataType() const
    149214    {
    150215        return typeid(T);
     
    153218
    154219template <typename T, class C>
    155 void OutputInterface<T, C>::send(const T & data, road_time_t t, road_timerange_t tr)
    156 {
    157     // FIXME Data Shared
     220void OutputInterface<T, C>::send(T const& data, road_time_t t, road_timerange_t tr)
     221{
     222    // FIXME: use shared data
    158223    //QSharedPointer<T> sharedPointer = new T(data);
    159224
    160225    for (QList<ConnectionBase>::iterator it = connections().begin(), itend = connections().end(); it != itend; ++it) {
    161         // Qt documentatino:
     226        // Qt documentation:
    162227        //  The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted.
    163228        //  It is not safe to access the event after it has been posted.   
    164         QEvent * newEvent = new PacpusTypedEvent<T>(TYPED_EVENT, data, t, tr);
     229        QEvent* pacpusEvent = new PacpusTypedEvent<T>(TYPED_EVENT, data, t, tr);
    165230        QCoreApplication::postEvent(
    166231            it->getInterface(),
    167             newEvent,
     232            pacpusEvent,
    168233            it->getPriority()
    169234        );
     
    173238
    174239template <typename T1, typename T2, class C>
    175 bool checkedSend(OutputInterface<T1, C> * sender, const T2 & data, road_time_t t = road_time(), road_timerange_t tr = 0);
     240bool checkedSend(OutputInterface<T1, C>* sender, T2 const& data, road_time_t t = road_time(), road_timerange_t tr = 0);
    176241
    177242template <typename T1, typename T2, class C>
    178 bool checkedSend(OutputInterface<T1, C> * sender, const T2 & data, road_time_t t, road_timerange_t tr)
     243bool checkedSend(OutputInterface<T1, C>* sender, T2 const& data, road_time_t t, road_timerange_t tr)
    179244{
    180245    if (sender && sender->hasConnection()) {
  • trunk/include/Pacpus/kernel/PacpusEvent.h

    r206 r269  
    4949    }
    5050
    51     road_time_t & time()
     51    road_time_t& time()
    5252    {
    5353        return m_time;
    5454    }
    5555
    56     const road_time_t & time() const
     56    road_time_t const& time() const
    5757    {
    5858        return m_time;
    5959    }
    6060
    61     road_timerange_t & timerange()
     61    road_timerange_t& timerange()
    6262    {
    6363        return m_timerange;
    6464    }
    6565
    66     const road_timerange_t & timerange() const
     66    road_timerange_t const& timerange() const
    6767    {
    6868        return m_timerange;
    6969    }
     70
    7071protected:
    7172    road_time_t m_time;
     
    121122    /// when T is convertible to S
    122123    template <typename S>
    123     PacpusTypedEvent(const PacpusTypedEvent<S> & other)
     124    PacpusTypedEvent(PacpusTypedEvent<S> const& other)
    124125        : PacpusEvent(static_cast<PacpusEventType>(other.type()), other.time(), other.timerange())
    125126        , TypedData<T>(other.data())
     
    146147{
    147148public:
    148     GenericData(const char * data, size_t size)
     149    GenericData(char const* data, size_t size)
    149150        : m_data(NULL)
    150151        , m_dataSize(size)
Note: See TracChangeset for help on using the changeset viewer.