Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
uri.h
1 /* Copyright (c) 2013-2014, ahmet.bilgili@epfl.ch
2  * 2014, Stefan.Eilemann@epfl.ch
3  *
4  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 2.1 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef LUNCHBOX_URI_H
21 #define LUNCHBOX_URI_H
22 
23 #include <lunchbox/api.h>
24 #include <lunchbox/types.h>
25 #include <boost/unordered_map.hpp> // iterator typedefs
26 #include <boost/lexical_cast.hpp>
27 
28 namespace lunchbox
29 {
30 namespace detail { class URI; }
31 
55 class URI
56 {
57 public:
58  typedef boost::unordered_map< std::string, std::string > KVMap;
59  typedef KVMap::const_iterator ConstKVIter;
60 
62  LUNCHBOX_API URI();
63 
70  LUNCHBOX_API explicit URI( const std::string& uri );
71 
73  LUNCHBOX_API explicit URI( const char* uri );
74 
76  LUNCHBOX_API URI( const URI& from );
77 
78  LUNCHBOX_API ~URI();
79 
81  LUNCHBOX_API URI& operator = ( const URI& rhs );
82 
85  LUNCHBOX_API const std::string& getScheme() const;
86  LUNCHBOX_API const std::string& getUserinfo() const;
87  LUNCHBOX_API uint16_t getPort() const;
88  LUNCHBOX_API const std::string& getHost() const;
89  LUNCHBOX_API const std::string& getPath() const;
90  LUNCHBOX_API const std::string& getQuery() const;
91  LUNCHBOX_API const std::string& getFragment() const;
93 
100  LUNCHBOX_API ConstKVIter queryBegin() const;
101 
106  LUNCHBOX_API ConstKVIter queryEnd() const;
107 
112  LUNCHBOX_API ConstKVIter findQuery( const std::string& key ) const;
113 
115  LUNCHBOX_API void addQuery( const std::string& key,
116  const std::string& value );
118 
119 private:
120  detail::URI* const _impl;
121 };
122 
123 inline std::ostream& operator << ( std::ostream& os, const URI& uri )
124 {
125  if( !uri.getScheme().empty( ))
126  os << uri.getScheme() << "://";
127  if( !uri.getUserinfo().empty( ))
128  os << uri.getUserinfo() << "@";
129  os << uri.getHost();
130  if( uri.getPort( ))
131  os << ':' << uri.getPort();
132  os << uri.getPath();
133  if( !uri.getQuery().empty( ))
134  os << '?' << uri.getQuery();
135  if( !uri.getFragment().empty( ))
136  os << '#' << uri.getFragment();
137  return os;
138 }
139 }
140 
141 namespace boost
142 {
143 template<> inline std::string lexical_cast( const lunchbox::URI& uri )
144 {
145  std::ostringstream os;
146  os << uri;
147  return os.str();
148 }
149 }
150 #endif // LUNCHBOX_URI_H
Defines export visibility macros for Lunchbox.
Basic type definitions not provided by the operating system.
LUNCHBOX_API URI()
Construct an empty URI.
LUNCHBOX_API URI & operator=(const URI &rhs)
Assign the data from another URI.
LUNCHBOX_API ConstKVIter queryBegin() const
The URI class parses the given uri string according to the regex given in RFC3986.
Definition: uri.h:55
LUNCHBOX_API ConstKVIter queryEnd() const
LUNCHBOX_API ConstKVIter findQuery(const std::string &key) const
LUNCHBOX_API void addQuery(const std::string &key, const std::string &value)
Add a key-value pair to the query.
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Pretty-print all members of the array.
Definition: array.h:47