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