TODO: Add default `util::comptime<>` parameters to constexpr `leven_diff()` for when size of strings are statically known. Fortune for libexopt's current commit: Curse − 凶boxed_is_boxed_value
parent
388068bc1d
commit
b778b47ae8
@ -0,0 +1,44 @@
|
||||
#ifndef _EXOPT_H
|
||||
#define _EXOPT_H
|
||||
|
||||
#define _EXOPT_INTERNAL_PREFIX _exopt__
|
||||
|
||||
#ifdef __cplusplus
|
||||
# define _exopt__cpp 1
|
||||
# define _exopt__c 1
|
||||
extern "C" {
|
||||
#else
|
||||
# define _exopt__cpp 0
|
||||
# define _exopt__c 1
|
||||
#endif
|
||||
|
||||
#define _EO_PASTE_(x,y) x##y
|
||||
#define _EO_PASTE(x,y) _EO_PASTE_(x,y)
|
||||
#define _EO(X) _EO_PASTE(_EXOPT_INTERNAL_PREFIX, X)
|
||||
|
||||
|
||||
#define _exopt__internal __attribute__((visibility("internal")))
|
||||
#define _exopt__readonly __attribute__((__pure__))
|
||||
#define _exopt__pure __attribute__((__const__))
|
||||
|
||||
#ifdef _EO__OPT_OLD_ASSUME
|
||||
# define _EO_ASSUME(X) ({ if(! (X)) __builtin_unreachable(); })
|
||||
#endif
|
||||
|
||||
#if _EO(cpp)
|
||||
# ifndef _EO_ASSUME
|
||||
# define _EO_ASSUME(X) [[assume(X)]]
|
||||
# endif
|
||||
# define _exopt__restrict __restrict__
|
||||
#else
|
||||
# ifndef _EO_ASSUME
|
||||
# define _EO_ASSUME(X) __attribute__((assume(X)))
|
||||
# endif
|
||||
# define _exopt__restrict restrict
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _EXOPT_H */
|
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "exopt.h"
|
||||
|
||||
#define _EO_CONSTANT_VALUE(X) ([] { \
|
||||
struct { \
|
||||
typedef decltype(X) comptime_constant_t; \
|
||||
consteval operator comptime_constant_t() const noexcept { return (X); } \
|
||||
} inner; \
|
||||
return inner; \
|
||||
}())
|
||||
|
||||
namespace exopt { namespace util [[gnu::visibility("internal")]] {
|
||||
template<typename T, typename U>
|
||||
concept comptime = std::convertible_to<T, U> and requires{
|
||||
typename T::comptime_constant_t;
|
||||
};
|
||||
|
||||
constexpr auto comptime_value(auto value) noexcept {
|
||||
using U = decltype(value);
|
||||
struct inner {
|
||||
typedef U comptime_constant_t;
|
||||
consteval operator comptime_constant_t() const noexcept { return std::move(value); }
|
||||
U value;
|
||||
|
||||
constexpr inner(std::add_rvalue_reference_t<U> m) noexcept : value(std::move(m)) {}
|
||||
constexpr ~inner() = default;
|
||||
};
|
||||
return inner{ std::move(value) };
|
||||
}
|
||||
|
||||
static_assert(comptime_value(std::string_view{"hello"}.size()) == 5, "Bad comptime_value()");
|
||||
static_assert(_EO_CONSTANT_VALUE(std::string_view{"hello world"}) == std::string_view{"hello world"}, "Bad CONSTANT_VALUE()");
|
||||
} }
|
Loading…
Reference in new issue