]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/mem/FlexibleArray.h
Added explicit braces to avoid ambiguous "else" and make GCC happy.
[thirdparty/squid.git] / src / ipc / mem / FlexibleArray.h
CommitLineData
3a8c5551
AR
1/*
2 */
3
4#ifndef SQUID_IPC_MEM_FLEXIBLE_ARRAY_H
5#define SQUID_IPC_MEM_FLEXIBLE_ARRAY_H
6
7// sometimes required for placement-new operator to be declared
8#include <new>
9
10namespace Ipc
11{
12
13namespace Mem
14{
15
16/// A "flexible array" of Items inside some shared memory space.
17/// A portable equivalent of a "Item items[];" data member.
18/// Some compilers such as Clang can only handle flexible arrays of PODs,
19/// and the current C++ standard does not allow flexible arrays at all.
20template <class Item>
21class FlexibleArray
22{
23public:
24 explicit FlexibleArray(const int capacity) {
25 if (capacity > 1) // the first item is initialized automatically
26 new (items+1) Item[capacity-1];
27 }
28
29 Item &operator [](const int idx) { return items[idx]; }
30 const Item &operator [](const int idx) const { return items[idx]; }
31
32 //const Item *operator ()() const { return items; }
33 //Item *operator ()() { return items; }
34
35 Item *raw() { return items; }
36
37private:
38 Item items[1]; // ensures proper alignment of array elements
39};
40
41} // namespace Mem
42
43} // namespace Ipc
44
45#endif /* SQUID_IPC_MEM_FLEXIBLE_ARRAY_H */