diff --git a/.gitignore b/.gitignore index 12404dd..ab09395 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,6 @@ compile_commands.json CTestTestfile.cmake _deps +# Clion +.idea/ +cmake-* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..24fd172 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.10) + +option(ULIB_BUILD_TESTS "Build tests" OFF) + +project(ulib LANGUAGES CXX VERSION 0.1.0) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS ON) + +add_library(${PROJECT_NAME} STATIC "" + src/base/location.cpp + src/strings/string_printf.cpp + src/system/system_time.cpp + src/base/task_queue_base.cpp + src/units/time_delta.cpp + src/synchronization/mutex.cpp + src/network/socket_factory.cpp + src/network/socket_address.cpp + src/network/socket_server.cpp + src/synchronization/event.cpp) +target_compile_definitions(${PROJECT_NAME} PRIVATE ULIB_LIBRARY_IMPL) +target_include_directories(${PROJECT_NAME} PUBLIC src) +target_sources(${PROJECT_NAME} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src//base/checks.h + ${CMAKE_CURRENT_SOURCE_DIR}/src//base/checks.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/types/optional.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/system/export_api.h +) + + +if (ULIB_BUILD_TESTS) + find_package(GTest CONFIG REQUIRED) + + add_executable(ulib_test "" + src/base/location_unittest.cpp) + + target_link_libraries(ulib_test PRIVATE + ulib + GTest::gtest + GTest::gtest_main + GTest::gmock + GTest::gmock_main + ) + + target_sources(ulib_test PRIVATE + src/base/location_unittest.cpp + ) + + add_test(AllTestsInUlib ulib_test) +endif () \ No newline at end of file diff --git a/src/3party/sigslot/sigslot.h b/src/3party/sigslot/sigslot.h new file mode 100644 index 0000000..912b235 --- /dev/null +++ b/src/3party/sigslot/sigslot.h @@ -0,0 +1,1682 @@ +// 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 WIN32 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/ +// +// + +#ifndef __SIGSLOT_H__ +#define __SIGSLOT_H__ + +#include +#include + +#if defined(SIGSLOT_PURE_ISO) || (!defined(WIN32) && !defined(__GNUG__) && !defined(SIGSLOT_USE_POSIX_THREADS)) +# define _SIGSLOT_SINGLE_THREADED +#elif defined(WIN32) +# define _SIGSLOT_HAS_WIN32_THREADS +# include +#elif defined(__GNUG__) || defined(SIGSLOT_USE_POSIX_THREADS) +# define _SIGSLOT_HAS_POSIX_THREADS +# include +#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 + +#ifndef SIGSLOT_EMIT +# ifdef QT_VERSION +# define SIGSLOT_EMIT broadcast +# else +# define SIGSLOT_EMIT emit +# endif +#endif + +namespace sigslot { + +class single_threaded +{ +public: + single_threaded() {} + virtual ~single_threaded() {} + virtual void lock() {} + virtual 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; + } + } + + multi_threaded_global(const multi_threaded_global&) + {} + + virtual ~multi_threaded_global() + {} + + virtual void lock() + { + EnterCriticalSection(get_critsec()); + } + + virtual 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); + } + + virtual ~multi_threaded_local() + { + DeleteCriticalSection(&m_critsec); + } + + virtual void lock() + { + EnterCriticalSection(&m_critsec); + } + + virtual 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: + multi_threaded_global() + { + pthread_mutex_init(get_mutex(), NULL); + } + + multi_threaded_global(const multi_threaded_global&) + {} + + virtual ~multi_threaded_global() + {} + + virtual void lock() + { + pthread_mutex_lock(get_mutex()); + } + + virtual void unlock() + { + pthread_mutex_unlock(get_mutex()); + } + +private: + pthread_mutex_t* get_mutex() + { + static pthread_mutex_t g_mutex; + return &g_mutex; + } +}; + +class multi_threaded_local +{ +public: + multi_threaded_local() + { + pthread_mutex_init(&m_mutex, NULL); + } + + multi_threaded_local(const multi_threaded_local&) + { + pthread_mutex_init(&m_mutex, NULL); + } + + virtual ~multi_threaded_local() + { + pthread_mutex_destroy(&m_mutex); + } + + virtual void lock() + { + pthread_mutex_lock(&m_mutex); + } + + virtual void unlock() + { + pthread_mutex_unlock(&m_mutex); + } + +private: + pthread_mutex_t m_mutex; +}; +#endif // _SIGSLOT_HAS_POSIX_THREADS + +template +class lock_block +{ +public: + mt_policy *m_mutex; + + lock_block(mt_policy *mtx) + : m_mutex(mtx) + { + m_mutex->lock(); + } + + ~lock_block() + { + m_mutex->unlock(); + } +}; + +template +class has_slots; + +template +class _connection_base0 +{ +public: + virtual ~_connection_base0() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT() = 0; + virtual _connection_base0* clone() = 0; + virtual _connection_base0* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base1 +{ +public: + virtual ~_connection_base1() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type) = 0; + virtual _connection_base1* clone() = 0; + virtual _connection_base1* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base2 +{ +public: + virtual ~_connection_base2() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type, arg2_type) = 0; + virtual _connection_base2* clone() = 0; + virtual _connection_base2* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base3 +{ +public: + virtual ~_connection_base3() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type, arg2_type, arg3_type) = 0; + virtual _connection_base3* clone() = 0; + virtual _connection_base3* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base4 +{ +public: + virtual ~_connection_base4() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type, arg2_type, arg3_type, arg4_type) = 0; + virtual _connection_base4* clone() = 0; + virtual _connection_base4* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base5 +{ +public: + virtual ~_connection_base5() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type, arg2_type, arg3_type, arg4_type, + arg5_type) = 0; + virtual _connection_base5* clone() = 0; + virtual _connection_base5* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base6 +{ +public: + virtual ~_connection_base6() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type, arg2_type, arg3_type, arg4_type, arg5_type, + arg6_type) = 0; + virtual _connection_base6* clone() = 0; + virtual _connection_base6* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base7 +{ +public: + virtual ~_connection_base7() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type, arg2_type, arg3_type, arg4_type, arg5_type, + arg6_type, arg7_type) = 0; + virtual _connection_base7* clone() = 0; + virtual _connection_base7* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _connection_base8 +{ +public: + virtual ~_connection_base8() {} + virtual has_slots* getdest() const = 0; + virtual void SIGSLOT_EMIT(arg1_type, arg2_type, arg3_type, arg4_type, arg5_type, + arg6_type, arg7_type, arg8_type) = 0; + virtual _connection_base8* clone() = 0; + virtual _connection_base8* duplicate(has_slots* pnewdest) = 0; +}; + +template +class _signal_base : public mt_policy +{ +public: + virtual void slot_disconnect(has_slots* pslot) = 0; + virtual void slot_duplicate(const has_slots* poldslot, has_slots* pnewslot) = 0; +}; + +// Implements common functionality of signalN classes. signalN classes +// derive from this class. +template +class _signal_base_middle: public _signal_base +{ +public: + + typedef T connections_list; + typedef typename connections_list::iterator iterator; + typedef typename connections_list::const_iterator const_iterator; + + _signal_base_middle() {} + + _signal_base_middle(const _signal_base_middle &s) + : _signal_base(s) + { + lock_block lock(this); + + const_iterator it = s.m_connected_slots.begin(); + const_iterator itEnd = s.m_connected_slots.end(); + + while(it != itEnd) + { + (*it)->getdest()->signal_connect(this); + m_connected_slots.push_back((*it)->clone()); + ++it; + } + } + + virtual ~_signal_base_middle() {} + + void disconnect_all() + { + lock_block lock(this); + + const_iterator it = m_connected_slots.begin(); + const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + (*it)->getdest()->signal_disconnect(this); + delete *it; + + ++it; + } + + m_connected_slots.erase(m_connected_slots.begin(), m_connected_slots.end()); + } + + bool is_empty() + { + lock_block lock(this); + + const_iterator it = m_connected_slots.begin(); + const_iterator itEnd = m_connected_slots.end(); + + return it == itEnd; + } + +#ifdef _DEBUG + bool connected(has_slots* pclass) + { + lock_block lock(this); + + const_iterator itNext, it = m_connected_slots.begin(); + const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + if ((*it)->getdest() == pclass) + return true; + it = itNext; + } + return false; + } +#endif + + void disconnect(has_slots* pclass) + { + lock_block lock(this); + + iterator it = m_connected_slots.begin(); + iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + if((*it)->getdest() == pclass) + { + delete *it; + m_connected_slots.erase(it); + pclass->signal_disconnect(this); + return; + } + + ++it; + } + } + + void slot_disconnect(has_slots* pslot) + { + lock_block lock(this); + + iterator it = m_connected_slots.begin(); + iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + iterator itNext = it; + ++itNext; + + if((*it)->getdest() == pslot) + { + delete *it; + m_connected_slots.erase(it); + } + + it = itNext; + } + } + + void slot_duplicate(const has_slots* oldtarget, has_slots* newtarget) + { + lock_block lock(this); + + iterator it = m_connected_slots.begin(); + iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + if((*it)->getdest() == oldtarget) + { + m_connected_slots.push_back((*it)->duplicate(newtarget)); + } + + ++it; + } + } + +protected: + + connections_list m_connected_slots; + +}; + +template +class has_slots : public mt_policy +{ +private: + typedef typename std::set<_signal_base *> sender_set; + typedef typename sender_set::const_iterator const_iterator; + +public: + has_slots() + {} + + has_slots(const has_slots& hs) + : mt_policy(hs) + { + lock_block lock(this); + const_iterator it = hs.m_senders.begin(); + const_iterator itEnd = hs.m_senders.end(); + + while(it != itEnd) + { + (*it)->slot_duplicate(&hs, this); + m_senders.insert(*it); + ++it; + } + } + + void signal_connect(_signal_base* sender) + { + lock_block lock(this); + m_senders.insert(sender); + } + + void signal_disconnect(_signal_base* sender) + { + lock_block lock(this); + m_senders.erase(sender); + } + + virtual ~has_slots() + { + disconnect_all(); + } + + void disconnect_all() + { + lock_block lock(this); + const_iterator it = m_senders.begin(); + const_iterator itEnd = m_senders.end(); + + while(it != itEnd) + { + (*it)->slot_disconnect(this); + ++it; + } + + m_senders.erase(m_senders.begin(), m_senders.end()); + } + +private: + sender_set m_senders; +}; + + + +template +class _connection0 : public _connection_base0 +{ +public: + _connection0() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection0(dest_type* pobject, void (dest_type::*pmemfun)()) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection0() + {} + + virtual _connection_base0* clone() + { + return new _connection0(*this); + } + + virtual _connection_base0* duplicate(has_slots* pnewdest) + { + return new _connection0((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT() + { + (m_pobject->*m_pmemfun)(); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(); +}; + +template +class _connection1 : public _connection_base1 +{ +public: + _connection1() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection1(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection1() + {} + + virtual _connection_base1* clone() + { + return new _connection1(*this); + } + + virtual _connection_base1* duplicate(has_slots* pnewdest) + { + return new _connection1((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1) + { + (m_pobject->*m_pmemfun)(a1); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type); +}; + +template +class _connection2 : public _connection_base2 +{ +public: + _connection2() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection2(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type, + arg2_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection2() + {} + + virtual _connection_base2* clone() + { + return new _connection2(*this); + } + + virtual _connection_base2* duplicate(has_slots* pnewdest) + { + return new _connection2((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1, arg2_type a2) + { + (m_pobject->*m_pmemfun)(a1, a2); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type, arg2_type); +}; + +template +class _connection3 : public _connection_base3 +{ +public: + _connection3() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection3(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type, + arg2_type, arg3_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection3() + {} + + virtual _connection_base3* clone() + { + return new _connection3(*this); + } + + virtual _connection_base3* duplicate(has_slots* pnewdest) + { + return new _connection3((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3) + { + (m_pobject->*m_pmemfun)(a1, a2, a3); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type, arg2_type, arg3_type); +}; + +template +class _connection4 : public _connection_base4 +{ +public: + _connection4() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection4(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection4() + {} + + virtual _connection_base4* clone() + { + return new _connection4(*this); + } + + virtual _connection_base4* duplicate(has_slots* pnewdest) + { + return new _connection4((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, + arg4_type a4) + { + (m_pobject->*m_pmemfun)(a1, a2, a3, a4); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type, arg2_type, arg3_type, + arg4_type); +}; + +template +class _connection5 : public _connection_base5 +{ +public: + _connection5() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection5(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection5() + {} + + virtual _connection_base5* clone() + { + return new _connection5(*this); + } + + virtual _connection_base5* duplicate(has_slots* pnewdest) + { + return new _connection5((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5) + { + (m_pobject->*m_pmemfun)(a1, a2, a3, a4, a5); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type, arg2_type, arg3_type, arg4_type, + arg5_type); +}; + +template +class _connection6 : public _connection_base6 +{ +public: + _connection6() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection6(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type, arg6_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection6() + {} + + virtual _connection_base6* clone() + { + return new _connection6(*this); + } + + virtual _connection_base6* duplicate(has_slots* pnewdest) + { + return new _connection6((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6) + { + (m_pobject->*m_pmemfun)(a1, a2, a3, a4, a5, a6); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type, arg2_type, arg3_type, arg4_type, + arg5_type, arg6_type); +}; + +template +class _connection7 : public _connection_base7 +{ +public: + _connection7() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection7(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type, arg6_type, arg7_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection7() + {} + + virtual _connection_base7* clone() + { + return new _connection7(*this); + } + + virtual _connection_base7* duplicate(has_slots* pnewdest) + { + return new _connection7((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6, arg7_type a7) + { + (m_pobject->*m_pmemfun)(a1, a2, a3, a4, a5, a6, a7); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type, arg2_type, arg3_type, arg4_type, + arg5_type, arg6_type, arg7_type); +}; + +template +class _connection8 : public _connection_base8 +{ +public: + _connection8() + { + m_pobject = NULL; + m_pmemfun = NULL; + } + + _connection8(dest_type* pobject, void (dest_type::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type, arg6_type, + arg7_type, arg8_type)) + { + m_pobject = pobject; + m_pmemfun = pmemfun; + } + + virtual ~_connection8() + {} + + virtual _connection_base8* clone() + { + return new _connection8(*this); + } + + virtual _connection_base8* duplicate(has_slots* pnewdest) + { + return new _connection8((dest_type *)pnewdest, m_pmemfun); + } + + virtual void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6, arg7_type a7, arg8_type a8) + { + (m_pobject->*m_pmemfun)(a1, a2, a3, a4, a5, a6, a7, a8); + } + + virtual has_slots* getdest() const + { + return m_pobject; + } + +private: + dest_type* m_pobject; + void (dest_type::* m_pmemfun)(arg1_type, arg2_type, arg3_type, arg4_type, + arg5_type, arg6_type, arg7_type, arg8_type); +}; + +template +class signal0 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal0 this_type; + typedef std::list<_connection_base0*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal0() + {} + + signal0(const this_type& s) + : base_type(s) + {} + + ~signal0() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)()) + { + lock_block lock(this); + + _connection0* conn = + new _connection0(pclass, pmemfun); + + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT() + { + lock_block lock(this); + + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(); + + it = itNext; + } + } + + void operator()() + { + SIGSLOT_EMIT(); + } +}; + +template +class signal1 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal1 this_type; + typedef std::list<_connection_base1*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal1() + {} + + signal1(const this_type& s) + : base_type(s) + {} + + ~signal1() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type)) + { + lock_block lock(this); + _connection1* conn = + new _connection1(pclass, pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1); + + it = itNext; + } + } + + void operator()(arg1_type a1) + { + SIGSLOT_EMIT(a1); + } +}; + +template +class signal2 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal2 this_type; + typedef std::list<_connection_base2*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal2() + {} + + signal2(const this_type& s) + : base_type(s) + {} + + ~signal2() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type, + arg2_type)) + { + lock_block lock(this); + _connection2* conn = new + _connection2(pclass, pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1, arg2_type a2) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1, a2); + + it = itNext; + } + } + + void operator()(arg1_type a1, arg2_type a2) + { + SIGSLOT_EMIT(a1, a2); + } +}; + +template +class signal3 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal3 this_type; + typedef std::list<_connection_base3*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal3() + {} + + signal3(const this_type& s) + : base_type(s) + {} + + ~signal3() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type, + arg2_type, arg3_type)) + { + lock_block lock(this); + _connection3* conn = + new _connection3(pclass, + pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1, a2, a3); + + it = itNext; + } + } + + void operator()(arg1_type a1, arg2_type a2, arg3_type a3) + { + SIGSLOT_EMIT(a1, a2, a3); + } +}; + +template +class signal4 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal4 this_type; + typedef std::list<_connection_base4*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal4() + {} + + signal4(const this_type& s) + : base_type(s) + {} + + ~signal4() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type)) + { + lock_block lock(this); + _connection4* + conn = new _connection4(pclass, pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1, a2, a3, a4); + + it = itNext; + } + } + + void operator()(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4) + { + SIGSLOT_EMIT(a1, a2, a3, a4); + } +}; + +template +class signal5 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal5 this_type; + typedef std::list<_connection_base5*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal5() + {} + + signal5(const this_type& s) + : base_type(s) + {} + + ~signal5() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type)) + { + lock_block lock(this); + _connection5* conn = new _connection5(pclass, pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1, a2, a3, a4, a5); + + it = itNext; + } + } + + void operator()(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5) + { + SIGSLOT_EMIT(a1, a2, a3, a4, a5); + } +}; + + +template +class signal6 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal6 this_type; + typedef std::list<_connection_base6*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal6() + {} + + signal6(const this_type& s) + : base_type(s) + {} + + ~signal6() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type, arg6_type)) + { + lock_block lock(this); + _connection6* conn = + new _connection6(pclass, pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1, a2, a3, a4, a5, a6); + + it = itNext; + } + } + + void operator()(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6) + { + SIGSLOT_EMIT(a1, a2, a3, a4, a5, a6); + } +}; + +template +class signal7 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal7 this_type; + typedef std::list<_connection_base7*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal7() + {} + + signal7(const this_type& s) + : base_type(s) + {} + + ~signal7() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type, arg6_type, + arg7_type)) + { + lock_block lock(this); + _connection7* conn = + new _connection7(pclass, pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6, arg7_type a7) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1, a2, a3, a4, a5, a6, a7); + + it = itNext; + } + } + + void operator()(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6, arg7_type a7) + { + SIGSLOT_EMIT(a1, a2, a3, a4, a5, a6, a7); + } +}; + +template +class signal8 : public _signal_base_middle*>, mt_policy> +{ +public: + + typedef signal8 this_type; + typedef std::list<_connection_base8*> connections_list; + typedef _signal_base_middle base_type; + + using base_type::m_connected_slots; + + signal8() + {} + + signal8(const this_type& s) + : base_type(s) + {} + + ~signal8() + { + base_type::disconnect_all(); + } + + template + void connect(desttype* pclass, void (desttype::*pmemfun)(arg1_type, + arg2_type, arg3_type, arg4_type, arg5_type, arg6_type, + arg7_type, arg8_type)) + { + lock_block lock(this); + _connection8* conn = + new _connection8(pclass, pmemfun); + m_connected_slots.push_back(conn); + pclass->signal_connect(this); + } + + void SIGSLOT_EMIT(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6, arg7_type a7, arg8_type a8) + { + lock_block lock(this); + typename connections_list::const_iterator itNext, it = m_connected_slots.begin(); + typename connections_list::const_iterator itEnd = m_connected_slots.end(); + + while(it != itEnd) + { + itNext = it; + ++itNext; + + (*it)->SIGSLOT_EMIT(a1, a2, a3, a4, a5, a6, a7, a8); + + it = itNext; + } + } + + void operator()(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4, + arg5_type a5, arg6_type a6, arg7_type a7, arg8_type a8) + { + SIGSLOT_EMIT(a1, a2, a3, a4, a5, a6, a7, a8); + } +}; + +namespace impl +{ + +struct empty {}; + +} // namespace impl + +// signal can be used instead of the numbered signalN classes. +// For example: +// +// sigslot::signal signal; +// +// instead of +// +// sigslot::signal2 signal; +template +struct signal; + +template<> +struct signal<>: public signal0<> +{}; + +template +struct signal: public signal1 +{}; + +template +struct signal: public signal2 +{}; + +template +struct signal: public signal3 +{}; + +template +struct signal: public signal4 +{}; + +template +struct signal: public signal5 +{}; + +template +struct signal: public signal6 +{}; + +template +struct signal: + public signal7 +{}; + +template +struct signal: public signal8 +{}; + +// Some convenience methods for signal handling. +template +struct has_signals +{ + virtual ~has_signals() {} + + // Connect a signal to a slot on the specified destination object. + template + static inline void connect(Signal &signal, Dst *dst, Sig memfun) + { + signal.connect(dst, memfun); + } + + // Connect a signal to a slot on 'this'. + template + inline void connect(Signal &signal, Sig memfun) + { + Derived* dst = static_cast(this); + connect(signal, dst, memfun); + } +}; + +}; // namespace sigslot + +#endif // __SIGSLOT_H__ \ No newline at end of file diff --git a/src/base/checks.cpp b/src/base/checks.cpp new file mode 100644 index 0000000..ff0e5ee --- /dev/null +++ b/src/base/checks.cpp @@ -0,0 +1,27 @@ +// +// Created by tqcq on 2023/11/16. +// +#include "checks.h" +#include +#include + + +namespace tqcq { + +namespace detail { + +void CheckLog(const char *expr, const char *file, int line, const char *msg, ...) { + std::fprintf(stderr, "Check failed: %s:%d \"%s\" ", file, line, expr); + if (msg) { + va_list ap; + va_start(ap, msg); + std::vfprintf(stderr, msg, ap); + va_end(ap); + } + std::fprintf(stderr, "\n"); + std::abort(); +} + +} + +} diff --git a/src/base/checks.h b/src/base/checks.h new file mode 100644 index 0000000..0df761c --- /dev/null +++ b/src/base/checks.h @@ -0,0 +1,58 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_BASE_CHECKS_H_ +#define ULIB_SRC_BASE_CHECKS_H_ + +#include + +namespace tqcq { + +namespace detail { +void CheckLog(const char* expr, const char* file, int line, const char* msg, ...); +} // namespace detail + +#define ULIB_CHECK_IMPL(condition, file, line) ::tqcq::detail::CheckLog(condition, file, line, "") + +#if !defined(NDEBUG) || defined(ULIB_DCHECK_ALWAYS_ON) +#define ULIB_DCHECK_IS_ON 1 +#else +#define ULIB_DCHECK_IS_ON 0 +#endif + +#define ULIB_CHECK(condition) \ + (condition) ? static_cast(0) : ULIB_CHECK_IMPL(#condition, __FILE__, __LINE__) +#define ULIB_CHECK_OP(val1, val2, op) \ + ((val1) op (val2)) ? static_cast(0) : ULIB_CHECK_OP_IMPL(#val1 " " #op " " #val2, __FILE__, __LINE__) + +#define ULIB_CHECK_EQ(val1, val2) ULIB_CHECK_OP(val1, val2, ==) +#define ULIB_CHECK_NE(val1, val2) ULIB_CHECK_OP(val1, val2, !=) +#define ULIB_CHECK_LE(val1, val2) ULIB_CHECK_OP(val1, val2, <=) +#define ULIB_CHECK_LT(val1, val2) ULIB_CHECK_OP(val1, val2, <) +#define ULIB_CHECK_GE(val1, val2) ULIB_CHECK_OP(val1, val2, >=) +#define ULIB_CHECK_GT(val1, val2) ULIB_CHECK_OP(val1, val2, >) + +#if ULIB_DCHECK_IS_ON +#define ULIB_DCHECK(condition) ULIB_CHECK(condition) +#define ULIB_DCHECK_EQ(val1, val2) ULIB_CHECK_EQ(val1, val2) +#define ULIB_DCHECK_NE(val1, val2) ULIB_CHECK_NE(val1, val2) +#define ULIB_DCHECK_LE(val1, val2) ULIB_CHECK_LE(val1, val2) +#define ULIB_DCHECK_LT(val1, val2) ULIB_CHECK_LT(val1, val2) +#define ULIB_DCHECK_GE(val1, val2) ULIB_CHECK_GE(val1, val2) +#define ULIB_DCHECK_GT(val1, val2) ULIB_CHECK_GT(val1, val2) +#else // ULIB_DCHECK_IS_ON +#define ULIB_DCHECK(condition) static_cast(0) +#define ULIB_DCHECK_EQ(val1, val2) static_cast(0) +#define ULIB_DCHECK_NE(val1, val2) static_cast(0) +#define ULIB_DCHECK_LE(val1, val2) static_cast(0) +#define ULIB_DCHECK_LT(val1, val2) static_cast(0) +#define ULIB_DCHECK_GE(val1, val2) static_cast(0) +#define ULIB_DCHECK_GT(val1, val2) static_cast(0) +#endif // ULIB_DCHECK_IS_ON + +#define ULIB_RATAL() ULIB_CHECK(false) + +} // namespacce tqcq + +#endif //ULIB_SRC_BASE_CHECKS_H_ diff --git a/src/base/inline.h b/src/base/inline.h new file mode 100644 index 0000000..5b90999 --- /dev/null +++ b/src/base/inline.h @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_BASE_INLINE_H_ +#define ULIB_SRC_BASE_INLINE_H_ + +#endif //ULIB_SRC_BASE_INLINE_H_ diff --git a/src/base/location.cpp b/src/base/location.cpp new file mode 100644 index 0000000..a3255a1 --- /dev/null +++ b/src/base/location.cpp @@ -0,0 +1,80 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "location.h" +#include "strings/string_printf.h" + +namespace tqcq { + +#if defined(COMPILER_MSVC) +#define RETURN_ADDRESS() _ReturnAddress() +#elif defined(COMPILER_GCC) +#define RETURN_ADDRESS() \ + __builtin_extract_return_addr(__builtin_return_address(0)) +#else +#define RETURN_ADDRESS() nullptr +#endif + +template +struct StrLenImpl { + static constexpr int value = StrLenImpl::value; +}; + +template +struct StrLenImpl<'\0', len, str> { + static constexpr int value = len; +}; + +template +struct StrLen { + static constexpr int value = StrLenImpl::value; +}; + +template<> +struct StrLen { + static constexpr int value = 0; +}; + + +// calc stripped length +constexpr char full_path[] = __FILE__; +#if defined(__clang__) && defined(_MSC_VER) +constexpr char stripped[] = "base\\location.cpp"; +#else +constexpr char stripped[] = "base/location.cpp"; +#endif + +static_assert(StrLen::value >= StrLen::value, + "The file name does not match the expected prefix format."); +constexpr size_t kStrippedFilePathPrefixLength= StrLen::value - StrLen::value; + +Location::Location() = default; +Location::Location(const Location &other) = default; +Location::Location(Location &&other) = default; +Location &Location::operator=(const Location &other) = default; + +Location Location::Current(const char *function_name, const char *file_name, int line_number) { + return Location(function_name, file_name + kStrippedFilePathPrefixLength, line_number, RETURN_ADDRESS()); +} + +std::string Location::ToString() const { + if (has_source_info()) { + return std::string(function_name_) + "@" + file_name_ + ":" + std::to_string(line_number_); + } + + return StringPrintf("pc:%p", program_counter_); +} + +Location::Location(const char *function_name, const char *file_name, int line_number, const void *program_counter) + : function_name_(function_name), + file_name_(file_name), + line_number_(line_number), + program_counter_(program_counter) { +} + +const void *GetProgramCounter() { + return RETURN_ADDRESS(); +} + +} // namespace tqcq diff --git a/src/base/location.h b/src/base/location.h new file mode 100644 index 0000000..8e0e8ca --- /dev/null +++ b/src/base/location.h @@ -0,0 +1,49 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_BASE_LOCATION_H_ +#define ULIB_SRC_BASE_LOCATION_H_ +#include +namespace tqcq { + +class Location { + public: + Location(); + Location(const Location &other); + Location(Location &&other); + Location &operator=(const Location &other); + + static Location Current(const char *function_name = __builtin_FUNCTION(), const char *file_name = __builtin_FILE(), int line_number = __builtin_LINE()); + + bool operator < (const Location& other) const { + return program_counter_ < other.program_counter_; + } + + bool operator == (const Location& other) const { + return program_counter_ == other.program_counter_; + } + + bool has_source_info() const { return function_name_ && file_name_; } + const char *function_name() const { return function_name_; } + const char *file_name() const { return file_name_; } + int line_number() const { return line_number_; } + const void *program_counter() const { return program_counter_; } + + std::string ToString() const; + + private: + Location(const char *function_name, const char *file_name, int line_number, const void *program_counter); + const char *function_name_ = nullptr; + const char *file_name_ = nullptr; + int line_number_ = -1; + const void *program_counter_ = nullptr; +}; + +const void *GetProgramCounter(); + +#define FROM_HERE ::tqcq::Location::Current(__FUNCTION__, __FILE__, __LINE__) + +} // namespace tqcq + +#endif //ULIB_SRC_BASE_LOCATION_H_ diff --git a/src/base/location_unittest.cpp b/src/base/location_unittest.cpp new file mode 100644 index 0000000..eb33043 --- /dev/null +++ b/src/base/location_unittest.cpp @@ -0,0 +1,21 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "location.h" +#include +#include + +namespace tqcq { +#define WhereAmI() (FROM_HERE) + +TEST(LocationTest, CurrentYieldsCorrectValue) { + int previous_line = __LINE__; + Location here = WhereAmI(); + EXPECT_NE(here.program_counter(), WhereAmI().program_counter()); + EXPECT_THAT(here.file_name(), ::testing::EndsWith("location_unittest.cpp")); + EXPECT_EQ(here.line_number(), previous_line + 1); + EXPECT_STREQ("TestBody", here.function_name()); +} + +} \ No newline at end of file diff --git a/src/base/scoped_clear_last_error.cpp b/src/base/scoped_clear_last_error.cpp new file mode 100644 index 0000000..d0dc20d --- /dev/null +++ b/src/base/scoped_clear_last_error.cpp @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "scoped_clear_last_error.h" + +namespace tqcq { +} // namespace tqcq diff --git a/src/base/scoped_clear_last_error.h b/src/base/scoped_clear_last_error.h new file mode 100644 index 0000000..3cae59d --- /dev/null +++ b/src/base/scoped_clear_last_error.h @@ -0,0 +1,22 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_BASE_SCOPED_CLEAR_LAST_ERROR_H_ +#define ULIB_SRC_BASE_SCOPED_CLEAR_LAST_ERROR_H_ +#include +namespace tqcq { + +class ScopedClearLastError { + public: + ScopedClearLastError() : last_errno_(errno) { errno = 0; } + ScopedClearLastError(const ScopedClearLastError &) = delete; + ScopedClearLastError &operator=(const ScopedClearLastError &) = delete; + ~ScopedClearLastError() { errno = last_errno_; } + private: + const int last_errno_; +}; + +} // namespace tqcq + +#endif //ULIB_SRC_BASE_SCOPED_CLEAR_LAST_ERROR_H_ diff --git a/src/base/task_queue_base.cpp b/src/base/task_queue_base.cpp new file mode 100644 index 0000000..9179dd5 --- /dev/null +++ b/src/base/task_queue_base.cpp @@ -0,0 +1,22 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "task_queue_base.h" + +namespace tqcq { + +namespace { +thread_local TaskQueueBase *current_task_queue = nullptr; +} + +TaskQueueBase::CurrentTaskQueueSetter::CurrentTaskQueueSetter(TaskQueueBase *task_queue) + : previous_(current_task_queue) { + current_task_queue = task_queue; +} + +TaskQueueBase::CurrentTaskQueueSetter::~CurrentTaskQueueSetter() { + current_task_queue = previous_; +} + +} // namespace tqcq diff --git a/src/base/task_queue_base.h b/src/base/task_queue_base.h new file mode 100644 index 0000000..734f7f7 --- /dev/null +++ b/src/base/task_queue_base.h @@ -0,0 +1,91 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_BASE_TASK_QUEUE_BASE_H_ +#define ULIB_SRC_BASE_TASK_QUEUE_BASE_H_ + +#include +#include "location.h" +#include "units/time_delta.h" + +namespace tqcq { + +class TaskQueueBase { + public: + enum class DelayPrecision { + kLow, + kHigh, + }; + + // 销毁队列 + virtual void Delete() = 0; + void PostTask(std::function task, + const Location &location = Location::Current()) { + PostTaskImpl(std::move(task), {}, location); + }; + + void PostDelayedTask(std::function task, + TimeDelta delay, + const Location &location = Location::Current()) { + PostDelayedTaskImpl(std::move(task), delay, {}, location); + }; + void PostDelayedHighPrecisionTask( + std::function task, + TimeDelta delay, + const Location &location = Location::Current()) { + PostDelayedTaskTraits traits; + traits.high_precision = true; + PostDelayedTaskImpl(std::move(task), delay, traits, location); + } + + void PostDelayedTaskWithPrecision( + DelayPrecision precision, + std::function task, + TimeDelta delay, + const Location &location = Location::Current()) { + switch (precision) { + case DelayPrecision::kLow:PostDelayedTask(std::move(task), delay, location); + break; + case DelayPrecision::kHigh:PostDelayedHighPrecisionTask(std::move(task), delay, location); + break; + } + }; + + static TaskQueueBase *Current(); + bool IsCurrent() const { return Current() == this; } + + protected: + struct PostTaskTraits {}; + struct PostDelayedTaskTraits { + bool high_precision = false; + }; + + virtual void PostTaskImpl(std::function task, + const PostTaskTraits &traits, + const Location &location) = 0; + virtual void PostDelayedTaskImpl(std::function task, + TimeDelta delay, + const PostDelayedTaskTraits &traits, + const Location &location) = 0; + + class CurrentTaskQueueSetter { + public: + explicit CurrentTaskQueueSetter(TaskQueueBase *task_queue); + CurrentTaskQueueSetter(const CurrentTaskQueueSetter &) = delete; + CurrentTaskQueueSetter &operator=(const CurrentTaskQueueSetter &) = delete; + ~CurrentTaskQueueSetter(); + private: + TaskQueueBase *const previous_; + }; +}; + +struct TaskQueueDeleter { + void operator()(TaskQueueBase *task_queue) const { + task_queue->Delete(); + } +}; + +} // namespace tqcq + +#endif //ULIB_SRC_BASE_TASK_QUEUE_BASE_H_ diff --git a/src/network/socket.cpp b/src/network/socket.cpp new file mode 100644 index 0000000..b49f22a --- /dev/null +++ b/src/network/socket.cpp @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "socket.h" + +namespace tqcq { +} // namespace tqcq diff --git a/src/network/socket.h b/src/network/socket.h new file mode 100644 index 0000000..3cff295 --- /dev/null +++ b/src/network/socket.h @@ -0,0 +1,57 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_NETWORK_SOCKET_H_ +#define ULIB_SRC_NETWORK_SOCKET_H_ + +#include +#include +#include + +#include "network/socket_address.h" +#include "3party/sigslot/sigslot.h" + +namespace tqcq { + +inline bool IsBlockingError(int e) { + return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS); +} + +class Socket { + public: + virtual ~Socket() = default; + Socket(const Socket &) = delete; + Socket &operator=(const Socket &) = delete; + + virtual SocketAddress GetLocalAddress() const = 0; + virtual SocketAddress GetRemoteAddress() const = 0; + + virtual int Bind(const SocketAddress &addr) = 0; + virtual int Connect(const SocketAddress &addr) = 0; + virtual int Send(const void *pv, size_t cb) = 0; + virtual int SendTo(const void *pv, size_t cb, const SocketAddress &addr) = 0; + virtual int Recv(void *pv, size_t cb, int64_t *timestamp) = 0; + virtual int RecvFrom(void *pv, + size_t cb, + SocketAddress *paddr, + int64_t *timestamp) = 0; + virtual int Listen(int backlog) = 0; + virtual Socket *Accept(SocketAddress *paddr) = 0; + virtual int Close() = 0; + virtual int GetError() const = 0; + virtual void SetError(int error) = 0; + inline bool IsBlocking() const { return IsBlockingError(GetError()); } + + enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED }; + virtual ConnState GetState() const = 0; + + sigslot::signal1 SignalReadEvent; + sigslot::signal1 SignalWriteEvent; + sigslot::signal1 SignalConnectEvent; + sigslot::signal2 SignalCloseEvent; +}; + +} // namespace tqcq + +#endif //ULIB_SRC_NETWORK_SOCKET_H_ diff --git a/src/network/socket_address.cpp b/src/network/socket_address.cpp new file mode 100644 index 0000000..0dd2331 --- /dev/null +++ b/src/network/socket_address.cpp @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "socket_address.h" + +namespace tqcq { +} // namespace tqcq diff --git a/src/network/socket_address.h b/src/network/socket_address.h new file mode 100644 index 0000000..625fcbe --- /dev/null +++ b/src/network/socket_address.h @@ -0,0 +1,15 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_NETWORK_SOCKET_ADDRESS_H_ +#define ULIB_SRC_NETWORK_SOCKET_ADDRESS_H_ +namespace tqcq { + +class SocketAddress { + +}; + +} // namespace tqcq + +#endif //ULIB_SRC_NETWORK_SOCKET_ADDRESS_H_ diff --git a/src/network/socket_factory.cpp b/src/network/socket_factory.cpp new file mode 100644 index 0000000..359969a --- /dev/null +++ b/src/network/socket_factory.cpp @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "socket_factory.h" + +namespace tqcq { +} // namespace tqcq diff --git a/src/network/socket_factory.h b/src/network/socket_factory.h new file mode 100644 index 0000000..07592dc --- /dev/null +++ b/src/network/socket_factory.h @@ -0,0 +1,20 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_NETWORK_SOCKET_FACTORY_H_ +#define ULIB_SRC_NETWORK_SOCKET_FACTORY_H_ + +#include "socket.h" + +namespace tqcq { + +class SocketFactory { + public: + virtual ~SocketFactory() = default; + virtual Socket* CreateSocket(int family, int type) = 0; +}; + +} // namespace tqcq + +#endif //ULIB_SRC_NETWORK_SOCKET_FACTORY_H_ diff --git a/src/network/socket_server.cpp b/src/network/socket_server.cpp new file mode 100644 index 0000000..572e5ed --- /dev/null +++ b/src/network/socket_server.cpp @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "socket_server.h" + +namespace tqcq { +} // namespace tqcq diff --git a/src/network/socket_server.h b/src/network/socket_server.h new file mode 100644 index 0000000..64f413b --- /dev/null +++ b/src/network/socket_server.h @@ -0,0 +1,31 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_NETWORK_SOCKET_SERVER_H_ +#define ULIB_SRC_NETWORK_SOCKET_SERVER_H_ + +#include "network/socket_factory.h" +#include "units/time_delta.h" +#include "synchronization/event.h" + +namespace tqcq { + +class Thread; + +class SocketServer : public SocketFactory { + public: + static const TimeDelta kForever; // = Event::kForever; + + static std::unique_ptr CreateDefault(); + + virtual void SetMessageQueue(Thread* queue) {} + + virtual bool Wait(TimeDelta max_wait_duration, bool process_io) = 0; + + virtual void WakeUp() = 0; +}; + +} // namespace tqcq + +#endif //ULIB_SRC_NETWORK_SOCKET_SERVER_H_ diff --git a/src/strings/string_printf.cpp b/src/strings/string_printf.cpp new file mode 100644 index 0000000..14c10cf --- /dev/null +++ b/src/strings/string_printf.cpp @@ -0,0 +1,80 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "string_printf.h" +#include +#include +#include "base/scoped_clear_last_error.h" + +namespace tqcq { +std::string StringPrintf(const char *format, ...) { + va_list ap; + va_start(ap, format); + std::string result; + StringAppendV(&result, format, ap); + va_end(ap); + + return result; +} + +void StringAppendF(std::string *dst, const char *format, ...) { + va_list ap; + va_start(ap, format); + StringAppendV(dst, format, ap); + va_end(ap); +} + +template +void StringAppendV(std::basic_string *dst, const char *format, va_list ap) { + // 32MB stack buffer, if the string is larger than this, failed + const size_t kMaxBufferSize = 32 * 1024 * 1024; + CharT stack_buf[1024]; + + va_list ap_copy; + va_copy(ap_copy, ap); + + ScopedClearLastError last_error; + int result = vsnprintf(stack_buf, sizeof(stack_buf), format, ap_copy); + va_end(ap_copy); + + if (result >= 0 && static_cast(result) < sizeof(stack_buf)) { + dst->append(stack_buf, static_cast(result)); + return; + } + + size_t mem_length = sizeof(stack_buf); + while (true) { + if (result < 0) { + if (errno != 0 && errno != EOVERFLOW) { + return; + } + + mem_length *= 2; + } else { + // increase 1 characters + mem_length = static_cast(result) + 1; + } + + if (mem_length > kMaxBufferSize) { + // log + return; + } + + std::vector mem_buf(mem_length); + va_copy(ap_copy, ap); + result = vsnprintf(mem_buf.data(), mem_length, format, ap_copy); + va_end(ap_copy); + + if (result >= 0 && static_cast(result) < mem_length) { + dst->append(mem_buf.data(), static_cast(result)); + return; + } + } +} + +void StringAppendV(std::string *dst, const char *format, va_list ap) { + StringAppendV(dst, format, ap); +} + +} // namespace tqcq diff --git a/src/strings/string_printf.h b/src/strings/string_printf.h new file mode 100644 index 0000000..acb877f --- /dev/null +++ b/src/strings/string_printf.h @@ -0,0 +1,18 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_STRINGS_STRING_PRINTF_H_ +#define ULIB_SRC_STRINGS_STRING_PRINTF_H_ +#include +#include + +namespace tqcq { + +std::string StringPrintf(const char* format, ...); +void StringAppendF(std::string* dst, const char* format, ...); +void StringAppendV(std::string* dst, const char* format, va_list ap); + +} // namespace tqcq + +#endif //ULIB_SRC_STRINGS_STRING_PRINTF_H_ diff --git a/src/synchronization/event.cpp b/src/synchronization/event.cpp new file mode 100644 index 0000000..2b17287 --- /dev/null +++ b/src/synchronization/event.cpp @@ -0,0 +1,80 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "event.h" +#include +#include + +namespace tqcq { + +const TimeDelta Event::kForever = TimeDelta::PlusInfinity(); + +Event::Event() : Event(false, false) {} + +Event::Event(bool manua_reset, bool initially_signaled) + : is_manual_reset_(manua_reset), event_status_(initially_signaled) { + pthread_mutex_init(&mutex_, nullptr); + pthread_cond_init(&cond_, nullptr); +} + +Event::~Event() { + pthread_mutex_destroy(&mutex_); + pthread_cond_destroy(&cond_); +} + +void Event::Set() { + pthread_mutex_lock(&mutex_); + event_status_ = true; + pthread_cond_broadcast(&cond_); + pthread_mutex_unlock(&mutex_); +} + +void Event::Reset() { + pthread_mutex_lock(&mutex_); + event_status_ = false; + pthread_mutex_unlock(&mutex_); +} + +namespace { +timespec GetTimespec(TimeDelta duration_from_now) { + timespec ts; + // use clock ? + if (false) { + // clock_gettime(CLOCK_MONOTONIC, &ts); + } else { + timeval tv; + gettimeofday(&tv, nullptr); + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = tv.tv_usec * 1000; + } + + int64_t microsecs_from_now = duration_from_now.us(); + ts.tv_sec += microsecs_from_now / 1000000; + ts.tv_nsec += (microsecs_from_now % 1000000) * 1000; + + if (ts.tv_nsec >= 1000000000) { + ts.tv_sec++; + ts.tv_nsec -= 1000000000; + } + + return ts; +} +} + +bool Event::Wait(TimeDelta give_up_after, TimeDelta warn_after) { + timespec ts; + + int error = 0; + pthread_mutex_lock(&mutex_); + if (give_up_after.IsPlusInfinity()) { + error = pthread_cond_wait(&cond_, &mutex_); + } else { + ts = GetTimespec(give_up_after); + error = pthread_cond_timedwait(&cond_, &mutex_, &ts); + } + + return error; +} + +} // namespace tqcq diff --git a/src/synchronization/event.h b/src/synchronization/event.h new file mode 100644 index 0000000..0653d89 --- /dev/null +++ b/src/synchronization/event.h @@ -0,0 +1,41 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_SYNCHRONIZATION_EVENT_H_ +#define ULIB_SRC_SYNCHRONIZATION_EVENT_H_ + +#include "units/time_delta.h" +#include + +namespace tqcq { + +class Event { + public: + static const TimeDelta kForever; // = TimeDelta::PlusInfinity(); + Event(); + Event(bool manua__reset, bool initially_signaled); + Event(const Event &) = delete; + ~Event(); + + void Set(); + void Reset(); + + bool Wait(TimeDelta give_up_after, TimeDelta warn_after); + bool Wait(TimeDelta give_up_after) { + // 如果是无限等待,那么超过3秒就可能是死锁了 + // 如果使用者有明确的等待时间,那么不用警告 + return Wait(give_up_after, give_up_after.IsPlusInfinity() + ? TimeDelta::Seconds(3) + : kForever); + } + private: + const bool is_manual_reset_; + bool event_status_; + pthread_mutex_t mutex_; + pthread_cond_t cond_; +}; + +} // namespace tqcq + +#endif //ULIB_SRC_SYNCHRONIZATION_EVENT_H_ diff --git a/src/synchronization/mutex.cpp b/src/synchronization/mutex.cpp new file mode 100644 index 0000000..48f5cbf --- /dev/null +++ b/src/synchronization/mutex.cpp @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "mutex.h" + +namespace tqcq { +} // namespace tqcq diff --git a/src/synchronization/mutex.h b/src/synchronization/mutex.h new file mode 100644 index 0000000..74a3037 --- /dev/null +++ b/src/synchronization/mutex.h @@ -0,0 +1,39 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_SYNCHRONIZATION_MUTEX_H_ +#define ULIB_SRC_SYNCHRONIZATION_MUTEX_H_ + +#include + +namespace tqcq { + +class Mutex final { + public: + Mutex() = default; + Mutex(const Mutex &) = delete; + Mutex &operator=(const Mutex &) = delete; + + void Lock() { mutex_.lock(); } + bool TryLock() { return mutex_.try_lock(); } + void Unlock() { mutex_.unlock(); } + void AssertHeld() const {}; + private: + std::mutex mutex_; +}; + +class MutexLock final { + public: + MutexLock(const MutexLock &) = delete; + MutexLock &operator=(const MutexLock &) = delete; + + explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); } + ~MutexLock() { mu_->Unlock(); } + private: + Mutex *const mu_; +}; + +} // namespace tqcq + +#endif //ULIB_SRC_SYNCHRONIZATION_MUTEX_H_ diff --git a/src/system/export_api.h b/src/system/export_api.h new file mode 100644 index 0000000..99c2a11 --- /dev/null +++ b/src/system/export_api.h @@ -0,0 +1,23 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_SYSTEM_EXPORTAPI_H_ +#define ULIB_SRC_SYSTEM_EXPORTAPI_H_ + +#ifdef _WIN32 +#ifdef ULIB_LIBRARY_IMPL +#define ULIB_EXPORT __declspec(dllexport) +#else +#define ULIB_EXPORT __declspec(dllimport) +#endif + +#else // _WIN32 + +#if __has_attribute(visibility) && defined(ULIB_LIBRARY_IMPL) +#define ULIB_EXPORT __attribute__((visibility("default"))) +#endif + +#endif // _WIN32 + +#endif //ULIB_SRC_SYSTEM_EXPORTAPI_H_ diff --git a/src/system/system_time.cpp b/src/system/system_time.cpp new file mode 100644 index 0000000..d24cf7e --- /dev/null +++ b/src/system/system_time.cpp @@ -0,0 +1,16 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "system_time.h" +#include + +namespace tqcq { +//TODO 后续使用系统相关API获取时间 +int64_t SystemTimeNanos() { + struct timeval tv; + gettimeofday(&tv, nullptr); + return tv.tv_sec * 1000000000LL + tv.tv_usec * 1000LL; +} + +} // namespace tqcq diff --git a/src/system/system_time.h b/src/system/system_time.h new file mode 100644 index 0000000..b975159 --- /dev/null +++ b/src/system/system_time.h @@ -0,0 +1,14 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_SYSTEM_SYSTEM_TIME_H_ +#define ULIB_SRC_SYSTEM_SYSTEM_TIME_H_ +#include +namespace tqcq { + +int64_t SystemTimeNanos(); + +} // namespace tqcq + +#endif //ULIB_SRC_SYSTEM_SYSTEM_TIME_H_ diff --git a/src/types/optional.h b/src/types/optional.h new file mode 100644 index 0000000..233a889 --- /dev/null +++ b/src/types/optional.h @@ -0,0 +1,2091 @@ + +/// +// optional - An implementation of std::optional with extensions +// Written in 2017 by Sy Brand (tartanllama@gmail.com, @TartanLlama) +// +// Documentation available at https://tl.tartanllama.xyz/ +// +// To the extent possible under law, the author(s) have dedicated all +// copyright and related and neighboring rights to this software to the +// public domain worldwide. This software is distributed without any warranty. +// +// You should have received a copy of the CC0 Public Domain Dedication +// along with this software. If not, see +// . +/// + +#ifndef TL_OPTIONAL_HPP +#define TL_OPTIONAL_HPP + +#define TL_OPTIONAL_VERSION_MAJOR 1 +#define TL_OPTIONAL_VERSION_MINOR 1 +#define TL_OPTIONAL_VERSION_PATCH 0 + +#include +#include +#include +#include +#include +#include + +#if (defined(_MSC_VER) && _MSC_VER == 1900) +#define TL_OPTIONAL_MSVC2015 +#endif + +#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ + !defined(__clang__)) +#define TL_OPTIONAL_GCC49 +#endif + +#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && \ + !defined(__clang__)) +#define TL_OPTIONAL_GCC54 +#endif + +#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && \ + !defined(__clang__)) +#define TL_OPTIONAL_GCC55 +#endif + +#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ + !defined(__clang__)) +// GCC < 5 doesn't support overloading on const&& for member functions +#define TL_OPTIONAL_NO_CONSTRR + +// GCC < 5 doesn't support some standard C++11 type traits +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ + std::has_trivial_copy_constructor::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::has_trivial_copy_assign::value + +// This one will be different for GCC 5.7 if it's ever supported +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value + +// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector +// for non-copyable types +#elif (defined(__GNUC__) && __GNUC__ < 8 && \ + !defined(__clang__)) +#ifndef TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX +#define TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX +namespace tl { +namespace detail { +template +struct is_trivially_copy_constructible : std::is_trivially_copy_constructible{}; +#ifdef _GLIBCXX_VECTOR +template +struct is_trivially_copy_constructible> + : std::is_trivially_copy_constructible{}; +#endif +} +} +#endif + +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ + tl::detail::is_trivially_copy_constructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ + std::is_trivially_copy_assignable::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value +#else +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ + std::is_trivially_copy_constructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ + std::is_trivially_copy_assignable::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value +#endif + +#if __cplusplus > 201103L +#define TL_OPTIONAL_CXX14 +#endif + +// constexpr implies const in C++11, not C++14 +#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || \ + defined(TL_OPTIONAL_GCC49)) +#define TL_OPTIONAL_11_CONSTEXPR +#else +#define TL_OPTIONAL_11_CONSTEXPR constexpr +#endif + +namespace tl { +#ifndef TL_MONOSTATE_INPLACE_MUTEX +#define TL_MONOSTATE_INPLACE_MUTEX +/// Used to represent an optional with no data; essentially a bool +class monostate {}; + +/// A tag type to tell optional to construct its value in-place +struct in_place_t { + explicit in_place_t() = default; +}; +/// A tag to tell optional to construct its value in-place +static constexpr in_place_t in_place{}; +#endif + +template class optional; + +namespace detail { +#ifndef TL_TRAITS_MUTEX +#define TL_TRAITS_MUTEX +// C++14-style aliases for brevity +template using remove_const_t = typename std::remove_const::type; +template +using remove_reference_t = typename std::remove_reference::type; +template using decay_t = typename std::decay::type; +template +using enable_if_t = typename std::enable_if::type; +template +using conditional_t = typename std::conditional::type; + +// std::conjunction from C++17 +template struct conjunction : std::true_type {}; +template struct conjunction : B {}; +template +struct conjunction + : std::conditional, B>::type {}; + +#if defined(_LIBCPP_VERSION) && __cplusplus == 201103L +#define TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND +#endif + +// In C++11 mode, there's an issue in libc++'s std::mem_fn +// which results in a hard-error when using it in a noexcept expression +// in some cases. This is a check to workaround the common failing case. +#ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND +template struct is_pointer_to_non_const_member_func : std::false_type{}; +template +struct is_pointer_to_non_const_member_func : std::true_type{}; +template +struct is_pointer_to_non_const_member_func : std::true_type{}; +template +struct is_pointer_to_non_const_member_func : std::true_type{}; +template +struct is_pointer_to_non_const_member_func : std::true_type{}; +template +struct is_pointer_to_non_const_member_func : std::true_type{}; +template +struct is_pointer_to_non_const_member_func : std::true_type{}; + +template struct is_const_or_const_ref : std::false_type{}; +template struct is_const_or_const_ref : std::true_type{}; +template struct is_const_or_const_ref : std::true_type{}; +#endif + +// std::invoke from C++17 +// https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround +template ::value + && is_const_or_const_ref::value)>, +#endif + typename = enable_if_t>::value>, + int = 0> +constexpr auto invoke(Fn &&f, Args &&... args) noexcept( + noexcept(std::mem_fn(f)(std::forward(args)...))) + -> decltype(std::mem_fn(f)(std::forward(args)...)) { + return std::mem_fn(f)(std::forward(args)...); +} + +template >::value>> +constexpr auto invoke(Fn &&f, Args &&... args) noexcept( + noexcept(std::forward(f)(std::forward(args)...))) + -> decltype(std::forward(f)(std::forward(args)...)) { + return std::forward(f)(std::forward(args)...); +} + +// std::invoke_result from C++17 +template struct invoke_result_impl; + +template +struct invoke_result_impl< + F, decltype(detail::invoke(std::declval(), std::declval()...), void()), + Us...> { + using type = decltype(detail::invoke(std::declval(), std::declval()...)); +}; + +template +using invoke_result = invoke_result_impl; + +template +using invoke_result_t = typename invoke_result::type; + +#if defined(_MSC_VER) && _MSC_VER <= 1900 +// TODO make a version which works with MSVC 2015 +template struct is_swappable : std::true_type {}; + +template struct is_nothrow_swappable : std::true_type {}; +#else +// https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept +namespace swap_adl_tests { +// if swap ADL finds this then it would call std::swap otherwise (same +// signature) +struct tag {}; + +template tag swap(T &, T &); +template tag swap(T (&a)[N], T (&b)[N]); + +// helper functions to test if an unqualified swap is possible, and if it +// becomes std::swap +template std::false_type can_swap(...) noexcept(false); +template (), std::declval()))> +std::true_type can_swap(int) noexcept(noexcept(swap(std::declval(), + std::declval()))); + +template std::false_type uses_std(...); +template +std::is_same(), std::declval())), tag> +uses_std(int); + +template +struct is_std_swap_noexcept + : std::integral_constant::value && + std::is_nothrow_move_assignable::value> {}; + +template +struct is_std_swap_noexcept : is_std_swap_noexcept {}; + +template +struct is_adl_swap_noexcept + : std::integral_constant(0))> {}; +} // namespace swap_adl_tests + +template +struct is_swappable + : std::integral_constant< + bool, + decltype(detail::swap_adl_tests::can_swap(0))::value && + (!decltype(detail::swap_adl_tests::uses_std(0))::value || + (std::is_move_assignable::value && + std::is_move_constructible::value))> {}; + +template +struct is_swappable + : std::integral_constant< + bool, + decltype(detail::swap_adl_tests::can_swap(0))::value && + (!decltype( + detail::swap_adl_tests::uses_std(0))::value || + is_swappable::value)> {}; + +template +struct is_nothrow_swappable + : std::integral_constant< + bool, + is_swappable::value && + ((decltype(detail::swap_adl_tests::uses_std(0))::value + &&detail::swap_adl_tests::is_std_swap_noexcept::value) || + (!decltype(detail::swap_adl_tests::uses_std(0))::value && + detail::swap_adl_tests::is_adl_swap_noexcept::value))> { +}; +#endif +#endif + +// std::void_t from C++17 +template struct voider { using type = void; }; +template using void_t = typename voider::type; + +// Trait for checking if a type is a tl::optional +template struct is_optional_impl : std::false_type {}; +template struct is_optional_impl> : std::true_type {}; +template using is_optional = is_optional_impl>; + +// Change void to tl::monostate +template +using fixup_void = conditional_t::value, monostate, U>; + +template > +using get_map_return = optional>>; + +// Check if invoking F for some Us returns void +template struct returns_void_impl; +template +struct returns_void_impl>, U...> + : std::is_void> {}; +template +using returns_void = returns_void_impl; + +template +using enable_if_ret_void = enable_if_t::value>; + +template +using disable_if_ret_void = enable_if_t::value>; + +template +using enable_forward_value = + detail::enable_if_t::value && + !std::is_same, in_place_t>::value && + !std::is_same, detail::decay_t>::value>; + +template +using enable_from_other = detail::enable_if_t< + std::is_constructible::value && + !std::is_constructible &>::value && + !std::is_constructible &&>::value && + !std::is_constructible &>::value && + !std::is_constructible &&>::value && + !std::is_convertible &, T>::value && + !std::is_convertible &&, T>::value && + !std::is_convertible &, T>::value && + !std::is_convertible &&, T>::value>; + +template +using enable_assign_forward = detail::enable_if_t< + !std::is_same, detail::decay_t>::value && + !detail::conjunction, + std::is_same>>::value && + std::is_constructible::value && std::is_assignable::value>; + +template +using enable_assign_from_other = detail::enable_if_t< + std::is_constructible::value && + std::is_assignable::value && + !std::is_constructible &>::value && + !std::is_constructible &&>::value && + !std::is_constructible &>::value && + !std::is_constructible &&>::value && + !std::is_convertible &, T>::value && + !std::is_convertible &&, T>::value && + !std::is_convertible &, T>::value && + !std::is_convertible &&, T>::value && + !std::is_assignable &>::value && + !std::is_assignable &&>::value && + !std::is_assignable &>::value && + !std::is_assignable &&>::value>; + +// The storage base manages the actual storage, and correctly propagates +// trivial destruction from T. This case is for when T is not trivially +// destructible. +template ::value> +struct optional_storage_base { + TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept + : m_dummy(), m_has_value(false) {} + + template + TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) + : m_value(std::forward(u)...), m_has_value(true) {} + + ~optional_storage_base() { + if (m_has_value) { + m_value.~T(); + m_has_value = false; + } + } + + struct dummy {}; + union { + dummy m_dummy; + T m_value; + }; + + bool m_has_value; +}; + +// This case is for when T is trivially destructible. +template struct optional_storage_base { + TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept + : m_dummy(), m_has_value(false) {} + + template + TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) + : m_value(std::forward(u)...), m_has_value(true) {} + + // No destructor, so this class is trivially destructible + + struct dummy {}; + union { + dummy m_dummy; + T m_value; + }; + + bool m_has_value = false; +}; + +// This base class provides some handy member functions which can be used in +// further derived classes +template struct optional_operations_base : optional_storage_base { + using optional_storage_base::optional_storage_base; + + void hard_reset() noexcept { + get().~T(); + this->m_has_value = false; + } + + template void construct(Args &&... args) { + new (std::addressof(this->m_value)) T(std::forward(args)...); + this->m_has_value = true; + } + + template void assign(Opt &&rhs) { + if (this->has_value()) { + if (rhs.has_value()) { + this->m_value = std::forward(rhs).get(); + } else { + this->m_value.~T(); + this->m_has_value = false; + } + } + + else if (rhs.has_value()) { + construct(std::forward(rhs).get()); + } + } + + bool has_value() const { return this->m_has_value; } + + TL_OPTIONAL_11_CONSTEXPR T &get() & { return this->m_value; } + TL_OPTIONAL_11_CONSTEXPR const T &get() const & { return this->m_value; } + TL_OPTIONAL_11_CONSTEXPR T &&get() && { return std::move(this->m_value); } +#ifndef TL_OPTIONAL_NO_CONSTRR + constexpr const T &&get() const && { return std::move(this->m_value); } +#endif +}; + +// This class manages conditionally having a trivial copy constructor +// This specialization is for when T is trivially copy constructible +template +struct optional_copy_base : optional_operations_base { + using optional_operations_base::optional_operations_base; +}; + +// This specialization is for when T is not trivially copy constructible +template +struct optional_copy_base : optional_operations_base { + using optional_operations_base::optional_operations_base; + + optional_copy_base() = default; + optional_copy_base(const optional_copy_base &rhs) + : optional_operations_base() { + if (rhs.has_value()) { + this->construct(rhs.get()); + } else { + this->m_has_value = false; + } + } + + optional_copy_base(optional_copy_base &&rhs) = default; + optional_copy_base &operator=(const optional_copy_base &rhs) = default; + optional_copy_base &operator=(optional_copy_base &&rhs) = default; +}; + +// This class manages conditionally having a trivial move constructor +// Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it +// doesn't implement an analogue to std::is_trivially_move_constructible. We +// have to make do with a non-trivial move constructor even if T is trivially +// move constructible +#ifndef TL_OPTIONAL_GCC49 +template ::value> +struct optional_move_base : optional_copy_base { + using optional_copy_base::optional_copy_base; +}; +#else +template struct optional_move_base; +#endif +template struct optional_move_base : optional_copy_base { + using optional_copy_base::optional_copy_base; + + optional_move_base() = default; + optional_move_base(const optional_move_base &rhs) = default; + + optional_move_base(optional_move_base &&rhs) noexcept( + std::is_nothrow_move_constructible::value) { + if (rhs.has_value()) { + this->construct(std::move(rhs.get())); + } else { + this->m_has_value = false; + } + } + optional_move_base &operator=(const optional_move_base &rhs) = default; + optional_move_base &operator=(optional_move_base &&rhs) = default; +}; + +// This class manages conditionally having a trivial copy assignment operator +template +struct optional_copy_assign_base : optional_move_base { + using optional_move_base::optional_move_base; +}; + +template +struct optional_copy_assign_base : optional_move_base { + using optional_move_base::optional_move_base; + + optional_copy_assign_base() = default; + optional_copy_assign_base(const optional_copy_assign_base &rhs) = default; + + optional_copy_assign_base(optional_copy_assign_base &&rhs) = default; + optional_copy_assign_base &operator=(const optional_copy_assign_base &rhs) { + this->assign(rhs); + return *this; + } + optional_copy_assign_base & + operator=(optional_copy_assign_base &&rhs) = default; +}; + +// This class manages conditionally having a trivial move assignment operator +// Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it +// doesn't implement an analogue to std::is_trivially_move_assignable. We have +// to make do with a non-trivial move assignment operator even if T is trivially +// move assignable +#ifndef TL_OPTIONAL_GCC49 +template ::value + &&std::is_trivially_move_constructible::value + &&std::is_trivially_move_assignable::value> +struct optional_move_assign_base : optional_copy_assign_base { + using optional_copy_assign_base::optional_copy_assign_base; +}; +#else +template struct optional_move_assign_base; +#endif + +template +struct optional_move_assign_base : optional_copy_assign_base { + using optional_copy_assign_base::optional_copy_assign_base; + + optional_move_assign_base() = default; + optional_move_assign_base(const optional_move_assign_base &rhs) = default; + + optional_move_assign_base(optional_move_assign_base &&rhs) = default; + + optional_move_assign_base & + operator=(const optional_move_assign_base &rhs) = default; + + optional_move_assign_base & + operator=(optional_move_assign_base &&rhs) noexcept( + std::is_nothrow_move_constructible::value + &&std::is_nothrow_move_assignable::value) { + this->assign(std::move(rhs)); + return *this; + } +}; + +// optional_delete_ctor_base will conditionally delete copy and move +// constructors depending on whether T is copy/move constructible +template ::value, + bool EnableMove = std::is_move_constructible::value> +struct optional_delete_ctor_base { + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base & + operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base & + operator=(optional_delete_ctor_base &&) noexcept = default; +}; + +template struct optional_delete_ctor_base { + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; + optional_delete_ctor_base & + operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base & + operator=(optional_delete_ctor_base &&) noexcept = default; +}; + +template struct optional_delete_ctor_base { + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base & + operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base & + operator=(optional_delete_ctor_base &&) noexcept = default; +}; + +template struct optional_delete_ctor_base { + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; + optional_delete_ctor_base & + operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base & + operator=(optional_delete_ctor_base &&) noexcept = default; +}; + +// optional_delete_assign_base will conditionally delete copy and move +// constructors depending on whether T is copy/move constructible + assignable +template ::value && + std::is_copy_assignable::value), + bool EnableMove = (std::is_move_constructible::value && + std::is_move_assignable::value)> +struct optional_delete_assign_base { + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base & + operator=(const optional_delete_assign_base &) = default; + optional_delete_assign_base & + operator=(optional_delete_assign_base &&) noexcept = default; +}; + +template struct optional_delete_assign_base { + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base & + operator=(const optional_delete_assign_base &) = default; + optional_delete_assign_base & + operator=(optional_delete_assign_base &&) noexcept = delete; +}; + +template struct optional_delete_assign_base { + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base & + operator=(const optional_delete_assign_base &) = delete; + optional_delete_assign_base & + operator=(optional_delete_assign_base &&) noexcept = default; +}; + +template struct optional_delete_assign_base { + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base & + operator=(const optional_delete_assign_base &) = delete; + optional_delete_assign_base & + operator=(optional_delete_assign_base &&) noexcept = delete; +}; + +} // namespace detail + +/// A tag type to represent an empty optional +struct nullopt_t { + struct do_not_use {}; + constexpr explicit nullopt_t(do_not_use, do_not_use) noexcept {} +}; +/// Represents an empty optional +static constexpr nullopt_t nullopt{nullopt_t::do_not_use{}, + nullopt_t::do_not_use{}}; + +class bad_optional_access : public std::exception { +public: + bad_optional_access() = default; + const char *what() const noexcept { return "Optional has no value"; } +}; + +/// An optional object is an object that contains the storage for another +/// object and manages the lifetime of this contained object, if any. The +/// contained object may be initialized after the optional object has been +/// initialized, and may be destroyed before the optional object has been +/// destroyed. The initialization state of the contained object is tracked by +/// the optional object. +template +class optional : private detail::optional_move_assign_base, + private detail::optional_delete_ctor_base, + private detail::optional_delete_assign_base { + using base = detail::optional_move_assign_base; + + static_assert(!std::is_same::value, + "instantiation of optional with in_place_t is ill-formed"); + static_assert(!std::is_same, nullopt_t>::value, + "instantiation of optional with nullopt_t is ill-formed"); + +public: +// The different versions for C++14 and 11 are needed because deduced return +// types are not SFINAE-safe. This provides better support for things like +// generic lambdas. C.f. +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ + !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } + + template constexpr auto and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template constexpr auto and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } +#endif +#else + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); + } + + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } + + template + constexpr detail::invoke_result_t and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + constexpr detail::invoke_result_t and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } +#endif +#endif + +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ + !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) + /// Carries out some operation on the stored object if there is one. + template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template constexpr auto map(F &&f) const & { + return optional_map_impl(*this, std::forward(f)); + } + + template constexpr auto map(F &&f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } +#else + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), + std::declval())) + map(F &&f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), + std::declval())) + map(F &&f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + map(F &&f) const & { + return optional_map_impl(*this, std::forward(f)); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + map(F &&f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } +#endif +#endif + +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ + !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) + /// Carries out some operation on the stored object if there is one. + template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template constexpr auto transform(F&& f) const & { + return optional_map_impl(*this, std::forward(f)); + } + + template constexpr auto transform(F&& f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } +#else + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) const & { + return optional_map_impl(*this, std::forward(f)); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } +#endif +#endif + + /// Calls `f` if the optional is empty + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + if (has_value()) + return *this; + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + return has_value() ? *this : std::forward(f)(); + } + + template * = nullptr> + optional or_else(F &&f) && { + if (has_value()) + return std::move(*this); + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) && { + return has_value() ? std::move(*this) : std::forward(f)(); + } + + template * = nullptr> + optional or_else(F &&f) const & { + if (has_value()) + return *this; + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const & { + return has_value() ? *this : std::forward(f)(); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template * = nullptr> + optional or_else(F &&f) const && { + if (has_value()) + return std::move(*this); + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional or_else(F &&f) const && { + return has_value() ? std::move(*this) : std::forward(f)(); + } +#endif + + /// Maps the stored value with `f` if there is one, otherwise returns `u`. + template U map_or(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } + + template U map_or(F &&f, U &&u) && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } + + template U map_or(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template U map_or(F &&f, U &&u) const && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } +#endif + + /// Maps the stored value with `f` if there is one, otherwise calls + /// `u` and returns the result. + template + detail::invoke_result_t map_or_else(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } +#endif + + /// Returns `u` if `*this` has a value, otherwise an empty optional. + template + constexpr optional::type> conjunction(U &&u) const { + using result = optional>; + return has_value() ? result{u} : result{nullopt}; + } + + /// Returns `rhs` if `*this` is empty, otherwise the current value. + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { + return has_value() ? *this : rhs; + } + + constexpr optional disjunction(const optional &rhs) const & { + return has_value() ? *this : rhs; + } + + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { + return has_value() ? std::move(*this) : rhs; + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + constexpr optional disjunction(const optional &rhs) const && { + return has_value() ? std::move(*this) : rhs; + } +#endif + + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { + return has_value() ? *this : std::move(rhs); + } + + constexpr optional disjunction(optional &&rhs) const & { + return has_value() ? *this : std::move(rhs); + } + + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { + return has_value() ? std::move(*this) : std::move(rhs); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + constexpr optional disjunction(optional &&rhs) const && { + return has_value() ? std::move(*this) : std::move(rhs); + } +#endif + + /// Takes the value out of the optional, leaving it empty + optional take() { + optional ret = std::move(*this); + reset(); + return ret; + } + + using value_type = T; + + /// Constructs an optional that does not contain a value. + constexpr optional() noexcept = default; + + constexpr optional(nullopt_t) noexcept {} + + /// Copy constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) = default; + + /// Move constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default; + + /// Constructs the stored value in-place using the given arguments. + template + constexpr explicit optional( + detail::enable_if_t::value, in_place_t>, + Args &&... args) + : base(in_place, std::forward(args)...) {} + + template + TL_OPTIONAL_11_CONSTEXPR explicit optional( + detail::enable_if_t &, + Args &&...>::value, + in_place_t>, + std::initializer_list il, Args &&... args) { + this->construct(il, std::forward(args)...); + } + + /// Constructs the stored value with `u`. + template < + class U = T, + detail::enable_if_t::value> * = nullptr, + detail::enable_forward_value * = nullptr> + constexpr optional(U &&u) : base(in_place, std::forward(u)) {} + + template < + class U = T, + detail::enable_if_t::value> * = nullptr, + detail::enable_forward_value * = nullptr> + constexpr explicit optional(U &&u) : base(in_place, std::forward(u)) {} + + /// Converting copy constructor. + template < + class U, detail::enable_from_other * = nullptr, + detail::enable_if_t::value> * = nullptr> + optional(const optional &rhs) { + if (rhs.has_value()) { + this->construct(*rhs); + } + } + + template * = nullptr, + detail::enable_if_t::value> * = + nullptr> + explicit optional(const optional &rhs) { + if (rhs.has_value()) { + this->construct(*rhs); + } + } + + /// Converting move constructor. + template < + class U, detail::enable_from_other * = nullptr, + detail::enable_if_t::value> * = nullptr> + optional(optional &&rhs) { + if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } + } + + template < + class U, detail::enable_from_other * = nullptr, + detail::enable_if_t::value> * = nullptr> + explicit optional(optional &&rhs) { + if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } + } + + /// Destroys the stored value if there is one. + ~optional() = default; + + /// Assignment to empty. + /// + /// Destroys the current value if there is one. + optional &operator=(nullopt_t) noexcept { + if (has_value()) { + this->m_value.~T(); + this->m_has_value = false; + } + + return *this; + } + + /// Copy assignment. + /// + /// Copies the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + optional &operator=(const optional &rhs) = default; + + /// Move assignment. + /// + /// Moves the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + optional &operator=(optional &&rhs) = default; + + /// Assigns the stored value from `u`, destroying the old value if there was + /// one. + template * = nullptr> + optional &operator=(U &&u) { + if (has_value()) { + this->m_value = std::forward(u); + } else { + this->construct(std::forward(u)); + } + + return *this; + } + + /// Converting copy assignment operator. + /// + /// Copies the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + template * = nullptr> + optional &operator=(const optional &rhs) { + if (has_value()) { + if (rhs.has_value()) { + this->m_value = *rhs; + } else { + this->hard_reset(); + } + } + + else if (rhs.has_value()) { + this->construct(*rhs); + } + + return *this; + } + + // TODO check exception guarantee + /// Converting move assignment operator. + /// + /// Moves the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + template * = nullptr> + optional &operator=(optional &&rhs) { + if (has_value()) { + if (rhs.has_value()) { + this->m_value = std::move(*rhs); + } else { + this->hard_reset(); + } + } + + else if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } + + return *this; + } + + /// Constructs the value in-place, destroying the current one if there is + /// one. + template T &emplace(Args &&... args) { + static_assert(std::is_constructible::value, + "T must be constructible with Args"); + + *this = nullopt; + this->construct(std::forward(args)...); + return value(); + } + + template + detail::enable_if_t< + std::is_constructible &, Args &&...>::value, + T &> + emplace(std::initializer_list il, Args &&... args) { + *this = nullopt; + this->construct(il, std::forward(args)...); + return value(); + } + + /// Swaps this optional with the other. + /// + /// If neither optionals have a value, nothing happens. + /// If both have a value, the values are swapped. + /// If one has a value, it is moved to the other and the movee is left + /// valueless. + void + swap(optional &rhs) noexcept(std::is_nothrow_move_constructible::value + &&detail::is_nothrow_swappable::value) { + using std::swap; + if (has_value()) { + if (rhs.has_value()) { + swap(**this, *rhs); + } else { + new (std::addressof(rhs.m_value)) T(std::move(this->m_value)); + this->m_value.T::~T(); + } + } else if (rhs.has_value()) { + new (std::addressof(this->m_value)) T(std::move(rhs.m_value)); + rhs.m_value.T::~T(); + } + swap(this->m_has_value, rhs.m_has_value); + } + + /// Returns a pointer to the stored value + constexpr const T *operator->() const { + return std::addressof(this->m_value); + } + + TL_OPTIONAL_11_CONSTEXPR T *operator->() { + return std::addressof(this->m_value); + } + + /// Returns the stored value + TL_OPTIONAL_11_CONSTEXPR T &operator*() & { return this->m_value; } + + constexpr const T &operator*() const & { return this->m_value; } + + TL_OPTIONAL_11_CONSTEXPR T &&operator*() && { + return std::move(this->m_value); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + constexpr const T &&operator*() const && { return std::move(this->m_value); } +#endif + + /// Returns whether or not the optional has a value + constexpr bool has_value() const noexcept { return this->m_has_value; } + + constexpr explicit operator bool() const noexcept { + return this->m_has_value; + } + + /// Returns the contained value if there is one, otherwise throws bad_optional_access + TL_OPTIONAL_11_CONSTEXPR T &value() & { + if (has_value()) + return this->m_value; + throw bad_optional_access(); + } + TL_OPTIONAL_11_CONSTEXPR const T &value() const & { + if (has_value()) + return this->m_value; + throw bad_optional_access(); + } + TL_OPTIONAL_11_CONSTEXPR T &&value() && { + if (has_value()) + return std::move(this->m_value); + throw bad_optional_access(); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + TL_OPTIONAL_11_CONSTEXPR const T &&value() const && { + if (has_value()) + return std::move(this->m_value); + throw bad_optional_access(); + } +#endif + + /// Returns the stored value if there is one, otherwise returns `u` + template constexpr T value_or(U &&u) const & { + static_assert(std::is_copy_constructible::value && + std::is_convertible::value, + "T must be copy constructible and convertible from U"); + return has_value() ? **this : static_cast(std::forward(u)); + } + + template TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && { + static_assert(std::is_move_constructible::value && + std::is_convertible::value, + "T must be move constructible and convertible from U"); + return has_value() ? std::move(**this) : static_cast(std::forward(u)); + } + + /// Destroys the stored value if one exists, making the optional empty + void reset() noexcept { + if (has_value()) { + this->m_value.~T(); + this->m_has_value = false; + } + } +}; // namespace tl + +/// Compares two optional objects +template +inline constexpr bool operator==(const optional &lhs, + const optional &rhs) { + return lhs.has_value() == rhs.has_value() && + (!lhs.has_value() || *lhs == *rhs); +} +template +inline constexpr bool operator!=(const optional &lhs, + const optional &rhs) { + return lhs.has_value() != rhs.has_value() || + (lhs.has_value() && *lhs != *rhs); +} +template +inline constexpr bool operator<(const optional &lhs, + const optional &rhs) { + return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs); +} +template +inline constexpr bool operator>(const optional &lhs, + const optional &rhs) { + return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs); +} +template +inline constexpr bool operator<=(const optional &lhs, + const optional &rhs) { + return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs); +} +template +inline constexpr bool operator>=(const optional &lhs, + const optional &rhs) { + return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs); +} + +/// Compares an optional to a `nullopt` +template +inline constexpr bool operator==(const optional &lhs, nullopt_t) noexcept { + return !lhs.has_value(); +} +template +inline constexpr bool operator==(nullopt_t, const optional &rhs) noexcept { + return !rhs.has_value(); +} +template +inline constexpr bool operator!=(const optional &lhs, nullopt_t) noexcept { + return lhs.has_value(); +} +template +inline constexpr bool operator!=(nullopt_t, const optional &rhs) noexcept { + return rhs.has_value(); +} +template +inline constexpr bool operator<(const optional &, nullopt_t) noexcept { + return false; +} +template +inline constexpr bool operator<(nullopt_t, const optional &rhs) noexcept { + return rhs.has_value(); +} +template +inline constexpr bool operator<=(const optional &lhs, nullopt_t) noexcept { + return !lhs.has_value(); +} +template +inline constexpr bool operator<=(nullopt_t, const optional &) noexcept { + return true; +} +template +inline constexpr bool operator>(const optional &lhs, nullopt_t) noexcept { + return lhs.has_value(); +} +template +inline constexpr bool operator>(nullopt_t, const optional &) noexcept { + return false; +} +template +inline constexpr bool operator>=(const optional &, nullopt_t) noexcept { + return true; +} +template +inline constexpr bool operator>=(nullopt_t, const optional &rhs) noexcept { + return !rhs.has_value(); +} + +/// Compares the optional with a value. +template +inline constexpr bool operator==(const optional &lhs, const U &rhs) { + return lhs.has_value() ? *lhs == rhs : false; +} +template +inline constexpr bool operator==(const U &lhs, const optional &rhs) { + return rhs.has_value() ? lhs == *rhs : false; +} +template +inline constexpr bool operator!=(const optional &lhs, const U &rhs) { + return lhs.has_value() ? *lhs != rhs : true; +} +template +inline constexpr bool operator!=(const U &lhs, const optional &rhs) { + return rhs.has_value() ? lhs != *rhs : true; +} +template +inline constexpr bool operator<(const optional &lhs, const U &rhs) { + return lhs.has_value() ? *lhs < rhs : true; +} +template +inline constexpr bool operator<(const U &lhs, const optional &rhs) { + return rhs.has_value() ? lhs < *rhs : false; +} +template +inline constexpr bool operator<=(const optional &lhs, const U &rhs) { + return lhs.has_value() ? *lhs <= rhs : true; +} +template +inline constexpr bool operator<=(const U &lhs, const optional &rhs) { + return rhs.has_value() ? lhs <= *rhs : false; +} +template +inline constexpr bool operator>(const optional &lhs, const U &rhs) { + return lhs.has_value() ? *lhs > rhs : false; +} +template +inline constexpr bool operator>(const U &lhs, const optional &rhs) { + return rhs.has_value() ? lhs > *rhs : true; +} +template +inline constexpr bool operator>=(const optional &lhs, const U &rhs) { + return lhs.has_value() ? *lhs >= rhs : false; +} +template +inline constexpr bool operator>=(const U &lhs, const optional &rhs) { + return rhs.has_value() ? lhs >= *rhs : true; +} + +template ::value> * = nullptr, + detail::enable_if_t::value> * = nullptr> +void swap(optional &lhs, + optional &rhs) noexcept(noexcept(lhs.swap(rhs))) { + return lhs.swap(rhs); +} + +namespace detail { +struct i_am_secret {}; +} // namespace detail + +template ::value, + detail::decay_t, T>> +inline constexpr optional make_optional(U &&v) { + return optional(std::forward(v)); +} + +template +inline constexpr optional make_optional(Args &&... args) { + return optional(in_place, std::forward(args)...); +} +template +inline constexpr optional make_optional(std::initializer_list il, + Args &&... args) { + return optional(in_place, il, std::forward(args)...); +} + +#if __cplusplus >= 201703L +template optional(T)->optional; +#endif + +/// \exclude +namespace detail { +#ifdef TL_OPTIONAL_CXX14 +template (), + *std::declval())), + detail::enable_if_t::value> * = nullptr> +constexpr auto optional_map_impl(Opt &&opt, F &&f) { + return opt.has_value() + ? detail::invoke(std::forward(f), *std::forward(opt)) + : optional(nullopt); +} + +template (), + *std::declval())), + detail::enable_if_t::value> * = nullptr> +auto optional_map_impl(Opt &&opt, F &&f) { + if (opt.has_value()) { + detail::invoke(std::forward(f), *std::forward(opt)); + return make_optional(monostate{}); + } + + return optional(nullopt); +} +#else +template (), + *std::declval())), + detail::enable_if_t::value> * = nullptr> + +constexpr auto optional_map_impl(Opt &&opt, F &&f) -> optional { + return opt.has_value() + ? detail::invoke(std::forward(f), *std::forward(opt)) + : optional(nullopt); +} + +template (), + *std::declval())), + detail::enable_if_t::value> * = nullptr> + +auto optional_map_impl(Opt &&opt, F &&f) -> optional { + if (opt.has_value()) { + detail::invoke(std::forward(f), *std::forward(opt)); + return monostate{}; + } + + return nullopt; +} +#endif +} // namespace detail + +/// Specialization for when `T` is a reference. `optional` acts similarly +/// to a `T*`, but provides more operations and shows intent more clearly. +template class optional { +public: +// The different versions for C++14 and 11 are needed because deduced return +// types are not SFINAE-safe. This provides better support for things like +// generic lambdas. C.f. +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ + !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) + + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template constexpr auto and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template constexpr auto and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } +#endif +#else + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template + constexpr detail::invoke_result_t and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + constexpr detail::invoke_result_t and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } +#endif +#endif + +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ + !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) + /// Carries out some operation on the stored object if there is one. + template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template constexpr auto map(F &&f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template constexpr auto map(F &&f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } +#else + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), + std::declval())) + map(F &&f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), + std::declval())) + map(F &&f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(detail::optional_map_impl(std::declval(), + std::declval())) + map(F &&f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + constexpr decltype(detail::optional_map_impl(std::declval(), + std::declval())) + map(F &&f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } +#endif +#endif + +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ + !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) + /// Carries out some operation on the stored object if there is one. + template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template constexpr auto transform(F&& f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template constexpr auto transform(F&& f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } +#else + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + /// \group map + /// \synopsis template auto transform(F &&f) &&; + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(detail::optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + constexpr decltype(detail::optional_map_impl(std::declval(), + std::declval())) + transform(F&& f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } +#endif +#endif + + /// Calls `f` if the optional is empty + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + if (has_value()) + return *this; + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + return has_value() ? *this : std::forward(f)(); + } + + template * = nullptr> + optional or_else(F &&f) && { + if (has_value()) + return std::move(*this); + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) && { + return has_value() ? std::move(*this) : std::forward(f)(); + } + + template * = nullptr> + optional or_else(F &&f) const & { + if (has_value()) + return *this; + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const & { + return has_value() ? *this : std::forward(f)(); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template * = nullptr> + optional or_else(F &&f) const && { + if (has_value()) + return std::move(*this); + + std::forward(f)(); + return nullopt; + } + + template * = nullptr> + optional or_else(F &&f) const && { + return has_value() ? std::move(*this) : std::forward(f)(); + } +#endif + + /// Maps the stored value with `f` if there is one, otherwise returns `u` + template U map_or(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } + + template U map_or(F &&f, U &&u) && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } + + template U map_or(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template U map_or(F &&f, U &&u) const && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } +#endif + + /// Maps the stored value with `f` if there is one, otherwise calls + /// `u` and returns the result. + template + detail::invoke_result_t map_or_else(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const && { + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } +#endif + + /// Returns `u` if `*this` has a value, otherwise an empty optional. + template + constexpr optional::type> conjunction(U &&u) const { + using result = optional>; + return has_value() ? result{u} : result{nullopt}; + } + + /// Returns `rhs` if `*this` is empty, otherwise the current value. + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { + return has_value() ? *this : rhs; + } + + constexpr optional disjunction(const optional &rhs) const & { + return has_value() ? *this : rhs; + } + + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { + return has_value() ? std::move(*this) : rhs; + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + constexpr optional disjunction(const optional &rhs) const && { + return has_value() ? std::move(*this) : rhs; + } +#endif + + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { + return has_value() ? *this : std::move(rhs); + } + + constexpr optional disjunction(optional &&rhs) const & { + return has_value() ? *this : std::move(rhs); + } + + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { + return has_value() ? std::move(*this) : std::move(rhs); + } + +#ifndef TL_OPTIONAL_NO_CONSTRR + constexpr optional disjunction(optional &&rhs) const && { + return has_value() ? std::move(*this) : std::move(rhs); + } +#endif + + /// Takes the value out of the optional, leaving it empty + optional take() { + optional ret = std::move(*this); + reset(); + return ret; + } + + using value_type = T &; + + /// Constructs an optional that does not contain a value. + constexpr optional() noexcept : m_value(nullptr) {} + + constexpr optional(nullopt_t) noexcept : m_value(nullptr) {} + + /// Copy constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) noexcept = default; + + /// Move constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default; + + /// Constructs the stored value with `u`. + template >::value> + * = nullptr> + constexpr optional(U &&u) noexcept : m_value(std::addressof(u)) { + static_assert(std::is_lvalue_reference::value, "U must be an lvalue"); + } + + template + constexpr explicit optional(const optional &rhs) noexcept : optional(*rhs) {} + + /// No-op + ~optional() = default; + + /// Assignment to empty. + /// + /// Destroys the current value if there is one. + optional &operator=(nullopt_t) noexcept { + m_value = nullptr; + return *this; + } + + /// Copy assignment. + /// + /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise + /// resets the stored value in `*this`. + optional &operator=(const optional &rhs) = default; + + /// Rebinds this optional to `u`. + template >::value> + * = nullptr> + optional &operator=(U &&u) { + static_assert(std::is_lvalue_reference::value, "U must be an lvalue"); + m_value = std::addressof(u); + return *this; + } + + /// Converting copy assignment operator. + /// + /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise + /// resets the stored value in `*this`. + template optional &operator=(const optional &rhs) noexcept { + m_value = std::addressof(rhs.value()); + return *this; + } + + /// Rebinds this optional to `u`. + template >::value> + * = nullptr> + optional &emplace(U &&u) noexcept { + return *this = std::forward(u); + } + + void swap(optional &rhs) noexcept { std::swap(m_value, rhs.m_value); } + + /// Returns a pointer to the stored value + constexpr const T *operator->() const noexcept { return m_value; } + + TL_OPTIONAL_11_CONSTEXPR T *operator->() noexcept { return m_value; } + + /// Returns the stored value + TL_OPTIONAL_11_CONSTEXPR T &operator*() noexcept { return *m_value; } + + constexpr const T &operator*() const noexcept { return *m_value; } + + constexpr bool has_value() const noexcept { return m_value != nullptr; } + + constexpr explicit operator bool() const noexcept { + return m_value != nullptr; + } + + /// Returns the contained value if there is one, otherwise throws bad_optional_access + TL_OPTIONAL_11_CONSTEXPR T &value() { + if (has_value()) + return *m_value; + throw bad_optional_access(); + } + TL_OPTIONAL_11_CONSTEXPR const T &value() const { + if (has_value()) + return *m_value; + throw bad_optional_access(); + } + + /// Returns the stored value if there is one, otherwise returns `u` + template constexpr T value_or(U &&u) const & noexcept { + static_assert(std::is_copy_constructible::value && + std::is_convertible::value, + "T must be copy constructible and convertible from U"); + return has_value() ? **this : static_cast(std::forward(u)); + } + + /// \group value_or + template TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && noexcept { + static_assert(std::is_move_constructible::value && + std::is_convertible::value, + "T must be move constructible and convertible from U"); + return has_value() ? **this : static_cast(std::forward(u)); + } + + /// Destroys the stored value if one exists, making the optional empty + void reset() noexcept { m_value = nullptr; } + +private: + T *m_value; +}; // namespace tl + + + +} // namespace tl + +namespace tqcq { +// TODO SFINAE +template struct hash> { + ::std::size_t operator()(const tl::optional &o) const { + if (!o.has_value()) + return 0; + + return std::hash>()(*o); + } +}; + +// if <= C++14 +#if __cplusplus < 201703L +template +using optional = tl::optional; + +template +using nullopt_t = tl::nullopt_t; + +// make_optional +template +constexpr tl::optional> make_optional(T&& v) { + return tl::make_optional(std::forward(v)); +} + +template +constexpr tl::optional make_optional(Args&&... args) { + return tl::make_optional(std::forward(args)...); +} + +template +constexpr tl::optional make_optional(std::initializer_list il, Args&&... args) { + return tl::make_optional(il, std::forward(args)...); +} + +constexpr tl::nullopt_t nullopt{tl::nullopt}; +#endif + +} // namespace tqcq + +#endif \ No newline at end of file diff --git a/src/units/time_delta.cpp b/src/units/time_delta.cpp new file mode 100644 index 0000000..5a58a8f --- /dev/null +++ b/src/units/time_delta.cpp @@ -0,0 +1,8 @@ +// +// Created by tqcq on 2023/11/16. +// + +#include "time_delta.h" + +namespace tqcq { +} // namespace tqcq diff --git a/src/units/time_delta.h b/src/units/time_delta.h new file mode 100644 index 0000000..6b30308 --- /dev/null +++ b/src/units/time_delta.h @@ -0,0 +1,81 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_UNITS_TIME_DELTA_H_ +#define ULIB_SRC_UNITS_TIME_DELTA_H_ + +#include +#include +#include +#include "units/unit_base.h" + +namespace tqcq { + +class TimeDelta final : public RelativeUnit{ + public: + TimeDelta() = delete; + template + static constexpr TimeDelta Minutes(T value) { + static_assert(std::is_arithmetic::value, ""); + return Seconds(value * 60); + } + + template + static constexpr TimeDelta Seconds(T value) { + static_assert(std::is_arithmetic::value, ""); + return TimeDelta(value * 1000000); + } + + template + static constexpr TimeDelta Millis(T value) { + static_assert(std::is_arithmetic::value, ""); + return TimeDelta(value * 1000); + } + + template + static constexpr TimeDelta Micros(T value) { + static_assert(std::is_arithmetic::value, ""); + return FromValue(value); + } + + static constexpr TimeDelta Max() { + return TimeDelta::Micros(std::numeric_limits::max()); + } + + template + constexpr T seconds() const { return value_ / 1000000; } + + template + constexpr T ms() const { return value_ / 1000; } + + template + constexpr T us() const { return value_; } + + template + constexpr T ns() const { return value_ * 1000; } + + constexpr TimeDelta Abs() const { + return us() < 0 ? TimeDelta::Micros(-us()) : *this; + } + + + private: + friend class UnitBase; + int64_t value_; + using RelativeUnit::RelativeUnit; + static constexpr bool one_sided = false; +}; + +std::string ToString(const TimeDelta &value); +inline std::string ToString(const TimeDelta &value) { + return std::to_string(value.us()); +} + +inline std::ostream& operator<<(std::ostream& stream, const TimeDelta& value) { + return stream << ToString(value); +} + +} // namespace tqcq + +#endif //ULIB_SRC_UNITS_TIME_DELTA_H_ diff --git a/src/units/unit_base.h b/src/units/unit_base.h new file mode 100644 index 0000000..a71f7db --- /dev/null +++ b/src/units/unit_base.h @@ -0,0 +1,109 @@ +// +// Created by tqcq on 2023/11/16. +// + +#ifndef ULIB_SRC_UNITS_UNITBASE_H_ +#define ULIB_SRC_UNITS_UNITBASE_H_ + +#include +#include +#include +#include "base/checks.h" + +template +class UnitBase { + public: + UnitBase() = delete; + explicit constexpr UnitBase(int64_t value) : value_(value) {} + static constexpr Unit_T Zero() { return Unit_T(0); } + static constexpr Unit_T PlusInfinity() { return Unit_T(std::numeric_limits::max()); } + static constexpr Unit_T MinusInfinity() { return Unit_T(std::numeric_limits::min()); } + + constexpr bool IsZero() const { return value_ == 0; } + constexpr bool IsFinite() const { return !IsInfinite(); } + constexpr bool IsInfinite() const { return value_ = PlusInfinity().value_ || value_ == MinusInfinity().value_; } + constexpr bool IsPlusInfinity() const { return value_ == PlusInfinity().value_; } + constexpr bool IsMinusInfinity() const { return value_ == MinusInfinity().value_; } + + constexpr bool operator==(const UnitBase &other) const { return value_ == other.value_; } + constexpr bool operator!=(const UnitBase &other) const { return value_ != other.value_; } + constexpr bool operator<=(const UnitBase &other) const { return value_ <= other.value_; } + constexpr bool operator>=(const UnitBase &other) const { return value_ >= other.value_; } + constexpr bool operator<(const UnitBase &other) const { return value_ < other.value_; } + constexpr bool operator>(const UnitBase &other) const { return value_ > other.value_; } + + protected: + template::value>::type * = nullptr> + static + constexpr Unit_T FromValue(T value) { + return Unit_T(static_cast(value)); + } + + template + constexpr typename std::enable_if::value, T>::type + ToValue() const { + return static_cast(value_); + } + + private: + int64_t value_; +}; + +template +class RelativeUnit : public UnitBase { + public: + constexpr Unit_T Clamped(Unit_T min_value, Unit_T max_value) const { + return std::min(min_value, std::max(*this, max_value)); + } + + void Clamp(Unit_T min_value, Unit_T max_value) { + *this = Clamped(min_value, max_value); + } + + Unit_T operator+(const Unit_T other) const { + if (this->IsPlusInfinity() || other.IsPlusInfinity()) { + ULIB_DCHECK(!this->IsMinusInfinity()); + ULIB_DCHECK(!other->IsMinusInfinity()); + return this->PlusInfinity(); + } else if (this->IsMinusInfinity() || other.IsMinusInfinity()) { + ULIB_DCHECK(!this->IsPlusInfinity()); + ULIB_DCHECK(!other->IsPlusInfinity()); + return this->MinusInfinity(); + } + + return UnitBase::FromValue(this->ToValue() + other.ToValue()); + } + + Unit_T operator-(const Unit_T &other) const { + if (this->IsPlusInfinity() || other.IsMinusInfinity()) { + ULIB_DCHECK(!this->IsMinusInfinity()); + ULIB_DCHECK(!other->IsPlusInfinity()); + return this->PlusInfinity(); + } else if (this->IsMinusInfinity() || other.IsPlusInfinity()) { + ULIB_DCHECK(!this->IsPlusInfinity()); + ULIB_DCHECK(!other->IsMinusInfinity()); + return this->MinusInfinity(); + } + + return UnitBase::FromValue(this->ToValue() - other.ToValue()); + } + + Unit_T &operator+=(const Unit_T &other) const { + *this = *this + other; + return *this; + } + + Unit_T &operator-=(const Unit_T &other) const { + *this = *this - other; + return *this; + } + + Unit_T operator*(int64_t scalar) { + return UnitBase::FromValue(this->ToValue() * scalar); + } + + protected: + using UnitBase::UnitBase; +}; + +#endif //ULIB_SRC_UNITS_UNITBASE_H_