Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-22 14:04:28 +01:00
parent 81b91f4139
commit f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions

View file

@ -0,0 +1,14 @@
//*********************************************************************
//* Base64 - a simple base64 encoder and decoder.
//*
//* Copyright (c) 1999, Bob Withers - bwit@pobox.com
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*
//* Enhancements by Stanley Yamane:
//* o reverse lookup table for the decode function
//* o reserve string buffer space in advance
//*
//*********************************************************************

View file

@ -0,0 +1,279 @@
//*********************************************************************
//* Base64 - a simple base64 encoder and decoder.
//*
//* Copyright (c) 1999, Bob Withers - bwit@pobox.com
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*
//* Enhancements by Stanley Yamane:
//* o reverse lookup table for the decode function
//* o reserve string buffer space in advance
//*
//*********************************************************************
#include "rtc_base/third_party/base64/base64.h"
#include <string.h>
#include "absl/strings/string_view.h"
#include "rtc_base/checks.h"
using std::vector;
namespace rtc {
static const char kPad = '=';
static const unsigned char pd = 0xFD; // Padding
static const unsigned char sp = 0xFE; // Whitespace
static const unsigned char il = 0xFF; // Illegal base64 character
const char Base64::Base64Table[] =
// 0000000000111111111122222222223333333333444444444455555555556666
// 0123456789012345678901234567890123456789012345678901234567890123
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Decode Table gives the index of any valid base64 character in the
// Base64 table
// 65 == A, 97 == a, 48 == 0, 43 == +, 47 == /
const unsigned char Base64::DecodeTable[] = {
// 0 1 2 3 4 5 6 7 8 9
il, il, il, il, il, il, il, il, il, sp, // 0 - 9
sp, sp, sp, sp, il, il, il, il, il, il, // 10 - 19
il, il, il, il, il, il, il, il, il, il, // 20 - 29
il, il, sp, il, il, il, il, il, il, il, // 30 - 39
il, il, il, 62, il, il, il, 63, 52, 53, // 40 - 49
54, 55, 56, 57, 58, 59, 60, 61, il, il, // 50 - 59
il, pd, il, il, il, 0, 1, 2, 3, 4, // 60 - 69
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80 - 89
25, il, il, il, il, il, il, 26, 27, 28, // 90 - 99
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119
49, 50, 51, il, il, il, il, il, il, il, // 120 - 129
il, il, il, il, il, il, il, il, il, il, // 130 - 139
il, il, il, il, il, il, il, il, il, il, // 140 - 149
il, il, il, il, il, il, il, il, il, il, // 150 - 159
il, il, il, il, il, il, il, il, il, il, // 160 - 169
il, il, il, il, il, il, il, il, il, il, // 170 - 179
il, il, il, il, il, il, il, il, il, il, // 180 - 189
il, il, il, il, il, il, il, il, il, il, // 190 - 199
il, il, il, il, il, il, il, il, il, il, // 200 - 209
il, il, il, il, il, il, il, il, il, il, // 210 - 219
il, il, il, il, il, il, il, il, il, il, // 220 - 229
il, il, il, il, il, il, il, il, il, il, // 230 - 239
il, il, il, il, il, il, il, il, il, il, // 240 - 249
il, il, il, il, il, il // 250 - 255
};
bool Base64::IsBase64Char(char ch) {
return (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z')) ||
(('0' <= ch) && (ch <= '9')) || (ch == '+') || (ch == '/');
}
bool Base64::GetNextBase64Char(char ch, char* next_ch) {
if (next_ch == nullptr) {
return false;
}
const char* p = strchr(Base64Table, ch);
if (!p)
return false;
++p;
*next_ch = (*p) ? *p : Base64Table[0];
return true;
}
bool Base64::IsBase64Encoded(absl::string_view str) {
for (size_t i = 0; i < str.size(); ++i) {
if (!IsBase64Char(str.at(i)))
return false;
}
return true;
}
void Base64::EncodeFromArray(const void* data,
size_t len,
std::string* result) {
RTC_DCHECK(result);
result->clear();
result->resize(((len + 2) / 3) * 4);
const unsigned char* byte_data = static_cast<const unsigned char*>(data);
unsigned char c;
size_t i = 0;
size_t dest_ix = 0;
while (i < len) {
c = (byte_data[i] >> 2) & 0x3f;
(*result)[dest_ix++] = Base64Table[c];
c = (byte_data[i] << 4) & 0x3f;
if (++i < len) {
c |= (byte_data[i] >> 4) & 0x0f;
}
(*result)[dest_ix++] = Base64Table[c];
if (i < len) {
c = (byte_data[i] << 2) & 0x3f;
if (++i < len) {
c |= (byte_data[i] >> 6) & 0x03;
}
(*result)[dest_ix++] = Base64Table[c];
} else {
(*result)[dest_ix++] = kPad;
}
if (i < len) {
c = byte_data[i] & 0x3f;
(*result)[dest_ix++] = Base64Table[c];
++i;
} else {
(*result)[dest_ix++] = kPad;
}
}
}
size_t Base64::GetNextQuantum(DecodeFlags parse_flags,
bool illegal_pads,
const char* data,
size_t len,
size_t* dpos,
unsigned char qbuf[4],
bool* padded) {
size_t byte_len = 0, pad_len = 0, pad_start = 0;
for (; (byte_len < 4) && (*dpos < len); ++*dpos) {
qbuf[byte_len] = DecodeTable[static_cast<unsigned char>(data[*dpos])];
if ((il == qbuf[byte_len]) || (illegal_pads && (pd == qbuf[byte_len]))) {
if (parse_flags != DO_PARSE_ANY)
break;
// Ignore illegal characters
} else if (sp == qbuf[byte_len]) {
if (parse_flags == DO_PARSE_STRICT)
break;
// Ignore spaces
} else if (pd == qbuf[byte_len]) {
if (byte_len < 2) {
if (parse_flags != DO_PARSE_ANY)
break;
// Ignore unexpected padding
} else if (byte_len + pad_len >= 4) {
if (parse_flags != DO_PARSE_ANY)
break;
// Ignore extra pads
} else {
if (1 == ++pad_len) {
pad_start = *dpos;
}
}
} else {
if (pad_len > 0) {
if (parse_flags != DO_PARSE_ANY)
break;
// Ignore pads which are followed by data
pad_len = 0;
}
++byte_len;
}
}
for (size_t i = byte_len; i < 4; ++i) {
qbuf[i] = 0;
}
if (4 == byte_len + pad_len) {
*padded = true;
} else {
*padded = false;
if (pad_len) {
// Roll back illegal padding
*dpos = pad_start;
}
}
return byte_len;
}
bool Base64::DecodeFromArray(const char* data,
size_t len,
DecodeFlags flags,
std::string* result,
size_t* data_used) {
return DecodeFromArrayTemplate<std::string>(data, len, flags, result,
data_used);
}
bool Base64::DecodeFromArray(const char* data,
size_t len,
DecodeFlags flags,
vector<char>* result,
size_t* data_used) {
return DecodeFromArrayTemplate<vector<char>>(data, len, flags, result,
data_used);
}
bool Base64::DecodeFromArray(const char* data,
size_t len,
DecodeFlags flags,
vector<uint8_t>* result,
size_t* data_used) {
return DecodeFromArrayTemplate<vector<uint8_t>>(data, len, flags, result,
data_used);
}
template <typename T>
bool Base64::DecodeFromArrayTemplate(const char* data,
size_t len,
DecodeFlags flags,
T* result,
size_t* data_used) {
RTC_DCHECK(result);
RTC_DCHECK_LE(flags, (DO_PARSE_MASK | DO_PAD_MASK | DO_TERM_MASK));
const DecodeFlags parse_flags = flags & DO_PARSE_MASK;
const DecodeFlags pad_flags = flags & DO_PAD_MASK;
const DecodeFlags term_flags = flags & DO_TERM_MASK;
RTC_DCHECK_NE(0, parse_flags);
RTC_DCHECK_NE(0, pad_flags);
RTC_DCHECK_NE(0, term_flags);
result->clear();
result->reserve(len);
size_t dpos = 0;
bool success = true, padded;
unsigned char c, qbuf[4];
while (dpos < len) {
size_t qlen = GetNextQuantum(parse_flags, (DO_PAD_NO == pad_flags), data,
len, &dpos, qbuf, &padded);
c = (qbuf[0] << 2) | ((qbuf[1] >> 4) & 0x3);
if (qlen >= 2) {
result->push_back(c);
c = ((qbuf[1] << 4) & 0xf0) | ((qbuf[2] >> 2) & 0xf);
if (qlen >= 3) {
result->push_back(c);
c = ((qbuf[2] << 6) & 0xc0) | qbuf[3];
if (qlen >= 4) {
result->push_back(c);
c = 0;
}
}
}
if (qlen < 4) {
if ((DO_TERM_ANY != term_flags) && (0 != c)) {
success = false; // unused bits
}
if ((DO_PAD_YES == pad_flags) && !padded) {
success = false; // expected padding
}
break;
}
}
if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) {
success = false; // unused chars
}
if (data_used) {
*data_used = dpos;
}
return success;
}
} // namespace rtc

