]> git.ipfire.org Git - thirdparty/squid.git/blame - src/DescriptorSet.h
Various audit updates
[thirdparty/squid.git] / src / DescriptorSet.h
CommitLineData
877ce944
AR
1#ifndef SQUID_DESCRIPTOR_SET_H
2#define SQUID_DESCRIPTOR_SET_H
3
3d41e53a 4#include <iosfwd>
877ce944
AR
5
6/** \ingroup Comm
7
8 \todo: Should we use std::set<int> with its flexibility? Our implementation
9 has constant overhead, which is smaller than log(n) of std::set.
10
11an unordered collection of unique descriptors with O(1) add/del/has ops */
26ac0430
AJ
12class DescriptorSet
13{
877ce944
AR
14public:
15 // for STL compatibility, should we decide to switch to std::set or similar
16 typedef const int *const_iterator;
17
18 DescriptorSet();
19 ~DescriptorSet();
20
21 /// checks whether fd is in the set
26ac0430
AJ
22 bool has(const int fd) const {
23 return 0 <= fd && fd < capacity_ &&
24 index_[fd] >= 0;
25 }
877ce944
AR
26
27 bool add(int fd); ///< adds if unique; returns true if added
28 bool del(int fd); ///< deletes if there; returns true if deleted
29 int pop(); ///< deletes and returns one descriptor, in unspecified order
30
31 bool empty() const { return !size_; } ///< number of descriptors in the set
32
33 /// begin iterator a la STL; may become invalid if the object is modified
34 const_iterator begin() const { return descriptors_; }
35 /// end iterator a la STL; may become invalid if the object is modified
36 const_iterator end() const { return begin() + size_; }
37
38 /// outputs debugging info about the set
39 void print(std::ostream &os) const;
40
41private:
42 // these would be easy to support when needed; prohibit for now
43 DescriptorSet(const DescriptorSet &s); // declared but undefined
44 DescriptorSet &operator =(const DescriptorSet &s); // declared, undefined
45
46 int *descriptors_; ///< descriptor values in random order
47 int *index_; ///< descriptor:position index into descriptors_
48 int capacity_; ///< total number of descriptor slots
49 int size_; ///< number of descriptors in the set
50};
51
52/// convenience wrapper to be used in debugs() context
53inline std::ostream &
54operator <<(std::ostream &os, const DescriptorSet &ds)
55{
56 ds.print(os);
57 return os;
58}
59
60#endif /* SQUID_DESCRIPTOR_SET_H */
61