comparison osx/include/kj/one-of.h @ 62:0994c39f1e94

Cap'n Proto v0.6 + build for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 22 May 2017 10:01:37 +0100
parents 3ab5a40c4e3b
children
comparison
equal deleted inserted replaced
61:d101c4099725 62:0994c39f1e94
72 KJ_IREQUIRE(is<T>(), "Must check OneOf::is<T>() before calling get<T>()."); 72 KJ_IREQUIRE(is<T>(), "Must check OneOf::is<T>() before calling get<T>().");
73 return *reinterpret_cast<const T*>(space); 73 return *reinterpret_cast<const T*>(space);
74 } 74 }
75 75
76 template <typename T, typename... Params> 76 template <typename T, typename... Params>
77 void init(Params&&... params) { 77 T& init(Params&&... params) {
78 if (tag != 0) destroy(); 78 if (tag != 0) destroy();
79 ctor(*reinterpret_cast<T*>(space), kj::fwd<Params>(params)...); 79 ctor(*reinterpret_cast<T*>(space), kj::fwd<Params>(params)...);
80 tag = typeIndex<T>(); 80 tag = typeIndex<T>();
81 return *reinterpret_cast<T*>(space);
81 } 82 }
82 83
83 private: 84 private:
84 uint tag; 85 uint tag;
85 86
92 } 93 }
93 // Returns the maximum of all the parameters. 94 // Returns the maximum of all the parameters.
94 // TODO(someday): Generalize the above template and make it common. I tried, but C++ decided to 95 // TODO(someday): Generalize the above template and make it common. I tried, but C++ decided to
95 // be difficult so I cut my losses. 96 // be difficult so I cut my losses.
96 97
98 static constexpr auto spaceSize = maxSize(sizeof(Variants)...);
99 // TODO(msvc): This constant could just as well go directly inside space's bracket's, where it's
100 // used, but MSVC suffers a parse error on `...`.
101
97 union { 102 union {
98 byte space[maxSize(sizeof(Variants)...)]; 103 byte space[spaceSize];
99 104
100 void* forceAligned; 105 void* forceAligned;
101 // TODO(someday): Use C++11 alignas() once we require GCC 4.8 / Clang 3.3. 106 // TODO(someday): Use C++11 alignas() once we require GCC 4.8 / Clang 3.3.
102 }; 107 };
103 108