View file

@ -0,0 +1,128 @@
//*********************************************************************
//* C_Base64 - a simple base64 encoder and decoder.
//*
//* Copyright (c) 1999, Bob Withers - bwit@pobox.com
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*********************************************************************
#ifndef RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_
#define RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "rtc_base/system/rtc_export.h"
namespace rtc {
class Base64 {
public:
enum DecodeOption {
DO_PARSE_STRICT = 1, // Parse only base64 characters
DO_PARSE_WHITE = 2, // Parse only base64 and whitespace characters
DO_PARSE_ANY = 3, // Parse all characters
DO_PARSE_MASK = 3,
DO_PAD_YES = 4, // Padding is required
DO_PAD_ANY = 8, // Padding is optional
DO_PAD_NO = 12, // Padding is disallowed
DO_PAD_MASK = 12,
DO_TERM_BUFFER = 16, // Must termiante at end of buffer
DO_TERM_CHAR = 32, // May terminate at any character boundary
DO_TERM_ANY = 48, // May terminate at a sub-character bit offset
DO_TERM_MASK = 48,
// Strictest interpretation
DO_STRICT = DO_PARSE_STRICT | DO_PAD_YES | DO_TERM_BUFFER,
DO_LAX = DO_PARSE_ANY | DO_PAD_ANY | DO_TERM_CHAR,
};
typedef int DecodeFlags;
static bool IsBase64Char(char ch);
// Get the char next to the `ch` from the Base64Table.
// If the `ch` is the last one in the Base64Table then returns
// the first one from the table.
// Expects the `ch` be a base64 char.
// The result will be saved in `next_ch`.
// Returns true on success.
static bool GetNextBase64Char(char ch, char* next_ch);
// Determines whether the given string consists entirely of valid base64
// encoded characters.
static bool IsBase64Encoded(absl::string_view str);
RTC_EXPORT static void EncodeFromArray(const void* data,
size_t len,
std::string* result);
RTC_EXPORT static bool DecodeFromArray(const char* data,
size_t len,
DecodeFlags flags,
std::string* result,
size_t* data_used);
static bool DecodeFromArray(const char* data,
size_t len,
DecodeFlags flags,
std::vector<char>* result,
size_t* data_used);
static bool DecodeFromArray(const char* data,
size_t len,
DecodeFlags flags,
std::vector<uint8_t>* result,
size_t* data_used);
// Convenience Methods
static inline std::string Encode(absl::string_view data) {
std::string result;
EncodeFromArray(data.data(), data.size(), &result);
return result;
}
static inline std::string Decode(absl::string_view data, DecodeFlags flags) {
std::string result;
DecodeFromArray(data.data(), data.size(), flags, &result, nullptr);
return result;
}
static inline bool Decode(absl::string_view data,
DecodeFlags flags,
std::string* result,
size_t* data_used) {
return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
}
static inline bool Decode(absl::string_view data,
DecodeFlags flags,
std::vector<char>* result,
size_t* data_used) {
return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
}
private:
static const char Base64Table[];
static const unsigned char DecodeTable[];
static size_t GetNextQuantum(DecodeFlags parse_flags,
bool illegal_pads,
const char* data,
size_t len,
size_t* dpos,
unsigned char qbuf[4],
bool* padded);
template <typename T>
static bool DecodeFromArrayTemplate(const char* data,
size_t len,
DecodeFlags flags,
T* result,
size_t* data_used);
};
} // namespace rtc
#endif /* RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_ */

