YARP
Yet Another Robot Platform
ResourceFinder.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_OS_RESOURCEFINDER_H
8 #define YARP_OS_RESOURCEFINDER_H
9 
10 #include <yarp/os/Bottle.h>
11 #include <yarp/os/Property.h>
13 #include <yarp/os/Searchable.h>
14 #include <yarp/os/Value.h>
15 
16 #include <string>
17 
18 namespace yarp {
19 namespace os {
20 
30 {
31 public:
33 
34  ResourceFinder(const ResourceFinder& alt);
35 
36  virtual ~ResourceFinder();
37 
38  const ResourceFinder& operator=(const ResourceFinder& alt);
39 
51  bool configure(int argc, char* argv[], bool skipFirstArgument = true);
52 
61  bool setDefaultContext(const std::string& contextName)
62  {
63  clearContext();
64  return addContext(contextName);
65  }
66 
73  bool setDefault(const std::string& key, const std::string& val);
74 
81  bool setDefault(const std::string& key, std::int32_t val);
82 
89  bool setDefault(const std::string& key, yarp::conf::float64_t val);
90 
96  bool setDefault(const std::string& key, const yarp::os::Value& val);
97 
104  bool setDefaultConfigFile(const std::string& fname)
105  {
106  return setDefault("from", fname);
107  }
108 
125  std::string findFile(const std::string& name);
126 
134  std::string findFileByName(const std::string& name);
135 
151  std::string findPath(const std::string& name);
152 
167  yarp::os::Bottle findPaths(const std::string& name);
168 
174  std::string findPath();
175 
176 
184  std::string getContext();
185 
192  yarp::os::Bottle getContexts();
193 
194  // Searchable interface
195  bool check(const std::string& key) const override;
196  Value& find(const std::string& key) const override;
197  Bottle& findGroup(const std::string& key) const override;
198  bool isNull() const override;
199  std::string toString() const override;
200 
201 
210  virtual ResourceFinder findNestedResourceFinder(const std::string& key);
211 
212 
213  bool isConfigured() const
214  {
215  return m_isConfiguredFlag;
216  }
217 
226  static ResourceFinder& getResourceFinderSingleton();
227 
228  using Searchable::check;
229  using Searchable::findGroup;
230 
234  std::string getHomeContextPath();
235 
239  std::string getHomeRobotPath();
240 
241  yarp::os::Bottle findPaths(const std::string& name,
242  const ResourceFinderOptions& options);
243 
244  std::string findPath(const std::string& name,
245  const ResourceFinderOptions& options);
246 
247  std::string findFile(const std::string& name,
248  const ResourceFinderOptions& options);
249 
250  std::string findFileByName(const std::string& name,
251  const ResourceFinderOptions& options);
252 
253  bool readConfig(Property& config,
254  const std::string& key,
255  const ResourceFinderOptions& options);
256 
257 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
269  bool setVerbose(bool verbose = true);
270 
281  bool setQuiet(bool quiet = true);
282 #endif // YARP_NO_DEPRECATED
283 
284 #ifndef YARP_NO_DEPRECATED // SINCE YARP 3.5
303  YARP_DEPRECATED_MSG("Use yarp::conf::dirs::data_home() + yarp::os::mkdir_p() instead")
304  static std::string getDataHome()
305  {
306  return getDataHomeWithPossibleCreation(true);
307  }
308 
309 
316  YARP_DEPRECATED_MSG("Use yarp::conf::dirs::data_home() instead")
317  static std::string getDataHomeNoCreate()
318  {
319  return getDataHomeWithPossibleCreation(false);
320  }
321 
337  YARP_DEPRECATED_MSG("Use yarp::conf::dirs::config_home() + yarp::os::mkdir_p() instead")
338  static std::string getConfigHome()
339  {
340  return getConfigHomeWithPossibleCreation(true);
341  }
342 
349  YARP_DEPRECATED_MSG("Use yarp::conf::dirs::config_home() instead")
350  static std::string getConfigHomeNoCreate()
351  {
352  return getConfigHomeWithPossibleCreation(false);
353  }
354 
370  YARP_DEPRECATED_MSG("Use yarp::conf::dirs::yarpdatadirs() instead")
371  static Bottle getDataDirs();
372 
388  YARP_DEPRECATED_MSG("Use yarp::conf::dirs::config_dirs() instead")
389  static Bottle getConfigDirs();
390 #endif // YARP_NO_DEPRECATED
391 
392 
393 private:
394  // this might be useful, but is not in spec
395  bool addContext(const std::string& appName);
396 
397  // this might be useful, but is not in spec
398  bool clearContext();
399 
400  bool m_owned;
401  bool m_nullConfig;
402  bool m_isConfiguredFlag;
403  yarp::os::Property m_configprop;
404 
405  static std::string createIfAbsent(bool mayCreate, const std::string& path);
406 
407 #ifndef YARP_NO_DEPRECATED // SINCE YARP 3.5
408  static std::string getDataHomeWithPossibleCreation(bool mayCreate);
409  static std::string getConfigHomeWithPossibleCreation(bool mayCreate);
410 #endif
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
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
A class for storing options and configuration information.
Definition: Property.h:34
These options are loosely based on http://wiki.icub.org/wiki/YARP_ResourceFinder.
Helper class for finding config files and other external resources.
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
static std::string getDataHome()
Location where user data files are stored.
bool setDefaultConfigFile(const std::string &fname)
Provide a default value for the configuration file (can be overridden from command line with the –fro...
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
A single value (typically within a Bottle).
Definition: Value.h:45
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
std::string toString(const T &value)
convert an arbitrary type to string.
double float64_t
Definition: numeric.h:77
std::string findGroup(const robotinterface::ParamList &list, const std::string &name)
Definition: Types.cpp:47
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_os_API
Definition: api.h:18