Orocos Real-Time Toolkit  2.8.3
MarshallingService.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: FMTC Tue Mar 11 21:49:27 CET 2008 MarshallingService.cpp
3 
4  MarshallingService.cpp - description
5  -------------------
6  begin : Tue March 11 2008
7  copyright : (C) 2008 FMTC
8  email : peter.soetens@fmtc.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 
40 #include "MarshallingService.hpp"
41 #include "../TaskContext.hpp"
42 #include "../plugin/ServicePlugin.hpp"
43 
44 #include "rtt-config.h"
45 #if !defined(ORO_EMBEDDED)
46 #include "../OperationCaller.hpp"
47 #endif
48 #include "PropertyLoader.hpp"
49 
50 //ORO_SERVICE_PLUGIN( RTT::marsh::MarshallingService )
52 
53 namespace RTT {
54  using namespace detail;
55 
56  MarshallingService::shared_ptr MarshallingService::Create(TaskContext* parent){
57  shared_ptr sp(new MarshallingService(parent));
58  parent->provides()->addService( sp );
59  return sp;
60  }
61 
62  MarshallingService::MarshallingService(TaskContext* parent)
63  : Service("marshalling", parent)
64  {
65  this->doc("Property marshalling interface. Use this service to read and write properties from/to a file.");
66  this->addOperation("loadProperties",&MarshallingService::loadProperties, this)
67  .doc("Read, and create if necessary, Properties from a file.")
68  .arg("Filename","The file to read the (new) Properties from.");
69 
70  this->addOperation("storeProperties", &MarshallingService::storeProperties, this)
71  .doc("Store properties in a file and overwrite any existing content.")
72  .arg("Filename", "The file to write the Properties to.");
73 
74  this->addOperation("readProperties", &MarshallingService::readProperties, this)
75  .doc("Read all Properties from a file. Returns false if one or more properties are missing or have a wrong type in that file.").arg("Filename", "The file to read the Properties from.");
76  this->addOperation("readProperty", &MarshallingService::readProperty, this)
77  .doc("Read a single Property from a file.").arg("Name", "The name of (or the path to) the property to read.").arg("Filename", "The file to read the Properties from.");
78 
79  this->addOperation("updateProperties", &MarshallingService::updateProperties, this)
80  .doc("Read some Properties from a file. Updates only matching properties. Returns false upon type mismatch.")
81  .arg("Filename", "The file to read the Properties from.");
82  this->addOperation("updateFile", &MarshallingService::updateFile, this)
83  .doc("Write some Properties to a file, ie, only the ones that are already present in the file.").arg("Filename", "The file to write the Properties to.");
84 
85  this->addOperation("writeProperties", &MarshallingService::writeProperties, this)
86  .doc("Write all Properties to a file, but keep existing ones in that file.").arg("Filename", "The file to write the Properties to.");
87  this->addOperation("writeProperty", &MarshallingService::writeProperty, this)
88  .doc("Write a single Property to a file and keep existing ones in that file.").arg("Name", "The name of (or the path to) the property to write.").arg("Filename", "The file to write the Properties to.");
89 
90  this->addOperation("loadServiceProperties",&MarshallingService::loadServiceProperties, this)
91  .doc("Read, and create if necessary, Properties from a file.")
92  .arg("Filename","The file to read the (new) Properties from.")
93  .arg("Servicename","The Service to load the (new) Properties to.");
94  this->addOperation("storeServiceProperties", &MarshallingService::storeServiceProperties, this)
95  .doc("Store properties in a file and overwrite any existing content.")
96  .arg("Filename", "The file to write the Properties to.")
97  .arg("Servicename","The Service store the Properties of.");
98 
99  this->addOperation("readServiceProperties", &MarshallingService::readServiceProperties, this)
100  .doc("Read all Properties from a file. Returns false if one or more properties are missing or have a wrong type in that file.")
101  .arg("Filename", "The file to read the Properties from.")
102  .arg("Servicename","The Service to load the Properties to.");
103  this->addOperation("readServiceProperty", &MarshallingService::readServiceProperty, this)
104  .doc("Read a single Property from a file.")
105  .arg("Name", "The name of (or the path to) the property to read.")
106  .arg("Filename", "The file to read the Property from.")
107  .arg("Servicename","The Service to load the Property to.");
108 
109  this->addOperation("updateServiceProperties", &MarshallingService::updateServiceProperties, this)
110  .doc("Read some Properties from a file. Updates only matching properties. Returns false upon type mismatch.")
111  .arg("Filename", "The file to read the Properties from.")
112  .arg("Servicename","The Service to update the Properties of.");
113 
114  this->addOperation("updateServiceFile", &MarshallingService::updateServiceFile, this)
115  .doc("Write some Properties to a file, ie, only the ones that are already present in the file.")
116  .arg("Filename", "The file to write the Properties to.")
117  .arg("Servicename","The Service to update the Properties of.");
118 
119  this->addOperation("writeServiceProperties", &MarshallingService::writeServiceProperties, this)
120  .doc("Write all Properties to a file, but keep existing ones in that file.")
121  .arg("Filename", "The file to write the Properties to.")
122  .arg("Servicename","The Service to write the Properties of.");
123 
124  this->addOperation("writeServiceProperty", &MarshallingService::writeServiceProperty, this)
125  .doc("Write a single Property to a file and keep existing ones in that file.")
126  .arg("Name", "The name of (or the path to) the property to write.")
127  .arg("Filename", "The file to write the Properties to.")
128  .arg("Servicename","The Service to write the Property of.");
129 
130  }
131 
132  bool MarshallingService::loadProperties(const std::string& filename) const
133  {
134  PropertyLoader pl(this->getParent().get());
135  return pl.load( filename );
136  }
137 
138  bool MarshallingService::storeProperties(const std::string& filename) const
139  {
140  PropertyLoader pl(this->getParent().get());
141  return pl.store( filename );
142  }
143 
144  bool MarshallingService::readProperties(const std::string& filename) const
145  {
146  PropertyLoader pl(this->getParent().get());
147  return pl.configure( filename, true); // all
148  }
149  bool MarshallingService::updateProperties(const std::string& filename) const
150  {
151  PropertyLoader pl(this->getParent().get());
152  return pl.configure( filename, false); // not all
153  }
154  bool MarshallingService::writeProperties(const std::string& filename) const
155  {
156  PropertyLoader pl(this->getParent().get());
157  return pl.save( filename, true);
158  }
159  bool MarshallingService::updateFile(const std::string& filename) const
160  {
161  PropertyLoader pl(this->getParent().get());
162  return pl.save( filename, false);
163  }
164 
165  bool MarshallingService::readProperty(const std::string& name, const std::string& filename) {
166  PropertyLoader pl(this->getParent().get());
167  return pl.configure(filename, name);
168  }
169 
170  bool MarshallingService::writeProperty(const std::string& name, const std::string& filename) {
171  PropertyLoader pl(this->getParent().get());
172  return pl.save(filename, name);
173  }
174 
175 
176  bool MarshallingService::loadServiceProperties(const std::string& filename, const std::string& servicename) const
177  {
178  if(!this->getParent()->hasService(servicename)){
179  Logger::In(this->getName());
180  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
181  return false;
182  }
183  PropertyLoader pl(this->getParent()->provides(servicename).get());
184  return pl.load( filename );
185  }
186 
187  bool MarshallingService::storeServiceProperties(const std::string& filename, const std::string& servicename) const
188  {
189  if(!this->getParent()->hasService(servicename)){
190  Logger::In(this->getName());
191  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
192  return false;
193  }
194  PropertyLoader pl(this->getParent()->provides(servicename).get());
195  return pl.store( filename );
196  }
197 
198  bool MarshallingService::readServiceProperties(const std::string& filename, const std::string& servicename) const
199  {
200  if(!this->getParent()->hasService(servicename)){
201  Logger::In(this->getName());
202  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
203  return false;
204  }
205  PropertyLoader pl(this->getParent()->provides(servicename).get());
206  return pl.configure( filename, true); // all
207  }
208  bool MarshallingService::updateServiceProperties(const std::string& filename, const std::string& servicename) const
209  {
210  if(!this->getParent()->hasService(servicename)){
211  Logger::In(this->getName());
212  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
213  return false;
214  }
215  PropertyLoader pl(this->getParent()->provides(servicename).get());
216  return pl.configure( filename, false); // not all
217  }
218  bool MarshallingService::writeServiceProperties(const std::string& filename, const std::string& servicename) const
219  {
220  if(!this->getParent()->hasService(servicename)){
221  Logger::In(this->getName());
222  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
223  return false;
224  }
225  PropertyLoader pl(this->getParent()->provides(servicename).get());
226  return pl.save( filename, true);
227  }
228  bool MarshallingService::updateServiceFile(const std::string& filename, const std::string& servicename) const
229  {
230  if(!this->getParent()->hasService(servicename)){
231  Logger::In(this->getName());
232  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
233  return false;
234  }
235  PropertyLoader pl(this->getParent()->provides(servicename).get());
236  return pl.save( filename, false);
237  }
238 
239  bool MarshallingService::readServiceProperty(const std::string& name, const std::string& filename, const std::string& servicename) {
240  if(!this->getParent()->hasService(servicename)){
241  Logger::In(this->getName());
242  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
243  return false;
244  }
245  PropertyLoader pl(this->getParent()->provides(servicename).get());
246  return pl.configure(filename, name);
247  }
248 
249  bool MarshallingService::writeServiceProperty(const std::string& name, const std::string& filename, const std::string& servicename) {
250  if(!this->getParent()->hasService(servicename)){
251  Logger::In(this->getName());
252  log(Error)<<this->getParent()->getName()<<" does not have a service called "<<servicename<<endlog();
253  return false;
254  }
255  PropertyLoader pl(this->getParent()->provides(servicename).get());
256  return pl.save(filename, name);
257  }
258 
259 
260 }
const std::string & getName() const
Returns the name of this service instance.
Definition: Service.hpp:139
bool readServiceProperties(const std::string &filename, const std::string &servicename) const
Read the property file and &#39;refresh&#39; all the properties of a Service.
bool updateServiceProperties(const std::string &filename, const std::string &servicename) const
Read the property file and &#39;refresh&#39; some properties of the Service.
Service::shared_ptr provides()
Returns this Service, unless no shared_ptr yet exists.
Definition: Service.cpp:112
bool writeProperties(const std::string &filename) const
Write the property file with the properties of a TaskContext.
Service::shared_ptr provides()
Returns this Service.
bool configure(const std::string &filename, bool all=true) const
Read the XML cpf file and &#39;refresh&#39; the matching properties of the given Service. ...
bool loadServiceProperties(const std::string &filename, const std::string &servicename) const
Read a property file and update (or create any missing) properties of a Service.
bool updateServiceFile(const std::string &filename, const std::string &servicename) const
Write the property file with the properties of a Service, which are already present in filename...
bool load(const std::string &filename) const
Read the XML cpf file and create (or refresh the matching properties) of the given Service...
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
You can use this macro to make any Service available as a plugin.
Operation< Signature > & addOperation(Operation< Signature > &op)
Add an operation object to the interface.
Definition: Service.hpp:341
shared_ptr getParent() const
The parent is the direct parent of this service.
Definition: Service.hpp:171
bool updateProperties(const std::string &filename) const
Read the property file and &#39;refresh&#39; some properties of the TaskContext.
bool writeServiceProperties(const std::string &filename, const std::string &servicename) const
Write the property file with the properties of a Service.
Load and save property files to a Service&#39;s PropertyBag.
This class allows storage and retrieval of operations, ports, attributes and properties provided by a...
Definition: Service.hpp:93
Service which loads and saves properties of a TaskContext.
bool save(const std::string &filename, bool all=true) const
Write the XML cpf file with the properties of the given Service.
bool loadProperties(const std::string &filename) const
Read a property file and update (or create any missing) properties in the TaskContext.
bool writeProperty(const std::string &name, const std::string &filename)
Write a single property to a file.
const std::string & doc() const
Returns a descriptive text for this service.
Definition: Service.hpp:144
bool hasService(const std::string &service_name)
Check if this service has the sub-service service_name.
Definition: Service.cpp:179
bool store(const std::string &filename) const
Stores all properties of a Service in a new file or overwrite an existing one.
bool readProperty(const std::string &name, const std::string &filename)
Read a single property from a file.
bool storeServiceProperties(const std::string &filename, const std::string &servicename) const
Stores all properties of a Service in a new file or overwrite an existing one.
Notify the Logger in which &#39;module&#39; the message occured.
Definition: Logger.hpp:159
bool readServiceProperty(const std::string &name, const std::string &filename, const std::string &servicename)
Read a single property from a file.
The TaskContext is the C++ representation of an Orocos component.
Definition: TaskContext.hpp:93
bool updateFile(const std::string &filename) const
Write the property file with the properties of a TaskContext, which are already present in filename...
bool writeServiceProperty(const std::string &name, const std::string &filename, const std::string &servicename)
Write a single property to a file.
bool readProperties(const std::string &filename) const
Read the property file and &#39;refresh&#39; all the properties of the TaskContext.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
bool storeProperties(const std::string &filename) const
Stores all properties of a TaskContext in a new file or overwrite an existing one.
boost::shared_ptr< MarshallingService > shared_ptr