CCA
|
StateConverter.cpp
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 #include "StateConverter.h" 00028 00029 #include <rsb/converter/SerializationException.h> 00030 #include <rsb/converter/ProtocolBufferConverter.h> 00031 00032 #include <rst/cbse/ComponentState.pb.h> 00033 00034 using namespace std; 00035 using namespace nemo; 00036 00037 namespace cca { 00038 00039 ComponentStateConverter::ComponentStateConverter() : 00040 rsb::converter::Converter<string>( 00041 rsc::runtime::typeName<rst::cbse::ComponentState>(), 00042 RSB_TYPE_TAG(cca::ComponentState)), converter() { 00043 converter = boost::shared_ptr<rsb::converter::Converter<string> >( 00044 new rsb::converter::ProtocolBufferConverter< 00045 rst::cbse::ComponentState>); 00046 } 00047 00048 ComponentStateConverter::~ComponentStateConverter() { 00049 } 00050 00051 string ComponentStateConverter::getWireSchema() const { 00052 return converter->getWireSchema(); 00053 } 00054 00055 string ComponentStateConverter::serialize(const rsb::AnnotatedData &data, 00056 string &wire) { 00057 00058 // Cast to original domain type 00059 boost::shared_ptr<cca::ComponentState> domain = boost::static_pointer_cast< 00060 cca::ComponentState>(data.second); 00061 00062 // Fill protocol buffer object 00063 rst::cbse::ComponentState proto; 00064 00065 // 1. reserve() 00066 // 2. std::copy(begin, begin+n) // oder memcpy 00067 if (*domain == *ComponentState::PAUSED()) { 00068 proto.set_state(rst::cbse::ComponentState::PAUSED); 00069 } else if (*domain == *ComponentState::EXECUTION()) { 00070 proto.set_state(rst::cbse::ComponentState::EXECUTION); 00071 } else if (*domain == *ComponentState::ONLINELEARNING()) { 00072 proto.set_state(rst::cbse::ComponentState::ONLINELEARNING); 00073 } else if (*domain == *ComponentState::OFFLINELEARNING()) { 00074 proto.set_state(rst::cbse::ComponentState::OFFLINELEARNING); 00075 } else if (*domain == *ComponentState::RESET()) { 00076 proto.set_state(rst::cbse::ComponentState::RESET); 00077 } else if (*domain == *ComponentState::LOADED()) { 00078 proto.set_state(rst::cbse::ComponentState::LOADED); 00079 } else if (*domain == *ComponentState::PERSISTED()) { 00080 proto.set_state(rst::cbse::ComponentState::PERSISTED); 00081 } else { 00082 proto.set_state(rst::cbse::ComponentState::OFF); 00083 } 00084 00085 // Use embedded ProtoBuf converter for serialization to wire 00086 return converter->serialize( 00087 make_pair(rsc::runtime::typeName<rst::cbse::ComponentState>(), 00088 boost::shared_ptr<void>(&proto, rsc::misc::NullDeleter())), 00089 wire); 00090 } 00091 00092 rsb::AnnotatedData ComponentStateConverter::deserialize( 00093 const std::string &wireType, const std::string &wire) { 00094 00095 // Deserialize and cast to specific ProtoBuf type 00096 boost::shared_ptr<rst::cbse::ComponentState> proto = 00097 boost::static_pointer_cast<rst::cbse::ComponentState>( 00098 converter->deserialize(wireType, wire).second); 00099 00100 // Read domain data from ProtoBuf 00101 ComponentStatePtr domain; 00102 switch (proto->state()) { 00103 case rst::cbse::ComponentState::EXECUTION: 00104 domain = ComponentState::EXECUTION(); 00105 break; 00106 case rst::cbse::ComponentState::LOADED: 00107 domain = ComponentState::LOADED(); 00108 break; 00109 case rst::cbse::ComponentState::OFFLINELEARNING: 00110 domain = ComponentState::OFFLINELEARNING(); 00111 break; 00112 case rst::cbse::ComponentState::ONLINELEARNING: 00113 domain = ComponentState::ONLINELEARNING(); 00114 break; 00115 case rst::cbse::ComponentState::PAUSED: 00116 domain = ComponentState::PAUSED(); 00117 break; 00118 case rst::cbse::ComponentState::PERSISTED: 00119 domain = ComponentState::PERSISTED(); 00120 break; 00121 case rst::cbse::ComponentState::RESET: 00122 domain = ComponentState::RESET(); 00123 break; 00124 case rst::cbse::ComponentState::ON: 00125 domain = ComponentState::EXECUTION(); 00126 break; 00127 case rst::cbse::ComponentState::OFF: 00128 domain = ComponentState::STOPPED(); 00129 break; 00130 default: 00131 domain = ComponentState::STOPPED(); 00132 break; 00133 } 00134 00135 return rsb::AnnotatedData(getDataType(), domain); 00136 00137 } 00138 00139 }