CorbaBufferProxy.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:20 CET 2006  CorbaBufferProxy.hpp
00003 
00004                         CorbaBufferProxy.hpp -  description
00005                            -------------------
00006     begin                : do november 02 2006
00007     copyright            : (C) 2006 FMTC
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 
00040 #ifndef ORO_CORBA_BUFFER_PROXY_HPP
00041 #define ORO_CORBA_BUFFER_PROXY_HPP
00042 
00043 #include "../BufferInterface.hpp"
00044 #include "corba.h"
00045 #include "DataFlowC.h"
00046 #include "corba.h"
00047 #ifdef CORBA_IS_TAO
00048 #include "DataFlowS.h"
00049 #endif
00050 #include "../DataSources.hpp"
00051 #include "CorbaLib.hpp"
00052 
00053 
00054 namespace RTT
00055 { namespace Corba {
00056 
00060     template<class T>
00061     class CorbaBufferProxy
00062         :public BufferInterface<T>
00063     {
00065         BufferChannel_var buf;
00066     public:
00067 
00068         typedef typename ReadInterface<T>::reference_t reference_t;
00069         typedef typename WriteInterface<T>::param_t param_t;
00070         typedef typename BufferInterface<T>::size_type size_type;
00071         typedef T value_t;
00072 
00078         CorbaBufferProxy( BufferChannel_ptr ec )
00079             : buf( BufferChannel::_duplicate(ec) )
00080         {
00081         }
00082 
00086         ~CorbaBufferProxy() {
00087         }
00088 
00089         bool Push( param_t item )
00090         {
00091             ValueDataSource<T> vds(item);
00092             vds.ref();
00093             CORBA::Any_var toset = (CORBA::Any_ptr)vds.createBlob(ORO_CORBA_PROTOCOL_ID);
00094             try {
00095                 buf->push( toset.in() );
00096             } catch (...) {
00097                 return false;
00098             }
00099 
00100             return true;
00101         }
00102 
00103         size_type Push(const std::vector<T>& items)
00104         {
00105             typename std::vector<T>::const_iterator itl( items.begin() );
00106             while ( itl != items.end() ) {
00107                 if ( this->Push( *itl ) == false )
00108                     break;
00109                 ++itl;
00110             }
00111             return (size_type)(itl - items.begin());
00112 
00113         }
00114         bool Pop( reference_t item )
00115         {
00116             CORBA::Any_var res;
00117             try {
00118                 if ( buf->pull( res.out() ) ) {
00119                     ReferenceDataSource<T> rds(item);
00120                     rds.ref();
00121                     if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &res.in() ) == false) {
00122                         Logger::log() <<Logger::Error << "Could not Convert remote value: wrong data type."<<Logger::endl;
00123                         return false;
00124                     }
00125                     return true;
00126                 }
00127             } catch(...) {
00128                 return false;
00129             }
00130             return false;
00131         }
00132 
00133         size_type Pop(std::vector<T>& items )
00134         {
00135             value_t item;
00136             if ( Pop(item) ) {
00137                 items.push_back(item);
00138                 return 1;
00139             }
00140             return 0;
00141         }
00142 
00143         value_t front() const
00144         {
00145             value_t item = value_t();
00146             // Corba's pull() is equal to Orocos' front().
00147             CORBA::Any_var res;
00148             res = buf->front();
00149             ReferenceDataSource<T> rds( item );
00150             rds.ref();
00151             if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &res.in() ) == false) {
00152                 Logger::log() <<Logger::Error << "Could not inspect remote value: wrong data type."<<Logger::endl;
00153             }
00154             return item;
00155         }
00156 
00157         size_type capacity() const {
00158             try {
00159                 return buf->capacity();
00160             } catch (...) {
00161                 return 0;
00162             }
00163         }
00164 
00165         size_type size() const {
00166             try {
00167                 return buf->size();
00168             } catch (...) {
00169                 return 0;
00170             }
00171         }
00172 
00173         void clear() {
00174             try {
00175                 buf->clear();
00176             } catch (...) {
00177             }
00178         }
00179 
00180         bool empty() const {
00181             try {
00182                 return buf->empty();
00183             } catch (...) {
00184                 return true;
00185             }
00186         }
00187 
00188         bool full() const {
00189             try {
00190                 return buf->full();
00191             } catch (...) {
00192                 return false;
00193             }
00194         }
00195     };
00196 }}
00197 
00198 #endif // BUFFERSIMPLE_HPP
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3