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