fix doc example typo
[boost.git] / boost / iostreams / detail / path.hpp
blobaf64c3018eb2e954b95ae580f9039e9e141da6a7
1 /*
2 * Distributed under the Boost Software License, Version 1.0.(See accompanying
3 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
4 *
5 * See http://www.boost.org/libs/iostreams for documentation.
7 * File: boost/iostreams/detail/path.hpp
8 * Date: Sat Jun 21 21:24:05 MDT 2008
9 * Copyright: 2008 CodeRage, LLC
10 * Author: Jonathan Turkanis
11 * Contact: turkanis at coderage dot com
13 * Defines the class boost::iostreams::detail::path, for storing a
14 * a std::string or std::wstring.
16 * This class allows interoperability with Boost.Filesystem without
17 * creating a dependence on Boost.Filesystem headers or implementation.
20 #ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED
21 #define BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED
23 #include <cstring>
24 #include <string>
25 #include <boost/iostreams/detail/config/wide_streams.hpp>
26 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
27 # include <cwchar>
28 #endif
29 #include <boost/static_assert.hpp>
30 #include <boost/type.hpp>
31 #include <boost/type_traits/is_same.hpp>
33 namespace boost { namespace iostreams { namespace detail {
35 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //------------------------------------//
37 class path {
38 public:
40 // Default constructor
41 path() : narrow_(), wide_(), is_wide_(false) { }
43 // Constructor taking a std::string
44 path(const std::string& p) : narrow_(p), wide_(), is_wide_(false) { }
46 // Constructor taking a C-style string
47 path(const char* p) : narrow_(p), wide_(), is_wide_(false) { }
49 // Constructor taking a boost::filesystem::path or boost::filesystem::wpath
50 template<typename Path>
51 explicit path(const Path& p)
53 typedef typename Path::external_string_type string_type;
54 init(p, boost::type<string_type>());
57 // Copy constructor
58 path(const path& p)
59 : narrow_(p.narrow_), wide_(p.wide_), is_wide_(p.is_wide_)
60 { }
62 // Assignment operator taking another path
63 path& operator=(const path& p)
65 narrow_ = p.narrow_;
66 wide_ = p.wide_;
67 is_wide_ = p.is_wide_;
68 return *this;
71 // Assignment operator taking a std::string
72 path& operator=(const std::string& p)
74 narrow_ = p;
75 wide_.clear();
76 is_wide_ = false;
77 return *this;
80 // Assignment operator taking a C-style string
81 path& operator=(const char* p)
83 narrow_.assign(p);
84 wide_.clear();
85 is_wide_ = false;
86 return *this;
89 // Assignment operator taking a Boost.Filesystem path
90 template<typename Path>
91 path& operator=(const Path& p)
93 typedef typename Path::external_string_type string_type;
94 init(p, boost::type<string_type>());
95 return *this;
98 bool is_wide() const { return is_wide_; }
100 // Returns a representation of the underlying path as a std::string
101 // Requires: is_wide() returns false
102 const char* c_str() const { return narrow_.c_str(); }
104 // Returns a representation of the underlying path as a std::wstring
105 // Requires: is_wide() returns true
106 const wchar_t* c_wstr() const { return wide_.c_str(); }
107 private:
109 // For wide-character paths, use a boost::filesystem::wpath instead of a
110 // std::wstring
111 path(const std::wstring&);
112 path& operator=(const std::wstring&);
114 template<typename Path>
115 void init(const Path& p, boost::type<std::string>)
117 narrow_ = p.external_file_string();
118 wide_.clear();
119 is_wide_ = false;
122 template<typename Path>
123 void init(const Path& p, boost::type<std::wstring>)
125 narrow_.clear();
126 wide_ = p.external_file_string();
127 is_wide_ = true;
130 std::string narrow_;
131 std::wstring wide_;
132 bool is_wide_;
135 inline bool operator==(const path& lhs, const path& rhs)
137 return lhs.is_wide() ?
138 rhs.is_wide() && std::wcscmp(lhs.c_wstr(), rhs.c_wstr()) == 0 :
139 !rhs.is_wide() && std::strcmp(lhs.c_str(), rhs.c_str()) == 0;
142 #else // #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //---------------------------//
144 class path {
145 public:
146 path() { }
147 path(const std::string& p) : path_(p) { }
148 path(const char* p) : path_(p) { }
149 template<typename Path>
150 path(const Path& p) : path_(p.external_file_string()) { }
151 path(const path& p) : path_(p.path_) { }
152 path& operator=(const path& other)
154 path_ = other.path_;
155 return *this;
157 path& operator=(const std::string& p)
159 path_ = p;
160 return *this;
162 path& operator=(const char* p)
164 path_ = p;
165 return *this;
167 template<typename Path>
168 path& operator=(const Path& p)
170 path_ = p.external_file_string();
171 return *this;
173 bool is_wide() const { return false; }
174 const char* c_str() const { return path_.c_str(); }
175 const wchar_t* c_wstr() const { return 0; }
176 private:
177 std::string path_;
180 inline bool operator==(const path& lhs, const path& rhs)
182 return std::strcmp(lhs.c_str(), rhs.c_str()) == 0 ;
185 #endif // #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //--------------------------//
187 } } } // End namespaces detail, iostreams, boost.
189 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED