Silicium
relative_path.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_RELATIVE_PATH_HPP
2 #define SILICIUM_RELATIVE_PATH_HPP
3 
4 #include <silicium/path.hpp>
6 #include <boost/filesystem/path.hpp>
7 
8 namespace Si
9 {
11  {
13 
14  relative_path() BOOST_NOEXCEPT
15  {
16  }
17 
18  explicit relative_path(noexcept_string const &value)
19  : m_value(value)
20  {
21  }
22 
23  explicit relative_path(boost::filesystem::path const &value)
24 #ifdef _WIN32
25  : m_value(value)
26 #else
27  : m_value(value.c_str())
28 #endif
29  {
30  }
31 
32  explicit relative_path(char_type const *c_str)
33  : m_value(c_str)
34  {
35  }
36 
37  template <std::size_t N>
38  explicit relative_path(char_type const (&c_str_literal)[N])
39  : m_value(c_str_literal)
40  {
41  }
42 
43 #ifdef _WIN32
44  explicit relative_path(char const *c_str)
45  : m_value(c_str)
46  {
47  }
48 
49  template <std::size_t N>
50  explicit relative_path(char const (&c_str_literal)[N])
51  : m_value(c_str_literal)
52  {
53  }
54 #endif
55 
56  template <class Iterator>
57  relative_path(Iterator begin, Iterator end)
58  : m_value(begin, end)
59  {
60  }
61 
62  relative_path(relative_path &&other) BOOST_NOEXCEPT
63  : m_value(std::move(other.m_value))
64  {
65  }
66 
68  : m_value(other.m_value)
69  {
70  }
71 
72  relative_path &operator = (relative_path &&other) BOOST_NOEXCEPT
73  {
74  m_value = std::move(other.m_value);
75  return *this;
76  }
77 
79  {
80  m_value = other.m_value;
81  return *this;
82  }
83 
84  void swap(relative_path &other) BOOST_NOEXCEPT
85  {
86  m_value.swap(other.m_value);
87  }
88 
90  boost::filesystem::path
91 #ifdef _WIN32
92  const &
93 #endif
94  to_boost_path() const
95  {
96  return m_value.to_boost_path();
97  }
98 
100 #ifdef _WIN32
101  boost::filesystem::path const &
102 #else
103  noexcept_string const &
104 #endif
105  underlying() const BOOST_NOEXCEPT
106  {
107  return m_value.underlying();
108  }
109 
111  char_type const *c_str() const BOOST_NOEXCEPT
112  {
113  return m_value.c_str();
114  }
115 
117  bool empty() const BOOST_NOEXCEPT
118  {
119  return underlying().empty();
120  }
121 
122  private:
123 
124  path m_value;
125  };
126 
127  BOOST_STATIC_ASSERT(is_handle<relative_path>::value);
128 
129  inline std::ostream &operator << (std::ostream &out, relative_path const &p)
130  {
131  return out << p.underlying();
132  }
133 
134  template <class ComparableToPath>
136  inline bool operator == (relative_path const &left, ComparableToPath const &right)
137  {
138  return left.underlying() == right;
139  }
140 
141  template <class ComparableToPath>
143  inline bool operator == (ComparableToPath const &left, relative_path const &right)
144  {
145  return left == right.underlying();
146  }
147 
149  inline bool operator == (relative_path const &left, boost::filesystem::path const &right)
150  {
151  return right == left.c_str();
152  }
153 
155  inline bool operator == (boost::filesystem::path const &left, relative_path const &right)
156  {
157  return left == right.c_str();
158  }
159 
161  inline bool operator == (relative_path const &left, relative_path const &right)
162  {
163  return left.underlying() == right.underlying();
164  }
165 
166  template <class ComparableToPath>
168  inline bool operator != (relative_path const &left, ComparableToPath const &right)
169  {
170  return !(left == right);
171  }
172 
173  template <class ComparableToPath>
175  inline bool operator != (ComparableToPath const &left, relative_path const &right)
176  {
177  return !(left == right);
178  }
179 
181  inline bool operator < (relative_path const &left, relative_path const &right)
182  {
183  return left.underlying() < right.underlying();
184  }
185 
187  inline std::size_t hash_value(relative_path const &value)
188  {
189  using boost::hash_value;
190  return hash_value(value.underlying());
191  }
192 
194  inline relative_path leaf(relative_path const &whole)
195  {
196  //TODO: do this efficiently
197  return relative_path(whole.to_boost_path().leaf());
198  }
199 
201  inline relative_path parent(relative_path const &whole)
202  {
203  //TODO: do this efficiently
204  return relative_path(whole.to_boost_path().parent_path());
205  }
206 
208  inline relative_path operator / (relative_path const &front, relative_path const &back)
209  {
210  //TODO: do this efficiently
211  return relative_path(front.to_boost_path() / back.to_boost_path());
212  }
213 }
214 
215 namespace std
216 {
217  template <>
218  struct hash< ::Si::relative_path>
219  {
221  std::size_t operator()(Si::relative_path const &value) const
222  {
223  return hash_value(value);
224  }
225  };
226 }
227 
228 #endif
SILICIUM_USE_RESULT optional< absolute_path > parent(absolute_path const &whole)
Definition: absolute_path.hpp:234
std::remove_reference< T >::type && move(T &&ref)
Definition: move.hpp:10
SILICIUM_USE_RESULT std::size_t hash_value(relative_path const &value)
Definition: relative_path.hpp:187
SILICIUM_USE_RESULT relative_path leaf(absolute_path const &whole)
Definition: absolute_path.hpp:227
relative_path(noexcept_string const &value)
Definition: relative_path.hpp:18
BOOST_STATIC_ASSERT(Si::is_handle< absolute_path >::value)
SILICIUM_USE_RESULT char_type const * c_str() const BOOST_NOEXCEPT
Definition: relative_path.hpp:111
std::ostream & operator<<(std::ostream &out, absolute_path const &p)
Definition: absolute_path.hpp:144
char_type const * c_str() const BOOST_NOEXCEPT
Definition: path.hpp:129
Definition: absolute_path.hpp:352
Definition: absolute_path.hpp:19
BOOST_CONSTEXPR Iterator const & end(iterator_range< Iterator > const &range)
Definition: iterator_range.hpp:136
boost::container::string noexcept_string
Definition: noexcept_string.hpp:26
relative_path(boost::filesystem::path const &value)
Definition: relative_path.hpp:23
noexcept_string const & underlying() const BOOST_NOEXCEPT
Definition: path.hpp:124
relative_path() BOOST_NOEXCEPT
Definition: relative_path.hpp:14
void swap(relative_path &other) BOOST_NOEXCEPT
Definition: relative_path.hpp:84
relative_path(Iterator begin, Iterator end)
Definition: relative_path.hpp:57
boost::filesystem::path to_boost_path() const
Definition: path.hpp:110
BOOST_CONSTEXPR Iterator const & begin(iterator_range< Iterator > const &range)
Definition: iterator_range.hpp:123
SILICIUM_USE_RESULT absolute_path operator/(absolute_path const &front, relative_path const &back)
Definition: absolute_path.hpp:246
SILICIUM_USE_RESULT bool operator==(absolute_path const &left, ComparableToPath const &right)
Definition: absolute_path.hpp:169
relative_path(char_type const *c_str)
Definition: relative_path.hpp:32
SILICIUM_USE_RESULT bool operator<(absolute_path const &left, absolute_path const &right)
Definition: absolute_path.hpp:214
relative_path(relative_path const &other)
Definition: relative_path.hpp:67
SILICIUM_USE_RESULT std::size_t operator()(Si::relative_path const &value) const
Definition: relative_path.hpp:221
SILICIUM_USE_RESULT boost::filesystem::path to_boost_path() const
Definition: relative_path.hpp:94
Definition: path.hpp:11
SILICIUM_USE_RESULT noexcept_string const & underlying() const BOOST_NOEXCEPT
Definition: relative_path.hpp:105
relative_path & operator=(relative_path &&other) BOOST_NOEXCEPT
Definition: relative_path.hpp:72
SILICIUM_USE_RESULT std::size_t hash_value(absolute_path const &value)
Definition: absolute_path.hpp:220
SILICIUM_USE_RESULT bool operator!=(absolute_path const &left, ComparableToPath const &right)
Definition: absolute_path.hpp:201
char native_path_char
Definition: path_char.hpp:14
relative_path(relative_path &&other) BOOST_NOEXCEPT
Definition: relative_path.hpp:62
SILICIUM_USE_RESULT bool empty() const BOOST_NOEXCEPT
Definition: relative_path.hpp:117
void swap(path &other) BOOST_NOEXCEPT
Definition: path.hpp:101
Definition: relative_path.hpp:10
#define SILICIUM_USE_RESULT
Definition: config.hpp:147
native_path_char char_type
Definition: relative_path.hpp:12
relative_path(char_type const (&c_str_literal)[N])
Definition: relative_path.hpp:38