doudou / orocos-rtt forked from psoetens/orocos-rtt

Orocos Real-Time Toolkit

This URL has Read+Write access

Janosch Machowinski (author)
Wed Nov 26 07:50:12 -0800 2008
Sylvain Joyeux (committer)
Fri Dec 19 07:31:38 -0800 2008
orocos-rtt / src / BufferPort.hpp
100644 605 lines (537 sloc) 20.139 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/***************************************************************************
tag: Peter Soetens Thu Mar 2 08:30:17 CET 2006 BufferPort.hpp
 
BufferPort.hpp - description
-------------------
begin : Thu March 02 2006
copyright : (C) 2006 Peter Soetens
email : peter.soetens@fmtc.be
 
***************************************************************************
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public *
* License as published by the Free Software Foundation; *
* version 2 of the License. *
* *
* As a special exception, you may use this file as part of a free *
* software library without restriction. Specifically, if other files *
* instantiate templates or use macros or inline functions from this *
* file, or you compile this file and link it with other files to *
* produce an executable, this file does not by itself cause the *
* resulting executable to be covered by the GNU General Public *
* License. This exception does not however invalidate any other *
* reasons why the executable file might be covered by the GNU General *
* Public License. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307 USA *
* *
***************************************************************************/
 
 
#ifndef ORO_EXECUTION_BUFFER_PORT_HPP
#define ORO_EXECUTION_BUFFER_PORT_HPP
 
#include <string>
#include "PortInterface.hpp"
#include "BufferConnection.hpp"
#include "OperationInterface.hpp"
#include "Method.hpp"
 
namespace RTT
{
    template<class T>
    class BufferPortBase
        : public PortInterface
    {
    protected:
        mutable typename BufferConnection<T>::shared_ptr mconn;
 
        /**
* Connect a readable buffer to this Port.
*/
        virtual bool connect( typename ConnectionInterface::shared_ptr conn) {
            if ( mconn || !conn )
                return false;
            mconn = boost::dynamic_pointer_cast< BufferConnection<T> >(conn);
            return (mconn);
        }
 
    public:
        /**
* Construct an unconnected Port to a readable buffer.
* @param name The name of this port.
*/
        BufferPortBase(const std::string& name) : PortInterface(name), mconn() {}
 
        ~BufferPortBase() {
            if (mconn)
                mconn->removePort(this);
        }
 
        /**
* Clears all contents of this buffer.
*/
        void clear() {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->clear();
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
        }
 
        /**
* Returns the actual number of items that are stored in the
* buffer.
* @return number of items.
*/
        BufferBase::size_type size() const {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->size();
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return 0;
        }
 
        /**
* Returns the maximum number of items that can be stored in the
* buffer.
* @return maximum number of items.
*/
        BufferBase::size_type capacity() const {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->capacity();
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return 0;
        }
 
        /**
* Check if this buffer is empty.
* @return true if size() == 0
*/
        bool empty() const {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->empty();
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return true;
        }
 
        /**
* Check if this buffer is full.
* @return true if size() == capacity()
*/
        bool full() const {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->full();
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return false;
        }
 
        virtual ConnectionModel getConnectionModel() const { return Buffered; }
 
        virtual const TypeInfo* getTypeInfo() const { return detail::DataSourceTypeInfo<T>::getTypeInfo(); }
 
        /**
* Provide a new implementation for the connection of this port.
* If this port is not connected, a new connection is created.
*/
        BufferPortBase<T>& operator=(BufferInterface<T>* impl);
 
        bool connected() const { return mconn; };
 
        void disconnect() {
            mconn = 0;
        }
 
        using PortInterface::connectTo;
        bool connectTo( ConnectionInterface::shared_ptr other) {
            return other && !mconn && other->addPort( this );
        }
 
        /**
* Get the buffer to read from or write to.
* @return 0 if !connected(), the buffer otherwise.
*/
        virtual BufferInterface<T>* buffer() { return mconn ? mconn->buffer() : 0; }
 
        virtual const BufferInterface<T>* buffer() const { return mconn ? mconn->buffer() : 0; }
 
        virtual ConnectionInterface::shared_ptr connection() const { return mconn; }
 
        ConnectionInterface::shared_ptr createConnection(BufferBase::shared_ptr impl) {
            typename BufferInterface<T>::shared_ptr buf = boost::dynamic_pointer_cast< BufferInterface<T> >( impl );
            ConnectionInterface::shared_ptr ci( new BufferConnection<T>( buf ) );
            ci->addPort(this);
            return ci;
        }
 
        virtual TaskObject* createPortObject() {
#ifndef ORO_EMBEDDED
            TaskObject* to = new TaskObject( this->getName() );
            to->methods()->addMethod( method("ready",&PortInterface::ready, this),
                                      "Check if this port is connected and ready for use.");
            to->methods()->addMethod(method("size", &BufferPortBase<T>::size, this),
                                     "Get the used size of the buffer.");
            to->methods()->addMethod(method("capacity", &BufferPortBase<T>::capacity, this),
                                     "Get the capacity of the buffer.");
            to->methods()->addMethod(method("empty", &BufferPortBase<T>::empty, this),
                                     "Inspect if the buffer is empty.");
            to->methods()->addMethod(method("full", &BufferPortBase<T>::full, this),
                                     "Inspect if the buffer is full.");
            to->methods()->addMethod(method("clear", &BufferPortBase<T>::clear, this),
                                     "Clear the contents of the buffer.");
            return to;
#else
            return 0;
#endif
        }
    };
 
