root/branches/libffado-2.0/src/libcontrol/Element.h

Revision 1295, 4.3 kB (checked in by ppalmers, 16 years ago)

add range checks for libcontrol indexes. add support for signals w/o arguments

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifndef CONTROL_ELEMENT_H
25 #define CONTROL_ELEMENT_H
26
27 #include "debugmodule/debugmodule.h"
28
29 #include <vector>
30 #include <string>
31
32 #include "libutil/Mutex.h"
33 #include "libutil/Functors.h"
34
35 namespace Control {
36
37 class Element;
38
39 /*!
40 @brief Base class for control signal functors
41
42  This class should be subclassed to implement ffado control signal handlers.
43 */
44 class SignalFunctor
45 {
46     friend class Element;
47 public:
48     SignalFunctor(int signal_id)
49     : m_id(signal_id) {};
50     virtual ~SignalFunctor() {}
51
52     virtual void operator() () = 0;
53     virtual void operator() (int) = 0;
54 protected:
55     int m_id;
56 };
57
58 /*!
59 @brief Base class for control elements
60
61  This class should be subclassed to implement ffado control elements.
62 */
63 class Element
64 {
65 public:
66     Element(Element *);
67     Element(Element *, std::string n);
68     virtual ~Element();
69
70     virtual std::string getName() {return m_Name;};
71     virtual bool setName( std::string n )
72         { m_Name=n; return true;};
73
74     virtual std::string getLabel() {return m_Label;};
75     virtual bool setLabel( std::string n )
76         { m_Label=n; return true;};
77
78     virtual std::string getDescription() {return m_Description;};
79     virtual bool setDescription( std::string n )
80         { m_Description=n; return true;};
81
82     uint64_t getId()
83         {return m_id;};
84
85     // these allow to prevent external access to the control elements
86     // e.g. when the config tree is rebuilt
87     virtual void lockControl();
88     virtual void unlockControl();
89
90     /**
91      * Update signal handler
92      */
93     bool addSignalHandler( SignalFunctor* functor );
94     bool remSignalHandler( SignalFunctor* functor );
95
96     virtual void show();
97
98     /**
99      * set verbosity level
100      */
101     virtual void setVerboseLevel(int l);
102     virtual int getVerboseLevel() {return getDebugLevel();};
103
104 protected:
105     bool            emitSignal(int id, int value);
106     bool            emitSignal(int id);
107     Util::Mutex&    getLock();
108
109 private:
110     Util::Mutex*    m_element_lock;
111     Element*        m_parent;
112     std::string m_Name;
113     std::string m_Label;
114     std::string m_Description;
115
116     uint64_t m_id;
117     std::vector< SignalFunctor* > m_signalHandlers;
118
119 protected:
120     DECLARE_DEBUG_MODULE;
121
122 };
123 typedef std::vector<Element *> ElementVector;
124 typedef std::vector<Element *>::iterator ElementVectorIterator;
125 typedef std::vector<Element *>::const_iterator ConstElementVectorIterator;
126
127 /*!
128 @brief Base class for control containers
129
130  This class should be subclassed to implement ffado control container elements.
131  Containers are classes that can hold a set of control elements. They themselves
132  are control elements such that hierarchies can be defined using them.
133  
134  Special control containers that act on all of their children can also be
135  implemented.
136 */
137 class Container : public Element
138 {
139 public:
140     Container(Element *);
141     Container(Element *, std::string n);
142     virtual ~Container();
143
144     virtual bool addElement(Element *e);
145     virtual bool deleteElement(Element *e);
146     virtual bool clearElements()
147         {return clearElements(false);};
148     virtual bool clearElements(bool delete_pointers);
149
150     unsigned int countElements();
151
152
153     /**
154      * Returns the element vector. be sure to lock the tree while using
155      * the return value.
156      * @return
157      */
158     const ElementVector & getElementVector();
159
160     virtual void show();
161     virtual void setVerboseLevel(int l);
162
163     enum eSignals {
164         eS_Updated,
165     };
166
167 private:
168     bool deleteElementNoLock(Element *e);
169
170 protected:
171     ElementVector m_Children;
172 };
173
174
175 }; // namespace Control
176
177 #endif // CONTROL_ELEMENT_H
Note: See TracBrowser for help on using the browser.