iop_xdevice.h
Go to the documentation of this file.
1 /** @file iop_xdevice.h Virtual device for interface definition */
2 
3 /*
4  FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2008, Thomas Moor
7  Exclusive copyright is granted to Klaus Schmidt
8 
9 */
10 
11 
12 
13 #ifndef FAUDES_XDEVICE_H
14 #define FAUDES_XDEVICE_H
15 
16 #include "corefaudes.h"
17 #include "tp_include.h"
18 #include "iop_vdevice.h"
19 
20 
21 
22 
23 
24 namespace faudes {
25 
26 
27 
28 
29 /**
30  * Container of devices.
31  *
32  * The xDevices is a container of vDevices. Input readings are
33  * combined in a union fashion over all participating devices,
34  * output writings are dispatched to the relevant device. Formally,
35  * the xDevice class is derived from the vDevice class to provide the
36  * same interaface to a simulator. Ie, the application does not
37  * need to know whether is acts on a single vDevice or an xDevice. This
38  * is also true for configuration from file, but of cause not for programatical
39  * configuration. An xDevice may not be configured to have individual
40  * outputs or inputs, but to hold particular vDevices.
41  *
42  * Technical detail: the xDevice uses the vDevice interface to register a
43  * common event fifo buffer and a common condition variable. Thus, the xDevice only works
44  * with devices that support this configuration feature.
45  *
46  * @ingroup IODevicePlugin
47  */
48 
49 class FAUDES_API xDevice : public vDevice {
50 
52 
53 
54  public:
55 
56 
57  /** Iterator for const access to individual devices*/
58  typedef std::vector<vDevice*>::const_iterator Iterator;
59 
60 
61  /**
62  * Default constructor
63  */
64  xDevice(void);
65 
66  /**
67  * Construct on heap from token reader.
68  *
69  * This constructor examines the token strean, determines the coressponding
70  * class and constructs the device on the heap. Todo: the implementation
71  * of this function is a hack, there must be proper
72  * solution to this issue.
73  *
74  * @param rTr
75  * TokenReader to read from
76  * @return
77  * vDevice pointer
78  *
79  * @exception Exception
80  * - token mismatch (id 552)
81  * - IO errors (id 1)
82  */
83  static xDevice* FromTokenReader(TokenReader& rTr);
84 
85  /**
86  * Construct on heap from file.
87  *
88  * This constructor examines the file, determines the coressponding
89  * class and constructs the device on the heap.
90  *
91  * @param rFileName
92  * Filename
93  * @return
94  * vDevice pointer
95  *
96  * @exception Exception
97  * - token mismatch (id 552)
98  * - IO errors (id 1)
99  */
100  static xDevice* FromFile(const std::string& rFileName);
101 
102  /**
103  * Explicit destructor.
104  */
105  virtual ~xDevice(void);
106 
107 
108  /**
109  * Set Iterator to first device
110  *
111  * @return
112  * Iterator to first device
113  */
114  Iterator Begin(void) const { return mDevices.begin() ; };
115 
116  /**
117  * Set Iterator to last device
118  *
119  * @return
120  * Iterator to last device
121  */
122  Iterator End(void) const { return mDevices.end() ; };
123 
124  /**
125  *
126  * Get number of devices
127  */
128  Idx Size(void) const { return (Idx) mDevices.size(); };
129 
130 
131  /**
132  * Insert a new device.
133  * An xDevice is configured by inserting vDevices. An xDevice cannot be
134  * configured by individual output/sensro events.
135  *
136  * Note: by inserting a device into an xdevice its ownership
137  * will be assumed by the xdevice and therewith the right to destoy
138  * it.
139  *
140  * @param device
141  * Pointer to vDevice
142  */
143  void Insert(vDevice* device);
144 
145  /**
146  * Insert a new device by Filename
147  *
148  * An xDevice is configured by inserting vDevices. An xDevice cannot be
149  * configured by individual output/sensro events.
150  *
151  * Note: by inserting a device into an xdevice its ownership
152  * will be assumed by the xdevice and therewith the right to destoy
153  *
154  * For information on how to write such a file see the "iodevice" - tutorial
155  *
156  * @param rFileName
157  * Configuration-file to build up device
158  */
159 
160  void Insert(const std::string& rFileName);
161 
162 
163 
164  /**
165  * Dummy. An xDevice does not provide event based configuration. Use Insert instead. This function
166  * will throw an execption
167  */
168  void Configure(Idx event, const AttributeDeviceEvent& attr);
169 
170  /**
171  * An xDevice does not provide event based configuration. Use Insert instead.
172  * This function will throw an execption
173  */
174  void Configure(const EventSet& rEvents);
175 
176  /**
177  *
178  * Build up internal data structures. I.e. (event-idx,int) - map
179  */
180  void Compile(void) ;
181 
182 
183  /**
184  * Clear all configuarations and destroy existing devices
185  *
186  */
187  void Clear(void);
188 
189 
190  /** reset all dynamic state, i.e. call reset on each device */
191  void Reset(void);
192 
193  /** Test for reset request */
194  bool ResetRequest(void);
195 
196  /**
197  * Activate the devices. This function enables output execution and input reading.
198  *
199  * @exception Exception
200  * - Not yet configured (id 551)
201  */
202  void Start(void);
203 
204  /**
205  * Deactivate the device. This function disables output execution and input reading.
206  *
207  */
208  void Stop(void);
209 
210  /**
211  * Get status. This function returns the current status of the device.
212  * In derived classes that use background threads for input reading etc,
213  * a device may change its status without notice. Dont forget to reimplement
214  * this method with an appropriate mutex.
215  *
216  */
217  DeviceState Status(void);
218 
219  /**
220  * Run output command on relevant device.
221  *
222  * @exception Exception
223  * - unknown output event (id 65)
224  *
225  */
226  void WriteOutput(Idx output);
227 
228  /**
229  * Flush any pending IO Operations.
230  *
231  * Pass on flush output buffers to participating devices.
232  *
233  */
234  virtual void FlushOutputs(void);
235 
236 
237  /**
238  * Report global fauDES-time
239  * Note: per convention we take the time of the first
240  * device inserted in xDevice as global time
241  *
242  * @return
243  * fauDES-time
244  */
245  Time::Type CurrentTime(void);
246 
247  /**
248  * Report global fauDES-time
249  * Note: per convention we take the time of the first
250  * device inserted in xDevice as global time
251  *
252  * @return
253  * fauDES-time
254  */
255  long int CurrentTimeMs(void);
256 
257  /**
258  * Set physical time in ftu.
259  *
260  * @param now
261  * now in faudes time units (ftu).
262  */
263  virtual void CurrentTime(Time::Type now);
264 
265  /**
266  * Set physical time in ms.
267  *
268  * @param nowms
269  * now in msec
270  */
271  virtual void CurrentTimeMs(long int nowms);
272 
273 
274 
275 protected:
276 
277  // internal iterator type
278  typedef std::vector<vDevice*>::iterator iterator;
279 
280 
281  /**
282  * Actual method to read device configuration from tokenreader.
283  *
284  * DoRead basically calls the DoWrite-function of all devices which are part of xDevice
285  *
286  * @param rTr
287  * TokenReader to read from
288  * @param rLabel
289  * Section to read
290  * @param pContext
291  * Read context to provide contextual information
292  *
293  * @exception Exception
294  * - IO error (id 1)
295  */
296  virtual void DoReadConfiguration(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
297 
298  /**
299  * Write the device patrameters to a TokenWriter.
300  *
301  *
302  * @param rTw
303  * Reference to TokenWriter
304  * @param rLabel
305  * Label of section to write
306  * @param pContext
307  * Read context to provide contextual information
308  * @exception Exception
309  * - IO errors (id 2)
310  */
311  virtual void DoWriteConfiguration(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
312 
313  /** Return first Device */
314  iterator Begin(void) { return mDevices.begin() ; };
315 
316  /** Return last Device */
317  iterator End(void) { return mDevices.end() ; };
318 
319  /** Vector of member-devices */
320  std::vector<vDevice*> mDevices;
321 
322  /** Vector of member-device-names*/
323  std::vector<std::string> mDeviceNames;
324 
325  /** Compiled data: Input map to map input idx to device no */
326  std::map<Idx,int> mInputToDevice;
327 
328  /** Compiled data: Output Map to map input idx to device no */
329  std::map<Idx,int> mOutputToDevice;
330 
331  /** Current device state: remember last stop/down command */
333 
334 
335 };
336 
337 
338 
339 }
340 
341 
342 #endif
343 
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
faudes type declaration macro
Definition: cfl_types.h:867
Attribute for the configuration of a input or output mapping.
Definition: iop_vdevice.h:68
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
Int Type
Datatype for point on time axis.
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
Virtual base class to define the interface for event io.
Definition: iop_vdevice.h:261
Container of devices.
Definition: iop_xdevice.h:49
Iterator End(void) const
Set Iterator to last device.
Definition: iop_xdevice.h:122
bool lastCommandWasStart
Current device state: remember last stop/down command.
Definition: iop_xdevice.h:332
std::vector< vDevice * > mDevices
Vector of member-devices.
Definition: iop_xdevice.h:317
iterator Begin(void)
Return first Device.
Definition: iop_xdevice.h:314
Iterator Begin(void) const
Set Iterator to first device.
Definition: iop_xdevice.h:114
Idx Size(void) const
Get number of devices.
Definition: iop_xdevice.h:128
std::map< Idx, int > mInputToDevice
Compiled data: Input map to map input idx to device no.
Definition: iop_xdevice.h:326
iterator End(void)
Return last Device.
Definition: iop_xdevice.h:317
std::map< Idx, int > mOutputToDevice
Compiled data: Output Map to map input idx to device no.
Definition: iop_xdevice.h:329
std::vector< vDevice * >::const_iterator Iterator
Iterator for const access to individual devices.
Definition: iop_xdevice.h:58
std::vector< std::string > mDeviceNames
Vector of member-device-names.
Definition: iop_xdevice.h:323
std::vector< vDevice * >::iterator iterator
Definition: iop_xdevice.h:278
Includes all libFAUDES headers, no plugins.
Virtual device for interface definition
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
Include timed plugin headers.

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen