Skip to content

Commit

Permalink
✨ reference_wrapper isn't constexpr, more polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Jun 25, 2021
1 parent 39ea6d6 commit dea4a46
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 12 deletions.
7 changes: 4 additions & 3 deletions include/ztd/text/count_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <ztd/text/unicode_code_point.hpp>
#include <ztd/text/encoding_error.hpp>
#include <ztd/text/reconstruct.hpp>
#include <ztd/text/reference_wrapper.hpp>

#include <cstddef>
#include <array>
Expand Down Expand Up @@ -143,7 +144,7 @@ namespace ztd { namespace text {
//////
/// @brief A reference to the state of the associated Encoding used for counting.
//////
::std::reference_wrapper<_State> state;
::ztd::text::reference_wrapper<_State> state;

//////
/// @brief Constructs a ztd::text::count_result, defaulting the error code to
Expand Down Expand Up @@ -197,13 +198,13 @@ namespace ztd { namespace text {
/// @brief A reference to the state of the associated Encoding used for counting which covers the decoding
/// portion of the transcode operation.
//////
::std::reference_wrapper<_FromState> from_state;
::ztd::text::reference_wrapper<_FromState> from_state;

//////
/// @brief A reference to the state of the associated Encoding used for counting which covers the encoding
/// portion of the transcode operation.
//////
::std::reference_wrapper<_ToState> to_state;
::ztd::text::reference_wrapper<_ToState> to_state;

//////
/// @brief Constructs a ztd::text::count_result, defaulting the error code to
Expand Down
3 changes: 2 additions & 1 deletion include/ztd/text/decode_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <ztd/text/unicode_code_point.hpp>
#include <ztd/text/encoding_error.hpp>
#include <ztd/text/reconstruct.hpp>
#include <ztd/text/reference_wrapper.hpp>

#include <ztd/text/detail/span.hpp>
#include <ztd/text/detail/encoding_range.hpp>
Expand Down Expand Up @@ -150,7 +151,7 @@ namespace ztd { namespace text {
/// @brief The state of the associated Encoding used for decoding input code units to code points.
///
//////
::std::reference_wrapper<_State> state;
::ztd::text::reference_wrapper<_State> state;

//////
/// @brief Constructs a ztd::text::decode_result, defaulting the error code to
Expand Down
6 changes: 6 additions & 0 deletions include/ztd/text/detail/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <ztd/text/forward.hpp>
#include <ztd/text/char8_t.hpp>
#include <ztd/text/reference_wrapper.hpp>

#include <type_traits>

Expand All @@ -56,6 +57,11 @@ namespace ztd { namespace text {
using type = ::std::add_lvalue_reference_t<_Type>;
};

template <typename _Dummy, typename _Type>
struct __unwrap_impl<_Dummy, ::ztd::text::reference_wrapper<_Type>> {
using type = ::std::add_lvalue_reference_t<_Type>;
};

template <typename _Type>
using __unwrap_t = typename __unwrap_impl<_Type>::type;

Expand Down
3 changes: 2 additions & 1 deletion include/ztd/text/detail/word_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <ztd/text/endian.hpp>
#include <ztd/text/reconstruct.hpp>
#include <ztd/text/reference_wrapper.hpp>

#include <ztd/text/detail/range.hpp>
#include <ztd/text/detail/type_traits.hpp>
Expand Down Expand Up @@ -283,7 +284,7 @@ namespace ztd { namespace text {
return this->_M_base_range_ref.get();
}

::std::reference_wrapper<_URange> _M_base_range_ref;
::ztd::text::reference_wrapper<_URange> _M_base_range_ref;
};

public:
Expand Down
3 changes: 2 additions & 1 deletion include/ztd/text/encoding_scheme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <ztd/text/decode_result.hpp>
#include <ztd/text/encoding_scheme.hpp>
#include <ztd/text/endian.hpp>
#include <ztd/text/reference_wrapper.hpp>

#include <ztd/text/detail/word_iterator.hpp>
#include <ztd/text/detail/ebco.hpp>
Expand All @@ -66,7 +67,7 @@ namespace ztd { namespace text {
template <typename _Byte, typename _UInputRange, typename _UOutputRange, typename _ErrorHandler>
class __scheme_decode_handler {
private:
::std::reference_wrapper<_ErrorHandler> _M_handler;
::ztd::text::reference_wrapper<_ErrorHandler> _M_handler;

public:
constexpr __scheme_decode_handler(_ErrorHandler& __handler) noexcept : _M_handler(__handler) {
Expand Down
92 changes: 92 additions & 0 deletions include/ztd/text/reference_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// =============================================================================
//
// ztd.text
// Copyright © 2021 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.text licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; 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 implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================>

#pragma once

#ifndef ZTD_TEXT_DETAIL_REFERENCE_WRAPPER_HPP
#define ZTD_TEXT_DETAIL_REFERENCE_WRAPPER_HPP

#include <ztd/text/version.hpp>

#include <functional>
#include <utility>
#include <type_traits>
#include <memory>

#include <ztd/text/detail/prologue.hpp>

namespace ztd { namespace text {
ZTD_TEXT_INLINE_ABI_NAMESPACE_OPEN_I_

namespace __txt_detail {

template <typename _Ty>
class __reference_wrapper {
private:
using _Ref = ::std::add_lvalue_reference_t<_Ty>;
using _Ptr = ::std::add_pointer_t<_Ty>;
_Ptr __ptr;

public:
constexpr __reference_wrapper(_Ref __ref) noexcept : __ptr(::std::addressof(__ref)) {
}

constexpr operator _Ref() const noexcept {
return *__ptr;
}

constexpr _Ref get() const noexcept {
return *__ptr;
}
};

template <typename _Ty>
__reference_wrapper(_Ty&) -> __reference_wrapper<_Ty>;

} // namespace __txt_detail

//////
/// @brief A subsitute for C++20's reference wrapper if the current @c std::reference_wrapper provided by the
/// standard library is not @c constexpr since it was only done then.
///
//////
template <typename _Ty>
using reference_wrapper =
#if ZTD_TEXT_IS_ON(ZTD_TEXT_STD_LIBRARY_REFERENCE_WRAPPER_CONSTEXPR_I_)
::std::reference_wrapper<_Ty>
#else
::ztd::text::__txt_detail::__reference_wrapper<_Ty>
#endif
;

ZTD_TEXT_INLINE_ABI_NAMESPACE_CLOSE_I_
}} // namespace ztd::text

#endif // ZTD_TEXT_DETAIL_REFERENCE_WRAPPER_HPP
5 changes: 3 additions & 2 deletions include/ztd/text/transcode_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <ztd/text/unicode_code_point.hpp>
#include <ztd/text/encoding_error.hpp>
#include <ztd/text/reconstruct.hpp>
#include <ztd/text/reference_wrapper.hpp>

#include <cstddef>
#include <array>
Expand Down Expand Up @@ -150,12 +151,12 @@ namespace ztd { namespace text {
/// @brief A reference to the state of the associated Encoding used for decoding input code units to
/// intermediate code points.
//////
::std::reference_wrapper<_FromState> from_state;
::ztd::text::reference_wrapper<_FromState> from_state;
//////
/// @brief A reference to the state of the associated Encoding used for encoding intermediate code points to
/// code units.
//////
::std::reference_wrapper<_ToState> to_state;
::ztd::text::reference_wrapper<_ToState> to_state;

//////
/// @brief Constructs a ztd::text::transcode_result, defaulting the error code to
Expand Down
10 changes: 6 additions & 4 deletions include/ztd/text/validate_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <ztd/text/encoding_error.hpp>
#include <ztd/text/reconstruct.hpp>
#include <ztd/text/reference_wrapper.hpp>

#include <cstddef>
#include <array>
Expand Down Expand Up @@ -107,7 +108,7 @@ namespace ztd { namespace text {
/// @brief A reference to the state of the associated Encoding used for validating the input.
///
//////
::std::reference_wrapper<_State> state;
::ztd::text::reference_wrapper<_State> state;

//////
/// @brief Constructs a ztd::text::validate_result, defaulting the error code to
Expand Down Expand Up @@ -137,12 +138,12 @@ namespace ztd { namespace text {
/// @brief A reference to the state of the associated Encoding used for validating the input.
///
//////
::std::reference_wrapper<_DecodeState> from_state;
::ztd::text::reference_wrapper<_DecodeState> from_state;
//////
/// @brief A reference to the state of the associated Encoding used for validating the input.
///
//////
::std::reference_wrapper<_EncodeState> to_state;
::ztd::text::reference_wrapper<_EncodeState> to_state;

//////
/// @brief Constructs a ztd::text::validate_result, defaulting the error code to
Expand Down Expand Up @@ -185,7 +186,8 @@ namespace ztd { namespace text {
template <typename _Input, typename _DecodeState, typename _EncodeState>
constexpr validate_result<_Input, _DecodeState>
__drop_single_state(validate_transcode_result<_Input, _DecodeState, _EncodeState>&& __result) noexcept(
::std::is_nothrow_constructible_v<validate_result<_Input, _DecodeState>, _Input&&, bool&, _DecodeState&>) {
::std::is_nothrow_constructible_v<validate_result<_Input, _DecodeState>, _Input&&, bool&,
_DecodeState&>) {
return validate_result<_Input, _DecodeState>(
::std::move(__result.input), ::std::move(__result.valid), __result.from_state);
}
Expand Down
12 changes: 12 additions & 0 deletions include/ztd/text/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@
#define ZTD_TEXT_STD_LIBRARY_ENDIAN_I_ ZTD_TEXT_DEFAULT_OFF
#endif

#if defined(ZTD_TEXT_STD_LIBRARY_REFERENCE_WRAPPER_CONSTEXPR)
#if (ZTD_TEXT_STD_LIBRARY_REFERENCE_WRAPPER_CONSTEXPR != 0)
#define ZTD_TEXT_STD_LIBRARY_REFERENCE_WRAPPER_CONSTEXPR_I_ ZTD_TEXT_ON
#else
#define ZTD_TEXT_STD_LIBRARY_REFERENCE_WRAPPER_CONSTEXPR_I_ ZTD_TEXT_OFF
#endif
#elif defined(__cpp_­lib_­constexpr_­utility) && (__cpp_­lib_­constexpr_­utility >= 201811L)
#define ZTD_TEXT_STD_LIBRARY_REFERENCE_WRAPPER_CONSTEXPR_I_ ZTD_TEXT_DEFAULT_ON
#else
#define ZTD_TEXT_STD_LIBRARY_REFERENCE_WRAPPER_CONSTEXPR_I_ ZTD_TEXT_DEFAULT_OFF
#endif

#if defined(ZTD_TEXT_STD_LIBRARY_CONCEPTS)
#if (ZTD_TEXT_STD_LIBRARY_CONCEPTS != 0)
#define ZTD_TEXT_STD_LIBRARY_CONCEPTS_I_ ZTD_TEXT_ON
Expand Down
31 changes: 31 additions & 0 deletions tests/inclusion/source/ztd/text/reference_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// =============================================================================
//
// ztd.text
// Copyright © 2021 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.text licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; 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 implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================>

#include <ztd/text/reference_wrapper.hpp>

0 comments on commit dea4a46

Please sign in to comment.