Generic.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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_SRC_GENERIC_H
10 #define SQUID_SRC_GENERIC_H
11 
12 #include "dlink.h"
13 
14 #include <ostream>
15 
16 template <class _Arg, class _Result>
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>
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>
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_SRC_GENERIC_H */
97 
std::ostream & os
Definition: Generic.h:92
Definition: parse.c:104
struct node * next
Definition: parse.c:105
PointerPrinter(std::ostream &astream, std::string aDelimiter)
Definition: Generic.h:86
InstanceToSingletonAdapter(C const *instance)
Definition: Generic.h:53
_Arg argument_type
Definition: Generic.h:18
void operator()(Pointer aNode)
Definition: Generic.h:88
static uint32 C
Definition: md4.c:43
std::string delimiter
Definition: Generic.h:93
const C & operator*() const
Definition: Generic.h:59
squidaio_request_t * head
Definition: aiops.cc:127
const C * operator->() const
Definition: Generic.h:55
_Result result_type
Definition: Generic.h:19
T & for_each(L const &head, T &visitor)
Definition: Generic.h:23

 

Introduction

Documentation

Support

Miscellaneous