Program Listing for File strongly_typed_numeric.hpp

Return to documentation for file (include/beluga/type_traits/strongly_typed_numeric.hpp)

// Copyright 2023 Ekumen, Inc.
//
// 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.

#ifndef BELUGA_TYPE_TRAITS_STRONGLY_TYPED_NUMERIC_HPP
#define BELUGA_TYPE_TRAITS_STRONGLY_TYPED_NUMERIC_HPP

#include <functional>
#include <limits>
#include <type_traits>
namespace beluga {


template <typename T, typename PhantomType, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
class Numeric final {
 public:
  constexpr Numeric() noexcept = default;

  constexpr Numeric(const T t) noexcept : value_{t} {};  // NOLINT
  constexpr operator T&() noexcept { return value_; }  // NOLINT
  constexpr operator const T&() const noexcept { return value_; }  // NOLINT

 private:
  T value_{0};  // Underlying primitive type.
};

}  // namespace beluga

namespace std {


template <typename T, typename PhantomType>
struct numeric_limits<beluga::Numeric<T, PhantomType>> : public numeric_limits<T> {};


template <typename T, typename PhantomType>
struct hash<beluga::Numeric<T, PhantomType>> {

  size_t operator()(beluga::Numeric<T, PhantomType> t) { return hash<T>{}(t); }
};

}  // namespace std

#endif