]>
Commit | Line | Data |
---|---|---|
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 | 37 | template <class _Arg, class _Result> |
26ac0430 | 38 | struct unary_function { |
742cce19 | 39 | typedef _Arg argument_type; |
40 | typedef _Result result_type; | |
41 | }; | |
42 | ||
43 | template <class L, class T> | |
44 | T& 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 | 52 | template <class T> |
53 | T& 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 | ||
b9ae18aa | 61 | /* RBC 20030718 - use this to provide instance expecting classes a pointer to a |
62 | * singleton | |
63 | */ | |
64 | ||
65 | template <class C> | |
b9ae18aa | 66 | class InstanceToSingletonAdapter : public C |
67 | { | |
68 | ||
69 | public: | |
70 | void *operator new (size_t byteCount) { return ::operator new (byteCount);} | |
71 | ||
72 | void operator delete (void *address) { ::operator delete (address);} | |
73 | ||
74 | InstanceToSingletonAdapter(C const *instance) : theInstance (instance) {} | |
75 | ||
76 | C const * operator-> () const {return theInstance; } | |
77 | ||
78 | C * operator-> () {return const_cast<C *>(theInstance); } | |
79 | ||
80 | C const & operator * () const {return *theInstance; } | |
81 | ||
82 | C & operator * () {return *const_cast<C *>(theInstance); } | |
83 | ||
84 | operator C const * () const {return theInstance;} | |
85 | ||
86 | operator C *() {return const_cast<C *>(theInstance);} | |
87 | ||
88 | private: | |
89 | C const *theInstance; | |
90 | }; | |
91 | ||
b8bad68c | 92 | template <class InputIterator , class Visitor> |
93 | Visitor& for_each(InputIterator from, InputIterator to, Visitor& visitor) | |
94 | { | |
95 | while (!(from == to)) { | |
96 | typename InputIterator::value_type &value = *from; | |
97 | ++from; | |
98 | visitor(value); | |
99 | } | |
100 | ||
101 | return visitor; | |
102 | } | |
103 | ||
104 | /* generic ostream printer */ | |
105 | template <class Pointer> | |
26ac0430 | 106 | struct PointerPrinter { |
b8bad68c | 107 | PointerPrinter(std::ostream &astream, std::string aDelimiter) : os(astream), delimiter (aDelimiter) {} |
108 | ||
26ac0430 | 109 | void operator () (Pointer aNode) { |
b8bad68c | 110 | os << *aNode << delimiter; |
111 | } | |
112 | ||
113 | std::ostream &os; | |
114 | std::string delimiter; | |
115 | }; | |
116 | ||
742cce19 | 117 | #endif /* SQUID_GENERIC_H */ |