]>
Commit | Line | Data |
---|---|---|
3a8c5551 | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
bbc27441 AJ |
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. | |
3a8c5551 AR |
7 | */ |
8 | ||
9 | #ifndef SQUID_IPC_MEM_FLEXIBLE_ARRAY_H | |
10 | #define SQUID_IPC_MEM_FLEXIBLE_ARRAY_H | |
11 | ||
12 | // sometimes required for placement-new operator to be declared | |
adc2a453 | 13 | #include <new> |
3a8c5551 AR |
14 | |
15 | namespace Ipc | |
16 | { | |
17 | ||
18 | namespace Mem | |
19 | { | |
20 | ||
21 | /// A "flexible array" of Items inside some shared memory space. | |
22 | /// A portable equivalent of a "Item items[];" data member. | |
23 | /// Some compilers such as Clang can only handle flexible arrays of PODs, | |
24 | /// and the current C++ standard does not allow flexible arrays at all. | |
25 | template <class Item> | |
26 | class FlexibleArray | |
27 | { | |
28 | public: | |
29 | explicit FlexibleArray(const int capacity) { | |
30 | if (capacity > 1) // the first item is initialized automatically | |
ed09a6c0 | 31 | new (raw()+1) Item[capacity-1]; |
3a8c5551 AR |
32 | } |
33 | ||
34 | Item &operator [](const int idx) { return items[idx]; } | |
35 | const Item &operator [](const int idx) const { return items[idx]; } | |
36 | ||
37 | //const Item *operator ()() const { return items; } | |
38 | //Item *operator ()() { return items; } | |
39 | ||
40 | Item *raw() { return items; } | |
41 | ||
42 | private: | |
43 | Item items[1]; // ensures proper alignment of array elements | |
44 | }; | |
45 | ||
46 | } // namespace Mem | |
47 | ||
48 | } // namespace Ipc | |
49 | ||
50 | #endif /* SQUID_IPC_MEM_FLEXIBLE_ARRAY_H */ | |
f53969cc | 51 |