RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
io.h
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #ifndef IO_H
21 #define IO_H
22 
23 #include <list>
24 #include <mutex.h>
25 #include <settings.h>
26 #include <string>
27 #include <vector>
28 
30 
34 namespace IO
35 {
36 
40 typedef unsigned long flags_t;
41 
45 static const flags_t INPUT = 0x1;
49 static const flags_t OUTPUT = 0x2;
50 
56 typedef struct
57 {
58  std::string name;
59  std::string description;
60  flags_t flags;
61 } channel_t;
62 
63 class Block;
64 
71 class Connector : public virtual Settings::Object
72 {
73 
74  friend class Block;
75 
76 public:
77 
84  static Connector *getInstance(void);
85 
96  void foreachBlock(void (*callback)(Block *,void *),void *param);
97 
112  void foreachConnection(void (*callback)(Block *,size_t,Block *,size_t,void *),void *param);
113 
126  void connect(IO::Block *outputBlock,size_t outputChannel,IO::Block *inputBlock,size_t inputChannel);
139  void disconnect(IO::Block *outputBlock,size_t outputChannel,IO::Block *inputBlock,size_t inputChannel);
140 
152  bool connected(IO::Block *outputBlock,size_t outputChannel,IO::Block *inputBlock,size_t inputChannel);
153 
154 private:
155 
156  void doDeferred(const Settings::Object::State &);
157  void doSave(Settings::Object::State &) const;
158 
159  /*****************************************************************
160  * The constructor, destructor, and assignment operator are made *
161  * private to control instantiation of the class. *
162  *****************************************************************/
163 
164  Connector(void) : mutex(Mutex::RECURSIVE) {};
165  ~Connector(void) {};
166  Connector(const Connector &) {};
168  {
169  return *getInstance();
170  };
171 
172  static Connector *instance;
173 
174  void insertBlock(Block *);
175  void removeBlock(Block *);
176 
178  std::list<Block *> blockList;
179 
180 }; // class Connector
181 
187 class Block : public virtual Settings::Object
188 {
189 
190  friend class Connector;
191 
192 public:
193 
206  Block(std::string name,channel_t *channels,size_t size);
207  virtual ~Block(void);
208 
214  std::string getName(void) const
215  {
216  return name;
217  };
224  virtual size_t getCount(flags_t type) const;
232  virtual std::string getName(flags_t type,size_t index) const;
240  virtual std::string getDescription(flags_t type,size_t index) const;
248  virtual double getValue(flags_t type,size_t index) const;
249 
256  double input(size_t index) const;
265  double output(size_t index) const;
266 
267 protected:
268 
278  double &output(size_t index);
279 
280 private:
281 
282  struct input_t;
283  struct output_t;
284 
285  /***************************************************************
286  * Calls to connect and disconnect are designed such that they *
287  * don't have to synchronize with input() and output(). That *
288  * is to say the lists are always in a readable state. But *
289  * this means there can only be one writer, so calls to *
290  * connect() and disconnect() are serialized with mutex. *
291  ***************************************************************/
292 
293  static Mutex mutex;
294  static void connect(Block *,size_t,Block *,size_t);
295  static void disconnect(Block *,size_t,Block *,size_t);
296 
297  /*************************************************************
298  * yogi exists because "double &output(size_t n)" has to *
299  * return a reference to something if n >= outputs.size(). *
300  *************************************************************/
301 
302  static double yogi;
303 
304  struct link_t
305  {
307  size_t channel;
308  };
309 
310  struct input_t
311  {
312  std::string name;
313  std::string description;
314  std::list<struct link_t> links;
315  };
316 
317  struct output_t
318  {
319  std::string name;
320  std::string description;
321  double value;
322  std::list<struct link_t> links;
323  };
324 
325  std::string name;
326  std::vector<struct input_t> inputs;
327  std::vector<struct output_t> outputs;
328 
329 }; // class Block
330 
331 
332 } // namespace IO
333 
334 #endif // IO_H
void disconnect(IO::Block *outputBlock, size_t outputChannel, IO::Block *inputBlock, size_t inputChannel)
Definition: io.cpp:293
std::string name
Definition: io.h:58
std::string description
Definition: io.h:320
Definition: io.h:187
void doDeferred(const Settings::Object::State &)
Definition: io.cpp:343
static Connector * instance
Definition: io.h:170
double value
Definition: io.h:321
static void disconnect(Block *, size_t, Block *, size_t)
Definition: io.cpp:205
virtual std::string getDescription(flags_t type, size_t index) const
Definition: io.cpp:100
double output(size_t index) const
Definition: io.cpp:129
static Connector * getInstance(void)
Definition: io.cpp:421
void removeBlock(Block *)
Definition: io.cpp:401
std::vector< struct output_t > outputs
Definition: io.h:327
std::string description
Definition: io.h:59
Mutex mutex
Definition: io.h:177
flags_t flags
Definition: io.h:60
Definition: mutex.h:28
Block(std::string name, channel_t *channels, size_t size)
Definition: io.cpp:30
void doSave(Settings::Object::State &) const
Definition: io.cpp:360
Connection Oriented Classes.
Definition: io.h:34
void connect(IO::Block *outputBlock, size_t outputChannel, IO::Block *inputBlock, size_t inputChannel)
Definition: io.cpp:274
std::string name
Definition: io.h:312
Connector(const Connector &)
Definition: io.h:166
void insertBlock(Block *)
Definition: io.cpp:378
Connector & operator=(const Connector &)
Definition: io.h:167
std::list< Block * > blockList
Definition: io.h:178
void foreachBlock(void(*callback)(Block *, void *), void *param)
Definition: io.cpp:257
std::string getName(void) const
Definition: io.h:214
std::list< struct link_t > links
Definition: io.h:314
unsigned long flags_t
Definition: io.h:40
std::vector< struct input_t > inputs
Definition: io.h:326
std::list< struct link_t > links
Definition: io.h:322
virtual ~Block(void)
Definition: io.cpp:66
static double yogi
Definition: io.h:302
double input(size_t index) const
Definition: io.cpp:118
virtual double getValue(flags_t type, size_t index) const
Definition: io.cpp:109
~Connector(void)
Definition: io.h:165
static Mutex mutex
Definition: io.h:283
std::string name
Definition: io.h:319
static void connect(Block *, size_t, Block *, size_t)
Definition: io.cpp:151
virtual size_t getCount(flags_t type) const
Definition: io.cpp:82
Connector(void)
Definition: io.h:164
std::string description
Definition: io.h:313
void foreachConnection(void(*callback)(Block *, size_t, Block *, size_t, void *), void *param)
Definition: io.cpp:264
std::string name
Definition: io.h:325
bool connected(IO::Block *outputBlock, size_t outputChannel, IO::Block *inputBlock, size_t inputChannel)
Definition: io.cpp:312