2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_GENERIC_H
10 #define SQUID_GENERIC_H
16 template <class _Arg
, class _Result
>
17 struct unary_function
{
18 typedef _Arg argument_type
;
19 typedef _Result result_type
;
22 template <class L
, class T
>
23 T
& for_each(L
const &head
, T
& visitor
)
25 for (L
const *node
= &head
; node
; node
=node
->next
)
32 T
& for_each(dlink_list
const &collection
, T
& visitor
)
34 for (dlink_node
const *node
= collection
.head
; node
; node
=node
->next
)
35 visitor(*(typename
T::argument_type
const *)node
->data
);
40 /* RBC 20030718 - use this to provide instance expecting classes a pointer to a
45 class InstanceToSingletonAdapter
: public C
49 void *operator new (size_t byteCount
) { return ::operator new (byteCount
);}
51 void operator delete (void *address
) { ::operator delete (address
);}
53 InstanceToSingletonAdapter(C
const *instance
) : theInstance (instance
) {}
55 C
const * operator-> () const {return theInstance
; }
57 C
* operator-> () {return const_cast<C
*>(theInstance
); }
59 C
const & operator * () const {return *theInstance
; }
61 C
& operator * () {return *const_cast<C
*>(theInstance
); }
63 operator C
const * () const {return theInstance
;}
65 operator C
*() {return const_cast<C
*>(theInstance
);}
71 template <class InputIterator
, class Visitor
>
72 Visitor
& for_each(InputIterator from
, InputIterator to
, Visitor
& visitor
)
74 while (!(from
== to
)) {
75 typename
InputIterator::value_type
&value
= *from
;
83 /* generic ostream printer */
84 template <class Pointer
>
85 struct PointerPrinter
{
86 PointerPrinter(std::ostream
&astream
, std::string aDelimiter
) : os(astream
), delimiter (aDelimiter
) {}
88 void operator () (Pointer aNode
) {
89 os
<< *aNode
<< delimiter
;
93 std::string delimiter
;
96 #endif /* SQUID_GENERIC_H */