Skip to content

Commit

Permalink
Added comms::util::construct() functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Dec 4, 2024
1 parent cdd0883 commit 9a32cc1
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 12 deletions.
3 changes: 2 additions & 1 deletion include/comms/comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion include/comms/util/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class StringView : public ArrayView<char>
return Base::operator[](pos);
}

/// @brief Similar to <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/at">std::string::at()</a>
/// @brief Similar to <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/at">std::string_view::at()</a>
/// @details Checks the range with @ref COMMS_ASSERT() macro without throwing exception.
const_reference at(size_type pos) const
{
Expand Down
2 changes: 1 addition & 1 deletion include/comms/util/assign.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#pragma once

#include "details/AssignHelper.h"
#include "comms/util/details/AssignHelper.h"

namespace comms
{
Expand Down
34 changes: 34 additions & 0 deletions include/comms/util/construct.h
Original file line number Diff line number Diff line change
@@ -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 <typename T, typename TIter>
T construct(TIter from, TIter to)
{
return details::ConstructHelper<T>::construct(from, to);
}

} // namespace util

} // namespace comms

15 changes: 6 additions & 9 deletions include/comms/util/details/AssignHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type_traits>
Expand Down Expand Up @@ -110,12 +107,12 @@ class AssignHelper
template <typename T, typename TIter, typename... TParams>
static void assignInternal(T& obj, TIter from, TIter to, StdSpanTag<TParams...>)
{
using ObjType = typename std::decay<decltype(obj)>::type;
using ConstPointerType = typename ObjType::const_pointer;
using PointerType = typename ObjType::pointer;
auto fromPtr = const_cast<PointerType>(reinterpret_cast<ConstPointerType>(&(*from)));
auto toPtr = const_cast<PointerType>(reinterpret_cast<ConstPointerType>(&(*to)));
assignInternal(obj, fromPtr, toPtr, UsePtrSizeConstructorTag<TParams...>());
// using ObjType = typename std::decay<decltype(obj)>::type;
// using ConstPointerType = typename ObjType::const_pointer;
// using PointerType = typename ObjType::pointer;
// auto fromPtr = const_cast<PointerType>(reinterpret_cast<ConstPointerType>(&(*from)));
// auto toPtr = const_cast<PointerType>(reinterpret_cast<ConstPointerType>(&(*to)));
assignInternal(obj, from, to, UsePtrSizeConstructorTag<TParams...>());
}
};

Expand Down
59 changes: 59 additions & 0 deletions include/comms/util/details/ConstructHelper.h
Original file line number Diff line number Diff line change
@@ -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 <iterator>

#if COMMS_HAS_CPP20_SPAN
#include <span>
#endif // #if COMMS_HAS_CPP20_SPAN

namespace comms
{

namespace util
{

namespace details
{

template <typename T>
class ConstructHelper
{
public:
using RetType = T;

template <typename TIter>
static RetType construct(TIter from, TIter to)
{
return RetType(from, to);
}
};

#if COMMS_HAS_CPP20_SPAN
template <typename T, std::size_t TExtent>
class ConstructHelper<std::span<T, TExtent>>
{
public:
using RetType = std::span<T, TExtent>;

template <typename TIter>
static RetType construct(TIter from, TIter to)
{
return RetType(&(*from), static_cast<typename RetType::size_type>(std::distance(from, to)));
}
};
#endif // #if COMMS_HAS_CPP20_SPAN

} // namespace details

} // namespace util

} // namespace comms
16 changes: 16 additions & 0 deletions test/Util.th
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public:
void test25();
void test26();
void test27();
void test28();
};

void UtilTestSuite::test1()
Expand Down Expand Up @@ -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<std::uint8_t>;
[[maybe_unused]] auto data1 = comms::util::construct<Data1>(std::begin(Data), std::end(Data));

#if COMMS_HAS_CPP20_SPAN
using Data2 = std::span<std::uint8_t>;
[[maybe_unused]] auto data2 = comms::util::construct<Data2>(std::begin(Data), std::end(Data));
#endif // #if COMMS_HAS_CPP20_SPAN
}

0 comments on commit 9a32cc1

Please sign in to comment.