~marcustomlinson/dspatchables/trunk

« back to all changes in this revision

Viewing changes to include/dspatch/DspSignal.h

  • Committer: Marcus Tomlinson
  • Date: 2015-04-05 22:25:24 UTC
  • mfrom: (1.1.10 trunk)
  • Revision ID: marcus.tomlinson@canonical.com-20150405222524-urntp725s5hbk2im
Merged add_basic_components

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************
 
2
DSPatch - Cross-Platform, Object-Oriented, Flow-Based Programming Library
 
3
Copyright (c) 2012-2015 Marcus Tomlinson
 
4
 
 
5
This file is part of DSPatch.
 
6
 
 
7
GNU Lesser General Public License Usage
 
8
This file may be used under the terms of the GNU Lesser General Public
 
9
License version 3.0 as published by the Free Software Foundation and
 
10
appearing in the file LGPLv3.txt included in the packaging of this
 
11
file. Please review the following information to ensure the GNU Lesser
 
12
General Public License version 3.0 requirements will be met:
 
13
http://www.gnu.org/copyleft/lgpl.html.
 
14
 
 
15
Other Usage
 
16
Alternatively, this file may be used in accordance with the terms and
 
17
conditions contained in a signed written agreement between you and
 
18
Marcus Tomlinson.
 
19
 
 
20
DSPatch is distributed in the hope that it will be useful,
 
21
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
23
************************************************************************/
 
24
 
 
25
#ifndef DSPSIGNAL_H
 
26
#define DSPSIGNAL_H
 
27
 
 
28
//-------------------------------------------------------------------------------------------------
 
29
 
 
30
#include <string>
 
31
#include <vector>
 
32
 
 
33
#include <dspatch/DspRunType.h>
 
34
#include <dspatch/DspThread.h>
 
35
 
 
36
//=================================================================================================
 
37
/// Value container used to carry data between components
 
38
 
 
39
/**
 
40
DspComponents process and transfer data between each other in the form of "signals" via
 
41
interconnecting wires. The DspSignal class holds a single value that can be dynamically typed at
 
42
runtime. Furthermore, a DspSignal has the ability to change it's data type at any point during
 
43
program execution. This is designed such that a signal bus can hold any number of different typed
 
44
variables, as well as to allow for a variable to dynamically change it's type when needed -this can
 
45
be useful for inputs that accept a number of different data types (E.g. Varying sample size in an
 
46
audio buffer: array of byte / int / float).
 
47
*/
 
48
 
 
49
class DLLEXPORT DspSignal
 
50
{
 
51
public:
 
52
    DspSignal(std::string signalName = "");
 
53
 
 
54
    virtual ~DspSignal();
 
55
 
 
56
    template <class ValueType>
 
57
    bool SetValue(ValueType const& newValue);
 
58
 
 
59
    template <class ValueType>
 
60
    bool GetValue(ValueType& returnValue) const;
 
61
 
 
62
    template <class ValueType>
 
63
    ValueType const* GetValue() const;
 
64
 
 
65
    bool SetSignal(DspSignal const* newSignal);
 
66
 
 
67
    void ClearValue();
 
68
 
 
69
    std::type_info const& GetSignalType() const;
 
70
 
 
71
    std::string GetSignalName() const;
 
72
 
 
73
private:
 
74
    DspRunType _signalValue;
 
75
    std::string _signalName;
 
76
    bool _valueAvailable;
 
77
};
 
78
 
 
79
//=================================================================================================
 
80
 
 
81
template <class ValueType>
 
82
bool DspSignal::SetValue(ValueType const& newValue)
 
83
{
 
84
    _signalValue = newValue;
 
85
    _valueAvailable = true;
 
86
    return true;
 
87
}
 
88
 
 
89
//-------------------------------------------------------------------------------------------------
 
90
 
 
91
template <class ValueType>
 
92
bool DspSignal::GetValue(ValueType& returnValue) const
 
93
{
 
94
    if (_valueAvailable)
 
95
    {
 
96
        ValueType const* returnValuePtr = DspRunType::RunTypeCast<ValueType>(&_signalValue);
 
97
        if (returnValuePtr != NULL)
 
98
        {
 
99
            returnValue = *returnValuePtr;
 
100
            return true;
 
101
        }
 
102
        else
 
103
        {
 
104
            return false;  // incorrect type matching
 
105
        }
 
106
    }
 
107
    else
 
108
    {
 
109
        return false;  // no value available
 
110
    }
 
111
}
 
112
 
 
113
//-------------------------------------------------------------------------------------------------
 
114
 
 
115
template <class ValueType>
 
116
ValueType const* DspSignal::GetValue() const
 
117
{
 
118
    if (_valueAvailable)
 
119
    {
 
120
        return DspRunType::RunTypeCast<ValueType>(&_signalValue);
 
121
    }
 
122
    else
 
123
    {
 
124
        return NULL;  // no value available
 
125
    }
 
126
}
 
127
 
 
128
//=================================================================================================
 
129
 
 
130
#endif  // DSPSIGNAL_H