folly/folly/lang/Ordering.h

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * 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

#include <cstdint>
#include <stdexcept>

#include <folly/lang/Exception.h>

namespace folly {

enum class ordering : std::int8_t {};

template <typename T>
constexpr ordering to_ordering(T c) {}

namespace detail {

template <typename C, ordering o, bool ne>
struct cmp_pred : private C {};

} // namespace detail

template <typename C>
struct compare_equal_to : detail::cmp_pred<C, ordering::eq, 0> {};

template <typename C>
struct compare_not_equal_to : detail::cmp_pred<C, ordering::eq, 1> {};

template <typename C>
struct compare_less : detail::cmp_pred<C, ordering::lt, 0> {};

template <typename C>
struct compare_less_equal : detail::cmp_pred<C, ordering::gt, 1> {};

template <typename C>
struct compare_greater : detail::cmp_pred<C, ordering::gt, 0> {};

template <typename C>
struct compare_greater_equal : detail::cmp_pred<C, ordering::lt, 1> {};

namespace detail {

// extracted to a template so initialization can be inline and visible in c++14
template <typename D>
struct partial_ordering_ {};

template <typename D>
FOLLY_STORAGE_CONSTEXPR D const partial_ordering_<D>::less{};
template <typename D>
FOLLY_STORAGE_CONSTEXPR D const partial_ordering_<D>::greater{};
template <typename D>
FOLLY_STORAGE_CONSTEXPR D const partial_ordering_<D>::equivalent{};
template <typename D>
FOLLY_STORAGE_CONSTEXPR D const //
    partial_ordering_<D>::unordered{};

} // namespace detail

/// @def partial_ordering
///
/// mimic: std::partial_ordering, c++20 (partial)
class partial_ordering : private detail::partial_ordering_<partial_ordering> {};

} // namespace folly