From 9a32cc1680d4e112cf04269f6f219733a2884cae Mon Sep 17 00:00:00 2001 From: Alex Robenko Date: Thu, 5 Dec 2024 09:06:17 +1000 Subject: [PATCH] Added comms::util::construct() functions. --- include/comms/comms.h | 3 +- include/comms/util/StringView.h | 2 +- include/comms/util/assign.h | 2 +- include/comms/util/construct.h | 34 +++++++++++ include/comms/util/details/AssignHelper.h | 15 ++--- include/comms/util/details/ConstructHelper.h | 59 ++++++++++++++++++++ test/Util.th | 16 ++++++ 7 files changed, 119 insertions(+), 12 deletions(-) create mode 100644 include/comms/util/construct.h create mode 100644 include/comms/util/details/ConstructHelper.h diff --git a/include/comms/comms.h b/include/comms/comms.h index 095c153..9f7b58a 100644 --- a/include/comms/comms.h +++ b/include/comms/comms.h @@ -28,6 +28,7 @@ #include "comms/MsgDispatcher.h" #include "comms/GenericMessage.h" -#include "comms/util/detect.h" #include "comms/util/assign.h" +#include "comms/util/construct.h" +#include "comms/util/detect.h" #include "comms/util/type_traits.h" diff --git a/include/comms/util/StringView.h b/include/comms/util/StringView.h index 50f9180..6d01d1f 100644 --- a/include/comms/util/StringView.h +++ b/include/comms/util/StringView.h @@ -241,7 +241,7 @@ class StringView : public ArrayView return Base::operator[](pos); } - /// @brief Similar to std::string::at() + /// @brief Similar to std::string_view::at() /// @details Checks the range with @ref COMMS_ASSERT() macro without throwing exception. const_reference at(size_type pos) const { diff --git a/include/comms/util/assign.h b/include/comms/util/assign.h index 891739e..312194c 100644 --- a/include/comms/util/assign.h +++ b/include/comms/util/assign.h @@ -10,7 +10,7 @@ #pragma once -#include "details/AssignHelper.h" +#include "comms/util/details/AssignHelper.h" namespace comms { diff --git a/include/comms/util/construct.h b/include/comms/util/construct.h new file mode 100644 index 0000000..bf15fc4 --- /dev/null +++ b/include/comms/util/construct.h @@ -0,0 +1,34 @@ +// +// 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 +/// @brief Provides helper construct() function to allow easy construction of various collection types. + +#pragma once + +#include "comms/util/details/ConstructHelper.h" + +namespace comms +{ + +namespace util +{ + +/// @brief Construct collection objects given two range iterators +/// @details The function selects proper constructor of the selected type +/// @param[in] from Iterator to the first element of the range +/// @param[in] to Iterator to one behind the last element of the range. +template +T construct(TIter from, TIter to) +{ + return details::ConstructHelper::construct(from, to); +} + +} // namespace util + +} // namespace comms + diff --git a/include/comms/util/details/AssignHelper.h b/include/comms/util/details/AssignHelper.h index 183fb6b..7623512 100644 --- a/include/comms/util/details/AssignHelper.h +++ b/include/comms/util/details/AssignHelper.h @@ -5,9 +5,6 @@ // 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 -/// Various compile-time detection functions of whether specific member functions and/or types exist - #pragma once #include @@ -110,12 +107,12 @@ class AssignHelper template static void assignInternal(T& obj, TIter from, TIter to, StdSpanTag) { - using ObjType = typename std::decay::type; - using ConstPointerType = typename ObjType::const_pointer; - using PointerType = typename ObjType::pointer; - auto fromPtr = const_cast(reinterpret_cast(&(*from))); - auto toPtr = const_cast(reinterpret_cast(&(*to))); - assignInternal(obj, fromPtr, toPtr, UsePtrSizeConstructorTag()); + // using ObjType = typename std::decay::type; + // using ConstPointerType = typename ObjType::const_pointer; + // using PointerType = typename ObjType::pointer; + // auto fromPtr = const_cast(reinterpret_cast(&(*from))); + // auto toPtr = const_cast(reinterpret_cast(&(*to))); + assignInternal(obj, from, to, UsePtrSizeConstructorTag()); } }; diff --git a/include/comms/util/details/ConstructHelper.h b/include/comms/util/details/ConstructHelper.h new file mode 100644 index 0000000..d66199b --- /dev/null +++ b/include/comms/util/details/ConstructHelper.h @@ -0,0 +1,59 @@ +// +// 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/. + +#pragma once + +#include "comms/CompileControl.h" + +#include + +#if COMMS_HAS_CPP20_SPAN +#include +#endif // #if COMMS_HAS_CPP20_SPAN + +namespace comms +{ + +namespace util +{ + +namespace details +{ + +template +class ConstructHelper +{ +public: + using RetType = T; + + template + static RetType construct(TIter from, TIter to) + { + return RetType(from, to); + } +}; + +#if COMMS_HAS_CPP20_SPAN +template +class ConstructHelper> +{ +public: + using RetType = std::span; + + template + static RetType construct(TIter from, TIter to) + { + return RetType(&(*from), static_cast(std::distance(from, to))); + } +}; +#endif // #if COMMS_HAS_CPP20_SPAN + +} // namespace details + +} // namespace util + +} // namespace comms diff --git a/test/Util.th b/test/Util.th index 51f09fb..6e38ad8 100644 --- a/test/Util.th +++ b/test/Util.th @@ -41,6 +41,7 @@ public: void test25(); void test26(); void test27(); + void test28(); }; void UtilTestSuite::test1() @@ -1065,3 +1066,18 @@ void UtilTestSuite::test27() TS_ASSERT_EQUALS(Str, s3); #endif } + +void UtilTestSuite::test28() +{ + static std::uint8_t Data[] = { + 0x01, 0x02, 0x03, 0x04 + }; + + using Data1 = std::vector; + [[maybe_unused]] auto data1 = comms::util::construct(std::begin(Data), std::end(Data)); + +#if COMMS_HAS_CPP20_SPAN + using Data2 = std::span; + [[maybe_unused]] auto data2 = comms::util::construct(std::begin(Data), std::end(Data)); +#endif // #if COMMS_HAS_CPP20_SPAN +}