libfoedus-core
FOEDUS Core Library
path.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_FS_PATH_HPP_
19 #define FOEDUS_FS_PATH_HPP_
20 #include <iosfwd>
21 #include <string>
22 #include <vector>
23 
24 #include "foedus/cxx11.hpp"
25 #include "foedus/error_code.hpp"
26 
27 namespace foedus {
28 namespace fs {
37 class Path {
38  public:
39  static const char kPreferredSeparator = '/';
40 
41  Path() {}
42  Path(const Path& p) : pathname_(p.pathname_) {}
44  explicit Path(const std::string& s);
45 
46  Path& operator=(const Path& p) { pathname_ = p.pathname_; return *this; }
47  Path& operator=(const std::string& s) { pathname_ = s; return *this; }
48  Path& operator+=(const Path& p) {pathname_ += p.pathname_; return *this;}
49  Path& operator+=(const std::string& s) {pathname_ += s; return *this;}
50 
52  if (!pathname_.empty() && pathname_.at(pathname_.size() - 1) != kPreferredSeparator) {
53  pathname_ += kPreferredSeparator;
54  }
55  }
56  Path& operator/=(const Path& p) { return operator/=(p.pathname_); }
57  Path& operator/=(const std::string& s) {
59  pathname_ += s;
60  return *this;
61  }
62 
63  const std::string& native() const { return pathname_; }
64  const char* c_str() const { return pathname_.c_str(); }
65  const std::string& string() const { return pathname_; }
66 
67  int compare(const Path& p) const CXX11_NOEXCEPT { return pathname_.compare(p.pathname_); }
68  int compare(const std::string& s) const { return compare(Path(s)); }
69 
70  Path parent_path() const;
71  std::vector< Path > child_paths() const;
72  Path filename() const; // returns 0 or 1 element path
73 
74  bool root() const { return pathname_.size() == 1 && pathname_.at(0) == kPreferredSeparator; }
75  bool empty() const { return pathname_.empty(); } // name consistent with std containers
76  bool has_parent_path() const { return !parent_path().empty(); }
77  bool has_filename() const { return !pathname_.empty(); }
78 
79  friend std::ostream& operator<<(std::ostream& o, const Path& v);
80 
81  private:
82  std::string pathname_;
83 };
84 
85 inline bool operator==(const Path& lhs, const Path& rhs) {return lhs.compare(rhs) == 0;}
86 inline bool operator==(const Path& lhs, const std::string& rhs) {return lhs.compare(rhs) == 0;}
87 inline bool operator==(const std::string& lhs, const Path& rhs) {return rhs.compare(lhs) == 0;}
88 
89 inline bool operator!=(const Path& lhs, const Path& rhs) {return lhs.compare(rhs) != 0;}
90 inline bool operator!=(const Path& lhs, const std::string& rhs) {return lhs.compare(rhs) != 0;}
91 inline bool operator!=(const std::string& lhs, const Path& rhs) {return rhs.compare(lhs) != 0;}
92 
93 inline bool operator<(const Path& lhs, const Path& rhs) {return lhs.compare(rhs) < 0;}
94 inline bool operator<=(const Path& lhs, const Path& rhs) {return !(rhs < lhs);}
95 inline bool operator> (const Path& lhs, const Path& rhs) {return rhs < lhs;}
96 inline bool operator>=(const Path& lhs, const Path& rhs) {return !(lhs < rhs);}
97 
98 
99 } // namespace fs
100 } // namespace foedus
101 #endif // FOEDUS_FS_PATH_HPP_
Path & operator/=(const Path &p)
Definition: path.hpp:56
Path & operator=(const Path &p)
Definition: path.hpp:46
const std::string & native() const
Definition: path.hpp:63
bool has_filename() const
Definition: path.hpp:77
bool operator>(const Path &lhs, const Path &rhs)
Definition: path.hpp:95
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
bool operator==(const Path &lhs, const Path &rhs)
Definition: path.hpp:85
int compare(const std::string &s) const
Definition: path.hpp:68
#define CXX11_NOEXCEPT
Used in public headers in place of "noexcept" of C++11.
Definition: cxx11.hpp:133
bool has_parent_path() const
Definition: path.hpp:76
Path & operator/=(const std::string &s)
Definition: path.hpp:57
bool root() const
Definition: path.hpp:74
Path & operator+=(const std::string &s)
Definition: path.hpp:49
bool empty() const
Definition: path.hpp:75
std::vector< Path > child_paths() const
Definition: path.cpp:64
Analogue of boost::filesystem::path.
Definition: path.hpp:37
bool operator!=(const Path &lhs, const Path &rhs)
Definition: path.hpp:89
bool operator<=(const Path &lhs, const Path &rhs)
Definition: path.hpp:94
const std::string & string() const
Definition: path.hpp:65
friend std::ostream & operator<<(std::ostream &o, const Path &v)
Definition: path.cpp:84
bool operator>=(const Path &lhs, const Path &rhs)
Definition: path.hpp:96
bool operator<(const Path &lhs, const Path &rhs)
Definition: path.hpp:93
Path & operator+=(const Path &p)
Definition: path.hpp:48
Path parent_path() const
Definition: path.cpp:55
static const char kPreferredSeparator
Definition: path.hpp:39
const char * c_str() const
Definition: path.hpp:64
Path & operator=(const std::string &s)
Definition: path.hpp:47
int compare(const Path &p) const noexcept
Definition: path.hpp:67
Path(const Path &p)
Definition: path.hpp:42
Path filename() const
Definition: path.cpp:46
void append_separator_if_needed()
Definition: path.hpp:51