CCA
OutputPort.h
Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of CCA project
00004  *
00005  * Copyright (C) 2011 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de>
00006  *
00007  * This file may be licensed under the terms of the
00008  * GNU Lesser General Public License Version 3 (the ``LGPL''),
00009  * or (at your option) any later version.
00010  *
00011  * Software distributed under the License is distributed
00012  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
00013  * express or implied. See the LGPL for the specific language
00014  * governing rights and limitations.
00015  *
00016  * You should have received a copy of the LGPL along with this
00017  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
00018  * or write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The development of this software was supported by:
00022  *   CoR-Lab, Research Institute for Cognition and Robotics
00023  *     Bielefeld University
00024  *
00025  * ============================================================ */
00026 
00027 #pragma once
00028 
00029 #include <iostream>
00030 #include <typeinfo>
00031 
00032 #include <boost/shared_ptr.hpp>
00033 
00034 #include <rsb/Informer.h>
00035 #include <rsb/Factory.h>
00036 
00037 #include "cca/Port.h"
00038 #include "cca/dto/DataTransferObject.h"
00039 
00040 namespace cca {
00041 
00042 class OutputPortBase;
00043 typedef ::boost::shared_ptr<OutputPortBase> OutputPortPtr;
00044 
00048 class OutputPortBase: public Port {
00049 public:
00050     OutputPortBase(bool optional = false);
00051     ~OutputPortBase();
00052 };
00053 
00057 template<class DATATYPE>
00058 class OutputPort: public OutputPortBase {
00059 
00060 public:
00061 
00063     typedef ::boost::shared_ptr<DATATYPE> DataPtr;
00064 
00066     typedef ::boost::shared_ptr<OutputPort<DATATYPE> > Ptr;
00067 
00074     OutputPort(bool optional = Port::MANDATORY) :
00075             OutputPortBase(optional) {
00076     }
00077 
00082     virtual ~OutputPort() {
00083     }
00084 
00085     void publish(DataPtr data) {
00086 
00087         if (!this->configured) {
00088             throw ::std::runtime_error("OutputPort::publish() Output port not yet configured.");
00089         }
00090 
00091         try {
00092             this->informer->uncheckedPublish(data);
00093         } catch (::std::runtime_error &e) {
00094             std::cout
00095                     << "[OutputPort] caught exception while trying to publish: "
00096                     << e.what() << std::endl;
00097         } catch (::std::exception &e) {
00098             std::cout
00099                     << "[OutputPort] caught exception while trying to publish: "
00100                     << e.what() << std::endl;
00101         }
00102     }
00103 
00108     static Ptr create() {
00109         return Ptr(new OutputPort<DATATYPE>());
00110     }
00111 
00115     virtual ::std::string print() const {
00116         ::std::ostringstream outstream(::std::ostringstream::out);
00117         outstream << "<" << rsc::runtime::typeName<DATATYPE>() << ">";
00118         if (configured) {
00119             outstream << " " << getConfig()->print();
00120             outstream << "(" << getScopePtr()->toString() << ")";
00121         } else {
00122             outstream << " <unconfigured>";
00123         }
00124         if (optional) {
00125             outstream << " [optional]";
00126         }
00127         return outstream.str();
00128     }
00129 
00133     virtual void configureSpecifics() {
00134         this->informer = rsb::getFactory().createInformer < DATATYPE
00135                 > (*(config->getScopePtr()), config->rsbConfig());
00136     }
00137 
00138 protected:
00143     OutputPort(OutputPort &port);
00144 
00149     void operator=(const OutputPort &port);
00150 
00151     typename ::rsb::Informer<DATATYPE>::Ptr informer;
00152 };
00153 
00154 }