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.)
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
25 #include <boost/iostreams/detail/config/wide_streams.hpp>
26 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
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 //------------------------------------//
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
>());
59 : narrow_(p
.narrow_
), wide_(p
.wide_
), is_wide_(p
.is_wide_
)
62 // Assignment operator taking another path
63 path
& operator=(const path
& p
)
67 is_wide_
= p
.is_wide_
;
71 // Assignment operator taking a std::string
72 path
& operator=(const std::string
& p
)
80 // Assignment operator taking a C-style string
81 path
& operator=(const char* p
)
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
>());
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(); }
109 // For wide-character paths, use a boost::filesystem::wpath instead of a
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();
122 template<typename Path
>
123 void init(const Path
& p
, boost::type
<std::wstring
>)
126 wide_
= p
.external_file_string();
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 //---------------------------//
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
)
157 path
& operator=(const std::string
& p
)
162 path
& operator=(const char* p
)
167 template<typename Path
>
168 path
& operator=(const Path
& p
)
170 path_
= p
.external_file_string();
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; }
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