Silicium
path_segment.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_PATH_SEGMENT_HPP
2 #define SILICIUM_PATH_SEGMENT_HPP
3 
6 #include <silicium/optional.hpp>
7 #include <boost/filesystem/path.hpp>
8 
9 namespace Si
10 {
11  struct path_segment
12  {
14 
15  path_segment() BOOST_NOEXCEPT
16  {
17  }
18 
19  path_segment(path_segment &&other) BOOST_NOEXCEPT
20  : m_value(std::move(other.m_value))
21  {
22  }
23 
24  path_segment(path_segment const &other)
25  : m_value(other.m_value)
26  {
27  }
28 
29  path_segment &operator = (path_segment &&other) BOOST_NOEXCEPT
30  {
31  m_value = std::move(other.m_value);
32  return *this;
33  }
34 
36  {
37  m_value = other.m_value;
38  return *this;
39  }
40 
41  void swap(path_segment &other) BOOST_NOEXCEPT
42  {
43  m_value.swap(other.m_value);
44  }
45 
46  boost::filesystem::path
47 #ifdef _WIN32
48  const &
49 #endif
50  to_boost_path() const
51  {
52  return m_value.to_boost_path();
53  }
54 
55 #ifdef _WIN32
56  boost::filesystem::path const &
57 #else
58  noexcept_string const &
59 #endif
60  underlying() const BOOST_NOEXCEPT
61  {
62  return m_value.underlying();
63  }
64 
65  char_type const *c_str() const BOOST_NOEXCEPT
66  {
67  return m_value.c_str();
68  }
69 
70  static optional<path_segment> create(boost::filesystem::path const &maybe_segment)
71  {
72  if (maybe_segment.parent_path().empty())
73  {
74  return path_segment(maybe_segment);
75  }
76  return none;
77  }
78 
79  private:
80 
81  path m_value;
82 
83  explicit path_segment(boost::filesystem::path const &value)
84  : m_value(value)
85  {
86  }
87  };
88 
89  BOOST_STATIC_ASSERT(is_handle<path_segment>::value);
90 
91  inline std::ostream &operator << (std::ostream &out, path_segment const &p)
92  {
93  return out << p.underlying();
94  }
95 
96  template <class ComparableToPath>
97  inline bool operator == (path_segment const &left, ComparableToPath const &right)
98  {
99  return left.underlying() == right;
100  }
101 
102  template <class ComparableToPath>
103  inline bool operator == (ComparableToPath const &left, path_segment const &right)
104  {
105  return left == right.underlying();
106  }
107 
108  inline bool operator == (path_segment const &left, boost::filesystem::path const &right)
109  {
110  return left.underlying().c_str() == right;
111  }
112 
113  inline bool operator == (boost::filesystem::path const &left, path_segment const &right)
114  {
115  return left == right.underlying().c_str();
116  }
117 
118  inline bool operator == (path_segment const &left, path_segment const &right)
119  {
120  return left.underlying() == right.underlying();
121  }
122 
123  template <class ComparableToPath>
124  inline bool operator != (path_segment const &left, ComparableToPath const &right)
125  {
126  return !(left == right);
127  }
128 
129  template <class ComparableToPath>
130  inline bool operator != (ComparableToPath const &left, path_segment const &right)
131  {
132  return !(left == right);
133  }
134 
135  inline bool operator < (path_segment const &left, path_segment const &right)
136  {
137  return left.underlying() < right.underlying();
138  }
139 
140  inline std::size_t hash_value(path_segment const &value)
141  {
142  using boost::hash_value;
143  return hash_value(value.underlying());
144  }
145 
146  inline relative_path operator / (path_segment const &front, path_segment const &back)
147  {
148  //TODO: do this efficiently
149  relative_path result(front.to_boost_path() / back.to_boost_path());
150  return result;
151  }
152 
153  inline relative_path operator / (relative_path const &front, path_segment const &back)
154  {
155  return front / relative_path(back.to_boost_path());
156  }
157 
158  inline absolute_path operator / (absolute_path const &front, path_segment const &back)
159  {
160  absolute_path result = front;
161  result.combine(relative_path(back.to_boost_path()));
162  return result;
163  }
164 }
165 
166 namespace std
167 {
168  template <>
169  struct hash< ::Si::path_segment>
170  {
171  std::size_t operator()(Si::path_segment const &value) const
172  {
173  return hash_value(value);
174  }
175  };
176 }
177 
178 #endif
std::size_t hash_value(path_segment const &value)
Definition: path_segment.hpp:140
std::remove_reference< T >::type && move(T &&ref)
Definition: move.hpp:10
Definition: absolute_path.hpp:21
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
Definition: absolute_path.hpp:352
Definition: absolute_path.hpp:19
boost::filesystem::path to_boost_path() const
Definition: path_segment.hpp:50
Definition: optional.hpp:39
static optional< path_segment > create(boost::filesystem::path const &maybe_segment)
Definition: path_segment.hpp:70
boost::container::string noexcept_string
Definition: noexcept_string.hpp:26
char_type const * c_str() const BOOST_NOEXCEPT
Definition: path_segment.hpp:65
noexcept_string const & underlying() const BOOST_NOEXCEPT
Definition: path.hpp:124
noexcept_string const & underlying() const BOOST_NOEXCEPT
Definition: path_segment.hpp:60
path_segment() BOOST_NOEXCEPT
Definition: path_segment.hpp:15
boost::filesystem::path to_boost_path() const
Definition: path.hpp:110
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
void swap(path_segment &other) BOOST_NOEXCEPT
Definition: path_segment.hpp:41
SILICIUM_USE_RESULT bool operator<(absolute_path const &left, absolute_path const &right)
Definition: absolute_path.hpp:214
path_segment(path_segment const &other)
Definition: path_segment.hpp:24
void combine(relative_path const &back)
Definition: absolute_path.hpp:90
Definition: path_segment.hpp:11
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
path_segment(path_segment &&other) BOOST_NOEXCEPT
Definition: path_segment.hpp:19
path_segment & operator=(path_segment &&other) BOOST_NOEXCEPT
Definition: path_segment.hpp:29
void swap(path &other) BOOST_NOEXCEPT
Definition: path.hpp:101
Definition: relative_path.hpp:10
native_path_char char_type
Definition: path_segment.hpp:13
std::size_t operator()(Si::path_segment const &value) const
Definition: path_segment.hpp:171