1 |
#ifndef RESIP_ParserContainer_hxx |
2 |
#define RESIP_ParserContainer_hxx |
3 |
|
4 |
#include "resiprocate/HeaderFieldValueList.hxx" |
5 |
#include "resiprocate/ParserContainerBase.hxx" |
6 |
|
7 |
namespace resip |
8 |
{ |
9 |
|
10 |
template<class T> |
11 |
class ParserContainer : public ParserContainerBase |
12 |
{ |
13 |
public: |
14 |
typedef T value_type; |
15 |
typedef value_type* pointer; |
16 |
typedef const value_type* const_pointer; |
17 |
typedef value_type& reference; |
18 |
typedef const value_type& const_reference; |
19 |
typedef ptrdiff_t difference_type; |
20 |
|
21 |
ParserContainer() |
22 |
: ParserContainerBase(Headers::UNKNOWN) |
23 |
{} |
24 |
|
25 |
// private to SipMessage |
26 |
ParserContainer(HeaderFieldValueList* hfvs, |
27 |
Headers::Type type = Headers::UNKNOWN) |
28 |
: ParserContainerBase(type) |
29 |
{ |
30 |
for (HeaderFieldValueList::iterator i = hfvs->begin(); |
31 |
i != hfvs->end(); i++) |
32 |
{ |
33 |
// create, store without copying -- |
34 |
// keeps the HeaderFieldValue from reallocating its buffer |
35 |
mParsers.push_back(new T(*i, type)); |
36 |
} |
37 |
} |
38 |
|
39 |
ParserContainer(const ParserContainer& other) |
40 |
: ParserContainerBase(other) |
41 |
{} |
42 |
|
43 |
ParserContainer& operator=(const ParserContainer& other) |
44 |
{ |
45 |
return static_cast<ParserContainer&>(ParserContainerBase::operator=(other)); |
46 |
} |
47 |
|
48 |
T& front() { return *static_cast<T*>(mParsers.front());} |
49 |
T& back() { return *static_cast<T*>(mParsers.back());} |
50 |
const T& front() const { return *static_cast<T*>(mParsers.front());} |
51 |
const T& back() const { return *static_cast<T*>(mParsers.back());} |
52 |
|
53 |
void push_front(const T & t) { mParsers.insert(mParsers.begin(), new T(t)); } |
54 |
void push_back(const T & t) { mParsers.push_back(new T(t)); } |
55 |
|
56 |
ParserContainer reverse() const |
57 |
{ |
58 |
ParserContainer tmp(*this); |
59 |
std::reverse(tmp.mParsers.begin(), tmp.mParsers.end()); |
60 |
return tmp; |
61 |
} |
62 |
|
63 |
// .dlb. these can be partially hoisted as well |
64 |
class const_iterator; |
65 |
|
66 |
class iterator |
67 |
{ |
68 |
public: |
69 |
iterator(typename std::vector<ParserCategory*>::iterator i) : mIt(i){} |
70 |
iterator() {} |
71 |
|
72 |
iterator operator++() {iterator it(++mIt); return it;} |
73 |
iterator operator++(int) {iterator it(mIt++); return it;} |
74 |
iterator operator--() {iterator it(--mIt); return it;} |
75 |
iterator operator--(int) {iterator it(mIt--); return it;} |
76 |
bool operator!=(const iterator& rhs) { return mIt != rhs.mIt; } |
77 |
bool operator==(const iterator& rhs) { return mIt == rhs.mIt; } |
78 |
bool operator!=(const const_iterator& rhs) { return mIt != rhs.mIt; } |
79 |
bool operator==(const const_iterator& rhs) { return mIt == rhs.mIt; } |
80 |
iterator& operator=(const iterator& rhs) { mIt = rhs.mIt; return *this;} |
81 |
T& operator*() {return *static_cast<T*>(*mIt);} |
82 |
T* operator->() {return static_cast<T*>(*mIt);} |
83 |
private: |
84 |
typename std::vector<ParserCategory*>::iterator mIt; |
85 |
friend class const_iterator; |
86 |
friend class ParserContainer; |
87 |
}; |
88 |
|
89 |
class const_iterator |
90 |
{ |
91 |
public: |
92 |
const_iterator(std::vector<ParserCategory*>::const_iterator i) : mIt(i) {} |
93 |
const_iterator() {} |
94 |
|
95 |
const_iterator operator++() {const_iterator it(++mIt); return it;} |
96 |
const_iterator operator++(int) {const_iterator it(mIt++); return it;} |
97 |
const_iterator operator--() {const_iterator it(--mIt); return it;} |
98 |
const_iterator operator--(int) {const_iterator it(mIt--); return it;} |
99 |
bool operator!=(const const_iterator& rhs) { return mIt != rhs.mIt; } |
100 |
bool operator==(const const_iterator& rhs) { return mIt == rhs.mIt; } |
101 |
bool operator!=(const iterator& rhs) { return mIt != rhs.mIt; } |
102 |
bool operator==(const iterator& rhs) { return mIt == rhs.mIt; } |
103 |
const_iterator& operator=(const const_iterator& rhs) { mIt = rhs.mIt; return *this;} |
104 |
const_iterator& operator=(const iterator& rhs) { mIt = rhs.mIt; return *this;} |
105 |
const T& operator*() {return *static_cast<T*>(*mIt);} |
106 |
const T* operator->() {return static_cast<T*>(*mIt);} |
107 |
private: |
108 |
friend class iterator; |
109 |
typename std::vector<ParserCategory*>::const_iterator mIt; |
110 |
}; |
111 |
|
112 |
iterator begin() { return iterator(mParsers.begin()); } |
113 |
iterator end() { return iterator(mParsers.end()); } |
114 |
|
115 |
iterator erase(iterator i) |
116 |
{ |
117 |
delete *i.mIt; |
118 |
return iterator(mParsers.erase(i.mIt)); |
119 |
} |
120 |
|
121 |
bool find(const T& rhs) const |
122 |
{ |
123 |
for (typename std::vector<ParserCategory*>::const_iterator i = mParsers.begin(); |
124 |
i != mParsers.end(); ++i) |
125 |
{ |
126 |
// operator== defined by default, but often not usefully |
127 |
if (rhs.isEqual(*static_cast<T*>(*i))) |
128 |
{ |
129 |
return true; |
130 |
} |
131 |
} |
132 |
|
133 |
return false; |
134 |
} |
135 |
|
136 |
const_iterator begin() const { return const_iterator(mParsers.begin()); } |
137 |
const_iterator end() const { return const_iterator(mParsers.end()); } |
138 |
|
139 |
virtual ParserContainerBase* clone() const |
140 |
{ |
141 |
return new ParserContainer(*this); |
142 |
} |
143 |
}; |
144 |
|
145 |
} |
146 |
|
147 |
#endif |
148 |
|
149 |
/* ==================================================================== |
150 |
* The Vovida Software License, Version 1.0 |
151 |
* |
152 |
* Copyright (c) 2000-2005 |
153 |
* |
154 |
* Redistribution and use in source and binary forms, with or without |
155 |
* modification, are permitted provided that the following conditions |
156 |
* are met: |
157 |
* |
158 |
* 1. Redistributions of source code must retain the above copyright |
159 |
* notice, this list of conditions and the following disclaimer. |
160 |
* |
161 |
* 2. Redistributions in binary form must reproduce the above copyright |
162 |
* notice, this list of conditions and the following disclaimer in |
163 |
* the documentation and/or other materials provided with the |
164 |
* distribution. |
165 |
* |
166 |
* 3. The names "VOCAL", "Vovida Open Communication Application Library", |
167 |
* and "Vovida Open Communication Application Library (VOCAL)" must |
168 |
* not be used to endorse or promote products derived from this |
169 |
* software without prior written permission. For written |
170 |
* permission, please contact vocal@vovida.org. |
171 |
* |
172 |
* 4. Products derived from this software may not be called "VOCAL", nor |
173 |
* may "VOCAL" appear in their name, without prior written |
174 |
* permission of Vovida Networks, Inc. |
175 |
* |
176 |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
177 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
178 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
179 |
* NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
180 |
* NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
181 |
* IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
182 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
183 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
184 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
185 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
186 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
187 |
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
188 |
* DAMAGE. |
189 |
* |
190 |
* ==================================================================== |
191 |
* |
192 |
* This software consists of voluntary contributions made by Vovida |
193 |
* Networks, Inc. and many individuals on behalf of Vovida Networks, |
194 |
* Inc. For more information on Vovida Networks, Inc., please see |
195 |
* <http://www.vovida.org/>. |
196 |
* |
197 |
*/ |