0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-02-18 09:40:12 +01:00

ircd::util: Split out typography related; minor cleanup.

This commit is contained in:
Jason Volk 2018-01-11 23:36:42 -08:00
parent 51d729fa3a
commit 2a65c17ce3
2 changed files with 319 additions and 256 deletions

View file

@ -0,0 +1,263 @@
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#pragma once
#define HAVE_IRCD_UTIL_TYPOGRAPHY_H
namespace ircd {
namespace util {
//
// Overloading macros
//
/// Macro to arrange a function overload scheme based on the following
/// convention: An available `name` is chosen, from this name a strong type
/// is created by appending `_t`. The name itself becomes a static constexpr
/// instance of this `name_t`. Functions can be declared with an argument
/// accepting `name_t`, and called by passing `name`
///
/// IRCD_OVERLOAD(foo) // declare overload
/// void function(int, foo_t) {} // overloaded version
/// void function(int) { function(0, foo); } // calls overloaded version
/// function(0); // calls regular version
///
#define IRCD_OVERLOAD(NAME) \
static constexpr struct NAME##_t {} NAME {};
/// Imports an overload scheme from elsewhere without redeclaring the type_t.
#define IRCD_USING_OVERLOAD(ALIAS, ORIGIN) \
static constexpr const auto &ALIAS{ORIGIN}
//
// Typedef macros
//
/// Creates a type `NAME` from original type `TYPE` by inheriting from `TYPE`
/// and passing through construction to `TYPE`. These implicit conversions
/// we consider to be a "weak" typedef
#define IRCD_WEAK_TYPEDEF(TYPE, NAME) \
struct NAME \
:TYPE \
{ \
using TYPE::TYPE; \
};
/// Creates a type `NAME` by wrapping instance of `TYPE` as a member and
/// providing explicit conversions to `TYPE` and aggregate construction only. We
/// consider this a "strong" typedef which is useful for wrapping POD types
/// for overloaded functions, etc.
#define IRCD_STRONG_TYPEDEF(TYPE, NAME) \
struct NAME \
{ \
TYPE val; \
\
explicit operator const TYPE &() const { return val; } \
explicit operator TYPE &() { return val; } \
};
/// Convenience for weak typedef statements
#define IRCD_WEAK_T(TYPE) \
IRCD_WEAK_TYPEDEF(TYPE, IRCD_UNIQUE(weak_t))
/// Convenience for strong typedef statements
/// ex: using foo_t = IRCD_STRONG_T(int)
#define IRCD_STRONG_T(TYPE) \
IRCD_STRONG_TYPEDEF(TYPE, IRCD_UNIQUE(strong_t))
//
// Debug size of structure at compile time.
//
/// Internal use only
template<size_t SIZE>
struct _TEST_SIZEOF_;
/// Output the sizeof a structure at compile time.
/// This stops the compiler with an error (good) containing the size of the target
/// in the message.
///
/// example: struct foo {}; IRCD_TEST_SIZEOF(foo)
///
#define IRCD_TEST_SIZEOF(name) \
ircd::util::_TEST_SIZEOF_<sizeof(name)> _test_;
//
// Test if type is forward declared or complete
//
template<class T,
class = void>
struct is_complete
:std::false_type
{};
template<class T>
struct is_complete<T, decltype(void(sizeof(T)))>
:std::true_type
{};
//
// Test if type is a specialization of a template
//
template<class,
template<class...>
class>
struct is_specialization_of
:std::false_type
{};
template<template<class...>
class T,
class... args>
struct is_specialization_of<T<args...>, T>
:std::true_type
{};
//
// Misc type testing boilerplates
//
template<class T>
constexpr bool
is_bool()
{
using type = typename std::remove_reference<T>::type;
return std::is_same<type, bool>::value;
}
template<class T>
constexpr bool
is_number()
{
using type = typename std::remove_reference<T>::type;
return std::is_arithmetic<type>::value;
}
template<class T>
constexpr bool
is_floating()
{
using type = typename std::remove_reference<T>::type;
return is_number<T>() && std::is_floating_point<type>();
}
template<class T>
constexpr bool
is_integer()
{
return is_number<T>() && !is_floating<T>();
}
//
// Convenience constexprs for iterators
//
template<class It>
constexpr auto
is_iterator()
{
return std::is_base_of<typename std::iterator_traits<It>::value_type, It>::value;
}
template<class It>
constexpr auto
is_forward_iterator()
{
return std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<It>::iterator_category>::value;
}
template<class It>
constexpr auto
is_input_iterator()
{
return std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<It>::iterator_category>::value;
}
/// Zero testing functor (work in progress)
///
struct is_zero
{
template<class T>
typename std::enable_if
<
is_bool<T>(),
bool>::type
test(const bool &value)
const
{
return !value;
}
template<class T>
typename std::enable_if
<
is_integer<T>() &&
!is_bool<T>(),
bool>::type
test(const size_t &value)
const
{
return value == 0;
}
template<class T>
typename std::enable_if
<
is_floating<T>(),
bool>::type
test(const double &value)
const
{
return !(value > 0.0 || value < 0.0);
}
template<class T>
bool operator()(T&& t)
const
{
return test<T>(std::forward<T>(t));
}
};
/// Convenience loop to test std::is* on a character sequence
template<int (&test)(int) = std::isprint>
ssize_t
ctype(const char *begin,
const char *const &end)
{
size_t i(0);
for(; begin != end; ++begin, ++i)
if(!test(static_cast<unsigned char>(*begin)))
return i;
return -1;
}
} // namespace ircd
} // namespace util

