]>
Commit | Line | Data |
---|---|---|
742cce19 | 1 | /* |
262a0e14 | 2 | * $Id$ |
742cce19 | 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. | |
26ac0430 | 21 | * |
742cce19 | 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. | |
26ac0430 | 26 | * |
742cce19 | 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 | */ | |
742cce19 | 32 | #ifndef SQUID_GENERIC_H |
33 | #define SQUID_GENERIC_H | |
34 | ||
e1f7507e AJ |
35 | /** \todo CLEANUP: checks towrap this header properly */ |
36 | #include <ostream> | |
37 | ||
38 | #include "dlink.h" | |
62e76326 | 39 | |
e1f7507e | 40 | template <class _Arg, class _Result> |
26ac0430 | 41 | struct unary_function { |
742cce19 | 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) | |
62e76326 | 50 | visitor(*node); |
51 | ||
742cce19 | 52 | return visitor; |
53 | } | |
54 | ||
742cce19 | 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) | |
62e76326 | 59 | visitor(*(typename T::argument_type const *)node->data); |
60 | ||
742cce19 | 61 | return visitor; |
62 | } | |
63 | ||
91caca83 | 64 | template <class S> |
91caca83 | 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 | ||
b9ae18aa | 76 | /* RBC 20030718 - use this to provide instance expecting classes a pointer to a |
77 | * singleton | |
78 | */ | |
79 | ||
80 | template <class C> | |
b9ae18aa | 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 | ||
b8bad68c | 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> | |
26ac0430 | 121 | struct PointerPrinter { |
b8bad68c | 122 | PointerPrinter(std::ostream &astream, std::string aDelimiter) : os(astream), delimiter (aDelimiter) {} |
123 | ||
26ac0430 | 124 | void operator () (Pointer aNode) { |
b8bad68c | 125 | os << *aNode << delimiter; |
126 | } | |
127 | ||
128 | std::ostream &os; | |
129 | std::string delimiter; | |
130 | }; | |
131 | ||
742cce19 | 132 | #endif /* SQUID_GENERIC_H */ |