View file

@ -0,0 +1,7 @@
// sigslot.h: Signal/Slot classes
//
// Written by Sarah Thompson (sarah@telergy.com) 2002.
//
// License: Public domain. You are free to use this code however you like, with
// the proviso that the author takes on no responsibility or liability for any
// use.

View file

@ -0,0 +1,22 @@
// sigslot.h: Signal/Slot classes
//
// Written by Sarah Thompson (sarah@telergy.com) 2002.
//
// License: Public domain. You are free to use this code however you like, with
// the proviso that the author takes on no responsibility or liability for any
// use.
#include "rtc_base/third_party/sigslot/sigslot.h"
namespace sigslot {
#ifdef _SIGSLOT_HAS_POSIX_THREADS
pthread_mutex_t* multi_threaded_global::get_mutex() {
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
return &g_mutex;
}
#endif // _SIGSLOT_HAS_POSIX_THREADS
} // namespace sigslot

View file

@ -0,0 +1,649 @@
// sigslot.h: Signal/Slot classes
//
// Written by Sarah Thompson (sarah@telergy.com) 2002.
//
// License: Public domain. You are free to use this code however you like, with
// the proviso that the author takes on no responsibility or liability for any
// use.
//
// QUICK DOCUMENTATION
//
// (see also the full documentation at http://sigslot.sourceforge.net/)
//
// #define switches
// SIGSLOT_PURE_ISO:
// Define this to force ISO C++ compliance. This also disables all of
// the thread safety support on platforms where it is available.
//
// SIGSLOT_USE_POSIX_THREADS:
// Force use of Posix threads when using a C++ compiler other than gcc
// on a platform that supports Posix threads. (When using gcc, this is
// the default - use SIGSLOT_PURE_ISO to disable this if necessary)
//
// SIGSLOT_DEFAULT_MT_POLICY:
// Where thread support is enabled, this defaults to
// multi_threaded_global. Otherwise, the default is single_threaded.
// #define this yourself to override the default. In pure ISO mode,
// anything other than single_threaded will cause a compiler error.
//
// PLATFORM NOTES
//
// Win32:
// On Win32, the WEBRTC_WIN symbol must be #defined. Most mainstream
// compilers do this by default, but you may need to define it yourself
// if your build environment is less standard. This causes the Win32
// thread support to be compiled in and used automatically.
//
// Unix/Linux/BSD, etc.:
// If you're using gcc, it is assumed that you have Posix threads
// available, so they are used automatically. You can override this (as
// under Windows) with the SIGSLOT_PURE_ISO switch. If you're using
// something other than gcc but still want to use Posix threads, you
// need to #define SIGSLOT_USE_POSIX_THREADS.
//
// ISO C++:
// If none of the supported platforms are detected, or if
// SIGSLOT_PURE_ISO is defined, all multithreading support is turned
// off, along with any code that might cause a pure ISO C++ environment
// to complain. Before you ask, gcc -ansi -pedantic won't compile this
// library, but gcc -ansi is fine. Pedantic mode seems to throw a lot of
// errors that aren't really there. If you feel like investigating this,
// please contact the author.
//
//
// THREADING MODES
//
// single_threaded:
// Your program is assumed to be single threaded from the point of view
// of signal/slot usage (i.e. all objects using signals and slots are
// created and destroyed from a single thread). Behaviour if objects are
// destroyed concurrently is undefined (i.e. you'll get the occasional
// segmentation fault/memory exception).
//
// multi_threaded_global:
// Your program is assumed to be multi threaded. Objects using signals
// and slots can be safely created and destroyed from any thread, even
// when connections exist. In multi_threaded_global mode, this is
// achieved by a single global mutex (actually a critical section on
// Windows because they are faster). This option uses less OS resources,
// but results in more opportunities for contention, possibly resulting
// in more context switches than are strictly necessary.
//
// multi_threaded_local:
// Behaviour in this mode is essentially the same as
// multi_threaded_global, except that each signal, and each object that
// inherits has_slots, all have their own mutex/critical section. In
// practice, this means that mutex collisions (and hence context
// switches) only happen if they are absolutely essential. However, on
// some platforms, creating a lot of mutexes can slow down the whole OS,
// so use this option with care.
//
// USING THE LIBRARY
//
// See the full documentation at http://sigslot.sourceforge.net/
//
// Libjingle specific:
//
// This file has been modified such that has_slots and signalx do not have to be
// using the same threading requirements. E.g. it is possible to connect a
// has_slots<single_threaded> and signal0<multi_threaded_local> or
// has_slots<multi_threaded_local> and signal0<single_threaded>.
// If has_slots is single threaded the user must ensure that it is not trying
// to connect or disconnect to signalx concurrently or data race may occur.
// If signalx is single threaded the user must ensure that disconnect, connect
// or signal is not happening concurrently or data race may occur.
#ifndef RTC_BASE_THIRD_PARTY_SIGSLOT_SIGSLOT_H_
#define RTC_BASE_THIRD_PARTY_SIGSLOT_SIGSLOT_H_
#include <cstring>
#include <list>
#include <set>
// On our copy of sigslot.h, we set single threading as default.
#define SIGSLOT_DEFAULT_MT_POLICY single_threaded
#if defined(SIGSLOT_PURE_ISO) || \
(!defined(WEBRTC_WIN) && !defined(__GNUG__) && \
!defined(SIGSLOT_USE_POSIX_THREADS))
#define _SIGSLOT_SINGLE_THREADED
#elif defined(WEBRTC_WIN)
#define _SIGSLOT_HAS_WIN32_THREADS
#include "windows.h"
#elif defined(__GNUG__) || defined(SIGSLOT_USE_POSIX_THREADS)
#define _SIGSLOT_HAS_POSIX_THREADS
#include <pthread.h>
#else
#define _SIGSLOT_SINGLE_THREADED
#endif
#ifndef SIGSLOT_DEFAULT_MT_POLICY
#ifdef _SIGSLOT_SINGLE_THREADED
#define SIGSLOT_DEFAULT_MT_POLICY single_threaded
#else
#define SIGSLOT_DEFAULT_MT_POLICY multi_threaded_local
#endif
#endif
// TODO: change this namespace to rtc?
namespace sigslot {
class single_threaded {
public:
void lock() {}
void unlock() {}
};
#ifdef _SIGSLOT_HAS_WIN32_THREADS
// The multi threading policies only get compiled in if they are enabled.
class multi_threaded_global {
public:
multi_threaded_global() {
static bool isinitialised = false;
if (!isinitialised) {
InitializeCriticalSection(get_critsec());
isinitialised = true;
}
}
void lock() { EnterCriticalSection(get_critsec()); }
void unlock() { LeaveCriticalSection(get_critsec()); }
private:
CRITICAL_SECTION* get_critsec() {
static CRITICAL_SECTION g_critsec;
return &g_critsec;
}
};
class multi_threaded_local {
public:
multi_threaded_local() { InitializeCriticalSection(&m_critsec); }
multi_threaded_local(const multi_threaded_local&) {
InitializeCriticalSection(&m_critsec);
}
~multi_threaded_local() { DeleteCriticalSection(&m_critsec); }
void lock() { EnterCriticalSection(&m_critsec); }
void unlock() { LeaveCriticalSection(&m_critsec); }
private:
CRITICAL_SECTION m_critsec;
};
#endif // _SIGSLOT_HAS_WIN32_THREADS
#ifdef _SIGSLOT_HAS_POSIX_THREADS
// The multi threading policies only get compiled in if they are enabled.
class multi_threaded_global {
public:
void lock() { pthread_mutex_lock(get_mutex()); }
void unlock() { pthread_mutex_unlock(get_mutex()); }
private:
static pthread_mutex_t* get_mutex();
};
class multi_threaded_local {
public:
multi_threaded_local() { pthread_mutex_init(&m_mutex, nullptr); }
multi_threaded_local(const multi_threaded_local&) {
pthread_mutex_init(&m_mutex, nullptr);
}
~multi_threaded_local() { pthread_mutex_destroy(&m_mutex); }
void lock() { pthread_mutex_lock(&m_mutex); }
void unlock() { pthread_mutex_unlock(&m_mutex); }
private:
pthread_mutex_t m_mutex;
};
#endif // _SIGSLOT_HAS_POSIX_THREADS
template <class mt_policy>
class lock_block {
public:
mt_policy* m_mutex;
lock_block(mt_policy* mtx) : m_mutex(mtx) { m_mutex->lock(); }
~lock_block() { m_mutex->unlock(); }
};
class _signal_base_interface;
class has_slots_interface {
private:
typedef void (*signal_connect_t)(has_slots_interface* self,
_signal_base_interface* sender);
typedef void (*signal_disconnect_t)(has_slots_interface* self,
_signal_base_interface* sender);
typedef void (*disconnect_all_t)(has_slots_interface* self);
const signal_connect_t m_signal_connect;
const signal_disconnect_t m_signal_disconnect;
const disconnect_all_t m_disconnect_all;
protected:
has_slots_interface(signal_connect_t conn,
signal_disconnect_t disc,
disconnect_all_t disc_all)
: m_signal_connect(conn),
m_signal_disconnect(disc),
m_disconnect_all(disc_all) {}
// Doesn't really need to be virtual, but is for backwards compatibility
// (it was virtual in a previous version of sigslot).
virtual ~has_slots_interface() {}
public:
void signal_connect(_signal_base_interface* sender) {
m_signal_connect(this, sender);
}
void signal_disconnect(_signal_base_interface* sender) {
m_signal_disconnect(this, sender);
}
void disconnect_all() { m_disconnect_all(this); }
};
class _signal_base_interface {
private:
typedef void (*slot_disconnect_t)(_signal_base_interface* self,
has_slots_interface* pslot);
typedef void (*slot_duplicate_t)(_signal_base_interface* self,
const has_slots_interface* poldslot,
has_slots_interface* pnewslot);
const slot_disconnect_t m_slot_disconnect;
const slot_duplicate_t m_slot_duplicate;
protected:
_signal_base_interface(slot_disconnect_t disc, slot_duplicate_t dupl)
: m_slot_disconnect(disc), m_slot_duplicate(dupl) {}
~_signal_base_interface() {}
public:
void slot_disconnect(has_slots_interface* pslot) {
m_slot_disconnect(this, pslot);
}
void slot_duplicate(const has_slots_interface* poldslot,
has_slots_interface* pnewslot) {
m_slot_duplicate(this, poldslot, pnewslot);
}
};
class _opaque_connection {
private:
typedef void (*emit_t)(const _opaque_connection*);
template <typename FromT, typename ToT>
union union_caster {
FromT from;
ToT to;
};
emit_t pemit;
has_slots_interface* pdest;
// Pointers to member functions may be up to 16 bytes (24 bytes for MSVC)
// for virtual classes, so make sure we have enough space to store it.
#if defined(_MSC_VER) && !defined(__clang__)
unsigned char pmethod[24];
#else
unsigned char pmethod[16];
#endif
public:
template <typename DestT, typename... Args>
_opaque_connection(DestT* pd, void (DestT::*pm)(Args...)) : pdest(pd) {
typedef void (DestT::*pm_t)(Args...);
static_assert(sizeof(pm_t) <= sizeof(pmethod),
"Size of slot function pointer too large.");
std::memcpy(pmethod, &pm, sizeof(pm_t));
typedef void (*em_t)(const _opaque_connection* self, Args...);
union_caster<em_t, emit_t> caster2;
caster2.from = &_opaque_connection::emitter<DestT, Args...>;
pemit = caster2.to;
}
has_slots_interface* getdest() const { return pdest; }
_opaque_connection duplicate(has_slots_interface* newtarget) const {
_opaque_connection res = *this;
res.pdest = newtarget;
return res;
}
// Just calls the stored "emitter" function pointer stored at construction
// time.
template <typename... Args>
void emit(Args... args) const {
typedef void (*em_t)(const _opaque_connection*, Args...);
union_caster<emit_t, em_t> caster;
caster.from = pemit;
(caster.to)(this, args...);
}
private:
template <typename DestT, typename... Args>
static void emitter(const _opaque_connection* self, Args... args) {
typedef void (DestT::*pm_t)(Args...);
pm_t pm;
static_assert(sizeof(pm_t) <= sizeof(pmethod),
"Size of slot function pointer too large.");
std::memcpy(&pm, self->pmethod, sizeof(pm_t));
(static_cast<DestT*>(self->pdest)->*(pm))(args...);
}
};
template <class mt_policy>
class _signal_base : public _signal_base_interface, public mt_policy {
protected:
typedef std::list<_opaque_connection> connections_list;
_signal_base()
: _signal_base_interface(&_signal_base::do_slot_disconnect,
&_signal_base::do_slot_duplicate),
m_current_iterator(m_connected_slots.end()) {}
~_signal_base() { disconnect_all(); }
private:
_signal_base& operator=(_signal_base const& that);
public:
_signal_base(const _signal_base& o)
: _signal_base_interface(&_signal_base::do_slot_disconnect,
&_signal_base::do_slot_duplicate),
m_current_iterator(m_connected_slots.end()) {
lock_block<mt_policy> lock(this);
for (const auto& connection : o.m_connected_slots) {
connection.getdest()->signal_connect(this);
m_connected_slots.push_back(connection);
}
}
bool is_empty() {
lock_block<mt_policy> lock(this);
return m_connected_slots.empty();
}
void disconnect_all() {
lock_block<mt_policy> lock(this);
while (!m_connected_slots.empty()) {
has_slots_interface* pdest = m_connected_slots.front().getdest();
m_connected_slots.pop_front();
pdest->signal_disconnect(static_cast<_signal_base_interface*>(this));
}
// If disconnect_all is called while the signal is firing, advance the
// current slot iterator to the end to avoid an invalidated iterator from
// being dereferenced.
m_current_iterator = m_connected_slots.end();
}
#if !defined(NDEBUG)
bool connected(has_slots_interface* pclass) {
lock_block<mt_policy> lock(this);
connections_list::const_iterator it = m_connected_slots.begin();
connections_list::const_iterator itEnd = m_connected_slots.end();
while (it != itEnd) {
if (it->getdest() == pclass)
return true;
++it;
}
return false;
}
#endif
void disconnect(has_slots_interface* pclass) {
lock_block<mt_policy> lock(this);
connections_list::iterator it = m_connected_slots.begin();
connections_list::iterator itEnd = m_connected_slots.end();
while (it != itEnd) {
if (it->getdest() == pclass) {
// If we're currently using this iterator because the signal is firing,
// advance it to avoid it being invalidated.
if (m_current_iterator == it) {
m_current_iterator = m_connected_slots.erase(it);
} else {
m_connected_slots.erase(it);
}
pclass->signal_disconnect(static_cast<_signal_base_interface*>(this));
return;
}
++it;
}
}
private:
static void do_slot_disconnect(_signal_base_interface* p,
has_slots_interface* pslot) {
_signal_base* const self = static_cast<_signal_base*>(p);
lock_block<mt_policy> lock(self);
connections_list::iterator it = self->m_connected_slots.begin();
connections_list::iterator itEnd = self->m_connected_slots.end();
while (it != itEnd) {
connections_list::iterator itNext = it;
++itNext;
if (it->getdest() == pslot) {
// If we're currently using this iterator because the signal is firing,
// advance it to avoid it being invalidated.
if (self->m_current_iterator == it) {
self->m_current_iterator = self->m_connected_slots.erase(it);
} else {
self->m_connected_slots.erase(it);
}
}
it = itNext;
}
}
static void do_slot_duplicate(_signal_base_interface* p,
const has_slots_interface* oldtarget,
has_slots_interface* newtarget) {
_signal_base* const self = static_cast<_signal_base*>(p);
lock_block<mt_policy> lock(self);
connections_list::iterator it = self->m_connected_slots.begin();
connections_list::iterator itEnd = self->m_connected_slots.end();
while (it != itEnd) {
if (it->getdest() == oldtarget) {
self->m_connected_slots.push_back(it->duplicate(newtarget));
}
++it;
}
}
protected:
connections_list m_connected_slots;
// Used to handle a slot being disconnected while a signal is
// firing (iterating m_connected_slots).
connections_list::iterator m_current_iterator;
bool m_erase_current_iterator = false;
};
template <class mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
class has_slots : public has_slots_interface, public mt_policy {
private:
typedef std::set<_signal_base_interface*> sender_set;
typedef sender_set::const_iterator const_iterator;
public:
has_slots()
: has_slots_interface(&has_slots::do_signal_connect,
&has_slots::do_signal_disconnect,
&has_slots::do_disconnect_all) {}
has_slots(has_slots const& o)
: has_slots_interface(&has_slots::do_signal_connect,
&has_slots::do_signal_disconnect,
&has_slots::do_disconnect_all) {
lock_block<mt_policy> lock(this);
for (auto* sender : o.m_senders) {
sender->slot_duplicate(&o, this);
m_senders.insert(sender);
}
}
~has_slots() { this->disconnect_all(); }
private:
has_slots& operator=(has_slots const&);
static void do_signal_connect(has_slots_interface* p,
_signal_base_interface* sender) {
has_slots* const self = static_cast<has_slots*>(p);
lock_block<mt_policy> lock(self);
self->m_senders.insert(sender);
}
static void do_signal_disconnect(has_slots_interface* p,
_signal_base_interface* sender) {
has_slots* const self = static_cast<has_slots*>(p);
lock_block<mt_policy> lock(self);
self->m_senders.erase(sender);
}
static void do_disconnect_all(has_slots_interface* p) {
has_slots* const self = static_cast<has_slots*>(p);
lock_block<mt_policy> lock(self);
while (!self->m_senders.empty()) {
std::set<_signal_base_interface*> senders;
senders.swap(self->m_senders);
const_iterator it = senders.begin();
const_iterator itEnd = senders.end();
while (it != itEnd) {
_signal_base_interface* s = *it;
++it;
s->slot_disconnect(p);
}
}
}
private:
sender_set m_senders;
};
template <class mt_policy, typename... Args>
class signal_with_thread_policy : public _signal_base<mt_policy> {
private:
typedef _signal_base<mt_policy> base;
protected:
typedef typename base::connections_list connections_list;
public:
signal_with_thread_policy() {}
template <class desttype>
void connect(desttype* pclass, void (desttype::*pmemfun)(Args...)) {
lock_block<mt_policy> lock(this);
this->m_connected_slots.push_back(_opaque_connection(pclass, pmemfun));
pclass->signal_connect(static_cast<_signal_base_interface*>(this));
}
void emit(Args... args) {
lock_block<mt_policy> lock(this);
this->m_current_iterator = this->m_connected_slots.begin();
while (this->m_current_iterator != this->m_connected_slots.end()) {
_opaque_connection const& conn = *this->m_current_iterator;
++(this->m_current_iterator);
conn.emit<Args...>(args...);
}
}
void operator()(Args... args) { emit(args...); }
};
// Alias with default thread policy. Needed because both default arguments
// and variadic template arguments must go at the end of the list, so we
// can't have both at once.
template <typename... Args>
using signal = signal_with_thread_policy<SIGSLOT_DEFAULT_MT_POLICY, Args...>;
// The previous verion of sigslot didn't use variadic templates, so you would
// need to write "sigslot::signal2<Arg1, Arg2>", for example.
// Now you can just write "sigslot::signal<Arg1, Arg2>", but these aliases
// exist for backwards compatibility.
template <typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal0 = signal_with_thread_policy<mt_policy>;
template <typename A1, typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal1 = signal_with_thread_policy<mt_policy, A1>;
template <typename A1,
typename A2,
typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal2 = signal_with_thread_policy<mt_policy, A1, A2>;
template <typename A1,
typename A2,
typename A3,
typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal3 = signal_with_thread_policy<mt_policy, A1, A2, A3>;
template <typename A1,
typename A2,
typename A3,
typename A4,
typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal4 = signal_with_thread_policy<mt_policy, A1, A2, A3, A4>;
template <typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal5 = signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5>;
template <typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal6 = signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5, A6>;
template <typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal7 =
signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5, A6, A7>;
template <typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
using signal8 =
signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5, A6, A7, A8>;
} // namespace sigslot
#endif /* RTC_BASE_THIRD_PARTY_SIGSLOT_SIGSLOT_H_ */