View file

@ -39,86 +39,7 @@ namespace ircd
#define IRCD_CONCAT(a, b) IRCD_EXPCAT(a, b)
#define IRCD_UNIQUE(a) IRCD_CONCAT(a, __COUNTER__)
/// Macro to arrange a function overload scheme based on the following
/// convention: An available `name` is chosen, from this name a strong type
/// is created by appending `_t`. The name itself becomes a static constexpr
/// instance of this `name_t`. Functions can be declared with an argument
/// accepting `name_t`, and called by passing `name`
///
/// IRCD_OVERLOAD(foo) // declare overload
/// void function(int, foo_t) {} // overloaded version
/// void function(int) { function(0, foo); } // calls overloaded version
/// function(0); // calls regular version
///
#define IRCD_OVERLOAD(NAME) \
static constexpr struct NAME##_t {} NAME {};
/// Imports an overload scheme from elsewhere without redeclaring the type_t.
#define IRCD_USING_OVERLOAD(ALIAS, ORIGIN) \
static constexpr const auto &ALIAS{ORIGIN}
//
// Typedef macros
//
/// Creates a type `NAME` from original type `TYPE` by inheriting from `TYPE`
/// and passing through construction to `TYPE`. These implicit conversions
/// we consider to be a "weak" typedef
#define IRCD_WEAK_TYPEDEF(TYPE, NAME) \
struct NAME \
:TYPE \
{ \
using TYPE::TYPE; \
};
/// Creates a type `NAME` by wrapping instance of `TYPE` as a member and
/// providing explicit conversions to `TYPE` and aggregate construction only. We
/// consider this a "strong" typedef which is useful for wrapping POD types
/// for overloaded functions, etc.
#define IRCD_STRONG_TYPEDEF(TYPE, NAME) \
struct NAME \
{ \
TYPE val; \
\
explicit operator const TYPE &() const { return val; } \
explicit operator TYPE &() { return val; } \
};
/// Convenience for weak typedef statements
#define IRCD_WEAK_T(TYPE) \
IRCD_WEAK_TYPEDEF(TYPE, IRCD_UNIQUE(weak_t))
/// Convenience for strong typedef statements
/// ex: using foo_t = IRCD_STRONG_T(int)
#define IRCD_STRONG_T(TYPE) \
IRCD_STRONG_TYPEDEF(TYPE, IRCD_UNIQUE(strong_t))
//
// Debug size of structure at compile time.
//
/// Internal use only
template<size_t SIZE>
struct _TEST_SIZEOF_;
/// Output the sizeof a structure at compile time.
/// This stops the compiler with an error (good) containing the size of the target
/// in the message.
///
/// example: struct foo {}; IRCD_TEST_SIZEOF(foo)
///
#define IRCD_TEST_SIZEOF(name) \
ircd::util::_TEST_SIZEOF_<sizeof(name)> _test_;
/// A standard unique_ptr but accepting an std::function for T as its custom
/// deleter. This reduces the boilerplate burden on declaring the right
/// unique_ptr template for custom deleters every single time.
///
template<class T>
using custom_ptr = std::unique_ptr<T, std::function<void (T *) noexcept>>;
#include "typography.h"
#include "unit_literal.h"
#include "unwind.h"
#include "reentrance.h"
@ -133,6 +54,19 @@ using custom_ptr = std::unique_ptr<T, std::function<void (T *) noexcept>>;
namespace ircd {
namespace util {
/// A standard unique_ptr but accepting an std::function for T as its custom
/// deleter. This reduces the boilerplate burden on declaring the right
/// unique_ptr template for custom deleters every single time.
///
template<class T>
using custom_ptr = std::unique_ptr<T, std::function<void (T *) noexcept>>;
//
// Misc size() participants.
//
inline size_t
size(std::ostream &s)
{
@ -172,6 +106,10 @@ size(const T &val)
}
//
// Misc data() participants
//
template<size_t SIZE>
constexpr const char *
data(const char (&buf)[SIZE])
@ -201,6 +139,10 @@ data(T &val)
}
//
// Misc string() participants
//
template<class T>
auto
string(const T &s)
@ -222,6 +164,23 @@ string(const uint8_t *const &buf, const size_t &size)
}
//
// Misc bang participants
//
inline auto
operator!(const std::string &str)
{
return str.empty();
}
inline auto
operator!(const std::string_view &str)
{
return str.empty();
}
//
// stringstream buffer set macros
//
@ -257,10 +216,12 @@ resizebuf(std::stringstream &ss,
}
/* This is a template alternative to nothrow overloads, which
* allows keeping the function arguments sanitized of the thrownness.
*/
//
// Template nothrow suite
//
/// Test for template geworfenheit
///
template<class exception_t>
constexpr bool
is_nothrow()
@ -268,79 +229,22 @@ is_nothrow()
return std::is_same<exception_t, std::nothrow_t>::value;
}
/// This is a template alternative to nothrow overloads, which
/// allows keeping the function arguments sanitized of the thrownness.
///
template<class exception_t = std::nothrow_t,
class return_t = bool>
using nothrow_overload = typename std::enable_if<is_nothrow<exception_t>(), return_t>::type;
/// Inverse of the nothrow_overload template
///
template<class exception_t,
class return_t = void>
using throw_overload = typename std::enable_if<!is_nothrow<exception_t>(), return_t>::type;
//
// Test if type is forward declared or complete
//
template<class T,
class = void>
struct is_complete
:std::false_type
{};
template<class T>
struct is_complete<T, decltype(void(sizeof(T)))>
:std::true_type
{};
//
// Test if type is a specialization of a template
//
template<class,
template<class...>
class>
struct is_specialization_of
:std::false_type
{};
template<template<class...>
class T,
class... args>
struct is_specialization_of<T<args...>, T>
:std::true_type
{};
//
// Convenience constexprs for iterators
//
template<class It>
constexpr auto
is_iterator()
{
return std::is_base_of<typename std::iterator_traits<It>::value_type, It>::value;
}
template<class It>
constexpr auto
is_forward_iterator()
{
return std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<It>::iterator_category>::value;
}
template<class It>
constexpr auto
is_input_iterator()
{
return std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<It>::iterator_category>::value;
}
// std::next with out_of_range exception
/// Like std::next() but with out_of_range exception
///
template<class It>
typename std::enable_if<is_forward_iterator<It>() || is_input_iterator<It>(), It>::type
at(It &&start,
@ -417,6 +321,7 @@ end(const iterpair<T> &i)
return std::get<1>(i);
}
//
// To collapse pairs of iterators down to a single type
// typed by an object with iterator typedefs.
@ -457,7 +362,6 @@ end(const const_iterators<T> &ci)
}
///
/// Compile-time comparison of string literals
///
constexpr bool
@ -468,23 +372,9 @@ _constexpr_equal(const char *a,
}
inline auto
operator!(const std::string &str)
{
return str.empty();
}
inline auto
operator!(const std::string_view &str)
{
return str.empty();
}
//
// Iterator based until() matching std::for_each except the function
// returns a bool to continue rather than void.
//
/// Iterator based until() matching std::for_each except the function
/// returns a bool to continue rather than void.
///
template<class it_a,
class it_b,
class boolean_function>
@ -501,21 +391,7 @@ until(it_a a,
}
/// Convenience loop to test std::is* on a character sequence
template<int (&test)(int) = std::isprint>
ssize_t
ctype(const char *begin,
const char *const &end)
{
size_t i(0);
for(; begin != end; ++begin, ++i)
if(!test(static_cast<unsigned char>(*begin)))
return i;
return -1;
}
/// Inverse of std::lock_guard<>
template<class lockable>
struct unlock_guard
{
@ -534,82 +410,6 @@ struct unlock_guard
};
template<class T>
constexpr bool
is_bool()
{
using type = typename std::remove_reference<T>::type;
return std::is_same<type, bool>::value;
}
template<class T>
constexpr bool
is_number()
{
using type = typename std::remove_reference<T>::type;
return std::is_arithmetic<type>::value;
}
template<class T>
constexpr bool
is_floating()
{
using type = typename std::remove_reference<T>::type;
return is_number<T>() && std::is_floating_point<type>();
}
template<class T>
constexpr bool
is_integer()
{
return is_number<T>() && !is_floating<T>();
}
struct is_zero
{
template<class T>
typename std::enable_if
<
is_bool<T>(),
bool>::type
test(const bool &value)
const
{
return !value;
}
template<class T>
typename std::enable_if
<
is_integer<T>() &&
!is_bool<T>(),
bool>::type
test(const size_t &value)
const
{
return value == 0;
}
template<class T>
typename std::enable_if
<
is_floating<T>(),
bool>::type
test(const double &value)
const
{
return !(value > 0.0 || value < 0.0);
}
template<class T>
bool operator()(T&& t)
const
{
return test<T>(std::forward<T>(t));
}
};
constexpr bool
is_powerof2(const long long v)
{