8#include <capnp/schema.h>
13#include <kj/string-tree.h>
30template <
typename... Types>
33 static constexpr size_t size =
sizeof...(Types);
44template <
template <
typename...>
class Class,
typename... Types,
typename... Args>
45Class<Types..., std::remove_reference_t<Args>...>
Make(Args&&...
args)
47 return Class<Types..., std::remove_reference_t<Args>...>{std::forward<Args>(
args)...};
55template <std::
size_t index,
typename List,
typename _First = TypeList<>,
bool done = index == 0>
59template <
typename _Second,
typename _First>
60struct Split<0, _Second, _First, true>
67template <std::size_t index,
typename Type,
typename... _Second,
typename... _First>
71 using First =
typename _Next::First;
72 using Second =
typename _Next::Second;
76template <
typename Callable>
77using ResultOf =
decltype(std::declval<Callable>()());
81using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
88template <
typename SfinaeExpr,
typename Result_>
95template <
typename SfinaeExpr,
typename Result =
void>
106template <
int priority>
127 const char* display_name = ::capnp::Schema::from<T>().getProto().getDisplayName().cStr();
128 const char* short_name = strchr(display_name,
':');
129 return short_name ? short_name + 1 : display_name;
137 template <
typename... Args>
147#if defined(__clang__) && (!defined(SWIG))
148#define MP_TSA(x) __attribute__((x))
153#define MP_CAPABILITY(x) MP_TSA(capability(x))
154#define MP_SCOPED_CAPABILITY MP_TSA(scoped_lockable)
155#define MP_REQUIRES(x) MP_TSA(requires_capability(x))
156#define MP_ACQUIRE(...) MP_TSA(acquire_capability(__VA_ARGS__))
157#define MP_RELEASE(...) MP_TSA(release_capability(__VA_ARGS__))
158#define MP_ASSERT_CAPABILITY(x) MP_TSA(assert_capability(x))
159#define MP_GUARDED_BY(x) MP_TSA(guarded_by(x))
160#define MP_NO_TSA MP_TSA(no_thread_safety_analysis)
165 void unlock()
MP_RELEASE() { m_mutex.unlock(); }
178 assert(m_lock.mutex() == &mutex.m_mutex);
186template <
typename Lock>
194template <
typename Lock,
typename Callback>
206std::string
LogEscape(
const kj::StringTree&
string,
size_t max_size);
209using FdToArgsFn = std::function<std::vector<std::string>(
int fd)>;
226inline char*
CharCast(
unsigned char* c) {
return (
char*)c; }
227inline const char*
CharCast(
const char* c) {
return c; }
228inline const char*
CharCast(
const unsigned char* c) {
return (
const char*)c; }
void assert_locked(Mutex &mutex) MP_ASSERT_CAPABILITY() MP_ASSERT_CAPABILITY(mutex)
void unlock() MP_RELEASE()
Lock(Mutex &m) MP_ACQUIRE(m)
~Lock() MP_RELEASE()=default
std::unique_lock< std::mutex > m_lock
#define T(expected, seed, data)
#define MP_SCOPED_CAPABILITY
#define MP_ASSERT_CAPABILITY(x)
Functions to serialize / deserialize common bitcoin types.
void Unlock(Lock &lock, Callback &&callback)
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
const char * TypeName()
Return capnp type name with filename prefix removed.
class MP_CAPABILITY("mutex") Mutex
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
typename _Require< SfinaeExpr, Result >::Result Require
SFINAE helper, basically the same as to C++17's void_t, but allowing types other than void to be retu...
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
Class< Types..., std::remove_reference_t< Args >... > Make(Args &&... args)
Construct a template class value by deducing template arguments from the types of constructor argumen...
std::decay_t< T > Decay
Type helper abbreviating std::decay.
std::remove_cv_t< std::remove_reference_t< T > > RemoveCvRef
Substitutue for std::remove_cvref_t.
decltype(std::declval< Callable >()()) ResultOf
Type helper giving return type of a callable type.
std::function< std::vector< std::string >(int fd)> FdToArgsFn
Callback type used by SpawnProcess below.
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
std::string LogEscape(const kj::StringTree &string, size_t max_size)
Escape binary string for use in log so it doesn't trigger unicode decode errors in python unit tests.
SFINAE helper, see using Require below.
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous.
Convenient wrapper around std::variant<T*, T>
std::variant< T *, T > data
PtrOrValue(T *ptr, Args &&... args)
typename _Next::Second Second
typename _Next::First First
Type helper splitting a TypeList into two halves at position index.
Generic utility functions used by capnp code.
static constexpr size_t size
Analog to std::lock_guard that unlocks instead of locks.