2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-10 22:16:34 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-10 22:16:34 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_UTIL_H
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
/// Utilities for IRCd.
|
|
|
|
///
|
|
|
|
/// This is an inline namespace: everything declared in it will be
|
|
|
|
/// accessible in ircd::. By first opening it here as inline all
|
|
|
|
/// subsequent openings of this namespace do not have to use the inline
|
|
|
|
/// keyword but will still be inlined to ircd::.
|
|
|
|
inline namespace util {}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Fundamental macros
|
|
|
|
//
|
|
|
|
|
|
|
|
#define IRCD_EXPCAT(a, b) a ## b
|
|
|
|
#define IRCD_CONCAT(a, b) IRCD_EXPCAT(a, b)
|
|
|
|
#define IRCD_UNIQUE(a) IRCD_CONCAT(a, __COUNTER__)
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
#include "typography.h"
|
2020-02-27 03:10:41 +01:00
|
|
|
#include "identity.h"
|
2018-01-10 22:16:34 +01:00
|
|
|
#include "unit_literal.h"
|
2019-08-25 01:38:05 +02:00
|
|
|
#include "construction.h"
|
2018-01-10 22:16:34 +01:00
|
|
|
#include "unwind.h"
|
|
|
|
#include "reentrance.h"
|
|
|
|
#include "enum.h"
|
2019-04-04 02:16:43 +02:00
|
|
|
#include "custom_ptr.h"
|
2018-09-15 10:47:03 +02:00
|
|
|
#include "env.h"
|
2018-01-10 22:16:34 +01:00
|
|
|
#include "va_rtti.h"
|
|
|
|
#include "unique_iterator.h"
|
|
|
|
#include "instance_list.h"
|
2018-09-02 06:04:34 +02:00
|
|
|
#include "instance_multimap.h"
|
2018-09-15 09:34:09 +02:00
|
|
|
#include "callbacks.h"
|
2018-01-10 22:16:34 +01:00
|
|
|
#include "bswap.h"
|
2018-01-12 11:52:45 +01:00
|
|
|
#include "tuple.h"
|
|
|
|
#include "timer.h"
|
2019-01-02 19:48:05 +01:00
|
|
|
#include "scope_count.h"
|
2019-01-03 00:18:15 +01:00
|
|
|
#include "scope_restore.h"
|
2018-01-12 11:52:45 +01:00
|
|
|
#include "life_guard.h"
|
2018-04-25 01:47:19 +02:00
|
|
|
#include "pubsetbuf.h"
|
2018-09-28 02:00:15 +02:00
|
|
|
#include "string.h"
|
2018-04-25 01:47:19 +02:00
|
|
|
#include "pointers.h"
|
|
|
|
#include "iterator.h"
|
|
|
|
#include "nothrow.h"
|
2018-05-08 03:36:27 +02:00
|
|
|
#include "what.h"
|
2018-10-21 09:50:41 +02:00
|
|
|
#include "u2a.h"
|
|
|
|
#include "pretty.h"
|
2018-11-09 02:04:15 +01:00
|
|
|
#include "hash.h"
|
2019-04-10 05:53:51 +02:00
|
|
|
#include "closure.h"
|
2019-05-25 03:24:33 +02:00
|
|
|
#include "test.h"
|
2019-08-03 02:15:35 +02:00
|
|
|
#include "boolean.h"
|
2019-09-03 20:24:15 +02:00
|
|
|
#include "maybe.h"
|
2020-06-08 23:51:06 +02:00
|
|
|
#include "all.h"
|
2020-12-21 07:49:05 +01:00
|
|
|
#include "compare_exchange.h"
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
// Unsorted section
|
|
|
|
namespace ircd {
|
2019-06-23 10:01:26 +02:00
|
|
|
inline namespace util {
|
2018-01-10 22:16:34 +01:00
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
//
|
|
|
|
// Misc size() participants.
|
|
|
|
//
|
|
|
|
|
2018-10-25 22:39:41 +02:00
|
|
|
size_t size(std::ostream &s);
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr size_t
|
|
|
|
size(const char (&buf)[SIZE])
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr size_t
|
|
|
|
size(const std::array<const char, SIZE> &buf)
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr size_t
|
|
|
|
size(const std::array<char, SIZE> &buf)
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr typename std::enable_if<std::is_integral<T>::value, size_t>::type
|
|
|
|
size(const T &val)
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return sizeof(T);
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
//
|
|
|
|
// Misc data() participants
|
|
|
|
//
|
|
|
|
|
2018-01-10 22:16:34 +01:00
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr const char *
|
|
|
|
data(const char (&buf)[SIZE])
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr char *
|
|
|
|
data(char (&buf)[SIZE])
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr typename std::enable_if<std::is_pod<T>::value, const uint8_t *>::type
|
|
|
|
data(const T &val)
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return reinterpret_cast<const uint8_t *>(&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr typename std::enable_if<std::is_pod<T>::value, uint8_t *>::type
|
|
|
|
data(T &val)
|
2020-03-15 01:15:53 +01:00
|
|
|
noexcept
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return reinterpret_cast<uint8_t *>(&val);
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
//
|
|
|
|
// Misc bang participants
|
|
|
|
//
|
|
|
|
|
|
|
|
inline auto
|
|
|
|
operator!(const std::string &str)
|
|
|
|
{
|
|
|
|
return str.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto
|
|
|
|
operator!(const std::string_view &str)
|
|
|
|
{
|
|
|
|
return str.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like std::next() but with out_of_range exception
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<class It>
|
|
|
|
typename std::enable_if<is_forward_iterator<It>() || is_input_iterator<It>(), It>::type
|
|
|
|
at(It &&start,
|
|
|
|
It &&stop,
|
|
|
|
ssize_t i)
|
|
|
|
{
|
|
|
|
for(; start != stop; --i, std::advance(start, 1))
|
|
|
|
if(!i)
|
2019-06-23 08:28:48 +02:00
|
|
|
return std::move(start);
|
2018-01-10 22:16:34 +01:00
|
|
|
|
2018-08-16 07:48:48 +02:00
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"at(a, b, i): 'i' out of range"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like std::find() -> std::distance(), but with out_of_range exception
|
|
|
|
///
|
|
|
|
template<class It,
|
|
|
|
class value>
|
|
|
|
typename std::enable_if<is_forward_iterator<It>() || is_input_iterator<It>(), size_t>::type
|
|
|
|
index(const It &start,
|
|
|
|
const It &stop,
|
|
|
|
value &&val)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::find(start, stop, std::forward<value>(val))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(likely(it != stop))
|
|
|
|
return std::distance(start, it);
|
|
|
|
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"index(a, b, val): 'val' not contained in set"
|
|
|
|
};
|
2018-01-10 22:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Some functors for STL
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class container>
|
|
|
|
struct keys
|
|
|
|
{
|
|
|
|
auto &operator()(typename container::reference v) const
|
|
|
|
{
|
|
|
|
return v.first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class container>
|
|
|
|
struct values
|
|
|
|
{
|
|
|
|
auto &operator()(typename container::reference v) const
|
|
|
|
{
|
|
|
|
return v.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
/// Iterator based until() matching std::for_each except the function
|
|
|
|
/// returns a bool to continue rather than void.
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class boolean_function>
|
|
|
|
bool
|
|
|
|
until(it_a a,
|
|
|
|
const it_b &b,
|
|
|
|
boolean_function&& f)
|
|
|
|
{
|
|
|
|
for(; a != b; ++a)
|
|
|
|
if(!f(*a))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:35:34 +02:00
|
|
|
template<class T>
|
|
|
|
T
|
|
|
|
minmax(T ret,
|
|
|
|
const T &min,
|
|
|
|
const T &max)
|
|
|
|
{
|
|
|
|
ret = std::max(ret, min);
|
|
|
|
ret = std::min(ret, max);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:16:34 +01:00
|
|
|
} // namespace util
|
|
|
|
} // namespace ircd
|