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