19#ifndef LM_PORTABLE_TEMPLATE__
20#define LM_PORTABLE_TEMPLATE__
27#include <yarp/os/Portable.h>
28#include <yarp/os/Bottle.h>
33namespace learningmachine {
81 delete(this->wrapped);
88 if(
this == &other)
return *
this;
92 this->
setWrapped(other.wrapped->clone(),
true);
103 bool write(yarp::os::ConnectionWriter& connection)
const {
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);
118 connection.convertTextMode();
128 bool read(yarp::os::ConnectionReader& connection) {
129 if(!connection.isValid()) {
133 connection.convertTextMode();
135 int header = connection.expectInt32();
136 int len = connection.expectInt32();
137 if(header != BOTTLE_TAG_LIST || len != 2) {
142 yarp::os::Bottle nameBottle;
143 nameBottle.read(connection);
144 std::string name = nameBottle.get(0).asString().c_str();
146 if(this->wrapped == (
T *) 0) {
162 std::ofstream stream(filename.c_str());
164 if(!stream.is_open()) {
165 throw std::runtime_error(std::string(
"Could not open file '") + filename +
"'");
168 stream << this->
getWrapped().getName() << std::endl;
183 std::ifstream stream(filename.c_str());
185 if(!stream.is_open()) {
186 throw std::runtime_error(std::string(
"Could not open file '") + filename +
"'");
193 std::stringstream strstr;
194 strstr << stream.rdbuf();
206 return (this->wrapped != (
T*) 0);
217 throw std::runtime_error(
"Attempt to retrieve inexistent wrapped object!");
219 return *(this->wrapped);
230 delete this->wrapped;
231 this->wrapped = (
T*) 0;
244 delete this->wrapped;
245 this->wrapped = (
T*) 0;
static FactoryT< K, T > & instance()
An instance retrieval method that follows the Singleton pattern.
A templated portable class intended to wrap abstract base classes.
void setWrapped(T *w, bool wipe=true)
The mutator for the wrapped object.
PortableT(const PortableT< T > &other)
Copy constructor.
virtual ~PortableT()
Destructor.
bool read(yarp::os::ConnectionReader &connection)
Reads a wrapped object from a connection.
PortableT(std::string name)
Constructor.
PortableT< T > & operator=(const PortableT< T > &other)
Assignment operator.
bool write(yarp::os::ConnectionWriter &connection) const
Writes a wrapped object to a connection.
bool hasWrapped() const
Returns true iff if there is a wrapped object.
PortableT(T *w=(T *) 0)
Constructor.
void setWrapped(std::string name, bool wipe=true)
The mutator for the wrapped object.
T & getWrapped() const
The accessor for the wrapped object.
bool writeToFile(std::string filename)
Writes a wrapped object to a file.
bool readFromFile(std::string filename)
Reads a wrapped object from a file.
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.