    /**
* A Port to a readable Buffer.
* Use connection() to access the buffer. If the port is not
* connected, connection() returns null.
* @param T The type of the data of the buffer.
* @ingroup Ports
*/
    template<class T>
    class ReadBufferPort
        : public BufferPortBase<T>
    {
        using BufferPortBase<T>::mconn;
    public:
        using BufferPortBase<T>::operator=;
 
        /**
* Construct an unconnected Port to a readable buffer.
* @param name The name of this port.
*/
        ReadBufferPort(const std::string& name) : BufferPortBase<T>(name){}
 
        /**
* Pop a value from the buffer of this Port's connection.
* @param data The location where to store the popped value.
* @retval this->buffer()->Pop(data) if this->connected()
* @retval false if !this->connected()
*/
        bool Pop(T& data)
        {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->Pop(data);
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return false;
        }
 
        /**
* Get the next value to be Pop()'ed, or
* the default value if empty.
*/
        T front() const {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->front();
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return T();
        }
 
        virtual PortInterface::PortType getPortType() const { return PortInterface::ReadPort; }
 
        virtual PortInterface* clone() const {
            return new ReadBufferPort<T>( this->getName() );
        }
 
        /**
* The anti-Clone of a ReadBufferPort is a WriteBufferPort with
* prefered buffer size '1'.
*/
        virtual PortInterface* antiClone() const;
 
        virtual TaskObject* createPortObject() {
#ifndef ORO_EMBEDDED
            TaskObject* to = BufferPortBase<T>::createPortObject();
            to->methods()->addMethod(method("Pop", &ReadBufferPort<T>::Pop, this),
                                     "Pop a single value from the Buffer. Returns false if empty.",
                                     "Val", "The value returned by argument.");
            to->methods()->addMethod(method("front", &ReadBufferPort<T>::front, this),
                                     "Get the next to be popped value from the buffer. Returns default value if buffer is empty.");
            return to;
#else
            return 0;
#endif
        }
    };
 
