]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Generic.h
Merged from trunk
[thirdparty/squid.git] / src / Generic.h
1 /*
2 *
3 * SQUID Web Proxy Cache http://www.squid-cache.org/
4 * ----------------------------------------------------------
5 *
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.
14 *
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.
19 *
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.
24 *
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.
28 *
29 */
30 #ifndef SQUID_GENERIC_H
31 #define SQUID_GENERIC_H
32
33 #include "dlink.h"
34
35 #if HAVE_OSTREAM
36 #include <ostream>
37 #endif
38
39 template <class _Arg, class _Result>
40 struct unary_function {
41 typedef _Arg argument_type;
42 typedef _Result result_type;
43 };
44
45 template <class L, class T>
46 T& for_each(L const &head, T& visitor)
47 {
48 for (L const *node = &head; node; node=node->next)
49 visitor(*node);
50
51 return visitor;
52 }
53
54 template <class T>
55 T& for_each(dlink_list const &collection, T& visitor)
56 {
57 for (dlink_node const *node = collection.head; node; node=node->next)
58 visitor(*(typename T::argument_type const *)node->data);
59
60 return visitor;
61 }
62
63 template <class S>
64 class Stack;
65
66 template <class E, class T>
67 T& for_each(Stack<E> const &collection, T& visitor)
68 {
69 for (size_t index = 0; index < collection.count; ++index)
70 visitor(*(typename T::argument_type const *)collection.items[index]);
71
72 return visitor;
73 };
74
75 /* RBC 20030718 - use this to provide instance expecting classes a pointer to a
76 * singleton
77 */
78
79 template <class C>
80 class InstanceToSingletonAdapter : public C
81 {
82
83 public:
84 void *operator new (size_t byteCount) { return ::operator new (byteCount);}
85
86 void operator delete (void *address) { ::operator delete (address);}
87
88 InstanceToSingletonAdapter(C const *instance) : theInstance (instance) {}
89
90 C const * operator-> () const {return theInstance; }
91
92 C * operator-> () {return const_cast<C *>(theInstance); }
93
94 C const & operator * () const {return *theInstance; }
95
96 C & operator * () {return *const_cast<C *>(theInstance); }
97
98 operator C const * () const {return theInstance;}
99
100 operator C *() {return const_cast<C *>(theInstance);}
101
102 private:
103 C const *theInstance;
104 };
105
106 template <class InputIterator , class Visitor>
107 Visitor& for_each(InputIterator from, InputIterator to, Visitor& visitor)
108 {
109 while (!(from == to)) {
110 typename InputIterator::value_type &value = *from;
111 ++from;
112 visitor(value);
113 }
114
115 return visitor;
116 }
117
118 /* generic ostream printer */
119 template <class Pointer>
120 struct PointerPrinter {
121 PointerPrinter(std::ostream &astream, std::string aDelimiter) : os(astream), delimiter (aDelimiter) {}
122
123 void operator () (Pointer aNode) {
124 os << *aNode << delimiter;
125 }
126
127 std::ostream &os;
128 std::string delimiter;
129 };
130
131 #endif /* SQUID_GENERIC_H */