]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | namespace Ipc | |
11 | { | |
12 | ||
13 | namespace 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. | |
20 | template <class Item> | |
21 | class FlexibleArray | |
22 | { | |
23 | public: | |
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 | ||
37 | private: | |
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 */ |