Skip to content

Commit

Permalink
Preparation for C++23 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Sep 10, 2024
1 parent e04c02e commit f7371cf
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 32 deletions.
1 change: 1 addition & 0 deletions include/comms/CompileControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
#define COMMS_IS_CPP14 (__cplusplus >= 201402L)
#define COMMS_IS_CPP17 (__cplusplus >= 201703L)
#define COMMS_IS_CPP20 (__cplusplus >= 202002L)
#define COMMS_IS_CPP23 (__cplusplus >= 202302L)

#if COMMS_IS_MSVC_2019_OR_BELOW // Visual Studio 2019
#undef COMMS_IS_CPP20
Expand Down
2 changes: 1 addition & 1 deletion include/comms/field/basic/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ class Variant<TFieldBase, TVersionDependency, std::tuple<TMembers...> > :
setVersion(VersionBaseImpl::version_);
}

ValueType storage_;
alignas(8) ValueType storage_;
std::size_t memIdx_ = MembersCount;
};

Expand Down
37 changes: 37 additions & 0 deletions include/comms/util/AlignedStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright 2024 - 2024 (C). Alex Robenko. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

/// @file comms/util/AlignedStorage.h
/// @brief Replacement of std::aligned_storage due to deprecation since C++23.

#pragma once

#include <cstddef>
#include <cstdint>
#include <type_traits>

namespace comms
{

namespace util
{

#if COMMS_IS_CPP23

template <std::size_t TSize, std::size_t TAlign>
using AlignedStorage = std::uint8_t[TSize];

#else // #if COMMS_IS_CPP23

template <std::size_t TSize, std::size_t TAlign>
using AlignedStorage = typename std::aligned_storage<TSize, TAlign>::type;

#endif // #if COMMS_IS_CPP23

} // namespace util

} // namespace comms
17 changes: 5 additions & 12 deletions include/comms/util/StaticQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "comms/Assert.h"
#include "comms/util/SizeToType.h"
#include "comms/util/type_traits.h"
#include "comms/util/AlignedStorage.h"

namespace comms
{
Expand All @@ -46,11 +47,7 @@ class StaticQueueBase

protected:
using ValueType = T;
using StorageType =
typename std::aligned_storage<
sizeof(ValueType),
std::alignment_of<ValueType>::value
>::type;
using StorageType = comms::util::AlignedStorage<sizeof(ValueType), std::alignment_of<ValueType>::value>;
using StorageTypePtr = StorageType*;
using ConstStorageTypePtr = const StorageType*;
using SizeType = std::size_t;
Expand Down Expand Up @@ -1232,11 +1229,7 @@ class CastWrapperQueueBase : public StaticQueueBase<TQueueElemType>

protected:
using ValueType = WrapperElemType;
using StorageType =
typename std::aligned_storage<
sizeof(ValueType),
std::alignment_of<ValueType>::value
>::type;
using StorageType = comms::util::AlignedStorage<sizeof(ValueType), std::alignment_of<ValueType>::value>;
using StorageTypePtr = StorageType*;
using Reference = ValueType&;
using ConstReference = const ValueType&;
Expand Down Expand Up @@ -1801,7 +1794,7 @@ class StaticQueueBaseOptimised<std::int64_t> : public CastWrapperQueueBase<std::
using Base = CastWrapperQueueBase<std::int64_t, std::uint64_t>;
protected:

using StorageTypePtr = stypename Base::StorageTypePtr;
using StorageTypePtr = typename Base::StorageTypePtr;

StaticQueueBaseOptimised(StorageTypePtr data, std::size_t capacity)
: Base(data, capacity)
Expand Down Expand Up @@ -2759,7 +2752,7 @@ class StaticQueue : public details::StaticQueueBaseOptimised<T>

private:
using ArrayType = std::array<StorageType, TSize>;
ArrayType array_;
alignas(alignof(T)) ArrayType array_;
};

/// @brief Const iterator for the elements of StaticQueue.
Expand Down
16 changes: 4 additions & 12 deletions include/comms/util/StaticVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "comms/CompileControl.h"
#include "comms/Assert.h"
#include "comms/util/AlignedStorage.h"

COMMS_GNU_WARNING_PUSH

Expand Down Expand Up @@ -51,12 +52,7 @@ class StaticVectorBase
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

using CellType =
typename std::aligned_storage<
sizeof(T),
std::alignment_of<T>::value
>::type;
using CellType = comms::util::AlignedStorage<sizeof(T), std::alignment_of<T>::value>;

static_assert(sizeof(CellType) == sizeof(T), "Type T must be padded");

Expand Down Expand Up @@ -552,13 +548,9 @@ class StaticVectorBase
template <typename T, std::size_t TSize>
struct StaticVectorStorageBase
{
using ElementType = typename std::aligned_storage<
sizeof(T),
std::alignment_of<T>::value
>::type;

using ElementType = comms::util::AlignedStorage<sizeof(T), std::alignment_of<T>::value>;
using StorageType = std::array<ElementType, TSize>;
StorageType data_;
alignas(alignof(T)) StorageType data_;
};

template <typename T, std::size_t TSize>
Expand Down
7 changes: 2 additions & 5 deletions include/comms/util/Tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "comms/CompileControl.h"
#include "comms/util/type_traits.h"
#include "comms/util/AlignedStorage.h"
#include "comms/Assert.h"

COMMS_GNU_WARNING_PUSH
Expand Down Expand Up @@ -175,11 +176,7 @@ class TupleAsAlignedUnion<std::tuple<TTypes...> >
using SizeType =
typename details::TupleAsAlignedUnionHelper<(0U < sizeof...(TTypes))>::template SizeType<TTypes...>;
public:
using Type =
typename std::aligned_storage<
SizeType::value,
AlignmentType::value
>::type;
using Type = comms::util::AlignedStorage<SizeType::value, AlignmentType::value>;
};
/// @endcond

Expand Down
4 changes: 2 additions & 2 deletions include/comms/util/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class InPlaceSingle
private:
using AlignedStorage = typename TupleAsAlignedUnion<TAllTypes>::Type;

AlignedStorage place_;
alignas(8) AlignedStorage place_;
bool allocated_ = false;

};
Expand Down Expand Up @@ -582,7 +582,7 @@ class InPlaceSingleNoVirtualDestructor
private:
using AlignedStorage = typename TupleAsAlignedUnion<TAllocMessages>::Type;

AlignedStorage place_;
alignas(8) AlignedStorage place_;
bool allocated_ = false;

};
Expand Down

0 comments on commit f7371cf

Please sign in to comment.