YARP
Yet Another Robot Platform
ResourceFinder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_RESOURCEFINDER_H
11 #define YARP_OS_RESOURCEFINDER_H
12 
13 #include <yarp/os/Bottle.h>
14 #include <yarp/os/Property.h>
16 #include <yarp/os/Searchable.h>
17 #include <yarp/os/Value.h>
18 
19 #include <string>
20 
21 namespace yarp {
22 namespace os {
23 
33 {
34 public:
36 
37  ResourceFinder(const ResourceFinder& alt);
38 
39  virtual ~ResourceFinder();
40 
41  const ResourceFinder& operator=(const ResourceFinder& alt);
42 
43 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
44 
55  bool setVerbose(bool verbose = true);
56 
67  bool setQuiet(bool quiet = true);
68 #endif // YARP_NO_DEPRECATED
69 
81  bool configure(int argc, char* argv[], bool skipFirstArgument = true);
82 
91  bool setDefaultContext(const std::string& contextName)
92  {
93  clearContext();
94  return addContext(contextName);
95  }
96 
103  bool setDefault(const std::string& key, const std::string& val);
104 
111  bool setDefault(const std::string& key, std::int32_t val);
112 
119  bool setDefault(const std::string& key, yarp::conf::float64_t val);
120 
126  bool setDefault(const std::string& key, const yarp::os::Value& val);
127 
134  bool setDefaultConfigFile(const std::string& fname)
135  {
136  return setDefault("from", fname);
137  }
138 
155  std::string findFile(const std::string& name);
156 
164  std::string findFileByName(const std::string& name);
165 
181  std::string findPath(const std::string& name);
182 
197  yarp::os::Bottle findPaths(const std::string& name);
198 
204  std::string findPath();
205 
206 
214  std::string getContext();
215 
222  yarp::os::Bottle getContexts();
223 
224  // Searchable interface
225  bool check(const std::string& key) const override;
226  Value& find(const std::string& key) const override;
227  Bottle& findGroup(const std::string& key) const override;
228  bool isNull() const override;
229  std::string toString() const override;
230 
231 
240  virtual ResourceFinder findNestedResourceFinder(const std::string& key);
241 
242 
243  bool isConfigured() const
244  {
245  return isConfiguredFlag;
246  }
247 
256  static ResourceFinder& getResourceFinderSingleton();
257 
258  using Searchable::check;
259  using Searchable::findGroup;
260 
261 
262  /* YARP 2.4 changes begin */
263 
282  static std::string getDataHome()
283  {
284  return getDataHomeWithPossibleCreation(true);
285  }
286 
287 
294  static std::string getDataHomeNoCreate()
295  {
296  return getDataHomeWithPossibleCreation(false);
297  }
298 
314  static std::string getConfigHome()
315  {
316  return getConfigHomeWithPossibleCreation(true);
317  }
318 
325  static std::string getConfigHomeNoCreate()
326  {
327  return getConfigHomeWithPossibleCreation(false);
328  }
329 
335  std::string getHomeContextPath();
336 
342  std::string getHomeRobotPath();
343 
359  static Bottle getDataDirs();
360 
376  static Bottle getConfigDirs();
377 
378  yarp::os::Bottle findPaths(const std::string& name,
379  const ResourceFinderOptions& options);
380 
381  std::string findPath(const std::string& name,
382  const ResourceFinderOptions& options);
383 
384  std::string findFile(const std::string& name,
385  const ResourceFinderOptions& options);
386 
387  std::string findFileByName(const std::string& name,
388  const ResourceFinderOptions& options);
389 
390  bool readConfig(Property& config,
391  const std::string& key,
392  const ResourceFinderOptions& options);
393 
394  /* YARP 2.4 changes end */
395 
396 private:
397  // this might be useful, but is not in spec
398  bool addContext(const std::string& appName);
399 
400  // this might be useful, but is not in spec
401  bool clearContext();
402 
403  bool owned;
404  bool nullConfig;
405  bool isConfiguredFlag;
406  yarp::os::Property config;
407 
408  static std::string getDataHomeWithPossibleCreation(bool mayCreate);
409  static std::string getConfigHomeWithPossibleCreation(bool mayCreate);
410  static std::string createIfAbsent(bool mayCreate, const std::string& path);
411 
412 #ifndef DOXYGEN_SHOULD_SKIP_THIS
413 private:
414  class Private;
415  Private* mPriv;
416  ResourceFinder(Searchable& data, Private* altPriv);
417 #endif // DOXYGEN_SHOULD_SKIP_THIS
418 };
419 
420 } // namespace os
421 } // namespace yarp
422 
423 #endif // YARP_OS_RESOURCEFINDER_H
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:72
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:68
yarp::os::Searchable::findGroup
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
yarp::os::ResourceFinderOptions
These options are loosely based on http://wiki.icub.org/wiki/YARP_ResourceFinder.
Definition: ResourceFinderOptions.h:27
Searchable.h
yarp::os::ResourceFinder::setDefaultContext
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
Definition: ResourceFinder.h:91
yarp::os::ResourceFinder::getConfigHome
static std::string getConfigHome()
Location where user config files are stored.
Definition: ResourceFinder.h:314
Property.h
ResourceFinderOptions.h
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
yarp::os::ResourceFinder::isConfigured
bool isConfigured() const
Definition: ResourceFinder.h:243
yarp::os::ResourceFinder::getDataHomeNoCreate
static std::string getDataHomeNoCreate()
Variant of getDataHome that will never create the directory returned.
Definition: ResourceFinder.h:294
toString
std::string toString(const T &value)
convert an arbitrary type to string.
Definition: fakeMotionControl.cpp:121
yarp
Definition: environment.h:18
YARP_os_API
#define YARP_os_API
Definition: api.h:19
yarp::os::ResourceFinder::getConfigHomeNoCreate
static std::string getConfigHomeNoCreate()
Variant of getConfigHome that will never create the directory returned.
Definition: ResourceFinder.h:325
yarp::conf::float64_t
double float64_t
Definition: numeric.h:51
yarp::os::ResourceFinder::setDefaultConfigFile
bool setDefaultConfigFile(const std::string &fname)
Provide a default value for the configuration file (can be overridden from command line with the –fro...
Definition: ResourceFinder.h:134
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:46
yarp::os::ResourceFinder::getDataHome
static std::string getDataHome()
Location where user data files are stored.
Definition: ResourceFinder.h:282
Bottle.h
Value.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:34
yarp::os::ResourceFinder
Helper class for finding config files and other external resources.
Definition: ResourceFinder.h:32