consumer.hpp 1.59 KB
Newer Older
Leander Schulten's avatar
Leander Schulten committed
1
2
3
4
#ifndef COMSUMER_H
#define COMSUMER_H

#include "types.h"
5
#include <cstring>
Leander Schulten's avatar
Leander Schulten committed
6
7
8
9
10
11
12

namespace Modules{

    /**
     * @brief The Consumer class represents a Consumer like a driver for dmx or leds
     * Treiber erben von dieser Klasse und zeigen die Entsprechenden Daten an.
     */
13
    class Consumer:public PropertyBase, public InputDataConsumer, public Named{
Leander Schulten's avatar
Leander Schulten committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    public:
        Consumer(const ValueType inputDataType):InputDataConsumer(inputDataType){}
        virtual void setInputLength(unsigned int)=0;
        virtual void start()=0;
        /**
         * @brief show zeigt die eingabedaten an
         */
        virtual void show() = 0;
        virtual void doStep(time_diff_t diff)=0;
        virtual ~Consumer() = default;
    };

    template<typename Type>
    class TypedConsumer : public Consumer{
    protected:
        Type * input = nullptr;
        unsigned int inputLength = 0;
    public:
        TypedConsumer():Consumer(typeToEnum<Type>()){}
        virtual void setInputData(void * data, unsigned int index, unsigned int length)override{
            if(index<inputLength){
                const auto l = index+length>inputLength?inputLength-index:length;
                std::memcpy(input+index,data,l*sizeof (Type));
            }
        }
        virtual void setInputLength(unsigned int length)override{
            if(inputLength!=length){
                delete [] input;
                inputLength = length;
                input = new Type[inputLength];
            }
        }
        virtual unsigned int getInputLength()const override final{return inputLength;}
    };
}
#endif // COMSUMER_H