3 * SQUID Web Proxy Cache http://www.squid-cache.org/
4 * ----------------------------------------------------------
6 * Squid is the result of efforts by numerous individuals from
7 * the Internet community; see the CONTRIBUTORS file for full
8 * details. Many organizations have provided support for Squid's
9 * development; see the SPONSORS file for full details. Squid is
10 * Copyrighted (C) 2001 by the Regents of the University of
11 * California; see the COPYRIGHT file for full details. Squid
12 * incorporates software developed and/or copyrighted by other
13 * sources; see the CREDITS file for full details.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 #ifndef SQUID_GENERIC_H
31 #define SQUID_GENERIC_H
39 template <class _Arg
, class _Result
>
40 struct unary_function
{
41 typedef _Arg argument_type
;
42 typedef _Result result_type
;
45 template <class L
, class T
>
46 T
& for_each(L
const &head
, T
& visitor
)
48 for (L
const *node
= &head
; node
; node
=node
->next
)
55 T
& for_each(dlink_list
const &collection
, T
& visitor
)
57 for (dlink_node
const *node
= collection
.head
; node
; node
=node
->next
)
58 visitor(*(typename
T::argument_type
const *)node
->data
);
66 template <class E
, class T
>
67 T
& for_each(Stack
<E
> const &collection
, T
& visitor
)
69 for (size_t index
= 0; index
< collection
.count
; ++index
)
70 visitor(*(typename
T::argument_type
const *)collection
.items
[index
]);
75 /* RBC 20030718 - use this to provide instance expecting classes a pointer to a
80 class InstanceToSingletonAdapter
: public C
84 void *operator new (size_t byteCount
) { return ::operator new (byteCount
);}
86 void operator delete (void *address
) { ::operator delete (address
);}
88 InstanceToSingletonAdapter(C
const *instance
) : theInstance (instance
) {}
90 C
const * operator-> () const {return theInstance
; }
92 C
* operator-> () {return const_cast<C
*>(theInstance
); }
94 C
const & operator * () const {return *theInstance
; }
96 C
& operator * () {return *const_cast<C
*>(theInstance
); }
98 operator C
const * () const {return theInstance
;}
100 operator C
*() {return const_cast<C
*>(theInstance
);}
103 C
const *theInstance
;
106 template <class InputIterator
, class Visitor
>
107 Visitor
& for_each(InputIterator from
, InputIterator to
, Visitor
& visitor
)
109 while (!(from
== to
)) {
110 typename
InputIterator::value_type
&value
= *from
;
118 /* generic ostream printer */
119 template <class Pointer
>
120 struct PointerPrinter
{
121 PointerPrinter(std::ostream
&astream
, std::string aDelimiter
) : os(astream
), delimiter (aDelimiter
) {}
123 void operator () (Pointer aNode
) {
124 os
<< *aNode
<< delimiter
;
128 std::string delimiter
;
131 #endif /* SQUID_GENERIC_H */