]>
Commit | Line | Data |
---|---|---|
742cce19 | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
742cce19 | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
742cce19 | 7 | */ |
bbc27441 | 8 | |
742cce19 | 9 | #ifndef SQUID_GENERIC_H |
10 | #define SQUID_GENERIC_H | |
11 | ||
e1f7507e | 12 | #include "dlink.h" |
62e76326 | 13 | |
27e059d4 | 14 | #include <ostream> |
27e059d4 | 15 | |
e1f7507e | 16 | template <class _Arg, class _Result> |
26ac0430 | 17 | struct unary_function { |
742cce19 | 18 | typedef _Arg argument_type; |
19 | typedef _Result result_type; | |
20 | }; | |
21 | ||
22 | template <class L, class T> | |
23 | T& for_each(L const &head, T& visitor) | |
24 | { | |
25 | for (L const *node = &head; node; node=node->next) | |
62e76326 | 26 | visitor(*node); |
27 | ||
742cce19 | 28 | return visitor; |
29 | } | |
30 | ||
742cce19 | 31 | template <class T> |
32 | T& for_each(dlink_list const &collection, T& visitor) | |
33 | { | |
34 | for (dlink_node const *node = collection.head; node; node=node->next) | |
62e76326 | 35 | visitor(*(typename T::argument_type const *)node->data); |
36 | ||
742cce19 | 37 | return visitor; |
38 | } | |
39 | ||
b9ae18aa | 40 | /* RBC 20030718 - use this to provide instance expecting classes a pointer to a |
41 | * singleton | |
42 | */ | |
43 | ||
44 | template <class C> | |
b9ae18aa | 45 | class InstanceToSingletonAdapter : public C |
46 | { | |
47 | ||
48 | public: | |
49 | void *operator new (size_t byteCount) { return ::operator new (byteCount);} | |
50 | ||
51 | void operator delete (void *address) { ::operator delete (address);} | |
52 | ||
53 | InstanceToSingletonAdapter(C const *instance) : theInstance (instance) {} | |
54 | ||
55 | C const * operator-> () const {return theInstance; } | |
56 | ||
57 | C * operator-> () {return const_cast<C *>(theInstance); } | |
58 | ||
59 | C const & operator * () const {return *theInstance; } | |
60 | ||
61 | C & operator * () {return *const_cast<C *>(theInstance); } | |
62 | ||
63 | operator C const * () const {return theInstance;} | |
64 | ||
65 | operator C *() {return const_cast<C *>(theInstance);} | |
66 | ||
67 | private: | |
68 | C const *theInstance; | |
69 | }; | |
70 | ||
b8bad68c | 71 | template <class InputIterator , class Visitor> |
72 | Visitor& for_each(InputIterator from, InputIterator to, Visitor& visitor) | |
73 | { | |
74 | while (!(from == to)) { | |
75 | typename InputIterator::value_type &value = *from; | |
76 | ++from; | |
77 | visitor(value); | |
78 | } | |
79 | ||
80 | return visitor; | |
81 | } | |
82 | ||
83 | /* generic ostream printer */ | |
84 | template <class Pointer> | |
26ac0430 | 85 | struct PointerPrinter { |
b8bad68c | 86 | PointerPrinter(std::ostream &astream, std::string aDelimiter) : os(astream), delimiter (aDelimiter) {} |
87 | ||
26ac0430 | 88 | void operator () (Pointer aNode) { |
b8bad68c | 89 | os << *aNode << delimiter; |
90 | } | |
91 | ||
92 | std::ostream &os; | |
93 | std::string delimiter; | |
94 | }; | |
95 | ||
742cce19 | 96 | #endif /* SQUID_GENERIC_H */ |
f53969cc | 97 |