]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Generic.h
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / Generic.h
1 /*
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #ifndef SQUID_GENERIC_H
10 #define SQUID_GENERIC_H
11
12 #include "dlink.h"
13
14 #include <ostream>
15
16 template <class _Arg, class _Result>
17 struct unary_function {
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)
26 visitor(*node);
27
28 return visitor;
29 }
30
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)
35 visitor(*(typename T::argument_type const *)node->data);
36
37 return visitor;
38 }
39
40 /* RBC 20030718 - use this to provide instance expecting classes a pointer to a
41 * singleton
42 */
43
44 template <class C>
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
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>
85 struct PointerPrinter {
86 PointerPrinter(std::ostream &astream, std::string aDelimiter) : os(astream), delimiter (aDelimiter) {}
87
88 void operator () (Pointer aNode) {
89 os << *aNode << delimiter;
90 }
91
92 std::ostream &os;
93 std::string delimiter;
94 };
95
96 #endif /* SQUID_GENERIC_H */
97