iCub-main
Loading...
Searching...
No Matches
PortableT.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2007-2011 RobotCub Consortium, European Commission FP6 Project IST-004370
3 * author: Arjan Gijsberts
4 * email: arjan.gijsberts@iit.it
5 * website: www.robotcub.org
6 * Permission is granted to copy, distribute, and/or modify this program
7 * under the terms of the GNU General Public License, version 2 or any
8 * later version published by the Free Software Foundation.
9 *
10 * A copy of the license can be found at
11 * http://www.robotcub.org/icub/license/gpl.txt
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 * Public License for more details
17 */
18
19#ifndef LM_PORTABLE_TEMPLATE__
20#define LM_PORTABLE_TEMPLATE__
21
22#include <stdexcept>
23#include <string>
24#include <fstream>
25#include <sstream>
26
27#include <yarp/os/Portable.h>
28#include <yarp/os/Bottle.h>
29
31
32namespace iCub {
33namespace learningmachine {
34
46template<class T>
47class PortableT : public yarp::os::Portable {
48private:
52 T* wrapped;
53
54public:
60 PortableT(T* w = (T*) 0) : wrapped(w) { }
61
68 PortableT(std::string name) : wrapped((T*) 0) {
69 this->setWrapped(name);
70 }
71
75 PortableT(const PortableT<T>& other) : wrapped(other.wrapped->clone()) { }
76
80 virtual ~PortableT() {
81 delete(this->wrapped);
82 }
83
88 if(this == &other) return *this;
89
90 // clone method is a safer bet than copy constructor or assignment
91 // operator in our case.
92 this->setWrapped(other.wrapped->clone(), true);
93
94 return *this;
95 }
96
103 bool write(yarp::os::ConnectionWriter& connection) const {
104 // return false directly if there is no machine. If not, we end up
105 // up writing things on the port, after which an exception will be
106 // thrown when accessing the machine.
107 if(!this->hasWrapped()) {
108 return false;
109 }
110 connection.appendInt32(BOTTLE_TAG_LIST);
111 connection.appendInt32(2);
112 yarp::os::Bottle nameBottle;
113 nameBottle.addString(this->wrapped->getName().c_str());
114 nameBottle.write(connection);
115 this->getWrapped().write(connection);
116
117 // for text readers
118 connection.convertTextMode();
119 return true;
120 }
121
128 bool read(yarp::os::ConnectionReader& connection) {
129 if(!connection.isValid()) {
130 return false;
131 }
132
133 connection.convertTextMode();
134 // check headers for the pair (name + actual object serialization)
135 int header = connection.expectInt32();
136 int len = connection.expectInt32();
137 if(header != BOTTLE_TAG_LIST || len != 2) {
138 return false;
139 }
140
141 // read machine identifier and use it to create object
142 yarp::os::Bottle nameBottle;
143 nameBottle.read(connection);
144 std::string name = nameBottle.get(0).asString().c_str();
145 this->setWrapped(name);
146 if(this->wrapped == (T *) 0) {
147 return false;
148 }
149
150 // call read method to construct specific object
151 bool ok = this->getWrapped().read(connection);
152 return ok;
153 }
154
161 bool writeToFile(std::string filename) {
162 std::ofstream stream(filename.c_str());
163
164 if(!stream.is_open()) {
165 throw std::runtime_error(std::string("Could not open file '") + filename + "'");
166 }
167
168 stream << this->getWrapped().getName() << std::endl;
169 stream << this->getWrapped().toString();
170
171 stream.close();
172
173 return true;
174 }
175
182 bool readFromFile(std::string filename) {
183 std::ifstream stream(filename.c_str());
184
185 if(!stream.is_open()) {
186 throw std::runtime_error(std::string("Could not open file '") + filename + "'");
187 }
188
189 std::string name;
190 stream >> name;
191
192 this->setWrapped(name);
193 std::stringstream strstr;
194 strstr << stream.rdbuf();
195 this->getWrapped().fromString(strstr.str());
196
197 return true;
198 }
199
205 bool hasWrapped() const {
206 return (this->wrapped != (T*) 0);
207 }
208
215 T& getWrapped() const {
216 if(!this->hasWrapped()) {
217 throw std::runtime_error("Attempt to retrieve inexistent wrapped object!");
218 }
219 return *(this->wrapped);
220 }
221
228 void setWrapped(T* w, bool wipe = true) {
229 if(wipe && this->hasWrapped()) {
230 delete this->wrapped;
231 this->wrapped = (T*) 0;
232 }
233 this->wrapped = w;
234 }
235
242 void setWrapped(std::string name, bool wipe = true) {
243 if(wipe && this->hasWrapped()) {
244 delete this->wrapped;
245 this->wrapped = (T*) 0;
246 }
247 this->wrapped = FactoryT<std::string, T>::instance().create(name);
248 }
249
250
251};
252
253} // learningmachine
254} // iCub
255
256#endif
static FactoryT< K, T > & instance()
An instance retrieval method that follows the Singleton pattern.
Definition FactoryT.h:86
A templated portable class intended to wrap abstract base classes.
Definition PortableT.h:47
void setWrapped(T *w, bool wipe=true)
The mutator for the wrapped object.
Definition PortableT.h:228
PortableT(const PortableT< T > &other)
Copy constructor.
Definition PortableT.h:75
virtual ~PortableT()
Destructor.
Definition PortableT.h:80
bool read(yarp::os::ConnectionReader &connection)
Reads a wrapped object from a connection.
Definition PortableT.h:128
PortableT(std::string name)
Constructor.
Definition PortableT.h:68
PortableT< T > & operator=(const PortableT< T > &other)
Assignment operator.
Definition PortableT.h:87
bool write(yarp::os::ConnectionWriter &connection) const
Writes a wrapped object to a connection.
Definition PortableT.h:103
bool hasWrapped() const
Returns true iff if there is a wrapped object.
Definition PortableT.h:205
PortableT(T *w=(T *) 0)
Constructor.
Definition PortableT.h:60
void setWrapped(std::string name, bool wipe=true)
The mutator for the wrapped object.
Definition PortableT.h:242
T & getWrapped() const
The accessor for the wrapped object.
Definition PortableT.h:215
bool writeToFile(std::string filename)
Writes a wrapped object to a file.
Definition PortableT.h:161
bool readFromFile(std::string filename)
Reads a wrapped object from a file.
Definition PortableT.h:182
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.