    /**
* A Port to a writable Buffer.
* Use connection() to access the buffer. If the port is not
* connected, connection() returns null.
* @param T The type of the data of the buffer.
* @ingroup Ports
*/
    template<class T>
    class WriteBufferPort
        : public BufferPortBase<T>
    {
    protected:
        size_t buf_size;
 
        T minitial_value;
        using BufferPortBase<T>::mconn;
    public:
        using BufferPortBase<T>::operator=;
        /**
* Construct an unconnected Port to a writeable buffer.
* @param name The name of this port.
* @param initial_value The initial value of this port's connection
* when the connection is created. If this port is connected to an
* existing connection, this value is ignored.
*/
        WriteBufferPort(const std::string& name, size_t preferred_size, const T& initial_value = T() )
            : BufferPortBase<T>(name), buf_size(preferred_size), minitial_value(initial_value) {}
 
        /**
* Set the prefered buffersize before this port is
* connected. If this->connected(), this value has no
* effect.
*/
        void setBufferSize(size_t b_size) { buf_size = b_size; }
 
        /**
* Push a value into the buffer of this Port's connection.
* @param data The data to push.
* @retval this->buffer()->Push(data) if this->connected()
* @retval false if !this->connected()
*/
        bool Push(const T& data)
        {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                {
                    bool ret = mconn->buffer()->Push(data);
                    if (ret)
                    {
                        this->signal();
                        mconn->signal();
                    }
                    return ret;
                }
 
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return false;
        }
 
        /**
* Set the initial value of the port's connection.
* This value is only used when the connection is created.
* If this port is connected to an existing connection,
* the value is ignored.
*/
        void Set(const T& data)
        {
            minitial_value = data;
        }
 
        virtual PortInterface::PortType getPortType() const { return PortInterface::WritePort; }
 
        ConnectionInterface::shared_ptr createConnection(ConnectionTypes::ConnectionType con_type = ConnectionTypes::lockfree);
 
        virtual PortInterface* clone() const {
            return new WriteBufferPort<T>( this->getName(), buf_size, minitial_value );
        }
 
        virtual PortInterface* antiClone() const {
            return new ReadBufferPort<T>( this->getName() );
        }
 
        virtual TaskObject* createPortObject() {
#ifndef ORO_EMBEDDED
            TaskObject* to = BufferPortBase<T>::createPortObject();
            to->methods()->addMethod(method("Push", &WriteBufferPort<T>::Push, this),
                                     "Push a single value in the Buffer. Returns false if full().",
                                     "Val", "The value.");
            return to;
#else
            return 0;
#endif
        }
    };
 
 
    /**
* A Port to a read-write Buffer.
* Use connection() to access the buffer. If the port is not
* connected, connection() returns null.
* @param T The type of the data of the buffer.
* @ingroup Ports
* @ingroup RTTComponentInterface
*/
    template<class T>
    class BufferPort
        : public BufferPortBase<T>
    {
    protected:
        size_t buf_size;
 
        T minitial_value;
        using BufferPortBase<T>::mconn;
    public:
        using BufferPortBase<T>::operator=;
 
        typedef PortInterface::PortType PortType;
        typedef PortInterface::ConnectionModel ConnectionModel;
 
        /**
* Construct an unconnected Port to a writeable buffer.
* @param name The name of this port.
*/
        BufferPort(const std::string& name, size_t prefered_size, const T& initial_value = T())
            : BufferPortBase<T>(name), buf_size(prefered_size), minitial_value(initial_value)
            {}
 
        /**
* Set the prefered buffersize before this port is
* connected. If this->connected(), this value has no
* effect.
*/
        void setBufferSize(size_t b_size) { buf_size = b_size; }
 
        /**
* Pop a value from the buffer of this Port's connection.
* @param data The location where to store the popped value.
* @retval this->buffer()->Pop(data) if this->connected()
* @retval false if !this->connected()
*/
        bool Pop(T& data)
        {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->Pop(data);
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return false;
        }
 
        /**
* Get the next value to be Pop()'ed, or
* the default value if empty.
*/
        T front() const {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->front();
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return T();
        }
 
        /**
* Push a value into the buffer of this Port's connection.
* @param data The data to push.
* @retval this->buffer()->Push(data) if this->connected()
* @retval false if !this->connected()
*/
        bool Push(const T& data)
        {
#ifndef ORO_EMBEDDED
            try {
#endif
                if ( mconn )
                    return mconn->buffer()->Push(data);
#ifndef ORO_EMBEDDED
            } catch (...) {
                mconn = 0;
            }
#endif
            return false;
        }
 
        /**
* Set the initial value of the port's connection.
* This value is only used when the connection is created.
* If this port is connected to an existing connection,
* the value is ignored.
*/
        void Set(const T& data)
        {
            minitial_value = data;
        }
 
 
        virtual PortInterface::PortType getPortType() const { return PortInterface::ReadWritePort; }
 
        virtual PortInterface* clone() const {
            return new BufferPort<T>( this->getName(), this->buf_size, this->minitial_value );
        }
 
        virtual PortInterface* antiClone() const {
            return this->clone();
        }
 
        virtual TaskObject* createPortObject() {
#ifndef ORO_EMBEDDED
            TaskObject* to = BufferPortBase<T>::createPortObject();
            to->methods()->addMethod(method("Push", &BufferPort<T>::Push, this),
                                     "Push a single value in the Buffer. Returns false if full().",
                                     "Val", "The value.");
            to->methods()->addMethod(method("Pop", &BufferPort<T>::Pop, this),
                                     "Pop a single value from the Buffer. Returns false if empty.",
                                     "Val", "The value returned by argument.");
            to->methods()->addMethod(method("front", &BufferPort<T>::front, this),
                                     "Get the next to be popped value from the buffer. Returns default value if buffer is empty.");
            return to;
#else
            return 0;
#endif
        }
 
        ConnectionInterface::shared_ptr createConnection(ConnectionTypes::ConnectionType con_type);
    };
}
#endif
 
#ifndef ORO_BUFFER_PORT_INLINE
#define ORO_BUFFER_PORT_INLINE
 
#include "ConnectionFactory.hpp"
#include "BufferConnection.hpp"
 
namespace RTT
{
 
    template<class T>
    PortInterface* ReadBufferPort<T>::antiClone() const {
        return new WriteBufferPort<T>( this->getName(), 1 );
    }
 
    template<class T>
    BufferPortBase<T>& BufferPortBase<T>::operator=(BufferInterface<T>* impl)
    {
        if ( !mconn ) {
            ConnectionInterface::shared_ptr con = new BufferConnection<T>( typename BufferInterface<T>::shared_ptr(impl) );
            this->connectTo(con);
            con->connect();
        } else
            mconn->setImplementation(impl);
        return *this;
    }
 
    template<class T>
    ConnectionInterface::shared_ptr WriteBufferPort<T>::createConnection(ConnectionTypes::ConnectionType con_type )
        {
            ConnectionFactory<T> cf;
            ConnectionInterface::shared_ptr res = cf.createBuffer(buf_size, minitial_value, con_type);
            res->addPort(this);
            return res;
        }
 
    template<class T>
    ConnectionInterface::shared_ptr BufferPort<T>::createConnection(ConnectionTypes::ConnectionType con_type )
        {
            ConnectionFactory<T> cf;
            ConnectionInterface::shared_ptr res = cf.createBuffer(buf_size, minitial_value, con_type);
            res->addPort(this);
            return res;
        }
}
#endif