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"
|
2018-01-10 22:16:34 +01:00
|
|
|
#include "unit_literal.h"
|
|
|
|
#include "unwind.h"
|
|
|
|
#include "reentrance.h"
|
|
|
|
#include "enum.h"
|
|
|
|
#include "syscall.h"
|
|
|
|
#include "va_rtti.h"
|
|
|
|
#include "unique_iterator.h"
|
|
|
|
#include "instance_list.h"
|
|
|
|
#include "bswap.h"
|
2018-01-12 11:52:45 +01:00
|
|
|
#include "tuple.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "life_guard.h"
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
// Unsorted section
|
|
|
|
namespace ircd {
|
|
|
|
namespace util {
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
/// 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.
|
|
|
|
//
|
|
|
|
|
2018-01-10 22:16:34 +01:00
|
|
|
inline size_t
|
|
|
|
size(std::ostream &s)
|
|
|
|
{
|
|
|
|
const auto cur(s.tellp());
|
|
|
|
s.seekp(0, std::ios::end);
|
|
|
|
const auto ret(s.tellp());
|
|
|
|
s.seekp(cur, std::ios::beg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr size_t
|
|
|
|
size(const char (&buf)[SIZE])
|
|
|
|
{
|
|
|
|
return SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr size_t
|
|
|
|
size(const std::array<const char, SIZE> &buf)
|
|
|
|
{
|
|
|
|
return SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr size_t
|
|
|
|
size(const std::array<char, SIZE> &buf)
|
|
|
|
{
|
|
|
|
return SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr typename std::enable_if<std::is_integral<T>::value, size_t>::type
|
|
|
|
size(const T &val)
|
|
|
|
{
|
|
|
|
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])
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
constexpr char *
|
|
|
|
data(char (&buf)[SIZE])
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr typename std::enable_if<std::is_pod<T>::value, const uint8_t *>::type
|
|
|
|
data(const T &val)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<uint8_t *>(&val);
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
//
|
2018-01-29 06:29:21 +01:00
|
|
|
// String generating patterns
|
2018-01-12 08:36:42 +01:00
|
|
|
//
|
|
|
|
|
2018-01-29 06:29:21 +01:00
|
|
|
/// This is the ubiquitous ircd::string() template serving as the "toString()"
|
|
|
|
/// for the project. Particpating types that want to have a string(T)
|
|
|
|
/// returning an std::string must friend an operator<<(std::ostream &, T);
|
|
|
|
/// this is primarily for debug strings, not meant for performance or service.
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<class T>
|
|
|
|
auto
|
|
|
|
string(const T &s)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2018-02-18 10:20:39 +01:00
|
|
|
ss << s;
|
|
|
|
return ss.str();
|
2018-01-10 22:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline auto
|
2018-01-29 06:29:21 +01:00
|
|
|
string(const char *const &buf,
|
|
|
|
const size_t &size)
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return std::string{buf, size};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto
|
2018-01-29 06:29:21 +01:00
|
|
|
string(const uint8_t *const &buf,
|
|
|
|
const size_t &size)
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
return string(reinterpret_cast<const char *>(buf), size);
|
|
|
|
}
|
|
|
|
|
2018-01-29 17:19:14 +01:00
|
|
|
/// Close over the common pattern to write directly into a post-C++11 standard
|
|
|
|
/// string through the data() member requiring a const_cast. Closure returns
|
|
|
|
/// the final size of the data written into the buffer.
|
|
|
|
inline auto
|
|
|
|
string(const size_t &size,
|
|
|
|
const std::function<size_t (const mutable_buffer &)> &closure)
|
|
|
|
{
|
|
|
|
std::string ret(size, char{});
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
const_cast<char *>(ret.data()), ret.size()
|
|
|
|
};
|
|
|
|
|
2018-02-19 00:21:37 +01:00
|
|
|
const size_t consumed
|
|
|
|
{
|
|
|
|
closure(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(consumed <= buffer::size(buf));
|
|
|
|
data(buf)[consumed] = '\0';
|
|
|
|
ret.resize(consumed);
|
2018-01-29 17:19:14 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close over the common pattern to write directly into a post-C++11 standard
|
|
|
|
/// string through the data() member requiring a const_cast. Closure returns
|
|
|
|
/// a view of the data actually written to the buffer.
|
|
|
|
inline auto
|
|
|
|
string(const size_t &size,
|
|
|
|
const std::function<string_view (const mutable_buffer &)> &closure)
|
|
|
|
{
|
|
|
|
return string(size, [&closure]
|
|
|
|
(const mutable_buffer &buffer)
|
|
|
|
{
|
|
|
|
return ircd::size(closure(buffer));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:16:34 +01:00
|
|
|
//
|
|
|
|
// stringstream buffer set macros
|
|
|
|
//
|
|
|
|
|
2018-03-26 08:15:26 +02:00
|
|
|
template<class stringstream>
|
|
|
|
stringstream &
|
|
|
|
pubsetbuf(stringstream &ss,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
ss.rdbuf()->pubsetbuf(data(buf), size(buf));
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class stringstream>
|
|
|
|
stringstream &
|
|
|
|
pubsetbuf(stringstream &ss,
|
2018-01-10 22:16:34 +01:00
|
|
|
std::string &s)
|
|
|
|
{
|
|
|
|
auto *const &data
|
|
|
|
{
|
|
|
|
const_cast<char *>(s.data())
|
|
|
|
};
|
|
|
|
|
|
|
|
ss.rdbuf()->pubsetbuf(data, s.size());
|
2018-03-26 08:15:26 +02:00
|
|
|
return ss;
|
2018-01-10 22:16:34 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 08:15:26 +02:00
|
|
|
template<class stringstream>
|
|
|
|
stringstream &
|
|
|
|
pubsetbuf(stringstream &ss,
|
2018-01-10 22:16:34 +01:00
|
|
|
std::string &s,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
s.resize(size, char{});
|
|
|
|
return pubsetbuf(ss, s);
|
|
|
|
}
|
|
|
|
|
2018-03-26 08:15:26 +02:00
|
|
|
template<class stringstream>
|
|
|
|
stringstream &
|
|
|
|
resizebuf(stringstream &ss,
|
2018-01-10 22:16:34 +01:00
|
|
|
std::string &s)
|
|
|
|
{
|
|
|
|
s.resize(ss.tellp());
|
2018-03-26 08:15:26 +02:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// buf has to match the rdbuf you gave the stringstream
|
|
|
|
template<class stringstream>
|
|
|
|
string_view
|
|
|
|
view(stringstream &ss,
|
|
|
|
const const_buffer &buf)
|
|
|
|
{
|
|
|
|
assert(size_t(ss.tellp()) <= size(buf));
|
|
|
|
ss.flush();
|
|
|
|
ss.rdbuf()->pubsync();
|
2018-04-07 07:35:14 +02:00
|
|
|
const string_view ret
|
2018-03-26 08:15:26 +02:00
|
|
|
{
|
|
|
|
data(buf), size_t(ss.tellp())
|
|
|
|
};
|
2018-04-07 07:35:14 +02:00
|
|
|
|
|
|
|
assert(size(ret) <= size(buf));
|
|
|
|
return ret;
|
2018-01-10 22:16:34 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
//
|
|
|
|
// Template nothrow suite
|
|
|
|
//
|
2018-01-10 22:16:34 +01:00
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
/// Test for template geworfenheit
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<class exception_t>
|
|
|
|
constexpr bool
|
|
|
|
is_nothrow()
|
|
|
|
{
|
|
|
|
return std::is_same<exception_t, std::nothrow_t>::value;
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
/// This is a template alternative to nothrow overloads, which
|
|
|
|
/// allows keeping the function arguments sanitized of the thrownness.
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
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;
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
/// Inverse of the nothrow_overload template
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<class exception_t,
|
|
|
|
class return_t = void>
|
|
|
|
using throw_overload = typename std::enable_if<!is_nothrow<exception_t>(), return_t>::type;
|
|
|
|
|
2018-01-12 08:36:42 +01:00
|
|
|
/// 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)
|
|
|
|
return start;
|
|
|
|
|
|
|
|
throw std::out_of_range("at(a, b, i): 'i' out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// To collapse pairs of iterators down to a single type
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct iterpair
|
|
|
|
:std::pair<T, T>
|
|
|
|
{
|
|
|
|
using std::pair<T, T>::pair;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T &
|
|
|
|
begin(iterpair<T> &i)
|
|
|
|
{
|
|
|
|
return std::get<0>(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T &
|
|
|
|
end(iterpair<T> &i)
|
|
|
|
{
|
|
|
|
return std::get<1>(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const T &
|
|
|
|
begin(const iterpair<T> &i)
|
|
|
|
{
|
|
|
|
return std::get<0>(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const T &
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using iterators = std::pair<typename T::iterator, typename T::iterator>;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using const_iterators = std::pair<typename T::const_iterator, typename T::const_iterator>;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename T::iterator
|
|
|
|
begin(const iterators<T> &i)
|
|
|
|
{
|
|
|
|
return i.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename T::iterator
|
|
|
|
end(const iterators<T> &i)
|
|
|
|
{
|
|
|
|
return i.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename T::const_iterator
|
|
|
|
begin(const const_iterators<T> &ci)
|
|
|
|
{
|
|
|
|
return ci.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename T::const_iterator
|
|
|
|
end(const const_iterators<T> &ci)
|
|
|
|
{
|
|
|
|
return ci.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile-time comparison of string literals
|
|
|
|
///
|
|
|
|
constexpr bool
|
|
|
|
_constexpr_equal(const char *a,
|
|
|
|
const char *b)
|
|
|
|
{
|
|
|
|
return *a == *b && (*a == '\0' || _constexpr_equal(a + 1, b + 1));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool
|
|
|
|
is_powerof2(const long long v)
|
|
|
|
{
|
|
|
|
return v && !(v & (v - 1LL));
|
|
|
|
}
|
|
|
|
|
2018-03-04 06:49:27 +01:00
|
|
|
//
|
|
|
|
// Transform to pointer utils
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Transform input sequence values to pointers in the output sequence
|
|
|
|
/// using two input iterators [begin, end] and one output iterator [begin]
|
|
|
|
template<class input_begin,
|
|
|
|
class input_end,
|
|
|
|
class output_begin>
|
|
|
|
auto
|
|
|
|
pointers(input_begin&& ib,
|
|
|
|
const input_end &ie,
|
|
|
|
output_begin&& ob)
|
|
|
|
{
|
|
|
|
return std::transform(ib, ie, ob, []
|
|
|
|
(auto&& value)
|
|
|
|
{
|
|
|
|
return std::addressof(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class input_container,
|
|
|
|
class output_container>
|
|
|
|
auto
|
|
|
|
pointers(input_container&& ic,
|
|
|
|
output_container &oc)
|
|
|
|
{
|
|
|
|
return pointers(begin(ic), end(ic), begin(oc));
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:23:50 +01:00
|
|
|
/// Get what() from exception_ptr
|
|
|
|
///
|
|
|
|
inline ircd::string_view
|
|
|
|
what(const std::exception_ptr &eptr)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(likely(eptr))
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
return e.what();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:16:34 +01:00
|
|
|
} // namespace util
|
|
|
|
} // namespace ircd
|