1 | // |
---|
2 | // NameValueCollection.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/NameValueCollection.h#1 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: Messages |
---|
8 | // Module: NameValueCollection |
---|
9 | // |
---|
10 | // Definition of the NameValueCollection class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_NameValueCollection_INCLUDED |
---|
40 | #define Net_NameValueCollection_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/String.h" |
---|
45 | #include <map> |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | namespace Net { |
---|
50 | |
---|
51 | |
---|
52 | class Net_API NameValueCollection |
---|
53 | /// A collection of name-value pairs that are used in |
---|
54 | /// various internet protocols like HTTP and SMTP. |
---|
55 | /// |
---|
56 | /// The name is case-insensitive. |
---|
57 | /// |
---|
58 | /// There can be more than one name-value pair with the |
---|
59 | /// same name. |
---|
60 | { |
---|
61 | public: |
---|
62 | struct ILT |
---|
63 | { |
---|
64 | bool operator() (const std::string& s1, const std::string& s2) const |
---|
65 | { |
---|
66 | return Poco::icompare(s1, s2) < 0; |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | typedef std::multimap<std::string, std::string, ILT> HeaderMap; |
---|
71 | typedef HeaderMap::iterator Iterator; |
---|
72 | typedef HeaderMap::const_iterator ConstIterator; |
---|
73 | |
---|
74 | NameValueCollection(); |
---|
75 | /// Creates an empty NameValueCollection. |
---|
76 | |
---|
77 | NameValueCollection(const NameValueCollection& nvc); |
---|
78 | /// Creates a NameValueCollection by copying another one. |
---|
79 | |
---|
80 | virtual ~NameValueCollection(); |
---|
81 | /// Destroys the NameValueCollection. |
---|
82 | |
---|
83 | NameValueCollection& operator = (const NameValueCollection& nvc); |
---|
84 | /// Assigns the name-value pairs of another NameValueCollection to this one. |
---|
85 | |
---|
86 | void swap(NameValueCollection& nvc); |
---|
87 | /// Swaps the NameValueCollection with another one. |
---|
88 | |
---|
89 | const std::string& operator [] (const std::string& name) const; |
---|
90 | /// Returns the value of the (first) name-value pair with the given name. |
---|
91 | /// |
---|
92 | /// Throws a NotFoundException if the name-value pair does not exist. |
---|
93 | |
---|
94 | void set(const std::string& name, const std::string& value); |
---|
95 | /// Sets the value of the (first) name-value pair with the given name. |
---|
96 | |
---|
97 | void add(const std::string& name, const std::string& value); |
---|
98 | /// Adds a new name-value pair with the given name and value. |
---|
99 | |
---|
100 | const std::string& get(const std::string& name) const; |
---|
101 | /// Returns the value of the first name-value pair with the given name. |
---|
102 | /// |
---|
103 | /// Throws a NotFoundException if the name-value pair does not exist. |
---|
104 | |
---|
105 | const std::string& get(const std::string& name, const std::string& defaultValue) const; |
---|
106 | /// Returns the value of the first name-value pair with the given name. |
---|
107 | /// If no value with the given name has been found, the defaultValue is returned. |
---|
108 | |
---|
109 | bool has(const std::string& name) const; |
---|
110 | /// Returns true if there is at least one name-value pair |
---|
111 | /// with the given name. |
---|
112 | |
---|
113 | ConstIterator find(const std::string& name) const; |
---|
114 | /// Returns an iterator pointing to the first name-value pair |
---|
115 | /// with the given name. |
---|
116 | |
---|
117 | ConstIterator begin() const; |
---|
118 | /// Returns an iterator pointing to the begin of |
---|
119 | /// the name-value pair collection. |
---|
120 | |
---|
121 | ConstIterator end() const; |
---|
122 | /// Returns an iterator pointing to the end of |
---|
123 | /// the name-value pair collection. |
---|
124 | |
---|
125 | bool empty() const; |
---|
126 | /// Returns true iff the header does not have any content. |
---|
127 | |
---|
128 | int size() const; |
---|
129 | /// Returns the number of name-value pairs in the |
---|
130 | /// collection. |
---|
131 | |
---|
132 | void erase(const std::string& name); |
---|
133 | /// Removes all name-value pairs with the given name. |
---|
134 | |
---|
135 | void clear(); |
---|
136 | /// Removes all name-value pairs and their values. |
---|
137 | |
---|
138 | private: |
---|
139 | HeaderMap _map; |
---|
140 | }; |
---|
141 | |
---|
142 | |
---|
143 | // |
---|
144 | // inlines |
---|
145 | // |
---|
146 | inline void swap(NameValueCollection& nvc1, NameValueCollection& nvc2) |
---|
147 | { |
---|
148 | nvc1.swap(nvc2); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | } } // namespace Poco::Net |
---|
153 | |
---|
154 | |
---|
155 | #endif // Net_NameValueCollection_INCLUDED |
---|