RTXI  2.4
The Real-Time eXperiment Interface Documentation
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;
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  // Who uses this??
157  //void doDeferred(const Settings::Object::State &);
158  //void doSave(Settings::Object::State &) const;
159 
160  /*****************************************************************
161  * The constructor, destructor, and assignment operator are made *
162  * private to control instantiation of the class. *
163  *****************************************************************/
164 
165  Connector(void) : mutex(Mutex::RECURSIVE) {};
166  ~Connector(void) {};
167  Connector(const Connector &) {};
168  Connector &operator=(const Connector &)
169  {
170  return *getInstance();
171  };
172 
173  static Connector *instance;
174 
175  void insertBlock(Block *);
176  void removeBlock(Block *);
177 
178  Mutex mutex;
179  std::list<Block *> blockList;
180 
181 }; // class Connector
182 
188 class Block : public virtual Settings::Object
189 {
190 
191  friend class Connector;
192 
193 public:
194 
207  Block(std::string name,channel_t *channels,size_t size);
208  virtual ~Block(void);
209 
215  std::string getName(void) const
216  {
217  return name;
218  };
225  virtual size_t getCount(flags_t type) const;
233  virtual std::string getName(flags_t type,size_t index) const;
241  virtual std::string getDescription(flags_t type,size_t index) const;
249  virtual double getValue(flags_t type,size_t index) const;
250 
257  double input(size_t index) const;
266  double output(size_t index) const;
267 
268 protected:
269 
279  double &output(size_t index);
280 
281 private:
282 
283  struct input_t;
284  struct output_t;
285 
286  /***************************************************************
287  * Calls to connect and disconnect are designed such that they *
288  * don't have to synchronize with input() and output(). That *
289  * is to say the lists are always in a readable state. But *
290  * this means there can only be one writer, so calls to *
291  * connect() and disconnect() are serialized with mutex. *
292  ***************************************************************/
293 
294  static Mutex mutex;
295  static void connect(Block *,size_t,Block *,size_t);
296  static void disconnect(Block *,size_t,Block *,size_t);
297 
298  /*************************************************************
299  * yogi exists because "double &output(size_t n)" has to *
300  * return a reference to something if n >= outputs.size(). *
301  *************************************************************/
302 
303  static double yogi;
304 
305  struct link_t
306  {
307  Block *block;
308  size_t channel;
309  };
310 
311  struct input_t
312  {
313  std::string name;
314  std::string description;
315  std::list<struct link_t> links;
316  };
317 
318  struct output_t
319  {
320  std::string name;
321  std::string description;
322  double value;
323  std::list<struct link_t> links;
324  };
325 
326  std::string name;
327  std::vector<struct input_t> inputs;
328  std::vector<struct output_t> outputs;
329 
330 }; // class Block
331 
332 
333 } // namespace IO
334 
335 #endif // IO_H
IO::Connector::disconnect
void disconnect(IO::Block *outputBlock, size_t outputChannel, IO::Block *inputBlock, size_t inputChannel)
Definition: io.cpp:293
Connector
Definition: connector.h:33
IO::Connector::connect
void connect(IO::Block *outputBlock, size_t outputChannel, IO::Block *inputBlock, size_t inputChannel)
Definition: io.cpp:274
IO::channel_t::description
std::string description
Definition: io.h:59
settings.h
IO::flags_t
unsigned long flags_t
Definition: io.h:40
IO::Connector::getInstance
static Connector * getInstance(void)
Definition: io.cpp:421
IO::channel_t::flags
flags_t flags
Definition: io.h:60
IO::Block::~Block
virtual ~Block(void)
Definition: io.cpp:66
IO
Connection Oriented Classes.
Definition: io.h:34
IO::Block::getValue
virtual double getValue(flags_t type, size_t index) const
Definition: io.cpp:109
IO::Connector::connected
bool connected(IO::Block *outputBlock, size_t outputChannel, IO::Block *inputBlock, size_t inputChannel)
Definition: io.cpp:312
IO::Block::getCount
virtual size_t getCount(flags_t type) const
Definition: io.cpp:82
IO::Block::getName
std::string getName(void) const
Definition: io.h:215
Mutex
Definition: mutex.h:28
IO::Block::Block
Block(std::string name, channel_t *channels, size_t size)
Definition: io.cpp:30
IO::Block::output
double output(size_t index) const
Definition: io.cpp:129
IO::Block
Definition: io.h:188
IO::Connector::foreachBlock
void foreachBlock(void(*callback)(Block *, void *), void *param)
Definition: io.cpp:257
IO::channel_t
Definition: io.h:56
IO::Connector::Block
friend class Block
Definition: io.h:74
IO::Block::input
double input(size_t index) const
Definition: io.cpp:118
mutex.h
Settings::Object
Definition: settings.h:43
IO::Block::getDescription
virtual std::string getDescription(flags_t type, size_t index) const
Definition: io.cpp:100
IO::channel_t::name
std::string name
Definition: io.h:58
IO::Connector::foreachConnection
void foreachConnection(void(*callback)(Block *, size_t, Block *, size_t, void *), void *param)
Definition: io.cpp:264