]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Generic.h
Author: Francesco Chemolli <kinkie@squid-cache.org>
[thirdparty/squid.git] / src / Generic.h
1 /*
2 * $Id$
3 *
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32 #ifndef SQUID_GENERIC_H
33 #define SQUID_GENERIC_H
34
35 #include "dlink.h"
36
37 #if HAVE_OSTREAM
38 #include <ostream>
39 #endif
40
41 template <class _Arg, class _Result>
42 struct unary_function {
43 typedef _Arg argument_type;
44 typedef _Result result_type;
45 };
46
47 template <class L, class T>
48 T& for_each(L const &head, T& visitor)
49 {
50 for (L const *node = &head; node; node=node->next)
51 visitor(*node);
52
53 return visitor;
54 }
55
56 template <class T>
57 T& for_each(dlink_list const &collection, T& visitor)
58 {
59 for (dlink_node const *node = collection.head; node; node=node->next)
60 visitor(*(typename T::argument_type const *)node->data);
61
62 return visitor;
63 }
64
65 template <class S>
66 class Stack;
67
68 template <class E, class T>
69 T& for_each(Stack<E> const &collection, T& visitor)
70 {
71 for (size_t index = 0; index < collection.count; ++index)
72 visitor(*(typename T::argument_type const *)collection.items[index]);
73
74 return visitor;
75 };
76
77 /* RBC 20030718 - use this to provide instance expecting classes a pointer to a
78 * singleton
79 */
80
81 template <class C>
82 class InstanceToSingletonAdapter : public C
83 {
84
85 public:
86 void *operator new (size_t byteCount) { return ::operator new (byteCount);}
87
88 void operator delete (void *address) { ::operator delete (address);}
89
90 InstanceToSingletonAdapter(C const *instance) : theInstance (instance) {}
91
92 C const * operator-> () const {return theInstance; }
93
94 C * operator-> () {return const_cast<C *>(theInstance); }
95
96 C const & operator * () const {return *theInstance; }
97
98 C & operator * () {return *const_cast<C *>(theInstance); }
99
100 operator C const * () const {return theInstance;}
101
102 operator C *() {return const_cast<C *>(theInstance);}
103
104 private:
105 C const *theInstance;
106 };
107
108 template <class InputIterator , class Visitor>
109 Visitor& for_each(InputIterator from, InputIterator to, Visitor& visitor)
110 {
111 while (!(from == to)) {
112 typename InputIterator::value_type &value = *from;
113 ++from;
114 visitor(value);
115 }
116
117 return visitor;
118 }
119
120 /* generic ostream printer */
121 template <class Pointer>
122 struct PointerPrinter {
123 PointerPrinter(std::ostream &astream, std::string aDelimiter) : os(astream), delimiter (aDelimiter) {}
124
125 void operator () (Pointer aNode) {
126 os << *aNode << delimiter;
127 }
128
129 std::ostream &os;
130 std::string delimiter;
131 };
132
133 #endif /* SQUID_GENERIC_H */