Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion include/wtf/fp/float.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <memory>
#include <wtf/concepts/floating_point.hpp>
#include <wtf/fp/detail_/float_model.hpp>
#include <wtf/fp/float_base.hpp>
#include <wtf/fp/float_view.hpp>
#include <wtf/warnings.hpp>

Expand All @@ -30,7 +31,11 @@ namespace wtf::fp {
* mutable as long as *this is not const. This is because *this owns the value
* it stores and does NOT alias it.
*/
class Float {
class Float : public FloatBase<Float> {
private:
/// The type *this derives from
using base_type = FloatBase<Float>;

public:
/// Type defining the API for accessing the type-erased value
using holder_type = detail_::FloatHolder;
Expand All @@ -47,6 +52,11 @@ class Float {
/// Type of converting *this to a string
using string_type = holder_type::string_type;

/// Pull in types from the base class
///@{
using typename base_type::rtti_type;
///@}

// -------------------------------------------------------------------------
// Ctors and assignment operators
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -237,6 +247,22 @@ class Float {
/// Creates a Float from an already existing holder. Used by make_float.
explicit Float(holder_pointer holder) : m_holder_(std::move(holder)) {}

/** @brief Returns the RTTI of the held float.
*
* This method returns the RTTI information for the floating-point type.
* If *this does not hold a value, the RTTI information will describe a
* nullptr_t type.
*
* @return The RTTI for the held floating-point type.
*
* @throw std::bad_alloc if there is a problem creating the type info.
* Strong throw guarantee.
*/
rtti_type type_info_() const override {
return is_holding_() ? m_holder_->type() :
rtti::wtf_typeid<std::nullptr_t>();
}

/// The holder that implements the type-erased floating-point API
holder_pointer m_holder_;
};
Expand Down
76 changes: 76 additions & 0 deletions include/wtf/fp/float_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2026 NWChemEx-Project
*
* 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 implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

namespace wtf::fp {

/** @brief Code factorization for Float and FloatView.
*
* @tparam DerivedType The derived type *this implements, either Float or
* FloatView.
*
* By design Float and FloatView share a lot of code. This class is used to
* consolidate that shared code in one place.
*
* @note This class was added late in the initial development and so it does
* not contain all of the factorized code. Migrating the missing code
* here should be considered tech debt that needs to be repaid.
*/
template<typename DerivedType>
class FloatBase {
public:
/// Type describing the RTTI information
using rtti_type = rtti::TypeInfo;

/** @brief Returns the RTTI of the held float.
*
* This method returns the RTTI information for the floating-point type.
* If *this does not hold a value, the RTTI information will describe a
* nullptr_t type.
*
* @return The RTTI for the held floating-point type.
*
* @throw std::bad_alloc if there is a problem creating the type info.
* Strong throw guarantee.
*/
rtti_type type_info() const { return type_info_(); }

protected:
/// Protected default ctor
FloatBase() = default;

/// Protected copy ctor
FloatBase(const FloatBase&) = default;

/// Protected move ctor
FloatBase(FloatBase&&) noexcept = default;

/// Protected copy assignment operator
FloatBase& operator=(const FloatBase&) = default;

/// Protected move assignment operator
FloatBase& operator=(FloatBase&&) noexcept = default;

/// Dtor
virtual ~FloatBase() = default;

private:
/// Derived class implements to return the RTTI information for *this
virtual rtti_type type_info_() const = 0;
};

} // namespace wtf::fp
23 changes: 22 additions & 1 deletion include/wtf/fp/float_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <wtf/concepts/floating_point.hpp>
#include <wtf/concepts/wtf_float.hpp>
#include <wtf/fp/detail_/float_view_model.hpp>
#include <wtf/fp/float_base.hpp>
#include <wtf/warnings.hpp>

namespace wtf::fp {
Expand All @@ -37,11 +38,14 @@ namespace wtf::fp {
* In other words, we can use FloatView objects like type-erased pointers!
*/
template<concepts::WTFFloat FloatType>
class FloatView {
class FloatView : public FloatBase<FloatView<FloatType>> {
private:
/// What is the type of *this
using view_type = FloatView<FloatType>;

/// What is the base type of *this?
using base_type = FloatBase<view_type>;

/// What is the type of a FloatView aliasing a const FloatType
using const_view_type = FloatView<const FloatType>;

Expand Down Expand Up @@ -72,6 +76,7 @@ class FloatView {
///@{
using holder_pointer = typename holder_type::holder_pointer;
using string_type = typename holder_type::string_type;
using rtti_type = typename base_type::rtti_type;
///@}

/** @brief Creates a FloatView that aliases @p value.
Expand Down Expand Up @@ -377,6 +382,22 @@ class FloatView {
/// Determines if *this is holding a value or not
bool is_holding_() const noexcept { return m_pfloat_ != nullptr; }

/** @brief Returns the RTTI of the held float.
*
* This method returns the RTTI information for the floating-point type.
* If *this does not hold a value, the RTTI information will describe a
* nullptr_t type.
*
* @return The RTTI for the held floating-point type.
*
* @throw std::bad_alloc if there is a problem creating the type info.
* Strong throw guarantee.
*/
rtti_type type_info_() const override {
return is_holding_() ? m_pfloat_->type() :
rtti::wtf_typeid<std::nullptr_t>();
}

/// The holder object
holder_pointer m_pfloat_;
};
Expand Down
48 changes: 26 additions & 22 deletions include/wtf/rtti/detail_/type_holder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
#include <map>
#include <memory>
#include <string>
#include <wtf/concepts/floating_point.hpp>
#include <wtf/detail_/dispatcher.hpp>
#include <wtf/type_traits/is_convertible.hpp>

namespace wtf::rtti::detail_ {

template<concepts::FloatingPoint T>
template<typename T>
class TypeModel;

/** @brief Defines the interface for interacting with the type-erased type.
Expand All @@ -45,9 +44,6 @@ class TypeHolder {
/// Type of pointer used to hold TypeHolder instances
using holder_pointer = std::shared_ptr<TypeHolder>;

/// Type used for specifying precision of floating-point types
using precision_type = std::size_t;

/// Default virtual destructor
virtual ~TypeHolder() = default;

Expand Down Expand Up @@ -82,30 +78,31 @@ class TypeHolder {
*/
const_string_reference name() const { return name_(); }

/** @brief In base 10, the number of significant digits this type can hold.
/** @brief True if the held type is const-qualified and false otherwise.
*
* The value returned by this method is determined by the
* type_traits::Precision class. See the documentation for that class for
* more information. The logic for determining the precision is in the
* private precision_ method.
* The derived class uses template meta-programming to determine if the
* held type is const-qualified. This logic is implemented by the
* private is_const_ method.
*
* @return The number of significant digits the held type can represent.
* @return True if the held type is const-qualified and false otherwise.
*
* @throw None No throw guarantee.
*/
precision_type precision() const { return precision_(); }
bool is_const() const { return is_const_(); }

/** @brief True if the held type is const-qualified and false otherwise.
/** @brief True if the held type is std::nullptr_t or a qualified version of
* it.
*
* The derived class uses template meta-programming to determine if the
* held type is const-qualified. This logic is implemented by the
* private is_const_ method.
* held type is std::nullptr_t (or a qualified version of it). This logic
* is implemented by the private is_nullptr_ method.
*
* @return True if the held type is const-qualified and false otherwise.
* @return True if the derived class holds std::nullptr_t (or a qualified
* version of it) and false otherwise.
*
* @throw None No throw guarantee.
*/
bool is_const() const { return is_const_(); }
bool is_nullptr() const { return is_nullptr_(); }

/** @brief Returns a TypeHolder for the const version of the type held by
* *this.
Expand Down Expand Up @@ -181,12 +178,12 @@ class TypeHolder {
/// Derived class overwrites to provide the name of the held type
virtual const_string_reference name_() const = 0;

/// Derived class overwrites to provide the precision of the held type
virtual precision_type precision_() const = 0;

/// Derived class overwrites to provide whether the held type is const
virtual bool is_const_() const = 0;

/// Derived class overwrites to provide whether the held type is const
virtual bool is_nullptr_() const = 0;

/// Derived class overwrites to provide a const-qualified version of *this
virtual holder_pointer make_const_() const = 0;

Expand All @@ -203,6 +200,14 @@ class TypeHolder {

template<typename TypeTuple>
bool TypeHolder::is_implicitly_convertible_to(const TypeHolder& other) const {
// Cannot convert away const-qualification
if(is_const() && !other.is_const()) return false;

// Handle nullptr_t conversions
bool this_null = this->is_nullptr();
bool other_null = other.is_nullptr();
if(this_null || other_null) { return this_null && other_null; }

auto lambda = [](auto&& from, auto&& to) {
using from_type = typename std::decay_t<decltype(from)>::value_type;
using to_type = typename std::decay_t<decltype(to)>::value_type;
Expand All @@ -211,8 +216,7 @@ bool TypeHolder::is_implicitly_convertible_to(const TypeHolder& other) const {
return wtf::type_traits::is_convertible_v<clean_from_type,
clean_to_type>;
};
// Cannot convert away const-qualification
if(is_const() && !other.is_const()) return false;

return wtf::detail_::dispatch<TypeModel, TypeTuple>(lambda, *this, other);
}

Expand Down
35 changes: 14 additions & 21 deletions include/wtf/rtti/detail_/type_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,27 @@
*/

#pragma once
#include <wtf/concepts/floating_point.hpp>
#include <wtf/rtti/detail_/type_holder.hpp>
#include <wtf/type_traits/float_traits.hpp>
#include <wtf/type_traits/precision.hpp>
#include <wtf/types.hpp>

namespace wtf::rtti::detail_ {

/** @brief Implements TypeHolder for type @p T.
*
* @tparam T A floating-point type, possibly const-qualified.
* @tparam T The qualified type we are creating a TypeInfo for.
*
* This class implements the TypeHolder interface for a specific floating-point
* type T.
* This class implements the TypeHolder interface for a specific type T.
*/
template<concepts::FloatingPoint T>
template<typename T>
class TypeModel : public TypeHolder {
public:
/// Type providing the type-erased interface used by the interface
using holder_type = TypeHolder;

/// Type providing traits for the held type T
using traits_type = wtf::type_traits::float_traits<T>;

/// Unpack traits_type into the API
///@{
using value_type = typename traits_type::value_type;
using unqualified_type = typename traits_type::unqualified_type;
using const_value_type = typename traits_type::const_value_type;
///@}
using value_type = T;
using unqualified_type = std::decay_t<value_type>;
using const_value_type = const unqualified_type;

/// Pull types from base class to make them part of this class's API
///@{
Expand All @@ -58,7 +49,7 @@ class TypeModel : public TypeHolder {
* "double".
*/
explicit TypeModel(string_type name) : m_name_(std::move(name)) {
m_qualified_name_ = traits_type::is_const ? "const " : "";
m_qualified_name_ = is_const() ? "const " : "";
m_qualified_name_ += m_name_;
register_model_(*this);
}
Expand All @@ -70,13 +61,15 @@ class TypeModel : public TypeHolder {
/// Implements TypeHolder::name_ by returning the stored name
const_string_reference name_() const override { return m_qualified_name_; }

/// Implements TypeHolder::precision_
precision_type precision_() const override {
return type_traits::precision_v<unqualified_type>;
/// Implements TypeHolder::is_const_ using the traits class
bool is_const_() const override {
return std::is_same_v<const_value_type, value_type>;
}

/// Implements TypeHolder::is_const_ using the traits class
bool is_const_() const override { return traits_type::is_const; }
/// Implements TypeHolder::is_nullptr_ by comparing to nullptr_t
bool is_nullptr_() const override {
return std::is_same_v<unqualified_type, std::nullptr_t>;
}

/// Implements TypeHolder::make_const_ by making a const-qualified copy
holder_pointer make_const_() const override {
Expand Down
Loading