//////////////////////////////////////////////////////////////////////////// // // Copyright 2016 Realm Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or utilied. // See the License for the specific language governing permissions and // limitations under the License. // //////////////////////////////////////////////////////////////////////////// #ifndef REALM_OS_ALIGNED_UNION_HPP #define REALM_OS_ALIGNED_UNION_HPP #include #include #include namespace realm { // Provide our own implementation of max as GCC 4.9's is not marked as constexpr. namespace _impl { template static constexpr const T& constexpr_max(const T& a, const T& b) { return a > b ? a : b; } template static constexpr const T& constexpr_max(const T* begin, const T *end) { return begin + 1 == end ? *begin : constexpr_max(*begin, constexpr_max(begin + 1, end)); } template static constexpr const T& constexpr_max(std::initializer_list list) { return constexpr_max(list.begin(), list.end()); } } // namespace _impl namespace util { // Provide our own implementation of `std::aligned_union` as it is missing from GCC 4.9. template struct AlignedUnion { static constexpr size_t alignment_value = _impl::constexpr_max({alignof(Types)...}); static constexpr size_t storage_size = _impl::constexpr_max({Len, sizeof(Types)...}); using type = typename std::aligned_storage::type; }; } // namespace util } // namespace realm #endif // REALM_OS_ALIGNED_UNION_HPP