Falaise  4.0.1
SuperNEMO Software Toolkit
path.h
Go to the documentation of this file.
1 #ifndef FALAISE_PATH_H
2 #define FALAISE_PATH_H
3 
4 #include <exception>
5 #include <ostream>
6 #include <string>
7 
8 namespace falaise {
9 namespace config {
10 //! Exception for paths that cannot be resolved by datatools::utils
11 class invalid_path_error : public std::logic_error {
12  using std::logic_error::logic_error;
13 };
14 
15 //! Class representing a filesystem path as held by a @ref property_set
16 /*!
17  * Filesystem paths can be defined in datatools::properties syntax as:
18  *
19  * ```ini
20  * mypath : string as path = "/some/path"
21  * ```
22  *
23  * Parsing of the properties file will expand any mount points or environment
24  * variables so that fetching the path will return a `std::string` containing
25  * the resolved absolute path.
26  *
27  * @ref path provides a simple type to distinguish raw `std::string` from
28  * explicit paths allowing users to validate that a @ref property_set value
29  * is a true path. It is nothing more than a simple holder of the std::string for
30  * the absolute path and may be used as:
31  *
32  * ```cpp
33  * void configure_me(property_set const& ps) {
34  * auto p = ps.get<falaise::config::path>("mypath"); // throws if "mypath" is not a path value
35  * std::cout << p << std::endl; // prints absolute path
36  * }
37  * ```
38  *
39  * A @ref path can be constructed from any string whose value is a valid path
40  * as understood by @ref datatools::fetch_path_with_env. Examples are:
41  *
42  * - An absolute path, e.g. `auto x = falaise::config::path{"/foo/bar/baz.txt"};`
43  * - A relative path, e.g. `auto x = falaise::config::path{"bar/baz.txt"};`
44  *
45  * The string may also include environment variables, e.g. `$HOME` or `${HOME}`,
46  * which will be expanded if they exist, otherwise a @ref invalid_path_error
47  * is thrown. When running in a program that provides the @ref datatools::kernel
48  * service, paths can also be supplied as:
49  *
50  * - A resource path, e.g. `@falaise:foo/bar.txt`
51  * - A @ref datatools::urn, e.g. `urn:subject:object`
52  *
53  * Note that in all cases, @ref path *does not* check that a file exists at the location
54  * specified by the path. If you require more specific filesystem functionality, you
55  * should use the value of path in a full library such as
56  * [boost::filesystem](https://www.boost.org/doc/libs/1_69_0/libs/filesystem/doc/index.htm) or
57  * C++17's [std::filesystem](https://en.cppreference.com/w/cpp/filesystem) if available.
58  *
59  * \sa property_set
60  * \sa datatools::fetch_path_with_env
61  */
62 class path {
63  public:
64  //! Default constructor
65  path() = default;
66 
67  //! Construct path from a std::string
68  /*!
69  * Not declared explicit as we allow conversions for compatibility with
70  * existing use of filesystem paths as std::string.
71  *
72  * \param[in] p
73  * \throws invalid_path_error if p is not a valid path string
74  */
75  path(std::string const& p);
76 
77  //! Conversion to std::string
78  operator std::string() const { return value; }
79 
80  //! Equality operator
81  /*!
82  * \returns true if the two operands are the same
83  */
84  bool operator==(path const& other) const { return value == other.value; }
85 
86  //! Inequality operator
87  /*!
88  * \returns true if the two operands are not the same
89  */
90  bool operator!=(path const& other) const { return !(*this == other); }
91 
92  //! Equality operator for path against a string
93  /*!
94  * \returns true if the value of the path is equal to the string
95  */
96  bool operator==(std::string const& other) const { return value == other; }
97 
98  //! Inequality operator for path against a string
99  /*!
100  * \returns true if the value of the path is not equal to the string
101  */
102  bool operator!=(std::string const& other) const { return !(*this == other); }
103 
104  private:
105  std::string value; //< absolute path value
106 };
107 
108 //! Output path to an ostream
109 inline std::ostream& operator<<(std::ostream& os, path const& p) {
110  os << std::string{p};
111  return os;
112 }
113 
114 } // namespace config
115 } // namespace falaise
116 
117 #endif /* FALAISE_PATH_H */
bool operator==(std::string const &other) const
Equality operator for path against a string.
Definition: path.h:96
bool operator==(path const &other) const
Equality operator.
Definition: path.h:84
bool operator!=(path const &other) const
Inequality operator.
Definition: path.h:90
bool operator!=(std::string const &other) const
Inequality operator for path against a string.
Definition: path.h:102
Class representing a filesystem path as held by a property_set.
Definition: path.h:62
Definition: metadata_utils.h:35
Exception for paths that cannot be resolved by datatools::utils.
Definition: path.h:11
std::ostream & operator<<(std::ostream &os, path const &p)
Output path to an ostream.
Definition: path.h:109
path()=default
Default constructor.