diff --git a/.clang-format b/.clang-format index ddab1f9..d68e552 100644 --- a/.clang-format +++ b/.clang-format @@ -13,7 +13,7 @@ AllowAllParametersOfDeclarationOnNextLine: false AllowShortBlocksOnASingleLine: Always AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: All -AllowShortIfStatementsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: WithoutElse AllowShortLambdasOnASingleLine: All AllowShortLoopsOnASingleLine: true AlwaysBreakTemplateDeclarations: Yes @@ -52,6 +52,7 @@ MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: false +PenaltyIndentedWhitespace: 1 PointerAlignment: Right ReflowComments: false SortIncludes: CaseSensitive diff --git a/3party/asyncplusplus/Async++Config.cmake.in b/3party/asyncplusplus/Async++Config.cmake.in new file mode 100644 index 0000000..ade2b3e --- /dev/null +++ b/3party/asyncplusplus/Async++Config.cmake.in @@ -0,0 +1,3 @@ +include(CMakeFindDependencyMacro) +find_dependency(Threads) +include("${CMAKE_CURRENT_LIST_DIR}/Async++.cmake") diff --git a/3party/asyncplusplus/CMakeLists.txt b/3party/asyncplusplus/CMakeLists.txt new file mode 100644 index 0000000..bb2de4d --- /dev/null +++ b/3party/asyncplusplus/CMakeLists.txt @@ -0,0 +1,165 @@ +# Copyright (c) 2015 Amanieu d'Antras +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +cmake_minimum_required(VERSION 3.1) +project(Async++ C CXX) + +option(BUILD_SHARED_LIBS "Build Async++ as a shared library" ON) +option(USE_CXX_EXCEPTIONS "Enable C++ exception support" ON) +if (APPLE) + option(BUILD_FRAMEWORK "Build a Mac OS X framework instead of a library" OFF) + if (BUILD_FRAMEWORK AND NOT BUILD_SHARED_LIBS) + message(FATAL_ERROR "Can't build a framework with static libraries") + endif() +endif() + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Add all source and header files so IDEs can see them +set(ASYNCXX_INCLUDE + ${PROJECT_SOURCE_DIR}/include/async++/aligned_alloc.h + ${PROJECT_SOURCE_DIR}/include/async++/cancel.h + ${PROJECT_SOURCE_DIR}/include/async++/continuation_vector.h + ${PROJECT_SOURCE_DIR}/include/async++/parallel_for.h + ${PROJECT_SOURCE_DIR}/include/async++/parallel_invoke.h + ${PROJECT_SOURCE_DIR}/include/async++/parallel_reduce.h + ${PROJECT_SOURCE_DIR}/include/async++/partitioner.h + ${PROJECT_SOURCE_DIR}/include/async++/range.h + ${PROJECT_SOURCE_DIR}/include/async++/ref_count.h + ${PROJECT_SOURCE_DIR}/include/async++/scheduler.h + ${PROJECT_SOURCE_DIR}/include/async++/scheduler_fwd.h + ${PROJECT_SOURCE_DIR}/include/async++/task.h + ${PROJECT_SOURCE_DIR}/include/async++/task_base.h + ${PROJECT_SOURCE_DIR}/include/async++/traits.h + ${PROJECT_SOURCE_DIR}/include/async++/when_all_any.h +) +set(ASYNCXX_SRC + ${PROJECT_SOURCE_DIR}/src/internal.h + ${PROJECT_SOURCE_DIR}/src/fifo_queue.h + ${PROJECT_SOURCE_DIR}/src/scheduler.cpp + ${PROJECT_SOURCE_DIR}/src/singleton.h + ${PROJECT_SOURCE_DIR}/src/task_wait_event.h + ${PROJECT_SOURCE_DIR}/src/threadpool_scheduler.cpp + ${PROJECT_SOURCE_DIR}/src/work_steal_queue.h +) +source_group(include FILES ${PROJECT_SOURCE_DIR}/include/async++.h ${ASYNCXX_INCLUDE}) +source_group(src FILES ${ASYNCXX_SRC}) +add_library(Async++ ${PROJECT_SOURCE_DIR}/include/async++.h ${ASYNCXX_INCLUDE} ${ASYNCXX_SRC}) + +# Async++ only depends on the C++11 standard libraries, but some implementations +# require the -pthread compiler flag to enable threading functionality. +if (NOT MSVC) + target_compile_options(Async++ PRIVATE -std=c++11) +endif() +if (APPLE) + # Use libc++ on Mac because the shipped libstdc++ version is ancient + target_compile_options(Async++ PRIVATE -stdlib=libc++) + set_target_properties(Async++ PROPERTIES LINK_FLAGS -stdlib=libc++) +endif() +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) +target_link_libraries(Async++ PUBLIC Threads::Threads) + +# Set up preprocessor definitions +target_include_directories(Async++ PRIVATE ${PROJECT_SOURCE_DIR}/include) +set_target_properties(Async++ PROPERTIES DEFINE_SYMBOL LIBASYNC_BUILD) +if (BUILD_SHARED_LIBS) + # Minimize the set of symbols exported by libraries + set_target_properties(Async++ PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON) +else() + target_compile_definitions(Async++ PUBLIC LIBASYNC_STATIC) +endif() + +# Enable warnings for strict C++ standard conformance +if (NOT MSVC) + target_compile_options(Async++ PRIVATE -Wall -Wextra -pedantic) +endif() + +# Async++ doesn't make use of RTTI information, so don't generate it. +# There are issues on Apple platforms with exceptions and -fno-rtti, so keep it +# enabled there. +# See https://stackoverflow.com/questions/21737201/problems-throwing-and-catching-exceptions-on-os-x-with-fno-rtti +if (MSVC) + target_compile_options(Async++ PRIVATE /GR-) +elseif(NOT APPLE) + target_compile_options(Async++ PRIVATE -fno-rtti) +endif() + +# Allow disabling exceptions, but warn the user about the consequences +if (NOT USE_CXX_EXCEPTIONS) + message(WARNING "Exceptions have been disabled. Any operation that would " + "throw an exception will result in a call to std::abort() instead.") + target_compile_definitions(Async++ PUBLIC LIBASYNC_NO_EXCEPTIONS) + if (MSVC) + target_compile_options(Async++ PUBLIC /EHs-c-) + else() + target_compile_options(Async++ PUBLIC -fno-exceptions) + endif() +endif() + +# /Zc:__cplusplus is required to make __cplusplus accurate +# /Zc:__cplusplus is available starting with Visual Studio 2017 version 15.7 +# (according to https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus) +# That version is equivalent to _MSC_VER==1914 +# (according to https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019) +# CMake's ${MSVC_VERSION} is equivalent to _MSC_VER +# (according to https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html#variable:MSVC_VERSION) +# GREATER and EQUAL are used because GREATER_EQUAL is available starting with CMake 3.7 +# (according to https://cmake.org/cmake/help/v3.7/release/3.7.html#commands) +if ((MSVC) AND ((MSVC_VERSION GREATER 1914) OR (MSVC_VERSION EQUAL 1914))) + target_compile_options(Async++ PUBLIC /Zc:__cplusplus) +endif() + +include(CMakePackageConfigHelpers) +configure_package_config_file("${CMAKE_CURRENT_LIST_DIR}/Async++Config.cmake.in" + "${PROJECT_BINARY_DIR}/Async++Config.cmake" + INSTALL_DESTINATION cmake +) + +install(FILES "${PROJECT_BINARY_DIR}/Async++Config.cmake" + DESTINATION cmake +) + +# Install the library and produce a CMake export script +include(GNUInstallDirs) +install(TARGETS Async++ + EXPORT Async++ + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + FRAMEWORK DESTINATION Frameworks +) +export(EXPORT Async++) +install(EXPORT Async++ DESTINATION cmake) +if (APPLE AND BUILD_FRAMEWORK) + set_target_properties(Async++ PROPERTIES OUTPUT_NAME Async++ FRAMEWORK ON) + set_source_files_properties(${ASYNCXX_INCLUDE} PROPERTIES MACOSX_PACKAGE_LOCATION Headers/async++) + set_source_files_properties(${PROJECT_SOURCE_DIR}/include/async++.h PROPERTIES MACOSX_PACKAGE_LOCATION Headers) +else() + set_target_properties(Async++ PROPERTIES OUTPUT_NAME async++) + target_include_directories(Async++ INTERFACE $ $) + install(FILES ${PROJECT_SOURCE_DIR}/include/async++.h DESTINATION include) + install(FILES ${ASYNCXX_INCLUDE} DESTINATION include/async++) +endif() + +SET(CPACK_GENERATOR "DEB") +SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "none") #required + +INCLUDE(CPack) diff --git a/3party/asyncplusplus/LICENSE b/3party/asyncplusplus/LICENSE new file mode 100644 index 0000000..f6c080d --- /dev/null +++ b/3party/asyncplusplus/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 Amanieu d'Antras + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/3party/asyncplusplus/README.md b/3party/asyncplusplus/README.md new file mode 100644 index 0000000..d19ce24 --- /dev/null +++ b/3party/asyncplusplus/README.md @@ -0,0 +1,91 @@ +Async++ +======= + +Async++ is a lightweight concurrency framework for C++11. The concept was inspired by the [Microsoft PPL library](http://msdn.microsoft.com/en-us/library/dd492418.aspx) and the [N3428](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3428.pdf) C++ standard proposal. + +Example +------- +Here is a short example which shows some features of Async++: + +```c++ +#include +#include + +int main() +{ + auto task1 = async::spawn([] { + std::cout << "Task 1 executes asynchronously" << std::endl; + }); + auto task2 = async::spawn([]() -> int { + std::cout << "Task 2 executes in parallel with task 1" << std::endl; + return 42; + }); + auto task3 = task2.then([](int value) -> int { + std::cout << "Task 3 executes after task 2, which returned " + << value << std::endl; + return value * 3; + }); + auto task4 = async::when_all(task1, task3); + auto task5 = task4.then([](std::tuple, + async::task> results) { + std::cout << "Task 5 executes after tasks 1 and 3. Task 3 returned " + << std::get<1>(results).get() << std::endl; + }); + + task5.get(); + std::cout << "Task 5 has completed" << std::endl; + + async::parallel_invoke([] { + std::cout << "This is executed in parallel..." << std::endl; + }, [] { + std::cout << "with this" << std::endl; + }); + + async::parallel_for(async::irange(0, 5), [](int x) { + std::cout << x; + }); + std::cout << std::endl; + + int r = async::parallel_reduce({1, 2, 3, 4}, 0, [](int x, int y) { + return x + y; + }); + std::cout << "The sum of {1, 2, 3, 4} is " << r << std::endl; +} + +// Output (order may vary in some places): +// Task 1 executes asynchronously +// Task 2 executes in parallel with task 1 +// Task 3 executes after task 2, which returned 42 +// Task 5 executes after tasks 1 and 3. Task 3 returned 126 +// Task 5 has completed +// This is executed in parallel... +// with this +// 01234 +// The sum of {1, 2, 3, 4} is 10 +``` + +Supported Platforms +------------------- + +The only requirement to use Async++ is a C++11 compiler and standard library. Unfortunately C++11 is not yet fully implemented on most platforms. Here is the list of OS and compiler combinations which are known to work. + +- Linux: Works with GCC 4.7+, Clang 3.2+ and Intel compiler 15+. +- Mac: Works with Apple Clang (using libc++). GCC also works but you must get a recent version (4.7+). +- iOS: Works with Apple Clang (using libc++). Note: because iOS has no thread local support, the library uses a workaround based on pthreads. +- Windows: Works with GCC 4.8+ (with pthread-win32) and Visual Studio 2013+. + +Building and Installing +----------------------- +Instructions for compiling Async++ and using it in your code are available on the [Building and Installing](https://github.com/Amanieu/asyncplusplus/wiki/Building-and-Installing) page. + +Documentation +------------ +The Async++ documentation is split into four parts: +- [Tasks](https://github.com/Amanieu/asyncplusplus/wiki/Tasks): This describes task objects which are the core Async++. Reading this first is strongly recommended. +- [Parallel algorithms](https://github.com/Amanieu/asyncplusplus/wiki/Parallel-algorithms): This describes functions to run work on ranges in parallel. +- [Schedulers](https://github.com/Amanieu/asyncplusplus/wiki/Schedulers): This describes the low-level details of Async++ and how to customize it. +- [API Reference](https://github.com/Amanieu/asyncplusplus/wiki/API-Reference): This gives detailed descriptions of all the classes and functions available in Async++. + +Contact +------- +You can contact me by email at amanieu@gmail.com. diff --git a/3party/asyncplusplus/examples/gtk_scheduler.cpp b/3party/asyncplusplus/examples/gtk_scheduler.cpp new file mode 100644 index 0000000..689eb85 --- /dev/null +++ b/3party/asyncplusplus/examples/gtk_scheduler.cpp @@ -0,0 +1,98 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include +#include +#include +#include +#include + +// Scheduler implementation +class gtk_scheduler_impl { + // Get the task from the void* and execute it in the UI thread + static gboolean callback(void* p) + { + async::task_run_handle::from_void_ptr(p).run(); + return FALSE; + } + +public: + // Convert a task to void* and send it to the gtk main loop + void schedule(async::task_run_handle t) + { + g_idle_add(callback, t.to_void_ptr()); + } +}; + +// Scheduler to run a task in the gtk main loop +gtk_scheduler_impl& gtk_scheduler() +{ + static gtk_scheduler_impl sched; + return sched; +} + +// In order to ensure the UI is always responsive, you can disallow blocking +// calls in the UI thread. Note that the wait handler isn't called when the +// result of a task is already available, so you can still call .get() on a +// completed task. This is completely optional and can be omitted if you don't +// need it. +void gtk_wait_handler(async::task_wait_handle) +{ + std::cerr << "Error: Blocking wait in UI thread" << std::endl; + std::abort(); +} + +// Thread which increments the label value every ms +void label_update_thread(GtkLabel *label) +{ + int counter = 0; + while (true) { + // Sleep for 1ms + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + counter++; + + // Update the label contents in the UI thread + async::spawn(gtk_scheduler(), [label, counter] { + gtk_label_set_text(label, std::to_string(counter).c_str()); + }); + } +} + +int main(int argc, char *argv[]) +{ + // Initialize GTK + gtk_init(&argc, &argv); + + // Set wait handler on GTK thread to disallow waiting for tasks + async::set_thread_wait_handler(gtk_wait_handler); + + // Create a window with a label + GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); + GtkLabel *label = GTK_LABEL(gtk_label_new("-")); + gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(label)); + gtk_widget_show_all(window); + + // Start a secondary thread to update the label + std::thread(label_update_thread, label).detach(); + + // Go to GTK main loop + gtk_main(); +} diff --git a/3party/asyncplusplus/include/async++.h b/3party/asyncplusplus/include/async++.h new file mode 100644 index 0000000..36e1516 --- /dev/null +++ b/3party/asyncplusplus/include/async++.h @@ -0,0 +1,158 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +#define ASYNCXX_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Export declaration to make symbols visible for dll/so +#ifdef LIBASYNC_STATIC +# define LIBASYNC_EXPORT +# define LIBASYNC_EXPORT_EXCEPTION +#else +# ifdef _WIN32 +# ifdef LIBASYNC_BUILD +# define LIBASYNC_EXPORT __declspec(dllexport) +# else +# define LIBASYNC_EXPORT __declspec(dllimport) +# endif +# define LIBASYNC_EXPORT_EXCEPTION +# else +# define LIBASYNC_EXPORT __attribute__((visibility("default"))) +# define LIBASYNC_EXPORT_EXCEPTION __attribute__((visibility("default"))) +# endif +#endif + +// Support compiling without exceptions +#ifndef LIBASYNC_NO_EXCEPTIONS +# ifdef __clang__ +# if !defined(__EXCEPTIONS) || !__has_feature(cxx_exceptions) +# define LIBASYNC_NO_EXCEPTIONS +# endif +# elif defined(__GNUC__) && !defined(__EXCEPTIONS) +# define LIBASYNC_NO_EXCEPTIONS +# elif defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS +# define LIBASYNC_NO_EXCEPTIONS +# endif +#endif +#ifdef LIBASYNC_NO_EXCEPTIONS +# define LIBASYNC_THROW(...) std::abort() +# define LIBASYNC_RETHROW() do {} while (false) +# define LIBASYNC_RETHROW_EXCEPTION(except) std::terminate() +# define LIBASYNC_TRY if (true) +# define LIBASYNC_CATCH(...) else if (false) +#else +# define LIBASYNC_THROW(...) throw __VA_ARGS__ +# define LIBASYNC_RETHROW() throw +# define LIBASYNC_RETHROW_EXCEPTION(except) std::rethrow_exception(except) +# define LIBASYNC_TRY try +# define LIBASYNC_CATCH(...) catch (__VA_ARGS__) +#endif + +// Optional debug assertions. If exceptions are enabled then use those, but +// otherwise fall back to an assert message. +#ifndef NDEBUG +# ifndef LIBASYNC_NO_EXCEPTIONS +# define LIBASYNC_ASSERT(pred, except, message) ((pred) ? ((void)0) : throw except(message)) +# else +# define LIBASYNC_ASSERT(pred, except, message) ((pred) ? ((void)0) : assert(message)) +# endif +#else +# define LIBASYNC_ASSERT(pred, except, message) ((void)0) +#endif + +// Annotate move constructors and move assignment with noexcept to allow objects +// to be moved if they are in containers. Compilers which don't support noexcept +// will usually move regardless. +#if defined(__GNUC__) || _MSC_VER >= 1900 +# define LIBASYNC_NOEXCEPT noexcept +#else +# define LIBASYNC_NOEXCEPT throw() +#endif + +// Cacheline alignment to avoid false sharing between different threads +#define LIBASYNC_CACHELINE_SIZE 64 +#ifdef __GNUC__ +# define LIBASYNC_CACHELINE_ALIGN __attribute__((aligned(LIBASYNC_CACHELINE_SIZE))) +#elif defined(_MSC_VER) +# define LIBASYNC_CACHELINE_ALIGN __declspec(align(LIBASYNC_CACHELINE_SIZE)) +#else +# define LIBASYNC_CACHELINE_ALIGN alignas(LIBASYNC_CACHELINE_SIZE) +#endif + +// Force symbol visibility to hidden unless explicity exported +#ifndef LIBASYNC_STATIC +#if defined(__GNUC__) && !defined(_WIN32) +# pragma GCC visibility push(hidden) +#endif +#endif + +// Some forward declarations +namespace async { + +template +class task; +template +class shared_task; +template +class event_task; + +} // namespace async + +// Include sub-headers +#include "async++/traits.h" +#include "async++/aligned_alloc.h" +#include "async++/ref_count.h" +#include "async++/scheduler_fwd.h" +#include "async++/continuation_vector.h" +#include "async++/task_base.h" +#include "async++/scheduler.h" +#include "async++/task.h" +#include "async++/when_all_any.h" +#include "async++/cancel.h" +#include "async++/range.h" +#include "async++/partitioner.h" +#include "async++/parallel_invoke.h" +#include "async++/parallel_for.h" +#include "async++/parallel_reduce.h" + +#ifndef LIBASYNC_STATIC +#if defined(__GNUC__) && !defined(_WIN32) +# pragma GCC visibility pop +#endif +#endif + +#endif diff --git a/3party/asyncplusplus/include/async++/aligned_alloc.h b/3party/asyncplusplus/include/async++/aligned_alloc.h new file mode 100644 index 0000000..cf4c2a3 --- /dev/null +++ b/3party/asyncplusplus/include/async++/aligned_alloc.h @@ -0,0 +1,99 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +namespace async { +namespace detail { + +// Allocate an aligned block of memory +LIBASYNC_EXPORT void* aligned_alloc(std::size_t size, std::size_t align); + +// Free an aligned block of memory +LIBASYNC_EXPORT void aligned_free(void* addr) LIBASYNC_NOEXCEPT; + +// Class representing an aligned array and its length +template::value> +class aligned_array { + std::size_t length; + T* ptr; + +public: + aligned_array() + : length(0), ptr(nullptr) {} + aligned_array(std::nullptr_t) + : length(0), ptr(nullptr) {} + explicit aligned_array(std::size_t length_) + : length(length_) + { + ptr = static_cast(aligned_alloc(length * sizeof(T), Align)); + std::size_t i; + LIBASYNC_TRY { + for (i = 0; i < length; i++) + new(ptr + i) T; + } LIBASYNC_CATCH(...) { + for (std::size_t j = 0; j < i; j++) + ptr[i].~T(); + aligned_free(ptr); + LIBASYNC_RETHROW(); + } + } + aligned_array(aligned_array&& other) LIBASYNC_NOEXCEPT + : length(other.length), ptr(other.ptr) + { + other.ptr = nullptr; + other.length = 0; + } + aligned_array& operator=(aligned_array&& other) LIBASYNC_NOEXCEPT + { + aligned_array(std::move(*this)); + std::swap(ptr, other.ptr); + std::swap(length, other.length); + return *this; + } + aligned_array& operator=(std::nullptr_t) + { + return *this = aligned_array(); + } + ~aligned_array() + { + for (std::size_t i = 0; i < length; i++) + ptr[i].~T(); + aligned_free(ptr); + } + + T& operator[](std::size_t i) const + { + return ptr[i]; + } + std::size_t size() const + { + return length; + } + T* get() const + { + return ptr; + } + explicit operator bool() const + { + return ptr != nullptr; + } +}; + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/include/async++/cancel.h b/3party/asyncplusplus/include/async++/cancel.h new file mode 100644 index 0000000..36d0307 --- /dev/null +++ b/3party/asyncplusplus/include/async++/cancel.h @@ -0,0 +1,68 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { + +// Exception thrown by cancel_current_task() +struct LIBASYNC_EXPORT_EXCEPTION task_canceled {}; + +// A flag which can be used to request cancellation +class cancellation_token { + std::atomic state; + +public: + cancellation_token() + : state(false) {} + + // Non-copyable and non-movable + cancellation_token(const cancellation_token&) = delete; + cancellation_token& operator=(const cancellation_token&) = delete; + + bool is_canceled() const + { + bool s = state.load(std::memory_order_relaxed); + if (s) + std::atomic_thread_fence(std::memory_order_acquire); + return s; + } + + void cancel() + { + state.store(true, std::memory_order_release); + } + + void reset() + { + state.store(false, std::memory_order_relaxed); + } +}; + +// Interruption point, throws task_canceled if the specified token is set. +inline void interruption_point(const cancellation_token& token) +{ + if (token.is_canceled()) + LIBASYNC_THROW(task_canceled()); +} + +} // namespace async diff --git a/3party/asyncplusplus/include/async++/continuation_vector.h b/3party/asyncplusplus/include/async++/continuation_vector.h new file mode 100644 index 0000000..f0d9095 --- /dev/null +++ b/3party/asyncplusplus/include/async++/continuation_vector.h @@ -0,0 +1,225 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Compress the flags in the low bits of the pointer if the structures are +// suitably aligned. Fall back to a separate flags variable otherwise. +template +class compressed_ptr { + void* ptr; + std::uintptr_t flags; + +public: + compressed_ptr() = default; + compressed_ptr(void* ptr_, std::uintptr_t flags_) + : ptr(ptr_), flags(flags_) {} + + template + T* get_ptr() const + { + return static_cast(ptr); + } + std::uintptr_t get_flags() const + { + return flags; + } + + void set_ptr(void* p) + { + ptr = p; + } + void set_flags(std::uintptr_t f) + { + flags = f; + } +}; +template +class compressed_ptr { + std::uintptr_t data; + +public: + compressed_ptr() = default; + compressed_ptr(void* ptr_, std::uintptr_t flags_) + : data(reinterpret_cast(ptr_) | flags_) {} + + template + T* get_ptr() const + { + return reinterpret_cast(data & ~Mask); + } + std::uintptr_t get_flags() const + { + return data & Mask; + } + + void set_ptr(void* p) + { + data = reinterpret_cast(p) | (data & Mask); + } + void set_flags(std::uintptr_t f) + { + data = (data & ~Mask) | f; + } +}; + +// Thread-safe vector of task_ptr which is optimized for the common case of +// only having a single continuation. +class continuation_vector { + // Heap-allocated data for the slow path + struct vector_data { + std::vector vector; + std::mutex lock; + }; + + // Flags to describe the state of the vector + enum flags { + // If set, no more changes are allowed to internal_data + is_locked = 1, + + // If set, the pointer is a vector_data* instead of a task_base*. If + // there are 0 or 1 elements in the vector, the task_base* form is used. + is_vector = 2 + }; + static const std::uintptr_t flags_mask = 3; + + // Embed the two bits in the data if they are suitably aligned. We only + // check the alignment of vector_data here because task_base isn't defined + // yet. Since we align task_base to LIBASYNC_CACHELINE_SIZE just use that. + typedef compressed_ptr::value & flags_mask) == 0> internal_data; + + // All changes to the internal data are atomic + std::atomic atomic_data; + +public: + // Start unlocked with zero elements in the fast path + continuation_vector() + { + // Workaround for a bug in certain versions of clang with libc++ + // error: no viable conversion from 'async::detail::compressed_ptr<3, true>' to '_Atomic(async::detail::compressed_ptr<3, true>)' + atomic_data.store(internal_data(nullptr, 0), std::memory_order_relaxed); + } + + // Free any left over data + ~continuation_vector() + { + // Converting to task_ptr instead of using remove_ref because task_base + // isn't defined yet at this point. + internal_data data = atomic_data.load(std::memory_order_relaxed); + if (data.get_flags() & flags::is_vector) { + // No need to lock the mutex, we are the only thread at this point + for (task_base* i: data.get_ptr()->vector) + (task_ptr(i)); + delete data.get_ptr(); + } else { + // If the data is locked then the inline pointer is already gone + if (!(data.get_flags() & flags::is_locked)) + task_ptr tmp(data.get_ptr()); + } + } + + // Try adding an element to the vector. This fails and returns false if + // the vector has been locked. In that case t is not modified. + bool try_add(task_ptr&& t) + { + // Cache to avoid re-allocating vector_data multiple times. This is + // automatically freed if it is not successfully saved to atomic_data. + std::unique_ptr vector; + + // Compare-exchange loop on atomic_data + internal_data data = atomic_data.load(std::memory_order_relaxed); + internal_data new_data; + do { + // Return immediately if the vector is locked + if (data.get_flags() & flags::is_locked) + return false; + + if (data.get_flags() & flags::is_vector) { + // Larger vectors use a mutex, so grab the lock + std::atomic_thread_fence(std::memory_order_acquire); + std::lock_guard locked(data.get_ptr()->lock); + + // We need to check again if the vector has been locked here + // to avoid a race condition with flush_and_lock + if (atomic_data.load(std::memory_order_relaxed).get_flags() & flags::is_locked) + return false; + + // Add the element to the vector and return + data.get_ptr()->vector.push_back(t.release()); + return true; + } else { + if (data.get_ptr()) { + // Going from 1 to 2 elements, allocate a vector_data + if (!vector) + vector.reset(new vector_data{{data.get_ptr(), t.get()}, {}}); + new_data = {vector.get(), flags::is_vector}; + } else { + // Going from 0 to 1 elements + new_data = {t.get(), 0}; + } + } + } while (!atomic_data.compare_exchange_weak(data, new_data, std::memory_order_release, std::memory_order_relaxed)); + + // If we reach this point then atomic_data was successfully changed. + // Since the pointers are now saved in the vector, release them from + // the smart pointers. + t.release(); + vector.release(); + return true; + } + + // Lock the vector and flush all elements through the given function + template void flush_and_lock(Func&& func) + { + // Try to lock the vector using a compare-exchange loop + internal_data data = atomic_data.load(std::memory_order_relaxed); + internal_data new_data; + do { + new_data = data; + new_data.set_flags(data.get_flags() | flags::is_locked); + } while (!atomic_data.compare_exchange_weak(data, new_data, std::memory_order_acquire, std::memory_order_relaxed)); + + if (data.get_flags() & flags::is_vector) { + // If we are using vector_data, lock it and flush all elements + std::lock_guard locked(data.get_ptr()->lock); + for (auto i: data.get_ptr()->vector) + func(task_ptr(i)); + + // Clear the vector to save memory. Note that we don't actually free + // the vector_data here because other threads may still be using it. + // This isn't a very significant cost since multiple continuations + // are relatively rare. + data.get_ptr()->vector.clear(); + } else { + // If there is an inline element, just pass it on + if (data.get_ptr()) + func(task_ptr(data.get_ptr())); + } + } +}; + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/include/async++/parallel_for.h b/3party/asyncplusplus/include/async++/parallel_for.h new file mode 100644 index 0000000..4fe5268 --- /dev/null +++ b/3party/asyncplusplus/include/async++/parallel_for.h @@ -0,0 +1,77 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Internal implementation of parallel_for that only accepts a partitioner +// argument. +template +void internal_parallel_for(Sched& sched, Partitioner partitioner, const Func& func) +{ + // Split the partition, run inline if no more splits are possible + auto subpart = partitioner.split(); + if (subpart.begin() == subpart.end()) { + for (auto&& i: partitioner) + func(std::forward(i)); + return; + } + + // Run the function over each half in parallel + auto&& t = async::local_spawn(sched, [&sched, &subpart, &func] { + detail::internal_parallel_for(sched, std::move(subpart), func); + }); + detail::internal_parallel_for(sched, std::move(partitioner), func); + t.get(); +} + +} // namespace detail + +// Run a function for each element in a range +template +void parallel_for(Sched& sched, Range&& range, const Func& func) +{ + detail::internal_parallel_for(sched, async::to_partitioner(std::forward(range)), func); +} + +// Overload with default scheduler +template +void parallel_for(Range&& range, const Func& func) +{ + async::parallel_for(::async::default_scheduler(), range, func); +} + +// Overloads with std::initializer_list +template +void parallel_for(Sched& sched, std::initializer_list range, const Func& func) +{ + async::parallel_for(sched, async::make_range(range.begin(), range.end()), func); +} +template +void parallel_for(std::initializer_list range, const Func& func) +{ + async::parallel_for(async::make_range(range.begin(), range.end()), func); +} + +} // namespace async diff --git a/3party/asyncplusplus/include/async++/parallel_invoke.h b/3party/asyncplusplus/include/async++/parallel_invoke.h new file mode 100644 index 0000000..0734bb6 --- /dev/null +++ b/3party/asyncplusplus/include/async++/parallel_invoke.h @@ -0,0 +1,70 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Recursively split the arguments so tasks are spawned in parallel +template +struct parallel_invoke_internal { + template + static void run(Sched& sched, const Tuple& args) + { + auto&& t = async::local_spawn(sched, [&sched, &args] { + parallel_invoke_internal::run(sched, args); + }); + parallel_invoke_internal::run(sched, args); + t.get(); + } +}; +template +struct parallel_invoke_internal { + template + static void run(Sched&, const Tuple& args) + { + // Make sure to preserve the rvalue/lvalue-ness of the original parameter + std::forward::type>(std::get(args))(); + } +}; +template +struct parallel_invoke_internal { + template + static void run(Sched&, const Tuple&) {} +}; + +} // namespace detail + +// Run several functions in parallel, optionally using the specified scheduler. +template +typename std::enable_if::value>::type parallel_invoke(Sched& sched, Args&&... args) +{ + detail::parallel_invoke_internal<0, sizeof...(Args)>::run(sched, std::forward_as_tuple(std::forward(args)...)); +} +template +void parallel_invoke(Args&&... args) +{ + async::parallel_invoke(::async::default_scheduler(), std::forward(args)...); +} + +} // namespace async diff --git a/3party/asyncplusplus/include/async++/parallel_reduce.h b/3party/asyncplusplus/include/async++/parallel_reduce.h new file mode 100644 index 0000000..06545f6 --- /dev/null +++ b/3party/asyncplusplus/include/async++/parallel_reduce.h @@ -0,0 +1,109 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Default map function which simply passes its parameter through unmodified +struct default_map { + template + T&& operator()(T&& x) const + { + return std::forward(x); + } +}; + +// Internal implementation of parallel_map_reduce that only accepts a +// partitioner argument. +template +Result internal_parallel_map_reduce(Sched& sched, Partitioner partitioner, Result init, const MapFunc& map, const ReduceFunc& reduce) +{ + // Split the partition, run inline if no more splits are possible + auto subpart = partitioner.split(); + if (subpart.begin() == subpart.end()) { + Result out = init; + for (auto&& i: partitioner) + out = reduce(std::move(out), map(std::forward(i))); + return out; + } + + // Run the function over each half in parallel + auto&& t = async::local_spawn(sched, [&sched, &subpart, init, &map, &reduce] { + return detail::internal_parallel_map_reduce(sched, std::move(subpart), init, map, reduce); + }); + Result out = detail::internal_parallel_map_reduce(sched, std::move(partitioner), init, map, reduce); + return reduce(std::move(out), t.get()); +} + +} // namespace detail + +// Run a function for each element in a range and then reduce the results of that function to a single value +template +Result parallel_map_reduce(Sched& sched, Range&& range, Result init, const MapFunc& map, const ReduceFunc& reduce) +{ + return detail::internal_parallel_map_reduce(sched, async::to_partitioner(std::forward(range)), init, map, reduce); +} + +// Overload with default scheduler +template +Result parallel_map_reduce(Range&& range, Result init, const MapFunc& map, const ReduceFunc& reduce) +{ + return async::parallel_map_reduce(::async::default_scheduler(), range, init, map, reduce); +} + +// Overloads with std::initializer_list +template +Result parallel_map_reduce(Sched& sched, std::initializer_list range, Result init, const MapFunc& map, const ReduceFunc& reduce) +{ + return async::parallel_map_reduce(sched, async::make_range(range.begin(), range.end()), init, map, reduce); +} +template +Result parallel_map_reduce(std::initializer_list range, Result init, const MapFunc& map, const ReduceFunc& reduce) +{ + return async::parallel_map_reduce(async::make_range(range.begin(), range.end()), init, map, reduce); +} + +// Variant with identity map operation +template +Result parallel_reduce(Sched& sched, Range&& range, Result init, const ReduceFunc& reduce) +{ + return async::parallel_map_reduce(sched, range, init, detail::default_map(), reduce); +} +template +Result parallel_reduce(Range&& range, Result init, const ReduceFunc& reduce) +{ + return async::parallel_reduce(::async::default_scheduler(), range, init, reduce); +} +template +Result parallel_reduce(Sched& sched, std::initializer_list range, Result init, const ReduceFunc& reduce) +{ + return async::parallel_reduce(sched, async::make_range(range.begin(), range.end()), init, reduce); +} +template +Result parallel_reduce(std::initializer_list range, Result init, const ReduceFunc& reduce) +{ + return async::parallel_reduce(async::make_range(range.begin(), range.end()), init, reduce); +} + +} // namespace async diff --git a/3party/asyncplusplus/include/async++/partitioner.h b/3party/asyncplusplus/include/async++/partitioner.h new file mode 100644 index 0000000..f6403bd --- /dev/null +++ b/3party/asyncplusplus/include/async++/partitioner.h @@ -0,0 +1,196 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Partitioners are essentially ranges with an extra split() function. The +// split() function returns a partitioner containing a range to be executed in a +// child task and modifies the parent partitioner's range to represent the rest +// of the original range. If the range cannot be split any more then split() +// should return an empty range. + +// Detect whether a range is a partitioner +template().split())> +two& is_partitioner_helper(int); +template +one& is_partitioner_helper(...); +template +struct is_partitioner: public std::integral_constant(0)) - 1> {}; + +// Automatically determine a grain size for a sequence length +inline std::size_t auto_grain_size(std::size_t dist) +{ + // Determine the grain size automatically using a heuristic + std::size_t grain = dist / (8 * hardware_concurrency()); + if (grain < 1) + grain = 1; + if (grain > 2048) + grain = 2048; + return grain; +} + +template +class static_partitioner_impl { + Iter iter_begin, iter_end; + std::size_t grain; + +public: + static_partitioner_impl(Iter begin_, Iter end_, std::size_t grain_) + : iter_begin(begin_), iter_end(end_), grain(grain_) {} + Iter begin() const + { + return iter_begin; + } + Iter end() const + { + return iter_end; + } + static_partitioner_impl split() + { + // Don't split if below grain size + std::size_t length = std::distance(iter_begin, iter_end); + static_partitioner_impl out(iter_end, iter_end, grain); + if (length <= grain) + return out; + + // Split our range in half + iter_end = iter_begin; + std::advance(iter_end, (length + 1) / 2); + out.iter_begin = iter_end; + return out; + } +}; + +template +class auto_partitioner_impl { + Iter iter_begin, iter_end; + std::size_t grain; + std::size_t num_threads; + std::thread::id last_thread; + +public: + // thread_id is initialized to "no thread" and will be set on first split + auto_partitioner_impl(Iter begin_, Iter end_, std::size_t grain_) + : iter_begin(begin_), iter_end(end_), grain(grain_) {} + Iter begin() const + { + return iter_begin; + } + Iter end() const + { + return iter_end; + } + auto_partitioner_impl split() + { + // Don't split if below grain size + std::size_t length = std::distance(iter_begin, iter_end); + auto_partitioner_impl out(iter_end, iter_end, grain); + if (length <= grain) + return out; + + // Check if we are in a different thread than we were before + std::thread::id current_thread = std::this_thread::get_id(); + if (current_thread != last_thread) + num_threads = hardware_concurrency(); + + // If we only have one thread, don't split + if (num_threads <= 1) + return out; + + // Split our range in half + iter_end = iter_begin; + std::advance(iter_end, (length + 1) / 2); + out.iter_begin = iter_end; + out.last_thread = current_thread; + last_thread = current_thread; + out.num_threads = num_threads / 2; + num_threads -= out.num_threads; + return out; + } +}; + +} // namespace detail + +// A simple partitioner which splits until a grain size is reached. If a grain +// size is not specified, one is chosen automatically. +template +detail::static_partitioner_impl()))> static_partitioner(Range&& range, std::size_t grain) +{ + return {std::begin(range), std::end(range), grain}; +} +template +detail::static_partitioner_impl()))> static_partitioner(Range&& range) +{ + std::size_t grain = detail::auto_grain_size(std::distance(std::begin(range), std::end(range))); + return {std::begin(range), std::end(range), grain}; +} + +// A more advanced partitioner which initially divides the range into one chunk +// for each available thread. The range is split further if a chunk gets stolen +// by a different thread. +template +detail::auto_partitioner_impl()))> auto_partitioner(Range&& range) +{ + std::size_t grain = detail::auto_grain_size(std::distance(std::begin(range), std::end(range))); + return {std::begin(range), std::end(range), grain}; +} + +// Wrap a range in a partitioner. If the input is already a partitioner then it +// is returned unchanged. This allows parallel algorithms to accept both ranges +// and partitioners as parameters. +template +typename std::enable_if::type>::value, Partitioner&&>::type to_partitioner(Partitioner&& partitioner) +{ + return std::forward(partitioner); +} +template +typename std::enable_if::type>::value, detail::auto_partitioner_impl()))>>::type to_partitioner(Range&& range) +{ + return async::auto_partitioner(std::forward(range)); +} + +// Overloads with std::initializer_list +template +detail::static_partitioner_impl>().begin())> static_partitioner(std::initializer_list range) +{ + return async::static_partitioner(async::make_range(range.begin(), range.end())); +} +template +detail::static_partitioner_impl>().begin())> static_partitioner(std::initializer_list range, std::size_t grain) +{ + return async::static_partitioner(async::make_range(range.begin(), range.end()), grain); +} +template +detail::auto_partitioner_impl>().begin())> auto_partitioner(std::initializer_list range) +{ + return async::auto_partitioner(async::make_range(range.begin(), range.end())); +} +template +detail::auto_partitioner_impl>().begin())> to_partitioner(std::initializer_list range) +{ + return async::auto_partitioner(async::make_range(range.begin(), range.end())); +} + +} // namespace async diff --git a/3party/asyncplusplus/include/async++/range.h b/3party/asyncplusplus/include/async++/range.h new file mode 100644 index 0000000..d23ee0c --- /dev/null +++ b/3party/asyncplusplus/include/async++/range.h @@ -0,0 +1,182 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { + +// Range type representing a pair of iterators +template +class range { + Iter iter_begin, iter_end; + +public: + range() = default; + range(Iter a, Iter b) + : iter_begin(a), iter_end(b) {} + + Iter begin() const + { + return iter_begin; + } + Iter end() const + { + return iter_end; + } +}; + +// Construct a range from 2 iterators +template +range make_range(Iter begin, Iter end) +{ + return {begin, end}; +} + +// A range of integers +template +class int_range { + T value_begin, value_end; + + static_assert(std::is_integral::value, "int_range can only be used with integral types"); + +public: + class iterator { + T current; + + explicit iterator(T a) + : current(a) {} + friend class int_range; + + public: + typedef T value_type; + typedef std::ptrdiff_t difference_type; + typedef iterator pointer; + typedef T reference; + typedef std::random_access_iterator_tag iterator_category; + + iterator() = default; + + T operator*() const + { + return current; + } + T operator[](difference_type offset) const + { + return current + offset; + } + + iterator& operator++() + { + ++current; + return *this; + } + iterator operator++(int) + { + return iterator(current++); + } + iterator& operator--() + { + --current; + return *this; + } + iterator operator--(int) + { + return iterator(current--); + } + + iterator& operator+=(difference_type offset) + { + current += offset; + return *this; + } + iterator& operator-=(difference_type offset) + { + current -= offset; + return *this; + } + + iterator operator+(difference_type offset) const + { + return iterator(current + offset); + } + iterator operator-(difference_type offset) const + { + return iterator(current - offset); + } + + friend iterator operator+(difference_type offset, iterator other) + { + return other + offset; + } + + friend difference_type operator-(iterator a, iterator b) + { + return a.current - b.current; + } + + friend bool operator==(iterator a, iterator b) + { + return a.current == b.current; + } + friend bool operator!=(iterator a, iterator b) + { + return a.current != b.current; + } + friend bool operator>(iterator a, iterator b) + { + return a.current > b.current; + } + friend bool operator<(iterator a, iterator b) + { + return a.current < b.current; + } + friend bool operator>=(iterator a, iterator b) + { + return a.current >= b.current; + } + friend bool operator<=(iterator a, iterator b) + { + return a.current <= b.current; + } + }; + + int_range(T begin, T end) + : value_begin(begin), value_end(end) {} + + iterator begin() const + { + return iterator(value_begin); + } + iterator end() const + { + return iterator(value_end); + } +}; + +// Construct an int_range between 2 values +template +int_range::type> irange(T begin, U end) +{ + return {begin, end}; +} + +} // namespace async diff --git a/3party/asyncplusplus/include/async++/ref_count.h b/3party/asyncplusplus/include/async++/ref_count.h new file mode 100644 index 0000000..5eb1185 --- /dev/null +++ b/3party/asyncplusplus/include/async++/ref_count.h @@ -0,0 +1,177 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Default deleter which just uses the delete keyword +template +struct default_deleter { + static void do_delete(T* p) + { + delete p; + } +}; + +// Reference-counted object base class +template> +struct ref_count_base { + std::atomic ref_count; + + // By default the reference count is initialized to 1 + explicit ref_count_base(std::size_t count = 1) + : ref_count(count) {} + + void add_ref(std::size_t count = 1) + { + ref_count.fetch_add(count, std::memory_order_relaxed); + } + void remove_ref(std::size_t count = 1) + { + if (ref_count.fetch_sub(count, std::memory_order_release) == count) { + std::atomic_thread_fence(std::memory_order_acquire); + Deleter::do_delete(static_cast(this)); + } + } + void add_ref_unlocked() + { + ref_count.store(ref_count.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed); + } + bool is_unique_ref(std::memory_order order) + { + return ref_count.load(order) == 1; + } +}; + +// Pointer to reference counted object, based on boost::intrusive_ptr +template +class ref_count_ptr { + T* p; + +public: + // Note that this doesn't increment the reference count, instead it takes + // ownership of a pointer which you already own a reference to. + explicit ref_count_ptr(T* t) + : p(t) {} + + ref_count_ptr() + : p(nullptr) {} + ref_count_ptr(std::nullptr_t) + : p(nullptr) {} + ref_count_ptr(const ref_count_ptr& other) LIBASYNC_NOEXCEPT + : p(other.p) + { + if (p) + p->add_ref(); + } + ref_count_ptr(ref_count_ptr&& other) LIBASYNC_NOEXCEPT + : p(other.p) + { + other.p = nullptr; + } + ref_count_ptr& operator=(std::nullptr_t) + { + if (p) + p->remove_ref(); + p = nullptr; + return *this; + } + ref_count_ptr& operator=(const ref_count_ptr& other) LIBASYNC_NOEXCEPT + { + if (p) { + p->remove_ref(); + p = nullptr; + } + p = other.p; + if (p) + p->add_ref(); + return *this; + } + ref_count_ptr& operator=(ref_count_ptr&& other) LIBASYNC_NOEXCEPT + { + if (p) { + p->remove_ref(); + p = nullptr; + } + p = other.p; + other.p = nullptr; + return *this; + } + ~ref_count_ptr() + { + if (p) + p->remove_ref(); + } + + T& operator*() const + { + return *p; + } + T* operator->() const + { + return p; + } + T* get() const + { + return p; + } + T* release() + { + T* out = p; + p = nullptr; + return out; + } + + explicit operator bool() const + { + return p != nullptr; + } + friend bool operator==(const ref_count_ptr& a, const ref_count_ptr& b) + { + return a.p == b.p; + } + friend bool operator!=(const ref_count_ptr& a, const ref_count_ptr& b) + { + return a.p != b.p; + } + friend bool operator==(const ref_count_ptr& a, std::nullptr_t) + { + return a.p == nullptr; + } + friend bool operator!=(const ref_count_ptr& a, std::nullptr_t) + { + return a.p != nullptr; + } + friend bool operator==(std::nullptr_t, const ref_count_ptr& a) + { + return a.p == nullptr; + } + friend bool operator!=(std::nullptr_t, const ref_count_ptr& a) + { + return a.p != nullptr; + } +}; + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/include/async++/scheduler.h b/3party/asyncplusplus/include/async++/scheduler.h new file mode 100644 index 0000000..6718095 --- /dev/null +++ b/3party/asyncplusplus/include/async++/scheduler.h @@ -0,0 +1,175 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { + +// Improved version of std::hardware_concurrency: +// - It never returns 0, 1 is returned instead. +// - It is guaranteed to remain constant for the duration of the program. +LIBASYNC_EXPORT std::size_t hardware_concurrency() LIBASYNC_NOEXCEPT; + +// Task handle used by a wait handler +class task_wait_handle { + detail::task_base* handle; + + // Allow construction in wait_for_task() + friend LIBASYNC_EXPORT void detail::wait_for_task(detail::task_base* t); + task_wait_handle(detail::task_base* t) + : handle(t) {} + + // Execution function for use by wait handlers + template + struct wait_exec_func: private detail::func_base { + template + explicit wait_exec_func(F&& f) + : detail::func_base(std::forward(f)) {} + void operator()(detail::task_base*) + { + // Just call the function directly, all this wrapper does is remove + // the task_base* parameter. + this->get_func()(); + } + }; + +public: + task_wait_handle() + : handle(nullptr) {} + + // Check if the handle is valid + explicit operator bool() const + { + return handle != nullptr; + } + + // Check if the task has finished executing + bool ready() const + { + return detail::is_finished(handle->state.load(std::memory_order_acquire)); + } + + // Queue a function to be executed when the task has finished executing. + template + void on_finish(Func&& func) + { + // Make sure the function type is callable + static_assert(detail::is_callable::value, "Invalid function type passed to on_finish()"); + + auto cont = new detail::task_func::type, wait_exec_func::type>, detail::fake_void>(std::forward(func)); + cont->sched = std::addressof(inline_scheduler()); + handle->add_continuation(inline_scheduler(), detail::task_ptr(cont)); + } +}; + +// Wait handler function prototype +typedef void (*wait_handler)(task_wait_handle t); + +// Set a wait handler to control what a task does when it has "free time", which +// is when it is waiting for another task to complete. The wait handler can do +// other work, but should return when it detects that the task has completed. +// The previously installed handler is returned. +LIBASYNC_EXPORT wait_handler set_thread_wait_handler(wait_handler w) LIBASYNC_NOEXCEPT; + +// Exception thrown if a task_run_handle is destroyed without being run +struct LIBASYNC_EXPORT_EXCEPTION task_not_executed {}; + +// Task handle used in scheduler, acts as a unique_ptr to a task object +class task_run_handle { + detail::task_ptr handle; + + // Allow construction in schedule_task() + template + friend void detail::schedule_task(Sched& sched, detail::task_ptr t); + explicit task_run_handle(detail::task_ptr t) + : handle(std::move(t)) {} + +public: + // Movable but not copyable + task_run_handle() = default; + task_run_handle(task_run_handle&& other) LIBASYNC_NOEXCEPT + : handle(std::move(other.handle)) {} + task_run_handle& operator=(task_run_handle&& other) LIBASYNC_NOEXCEPT + { + handle = std::move(other.handle); + return *this; + } + + // If the task is not executed, cancel it with an exception + ~task_run_handle() + { + if (handle) + handle->vtable->cancel(handle.get(), std::make_exception_ptr(task_not_executed())); + } + + // Check if the handle is valid + explicit operator bool() const + { + return handle != nullptr; + } + + // Run the task and release the handle + void run() + { + handle->vtable->run(handle.get()); + handle = nullptr; + } + + // Run the task but run the given wait handler when waiting for a task, + // instead of just sleeping. + void run_with_wait_handler(wait_handler handler) + { + wait_handler old = set_thread_wait_handler(handler); + run(); + set_thread_wait_handler(old); + } + + // Conversion to and from void pointer. This allows the task handle to be + // sent through C APIs which don't preserve types. + void* to_void_ptr() + { + return handle.release(); + } + static task_run_handle from_void_ptr(void* ptr) + { + return task_run_handle(detail::task_ptr(static_cast(ptr))); + } +}; + +namespace detail { + +// Schedule a task for execution using its scheduler +template +void schedule_task(Sched& sched, task_ptr t) +{ + static_assert(is_scheduler::value, "Type is not a valid scheduler"); + sched.schedule(task_run_handle(std::move(t))); +} + +// Inline scheduler implementation +inline void inline_scheduler_impl::schedule(task_run_handle t) +{ + t.run(); +} + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/include/async++/scheduler_fwd.h b/3party/asyncplusplus/include/async++/scheduler_fwd.h new file mode 100644 index 0000000..8b41c58 --- /dev/null +++ b/3party/asyncplusplus/include/async++/scheduler_fwd.h @@ -0,0 +1,164 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +#error "Do not include this header directly, include instead." +#endif + +namespace async { + +// Forward declarations +class task_run_handle; +class threadpool_scheduler; + +// Scheduler interface: +// A scheduler is any type that implements this function: +// void schedule(async::task_run_handle t); +// This function should result in t.run() being called at some future point. + +namespace detail { + +// Detect whether an object is a scheduler +template().schedule(std::declval()))> +two &is_scheduler_helper(int); +template +one &is_scheduler_helper(...); + +template +struct is_scheduler : public std::integral_constant(0)) - 1> {}; + +// Singleton scheduler classes +class thread_scheduler_impl { +public: + LIBASYNC_EXPORT static void schedule(task_run_handle t); +}; + +class inline_scheduler_impl { +public: + static void schedule(task_run_handle t); +}; + +// Reference counted pointer to task data +struct task_base; +typedef ref_count_ptr task_ptr; + +// Helper function to schedule a task using a scheduler +template +void schedule_task(Sched &sched, task_ptr t); + +// Wait for the given task to finish. This will call the wait handler currently +// active for this thread, which causes the thread to sleep by default. +#ifndef LIBASYNC_CUSTOM_WAIT_FOR_TASK +LIBASYNC_EXPORT void wait_for_task(task_base *wait_task); +#endif + +// Forward-declaration for data used by threadpool_scheduler +struct threadpool_data; + +}// namespace detail + +// Run a task in the current thread as soon as it is scheduled +inline detail::inline_scheduler_impl & +inline_scheduler() +{ + static detail::inline_scheduler_impl instance; + return instance; +} + +// Run a task in a separate thread. Note that this scheduler does not wait for +// threads to finish at process exit. You must ensure that all threads finish +// before ending the process. +inline detail::thread_scheduler_impl & +thread_scheduler() +{ + static detail::thread_scheduler_impl instance; + return instance; +} + +// Built-in thread pool scheduler with a size that is configurable from the +// LIBASYNC_NUM_THREADS environment variable. If that variable does not exist +// then the number of CPUs in the system is used instead. +LIBASYNC_EXPORT threadpool_scheduler &default_threadpool_scheduler(); + +// Default scheduler that is used when one isn't specified. This defaults to +// default_threadpool_scheduler(), but can be overriden by defining +// LIBASYNC_CUSTOM_DEFAULT_SCHEDULER before including async++.h. Keep in mind +// that in that case async::default_scheduler should be declared before +// including async++.h. + +#ifndef LIBASYNC_CUSTOM_DEFAULT_SCHEDULER +inline threadpool_scheduler & +default_scheduler() +{ + return default_threadpool_scheduler(); +} +#endif + +// Scheduler that holds a list of tasks which can then be explicitly executed +// by a thread. Both adding and running tasks are thread-safe operations. +class fifo_scheduler { + struct internal_data; + std::unique_ptr impl; + +public: + LIBASYNC_EXPORT fifo_scheduler(); + LIBASYNC_EXPORT ~fifo_scheduler(); + + // Add a task to the queue + LIBASYNC_EXPORT void schedule(task_run_handle t); + + // Try running one task from the queue. Returns false if the queue was empty. + LIBASYNC_EXPORT bool try_run_one_task(); + + // Run all tasks in the queue + LIBASYNC_EXPORT void run_all_tasks(); +}; + +// Scheduler that runs tasks in a work-stealing thread pool of the given size. +// Note that destroying the thread pool before all tasks have completed may +// result in some tasks not being executed. +class threadpool_scheduler { + std::unique_ptr impl; + +public: + LIBASYNC_EXPORT threadpool_scheduler(threadpool_scheduler &&other); + + // Create a thread pool with the given number of threads + LIBASYNC_EXPORT threadpool_scheduler(std::size_t num_threads); + + // Create a thread pool with the given number of threads. Call `prerun` + // function before execution loop and `postrun` after. + LIBASYNC_EXPORT + threadpool_scheduler(std::size_t num_threads, std::function &&prerun_, std::function &&postrun_); + + // Destroy the thread pool, tasks that haven't been started are dropped + LIBASYNC_EXPORT ~threadpool_scheduler(); + + // Schedule a task to be run in the thread pool + LIBASYNC_EXPORT void schedule(task_run_handle t); +}; + +namespace detail { + +// Work-around for Intel compiler handling decltype poorly in function returns +typedef std::remove_reference::type default_scheduler_type; + +}// namespace detail +}// namespace async diff --git a/3party/asyncplusplus/include/async++/task.h b/3party/asyncplusplus/include/async++/task.h new file mode 100644 index 0000000..7735c30 --- /dev/null +++ b/3party/asyncplusplus/include/async++/task.h @@ -0,0 +1,579 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { + +// Exception thrown when an event_task is destroyed without setting a value +struct LIBASYNC_EXPORT_EXCEPTION abandoned_event_task {}; + +namespace detail { + +// Common code for task and shared_task +template +class basic_task { + // Reference counted internal task object + detail::task_ptr internal_task; + + // Real result type, with void turned into fake_void + typedef typename void_to_fake_void::type internal_result; + + // Type-specific task object + typedef task_result internal_task_type; + + // Friend access + friend async::task; + friend async::shared_task; + template + friend typename T::internal_task_type* get_internal_task(const T& t); + template + friend void set_internal_task(T& t, task_ptr p); + + // Common code for get() + void get_internal() const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty task object"); + + // If the task was canceled, throw the associated exception + get_internal_task(*this)->wait_and_throw(); + } + + // Common code for then() + template + typename continuation_traits::task_type then_internal(Sched& sched, Func&& f, Parent&& parent) const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty task object"); + + // Save a copy of internal_task because it might get moved into exec_func + task_base* my_internal = internal_task.get(); + + // Create continuation + typedef continuation_traits traits; + typedef typename void_to_fake_void::type cont_internal_result; + typedef continuation_exec_func::type, cont_internal_result, typename traits::decay_func, typename traits::is_value_cont, is_task::value> exec_func; + typename traits::task_type cont; + set_internal_task(cont, task_ptr(new task_func(std::forward(f), std::forward(parent)))); + + // Add the continuation to this task + // Avoid an expensive ref-count modification since the task isn't shared yet + get_internal_task(cont)->add_ref_unlocked(); + get_internal_task(cont)->sched = std::addressof(sched); + my_internal->add_continuation(sched, task_ptr(get_internal_task(cont))); + + return cont; + } + +public: + // Task result type + typedef Result result_type; + + // Check if this task is not empty + bool valid() const + { + return internal_task != nullptr; + } + + // Query whether the task has finished executing + bool ready() const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty task object"); + return internal_task->ready(); + } + + // Query whether the task has been canceled with an exception + bool canceled() const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty task object"); + return internal_task->state.load(std::memory_order_acquire) == task_state::canceled; + } + + // Wait for the task to complete + void wait() const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty task object"); + internal_task->wait(); + } + + // Get the exception associated with a canceled task + std::exception_ptr get_exception() const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty task object"); + if (internal_task->wait() == task_state::canceled) + return get_internal_task(*this)->get_exception(); + else + return std::exception_ptr(); + } +}; + +// Common code for event_task specializations +template +class basic_event { + // Reference counted internal task object + detail::task_ptr internal_task; + + // Real result type, with void turned into fake_void + typedef typename detail::void_to_fake_void::type internal_result; + + // Type-specific task object + typedef detail::task_result internal_task_type; + + // Friend access + friend async::event_task; + template + friend typename T::internal_task_type* get_internal_task(const T& t); + + // Common code for set() + template + bool set_internal(T&& result) const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty event_task object"); + + // Only allow setting the value once + detail::task_state expected = detail::task_state::pending; + if (!internal_task->state.compare_exchange_strong(expected, detail::task_state::locked, std::memory_order_acquire, std::memory_order_relaxed)) + return false; + + LIBASYNC_TRY { + // Store the result and finish + get_internal_task(*this)->set_result(std::forward(result)); + internal_task->finish(); + } LIBASYNC_CATCH(...) { + // At this point we have already committed to setting a value, so + // we can't return the exception to the caller. If we did then it + // could cause concurrent set() calls to fail, thinking a value has + // already been set. Instead, we simply cancel the task with the + // exception we just got. + get_internal_task(*this)->cancel_base(std::current_exception()); + } + return true; + } + +public: + // Movable but not copyable + basic_event(basic_event&& other) LIBASYNC_NOEXCEPT + : internal_task(std::move(other.internal_task)) {} + basic_event& operator=(basic_event&& other) LIBASYNC_NOEXCEPT + { + internal_task = std::move(other.internal_task); + return *this; + } + + // Main constructor + basic_event() + : internal_task(new internal_task_type) + { + internal_task->event_task_got_task = false; + } + + // Cancel events if they are destroyed before they are set + ~basic_event() + { + // This check isn't thread-safe but set_exception does a proper check + if (internal_task && !internal_task->ready() && !internal_task->is_unique_ref(std::memory_order_relaxed)) { +#ifdef LIBASYNC_NO_EXCEPTIONS + // This will result in an abort if the task result is read + set_exception(std::exception_ptr()); +#else + set_exception(std::make_exception_ptr(abandoned_event_task())); +#endif + } + } + + // Get the task linked to this event. This can only be called once. + task get_task() + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty event_task object"); + LIBASYNC_ASSERT(!internal_task->event_task_got_task, std::logic_error, "get_task() called twice on event_task"); + + // Even if we didn't trigger an assert, don't return a task if one has + // already been returned. + task out; + if (!internal_task->event_task_got_task) + set_internal_task(out, internal_task); + internal_task->event_task_got_task = true; + return out; + } + + // Cancel the event with an exception and cancel continuations + bool set_exception(std::exception_ptr except) const + { + LIBASYNC_ASSERT(internal_task, std::invalid_argument, "Use of empty event_task object"); + + // Only allow setting the value once + detail::task_state expected = detail::task_state::pending; + if (!internal_task->state.compare_exchange_strong(expected, detail::task_state::locked, std::memory_order_acquire, std::memory_order_relaxed)) + return false; + + // Cancel the task + get_internal_task(*this)->cancel_base(std::move(except)); + return true; + } +}; + +} // namespace detail + +template +class task: public detail::basic_task { +public: + // Movable but not copyable + task() = default; + task(task&& other) LIBASYNC_NOEXCEPT + : detail::basic_task(std::move(other)) {} + task& operator=(task&& other) LIBASYNC_NOEXCEPT + { + detail::basic_task::operator=(std::move(other)); + return *this; + } + + // Get the result of the task + Result get() + { + this->get_internal(); + + // Move the internal state pointer so that the task becomes invalid, + // even if an exception is thrown. + detail::task_ptr my_internal = std::move(this->internal_task); + return detail::fake_void_to_void(static_cast(my_internal.get())->get_result(*this)); + } + + // Add a continuation to the task + template + typename detail::continuation_traits::task_type then(Sched& sched, Func&& f) + { + return this->then_internal(sched, std::forward(f), std::move(*this)); + } + template + typename detail::continuation_traits::task_type then(Func&& f) + { + return then(::async::default_scheduler(), std::forward(f)); + } + + // Create a shared_task from this task + shared_task share() + { + LIBASYNC_ASSERT(this->internal_task, std::invalid_argument, "Use of empty task object"); + + shared_task out; + detail::set_internal_task(out, std::move(this->internal_task)); + return out; + } +}; + +template +class shared_task: public detail::basic_task { + // get() return value: const Result& -or- void + typedef typename std::conditional< + std::is_void::value, + void, + typename std::add_lvalue_reference< + typename std::add_const::type + >::type + >::type get_result; + +public: + // Movable and copyable + shared_task() = default; + + // Get the result of the task + get_result get() const + { + this->get_internal(); + return detail::fake_void_to_void(detail::get_internal_task(*this)->get_result(*this)); + } + + // Add a continuation to the task + template + typename detail::continuation_traits::task_type then(Sched& sched, Func&& f) const + { + return this->then_internal(sched, std::forward(f), *this); + } + template + typename detail::continuation_traits::task_type then(Func&& f) const + { + return then(::async::default_scheduler(), std::forward(f)); + } +}; + +// Special task type which can be triggered manually rather than when a function executes. +template +class event_task: public detail::basic_event { +public: + // Movable but not copyable + event_task() = default; + event_task(event_task&& other) LIBASYNC_NOEXCEPT + : detail::basic_event(std::move(other)) {} + event_task& operator=(event_task&& other) LIBASYNC_NOEXCEPT + { + detail::basic_event::operator=(std::move(other)); + return *this; + } + + // Set the result of the task, mark it as completed and run its continuations + bool set(const Result& result) const + { + return this->set_internal(result); + } + bool set(Result&& result) const + { + return this->set_internal(std::move(result)); + } +}; + +// Specialization for references +template +class event_task: public detail::basic_event { +public: + // Movable but not copyable + event_task() = default; + event_task(event_task&& other) LIBASYNC_NOEXCEPT + : detail::basic_event(std::move(other)) {} + event_task& operator=(event_task&& other) LIBASYNC_NOEXCEPT + { + detail::basic_event::operator=(std::move(other)); + return *this; + } + + // Set the result of the task, mark it as completed and run its continuations + bool set(Result& result) const + { + return this->set_internal(result); + } +}; + +// Specialization for void +template<> +class event_task: public detail::basic_event { +public: + // Movable but not copyable + event_task() = default; + event_task(event_task&& other) LIBASYNC_NOEXCEPT + : detail::basic_event(std::move(other)) {} + event_task& operator=(event_task&& other) LIBASYNC_NOEXCEPT + { + detail::basic_event::operator=(std::move(other)); + return *this; + } + + // Set the result of the task, mark it as completed and run its continuations + bool set() + { + return this->set_internal(detail::fake_void()); + } +}; + +// Task type returned by local_spawn() +template +class local_task { + // Make sure the function type is callable + typedef typename std::decay::type decay_func; + static_assert(detail::is_callable::value, "Invalid function type passed to local_spawn()"); + + // Task result type + typedef typename detail::remove_task()())>::type result_type; + typedef typename detail::void_to_fake_void::type internal_result; + + // Task execution function type + typedef detail::root_exec_func()())>::value> exec_func; + + // Task object embedded directly. The ref-count is initialized to 1 so it + // will never be freed using delete, only when the local_task is destroyed. + detail::task_func internal_task; + + // Friend access for local_spawn + template + friend local_task local_spawn(S& sched, F&& f); + template + friend local_task local_spawn(F&& f); + + // Constructor, used by local_spawn + local_task(Sched& sched, Func&& f) + : internal_task(std::forward(f)) + { + // Avoid an expensive ref-count modification since the task isn't shared yet + internal_task.add_ref_unlocked(); + detail::schedule_task(sched, detail::task_ptr(&internal_task)); + } + +public: + // Non-movable and non-copyable + local_task(const local_task&) = delete; + local_task& operator=(const local_task&) = delete; + + // Wait for the task to complete when destroying + ~local_task() + { + wait(); + + // Now spin until the reference count drops to 1, since the scheduler + // may still have a reference to the task. + while (!internal_task.is_unique_ref(std::memory_order_acquire)) { +#if defined(__GLIBCXX__) && __GLIBCXX__ <= 20140612 + // Some versions of libstdc++ (4.7 and below) don't include a + // definition of std::this_thread::yield(). + sched_yield(); +#else + std::this_thread::yield(); +#endif + } + } + + // Query whether the task has finished executing + bool ready() const + { + return internal_task.ready(); + } + + // Query whether the task has been canceled with an exception + bool canceled() const + { + return internal_task.state.load(std::memory_order_acquire) == detail::task_state::canceled; + } + + // Wait for the task to complete + void wait() + { + internal_task.wait(); + } + + // Get the result of the task + result_type get() + { + internal_task.wait_and_throw(); + return detail::fake_void_to_void(internal_task.get_result(task())); + } + + // Get the exception associated with a canceled task + std::exception_ptr get_exception() const + { + if (internal_task.wait() == detail::task_state::canceled) + return internal_task.get_exception(); + else + return std::exception_ptr(); + } +}; + +// Spawn a function asynchronously +#if (__cplusplus >= 201703L) +// Use std::invoke_result instead of std::result_of for C++17 or greater because std::result_of was deprecated in C++17 and removed in C++20 +template +task>>::type> spawn(Sched& sched, Func&& f) +#else +template +task::type()>::type>::type> spawn(Sched& sched, Func&& f) +#endif +{ + // Using result_of in the function return type to work around bugs in the Intel + // C++ compiler. + + // Make sure the function type is callable + typedef typename std::decay::type decay_func; + static_assert(detail::is_callable::value, "Invalid function type passed to spawn()"); + + // Create task + typedef typename detail::void_to_fake_void()())>::type>::type internal_result; + typedef detail::root_exec_func()())>::value> exec_func; + task()())>::type> out; + detail::set_internal_task(out, detail::task_ptr(new detail::task_func(std::forward(f)))); + + // Avoid an expensive ref-count modification since the task isn't shared yet + detail::get_internal_task(out)->add_ref_unlocked(); + detail::schedule_task(sched, detail::task_ptr(detail::get_internal_task(out))); + + return out; +} +template +decltype(async::spawn(::async::default_scheduler(), std::declval())) spawn(Func&& f) +{ + return async::spawn(::async::default_scheduler(), std::forward(f)); +} + +// Create a completed task containing a value +template +task::type> make_task(T&& value) +{ + task::type> out; + + detail::set_internal_task(out, detail::task_ptr(new detail::task_result::type>)); + detail::get_internal_task(out)->set_result(std::forward(value)); + detail::get_internal_task(out)->state.store(detail::task_state::completed, std::memory_order_relaxed); + + return out; +} +template +task make_task(std::reference_wrapper value) +{ + task out; + + detail::set_internal_task(out, detail::task_ptr(new detail::task_result)); + detail::get_internal_task(out)->set_result(value.get()); + detail::get_internal_task(out)->state.store(detail::task_state::completed, std::memory_order_relaxed); + + return out; +} +inline task make_task() +{ + task out; + + detail::set_internal_task(out, detail::task_ptr(new detail::task_result)); + detail::get_internal_task(out)->state.store(detail::task_state::completed, std::memory_order_relaxed); + + return out; +} + +// Create a canceled task containing an exception +template +task make_exception_task(std::exception_ptr except) +{ + task out; + + detail::set_internal_task(out, detail::task_ptr(new detail::task_result::type>)); + detail::get_internal_task(out)->set_exception(std::move(except)); + detail::get_internal_task(out)->state.store(detail::task_state::canceled, std::memory_order_relaxed); + + return out; +} + +// Spawn a very limited task which is restricted to the current function and +// joins on destruction. Because local_task is not movable, the result must +// be captured in a reference, like this: +// auto&& x = local_spawn(...); +template +#ifdef __GNUC__ +__attribute__((warn_unused_result)) +#endif +local_task local_spawn(Sched& sched, Func&& f) +{ + // Since local_task is not movable, we construct it in-place and let the + // caller extend the lifetime of the returned object using a reference. + return {sched, std::forward(f)}; +} +template +#ifdef __GNUC__ +__attribute__((warn_unused_result)) +#endif +local_task local_spawn(Func&& f) +{ + return {::async::default_scheduler(), std::forward(f)}; +} + +} // namespace async diff --git a/3party/asyncplusplus/include/async++/task_base.h b/3party/asyncplusplus/include/async++/task_base.h new file mode 100644 index 0000000..377ac8f --- /dev/null +++ b/3party/asyncplusplus/include/async++/task_base.h @@ -0,0 +1,615 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Task states +enum class task_state: unsigned char { + pending, // Task has not completed yet + locked, // Task is locked (used by event_task to prevent double set) + unwrapped, // Task is waiting for an unwrapped task to finish + completed, // Task has finished execution and a result is available + canceled // Task has been canceled and an exception is available +}; + +// Determine whether a task is in a final state +inline bool is_finished(task_state s) +{ + return s == task_state::completed || s == task_state::canceled; +} + +// Virtual function table used to allow dynamic dispatch for task objects. +// While this is very similar to what a compiler would generate with virtual +// functions, this scheme was found to result in significantly smaller +// generated code size. +struct task_base_vtable { + // Destroy the function and result + void (*destroy)(task_base*) LIBASYNC_NOEXCEPT; + + // Run the associated function + void (*run)(task_base*) LIBASYNC_NOEXCEPT; + + // Cancel the task with an exception + void (*cancel)(task_base*, std::exception_ptr&&) LIBASYNC_NOEXCEPT; + + // Schedule the task using its scheduler + void (*schedule)(task_base* parent, task_ptr t); +}; + +// Type-generic base task object +struct task_base_deleter; +struct LIBASYNC_CACHELINE_ALIGN task_base: public ref_count_base { + // Task state + std::atomic state; + + // Whether get_task() was already called on an event_task + bool event_task_got_task; + + // Vector of continuations + continuation_vector continuations; + + // Virtual function table used for dynamic dispatch + const task_base_vtable* vtable; + + // Use aligned memory allocation + static void* operator new(std::size_t size) + { + return aligned_alloc(size, LIBASYNC_CACHELINE_SIZE); + } + static void operator delete(void* ptr) + { + aligned_free(ptr); + } + + // Initialize task state + task_base() + : state(task_state::pending) {} + + // Check whether the task is ready and include an acquire barrier if it is + bool ready() const + { + return is_finished(state.load(std::memory_order_acquire)); + } + + // Run a single continuation + template + void run_continuation(Sched& sched, task_ptr&& cont) + { + LIBASYNC_TRY { + detail::schedule_task(sched, cont); + } LIBASYNC_CATCH(...) { + // This is suboptimal, but better than letting the exception leak + cont->vtable->cancel(cont.get(), std::current_exception()); + } + } + + // Run all of the task's continuations after it has completed or canceled. + // The list of continuations is emptied and locked to prevent any further + // continuations from being added. + void run_continuations() + { + continuations.flush_and_lock([this](task_ptr t) { + const task_base_vtable* vtable_ptr = t->vtable; + vtable_ptr->schedule(this, std::move(t)); + }); + } + + // Add a continuation to this task + template + void add_continuation(Sched& sched, task_ptr cont) + { + // Check for task completion + task_state current_state = state.load(std::memory_order_relaxed); + if (!is_finished(current_state)) { + // Try to add the task to the continuation list. This can fail only + // if the task has just finished, in which case we run it directly. + if (continuations.try_add(std::move(cont))) + return; + } + + // Otherwise run the continuation directly + std::atomic_thread_fence(std::memory_order_acquire); + run_continuation(sched, std::move(cont)); + } + + // Finish the task after it has been executed and the result set + void finish() + { + state.store(task_state::completed, std::memory_order_release); + run_continuations(); + } + + // Wait for the task to finish executing + task_state wait() + { + task_state s = state.load(std::memory_order_acquire); + if (!is_finished(s)) { + wait_for_task(this); + s = state.load(std::memory_order_relaxed); + } + return s; + } +}; + +// Deleter for task_ptr +struct task_base_deleter { + static void do_delete(task_base* p) + { + // Go through the vtable to delete p with its proper type + p->vtable->destroy(p); + } +}; + +// Result type-specific task object +template +struct task_result_holder: public task_base { + union { + alignas(Result) std::uint8_t result[sizeof(Result)]; + alignas(std::exception_ptr) std::uint8_t except[sizeof(std::exception_ptr)]; + + // Scheduler that should be used to schedule this task. The scheduler + // type has been erased and is held by vtable->schedule. + void* sched; + }; + + template + void set_result(T&& t) + { + new(&result) Result(std::forward(t)); + } + + // Return a result using an lvalue or rvalue reference depending on the task + // type. The task parameter is not used, it is just there for overload resolution. + template + Result&& get_result(const task&) + { + return std::move(*reinterpret_cast(&result)); + } + template + const Result& get_result(const shared_task&) + { + return *reinterpret_cast(&result); + } + + // Destroy the result + ~task_result_holder() + { + // Result is only present if the task completed successfully + if (state.load(std::memory_order_relaxed) == task_state::completed) + reinterpret_cast(&result)->~Result(); + } +}; + +// Specialization for references +template +struct task_result_holder: public task_base { + union { + // Store as pointer internally + Result* result; + alignas(std::exception_ptr) std::uint8_t except[sizeof(std::exception_ptr)]; + void* sched; + }; + + void set_result(Result& obj) + { + result = std::addressof(obj); + } + + template + Result& get_result(const task&) + { + return *result; + } + template + Result& get_result(const shared_task&) + { + return *result; + } +}; + +// Specialization for void +template<> +struct task_result_holder: public task_base { + union { + alignas(std::exception_ptr) std::uint8_t except[sizeof(std::exception_ptr)]; + void* sched; + }; + + void set_result(fake_void) {} + + // Get the result as fake_void so that it can be passed to set_result and + // continuations + template + fake_void get_result(const task&) + { + return fake_void(); + } + template + fake_void get_result(const shared_task&) + { + return fake_void(); + } +}; + +template +struct task_result: public task_result_holder { + // Virtual function table for task_result + static const task_base_vtable vtable_impl; + task_result() + { + this->vtable = &vtable_impl; + } + + // Destroy the exception + ~task_result() + { + // Exception is only present if the task was canceled + if (this->state.load(std::memory_order_relaxed) == task_state::canceled) + reinterpret_cast(&this->except)->~exception_ptr(); + } + + // Cancel a task with the given exception + void cancel_base(std::exception_ptr&& except_) + { + set_exception(std::move(except_)); + this->state.store(task_state::canceled, std::memory_order_release); + this->run_continuations(); + } + + // Set the exception value of the task + void set_exception(std::exception_ptr&& except_) + { + new(&this->except) std::exception_ptr(std::move(except_)); + } + + // Get the exception a task was canceled with + std::exception_ptr& get_exception() + { + return *reinterpret_cast(&this->except); + } + + // Wait and throw the exception if the task was canceled + void wait_and_throw() + { + if (this->wait() == task_state::canceled) + LIBASYNC_RETHROW_EXCEPTION(get_exception()); + } + + // Delete the task using its proper type + static void destroy(task_base* t) LIBASYNC_NOEXCEPT + { + delete static_cast*>(t); + } +}; +template +const task_base_vtable task_result::vtable_impl = { + task_result::destroy, // destroy + nullptr, // run + nullptr, // cancel + nullptr // schedule +}; + +// Class to hold a function object, with empty base class optimization +template +struct func_base { + Func func; + + template + explicit func_base(F&& f) + : func(std::forward(f)) {} + Func& get_func() + { + return func; + } +}; +template +struct func_base::value>::type> { + template + explicit func_base(F&& f) + { + new(this) Func(std::forward(f)); + } + ~func_base() + { + get_func().~Func(); + } + Func& get_func() + { + return *reinterpret_cast(this); + } +}; + +// Class to hold a function object and initialize/destroy it at any time +template +struct func_holder { + alignas(Func) std::uint8_t func[sizeof(Func)]; + + Func& get_func() + { + return *reinterpret_cast(&func); + } + template + void init_func(Args&&... args) + { + new(&func) Func(std::forward(args)...); + } + void destroy_func() + { + get_func().~Func(); + } +}; +template +struct func_holder::value>::type> { + Func& get_func() + { + return *reinterpret_cast(this); + } + template + void init_func(Args&&... args) + { + new(this) Func(std::forward(args)...); + } + void destroy_func() + { + get_func().~Func(); + } +}; + +// Task object with an associated function object +// Using private inheritance so empty Func doesn't take up space +template +struct task_func: public task_result, func_holder { + // Virtual function table for task_func + static const task_base_vtable vtable_impl; + template + explicit task_func(Args&&... args) + { + this->vtable = &vtable_impl; + this->init_func(std::forward(args)...); + } + + // Run the stored function + static void run(task_base* t) LIBASYNC_NOEXCEPT + { + LIBASYNC_TRY { + // Dispatch to execution function + static_cast*>(t)->get_func()(t); + } LIBASYNC_CATCH(...) { + cancel(t, std::current_exception()); + } + } + + // Cancel the task + static void cancel(task_base* t, std::exception_ptr&& except) LIBASYNC_NOEXCEPT + { + // Destroy the function object when canceling since it won't be + // used anymore. + static_cast*>(t)->destroy_func(); + static_cast*>(t)->cancel_base(std::move(except)); + } + + // Schedule a continuation task using its scheduler + static void schedule(task_base* parent, task_ptr t) + { + void* sched = static_cast*>(t.get())->sched; + parent->run_continuation(*static_cast(sched), std::move(t)); + } + + // Free the function + ~task_func() + { + // If the task hasn't completed yet, destroy the function object. Note + // that an unwrapped task has already destroyed its function object. + if (this->state.load(std::memory_order_relaxed) == task_state::pending) + this->destroy_func(); + } + + // Delete the task using its proper type + static void destroy(task_base* t) LIBASYNC_NOEXCEPT + { + delete static_cast*>(t); + } +}; +template +const task_base_vtable task_func::vtable_impl = { + task_func::destroy, // destroy + task_func::run, // run + task_func::cancel, // cancel + task_func::schedule // schedule +}; + +// Helper functions to access the internal_task member of a task object, which +// avoids us having to specify half of the functions in the detail namespace +// as friend. Also, internal_task is downcast to the appropriate task_result<>. +template +typename Task::internal_task_type* get_internal_task(const Task& t) +{ + return static_cast(t.internal_task.get()); +} +template +void set_internal_task(Task& t, task_ptr p) +{ + t.internal_task = std::move(p); +} + +// Common code for task unwrapping +template +struct unwrapped_func { + explicit unwrapped_func(task_ptr t) + : parent_task(std::move(t)) {} + void operator()(Child child_task) const + { + // Forward completion state and result to parent task + task_result* parent = static_cast*>(parent_task.get()); + LIBASYNC_TRY { + if (get_internal_task(child_task)->state.load(std::memory_order_relaxed) == task_state::completed) { + parent->set_result(get_internal_task(child_task)->get_result(child_task)); + parent->finish(); + } else { + // We don't call the generic cancel function here because + // the function of the parent task has already been destroyed. + parent->cancel_base(std::exception_ptr(get_internal_task(child_task)->get_exception())); + } + } LIBASYNC_CATCH(...) { + // If the copy/move constructor of the result threw, propagate the exception + parent->cancel_base(std::current_exception()); + } + } + task_ptr parent_task; +}; +template +void unwrapped_finish(task_base* parent_base, Child child_task) +{ + // Destroy the parent task's function since it has been executed + parent_base->state.store(task_state::unwrapped, std::memory_order_relaxed); + static_cast*>(parent_base)->destroy_func(); + + // Set up a continuation on the child to set the result of the parent + LIBASYNC_TRY { + parent_base->add_ref(); + child_task.then(inline_scheduler(), unwrapped_func(task_ptr(parent_base))); + } LIBASYNC_CATCH(...) { + // Use cancel_base here because the function object is already destroyed. + static_cast*>(parent_base)->cancel_base(std::current_exception()); + } +} + +// Execution functions for root tasks: +// - With and without task unwraping +template +struct root_exec_func: private func_base { + template + explicit root_exec_func(F&& f) + : func_base(std::forward(f)) {} + void operator()(task_base* t) + { + static_cast*>(t)->set_result(detail::invoke_fake_void(std::move(this->get_func()))); + static_cast*>(t)->destroy_func(); + t->finish(); + } +}; +template +struct root_exec_func: private func_base { + template + explicit root_exec_func(F&& f) + : func_base(std::forward(f)) {} + void operator()(task_base* t) + { + unwrapped_finish(t, std::move(this->get_func())()); + } +}; + +// Execution functions for continuation tasks: +// - With and without task unwraping +// - For void, value-based and task-based continuations +template +struct continuation_exec_func: private func_base { + template + continuation_exec_func(F&& f, P&& p) + : func_base(std::forward(f)), parent(std::forward

(p)) {} + void operator()(task_base* t) + { + static_cast*>(t)->set_result(detail::invoke_fake_void(std::move(this->get_func()), std::move(parent))); + static_cast*>(t)->destroy_func(); + t->finish(); + } + Parent parent; +}; +template +struct continuation_exec_func: private func_base { + template + continuation_exec_func(F&& f, P&& p) + : func_base(std::forward(f)), parent(std::forward

(p)) {} + void operator()(task_base* t) + { + if (get_internal_task(parent)->state.load(std::memory_order_relaxed) == task_state::canceled) + task_func::cancel(t, std::exception_ptr(get_internal_task(parent)->get_exception())); + else { + static_cast*>(t)->set_result(detail::invoke_fake_void(std::move(this->get_func()), get_internal_task(parent)->get_result(parent))); + static_cast*>(t)->destroy_func(); + t->finish(); + } + } + Parent parent; +}; +template +struct continuation_exec_func: private func_base { + template + continuation_exec_func(F&& f, P&& p) + : func_base(std::forward(f)), parent(std::forward

(p)) {} + void operator()(task_base* t) + { + if (get_internal_task(parent)->state.load(std::memory_order_relaxed) == task_state::canceled) + task_func::cancel(t, std::exception_ptr(get_internal_task(parent)->get_exception())); + else { + static_cast*>(t)->set_result(detail::invoke_fake_void(std::move(this->get_func()), fake_void())); + static_cast*>(t)->destroy_func(); + t->finish(); + } + } + Parent parent; +}; +template +struct continuation_exec_func: private func_base { + template + continuation_exec_func(F&& f, P&& p) + : func_base(std::forward(f)), parent(std::forward

(p)) {} + void operator()(task_base* t) + { + unwrapped_finish(t, detail::invoke_fake_void(std::move(this->get_func()), std::move(parent))); + } + Parent parent; +}; +template +struct continuation_exec_func: private func_base { + template + continuation_exec_func(F&& f, P&& p) + : func_base(std::forward(f)), parent(std::forward

(p)) {} + void operator()(task_base* t) + { + if (get_internal_task(parent)->state.load(std::memory_order_relaxed) == task_state::canceled) + task_func::cancel(t, std::exception_ptr(get_internal_task(parent)->get_exception())); + else + unwrapped_finish(t, detail::invoke_fake_void(std::move(this->get_func()), get_internal_task(parent)->get_result(parent))); + } + Parent parent; +}; +template +struct continuation_exec_func: private func_base { + template + continuation_exec_func(F&& f, P&& p) + : func_base(std::forward(f)), parent(std::forward

(p)) {} + void operator()(task_base* t) + { + if (get_internal_task(parent)->state.load(std::memory_order_relaxed) == task_state::canceled) + task_func::cancel(t, std::exception_ptr(get_internal_task(parent)->get_exception())); + else + unwrapped_finish(t, detail::invoke_fake_void(std::move(this->get_func()), fake_void())); + } + Parent parent; +}; + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/include/async++/traits.h b/3party/asyncplusplus/include/async++/traits.h new file mode 100644 index 0000000..087e60b --- /dev/null +++ b/3party/asyncplusplus/include/async++/traits.h @@ -0,0 +1,140 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Pseudo-void type: it takes up no space but can be moved and copied +struct fake_void {}; +template +struct void_to_fake_void { + typedef T type; +}; +template<> +struct void_to_fake_void { + typedef fake_void type; +}; +template +T fake_void_to_void(T&& x) +{ + return std::forward(x); +} +inline void fake_void_to_void(fake_void) {} + +// Check if type is a task type, used to detect task unwraping +template +struct is_task: public std::false_type {}; +template +struct is_task>: public std::true_type {}; +template +struct is_task>: public std::true_type {}; +template +struct is_task>: public std::true_type {}; +template +struct is_task>: public std::true_type {}; + +// Extract the result type of a task if T is a task, otherwise just return T +template +struct remove_task { + typedef T type; +}; +template +struct remove_task> { + typedef T type; +}; +template +struct remove_task> { + typedef T type; +}; +template +struct remove_task> { + typedef T type; +}; +template +struct remove_task> { + typedef T type; +}; + +// Check if a type is callable with the given arguments +typedef char one[1]; +typedef char two[2]; +template()(std::declval()...))> +two& is_callable_helper(int); +template +one& is_callable_helper(...); +template +struct is_callable; +template +struct is_callable: public std::integral_constant(0)) - 1> {}; + +// Wrapper to run a function object with an optional parameter: +// - void returns are turned into fake_void +// - fake_void parameter will invoke the function with no arguments +template()())>::value>::type> +decltype(std::declval()()) invoke_fake_void(Func&& f) +{ + return std::forward(f)(); +} +template()())>::value>::type> +fake_void invoke_fake_void(Func&& f) +{ + std::forward(f)(); + return fake_void(); +} +template +typename void_to_fake_void()(std::declval()))>::type invoke_fake_void(Func&& f, Param&& p) +{ + return detail::invoke_fake_void([&f, &p] {return std::forward(f)(std::forward(p));}); +} +template +typename void_to_fake_void()())>::type invoke_fake_void(Func&& f, fake_void) +{ + return detail::invoke_fake_void(std::forward(f)); +} + +// Various properties of a continuation function +template()())> +fake_void is_value_cont_helper(const Parent&, int, int); +template()(std::declval().get()))> +std::true_type is_value_cont_helper(const Parent&, int, int); +template()())> +std::true_type is_value_cont_helper(const task&, int, int); +template()())> +std::true_type is_value_cont_helper(const shared_task&, int, int); +template()(std::declval()))> +std::false_type is_value_cont_helper(const Parent&, int, ...); +template +void is_value_cont_helper(const Parent&, ...); +template +struct continuation_traits { + typedef typename std::decay::type decay_func; + typedef decltype(detail::is_value_cont_helper(std::declval(), 0, 0)) is_value_cont; + static_assert(!std::is_void::value, "Parameter type for continuation function is invalid for parent task type"); + typedef typename std::conditional::value, fake_void, typename std::conditional::value, typename void_to_fake_void().get())>::type, Parent>::type>::type param_type; + typedef decltype(detail::fake_void_to_void(detail::invoke_fake_void(std::declval(), std::declval()))) result_type; + typedef task::type> task_type; +}; + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/include/async++/when_all_any.h b/3party/asyncplusplus/include/async++/when_all_any.h new file mode 100644 index 0000000..7166026 --- /dev/null +++ b/3party/asyncplusplus/include/async++/when_all_any.h @@ -0,0 +1,292 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { + +// Result type for when_any +template +struct when_any_result { + // Index of the task that finished first + std::size_t index; + + // List of tasks that were passed in + Result tasks; +}; + +namespace detail { + +// Shared state for when_all +template +struct when_all_state: public ref_count_base> { + event_task event; + Result result; + + when_all_state(std::size_t count) + : ref_count_base>(count) {} + + // When all references are dropped, signal the event + ~when_all_state() + { + event.set(std::move(result)); + } +}; + +// Execution functions for when_all, for ranges and tuples +template +struct when_all_func_range { + std::size_t index; + ref_count_ptr> state; + + when_all_func_range(std::size_t index_, ref_count_ptr> state_) + : index(index_), state(std::move(state_)) {} + + // Copy the completed task object to the shared state. The event is + // automatically signaled when all references are dropped. + void operator()(Task t) const + { + state->result[index] = std::move(t); + } +}; +template +struct when_all_func_tuple { + ref_count_ptr> state; + + when_all_func_tuple(ref_count_ptr> state_) + : state(std::move(state_)) {} + + // Copy the completed task object to the shared state. The event is + // automatically signaled when all references are dropped. + void operator()(Task t) const + { + std::get(state->result) = std::move(t); + } +}; + +// Shared state for when_any +template +struct when_any_state: public ref_count_base> { + event_task> event; + Result result; + + when_any_state(std::size_t count) + : ref_count_base>(count) {} + + // Signal the event when the first task reaches here + void set(std::size_t i) + { + event.set({i, std::move(result)}); + } +}; + +// Execution function for when_any +template +struct when_any_func { + std::size_t index; + ref_count_ptr> state; + + when_any_func(std::size_t index_, ref_count_ptr> state_) + : index(index_), state(std::move(state_)) {} + + // Simply tell the state that our task has finished, it already has a copy + // of the task object. + void operator()(Task) const + { + state->set(index); + } +}; + +// Internal implementation of when_all for variadic arguments +template +void when_all_variadic(when_all_state*) {} +template +void when_all_variadic(when_all_state* state, First&& first, T&&... tasks) +{ + typedef typename std::decay::type task_type; + + // Add a continuation to the task + LIBASYNC_TRY { + first.then(inline_scheduler(), detail::when_all_func_tuple(detail::ref_count_ptr>(state))); + } LIBASYNC_CATCH(...) { + // Make sure we don't leak memory if then() throws + state->remove_ref(sizeof...(T)); + LIBASYNC_RETHROW(); + } + + // Add continuations to remaining tasks + detail::when_all_variadic(state, std::forward(tasks)...); +} + +// Internal implementation of when_any for variadic arguments +template +void when_any_variadic(when_any_state*) {} +template +void when_any_variadic(when_any_state* state, First&& first, T&&... tasks) +{ + typedef typename std::decay::type task_type; + + // Add a copy of the task to the results because the event may be + // set before all tasks have finished. + detail::task_base* t = detail::get_internal_task(first); + t->add_ref(); + detail::set_internal_task(std::get(state->result), detail::task_ptr(t)); + + // Add a continuation to the task + LIBASYNC_TRY { + first.then(inline_scheduler(), detail::when_any_func(index, detail::ref_count_ptr>(state))); + } LIBASYNC_CATCH(...) { + // Make sure we don't leak memory if then() throws + state->remove_ref(sizeof...(T)); + LIBASYNC_RETHROW(); + } + + // Add continuations to remaining tasks + detail::when_any_variadic(state, std::forward(tasks)...); +} + +} // namespace detail + +// Combine a set of tasks into one task which is signaled when all specified tasks finish +template +task::value_type>::type>> when_all(Iter begin, Iter end) +{ + typedef typename std::decay::value_type>::type task_type; + typedef std::vector result_type; + + // Handle empty ranges + if (begin == end) + return make_task(result_type()); + + // Create shared state, initialized with the proper reference count + std::size_t count = std::distance(begin, end); + auto* state = new detail::when_all_state(count); + state->result.resize(count); + auto out = state->event.get_task(); + + // Add a continuation to each task to add its result to the shared state + // Last task sets the event result + for (std::size_t i = 0; begin != end; i++, ++begin) { + LIBASYNC_TRY { + (*begin).then(inline_scheduler(), detail::when_all_func_range(i, detail::ref_count_ptr>(state))); + } LIBASYNC_CATCH(...) { + // Make sure we don't leak memory if then() throws + state->remove_ref(std::distance(begin, end) - 1); + LIBASYNC_RETHROW(); + } + } + + return out; +} + +// Combine a set of tasks into one task which is signaled when one of the tasks finishes +template +task::value_type>::type>>> when_any(Iter begin, Iter end) +{ + typedef typename std::decay::value_type>::type task_type; + typedef std::vector result_type; + + // Handle empty ranges + if (begin == end) + return make_task(when_any_result()); + + // Create shared state, initialized with the proper reference count + std::size_t count = std::distance(begin, end); + auto* state = new detail::when_any_state(count); + state->result.resize(count); + auto out = state->event.get_task(); + + // Add a continuation to each task to set the event. First one wins. + for (std::size_t i = 0; begin != end; i++, ++begin) { + // Add a copy of the task to the results because the event may be + // set before all tasks have finished. + detail::task_base* t = detail::get_internal_task(*begin); + t->add_ref(); + detail::set_internal_task(state->result[i], detail::task_ptr(t)); + + LIBASYNC_TRY { + (*begin).then(inline_scheduler(), detail::when_any_func(i, detail::ref_count_ptr>(state))); + } LIBASYNC_CATCH(...) { + // Make sure we don't leak memory if then() throws + state->remove_ref(std::distance(begin, end) - 1); + LIBASYNC_RETHROW(); + } + } + + return out; +} + +// when_all wrapper accepting ranges +template +decltype(async::when_all(std::begin(std::declval()), std::end(std::declval()))) when_all(T&& tasks) +{ + return async::when_all(std::begin(std::forward(tasks)), std::end(std::forward(tasks))); +} + +// when_any wrapper accepting ranges +template +decltype(async::when_any(std::begin(std::declval()), std::end(std::declval()))) when_any(T&& tasks) +{ + return async::when_any(std::begin(std::forward(tasks)), std::end(std::forward(tasks))); +} + +// when_all with variadic arguments +inline task> when_all() +{ + return async::make_task(std::tuple<>()); +} +template +task::type...>> when_all(T&&... tasks) +{ + typedef std::tuple::type...> result_type; + + // Create shared state + auto state = new detail::when_all_state(sizeof...(tasks)); + auto out = state->event.get_task(); + + // Register all the tasks on the event + detail::when_all_variadic<0>(state, std::forward(tasks)...); + + return out; +} + +// when_any with variadic arguments +inline task>> when_any() +{ + return async::make_task(when_any_result>()); +} +template +task::type...>>> when_any(T&&... tasks) +{ + typedef std::tuple::type...> result_type; + + // Create shared state + auto state = new detail::when_any_state(sizeof...(tasks)); + auto out = state->event.get_task(); + + // Register all the tasks on the event + detail::when_any_variadic<0>(state, std::forward(tasks)...); + + return out; +} + +} // namespace async diff --git a/3party/asyncplusplus/src/fifo_queue.h b/3party/asyncplusplus/src/fifo_queue.h new file mode 100644 index 0000000..5556977 --- /dev/null +++ b/3party/asyncplusplus/src/fifo_queue.h @@ -0,0 +1,77 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Queue which holds tasks in FIFO order. Note that this queue is not +// thread-safe and must be protected by a lock. +class fifo_queue { + detail::aligned_array items; + std::size_t head, tail; + +public: + fifo_queue() + : items(32), head(0), tail(0) {} + ~fifo_queue() + { + // Free any unexecuted tasks + for (std::size_t i = head; i != tail; i = (i + 1) & (items.size() - 1)) + task_run_handle::from_void_ptr(items[i]); + } + + // Push a task to the end of the queue + void push(task_run_handle t) + { + // Resize queue if it is full + if (head == ((tail + 1) & (items.size() - 1))) { + detail::aligned_array new_items(items.size() * 2); + for (std::size_t i = 0; i != items.size(); i++) + new_items[i] = items[(i + head) & (items.size() - 1)]; + head = 0; + tail = items.size() - 1; + items = std::move(new_items); + } + + // Push the item + items[tail] = t.to_void_ptr(); + tail = (tail + 1) & (items.size() - 1); + } + + // Pop a task from the front of the queue + task_run_handle pop() + { + // See if an item is available + if (head == tail) + return task_run_handle(); + else { + void* x = items[head]; + head = (head + 1) & (items.size() - 1); + return task_run_handle::from_void_ptr(x); + } + } +}; + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/src/internal.h b/3party/asyncplusplus/src/internal.h new file mode 100644 index 0000000..4fa08c9 --- /dev/null +++ b/3party/asyncplusplus/src/internal.h @@ -0,0 +1,95 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +// For posix_memalign/_aligned_malloc +#ifdef _WIN32 +# include +# ifdef __MINGW32__ +# define _aligned_malloc __mingw_aligned_malloc +# define _aligned_free __mingw_aligned_free +# endif +#else +# include +#endif + +// We don't make use of dynamic TLS initialization/destruction so we can just +// use the legacy TLS attributes. +#ifdef __GNUC__ +# define THREAD_LOCAL __thread +#elif defined (_MSC_VER) +# define THREAD_LOCAL __declspec(thread) +#else +# define THREAD_LOCAL thread_local +#endif + +// GCC, Clang and the Linux version of the Intel compiler and MSVC 2015 support +// thread-safe initialization of function-scope static variables. +#ifdef __GNUC__ +# define HAVE_THREAD_SAFE_STATIC +#elif _MSC_VER >= 1900 && !defined(__INTEL_COMPILER) +# define HAVE_THREAD_SAFE_STATIC +#endif + +// MSVC deadlocks when joining a thread from a static destructor. Use a +// workaround in that case to avoid the deadlock. +#if defined(_MSC_VER) && _MSC_VER < 1900 +# define BROKEN_JOIN_IN_DESTRUCTOR +#endif + +// Apple's iOS has no thread local support yet. They claim that they don't want to +// introduce a binary compatility issue when they got a better implementation available. +// Luckily, pthreads supports some kind of "emulation" for that. This detects if the we +// are compiling for iOS and enables the workaround accordingly. +// It is also possible enabling it forcibly by setting the EMULATE_PTHREAD_THREAD_LOCAL +// macro. Obviously, this will only works on platforms with pthread available. +#if __APPLE__ +# include "TargetConditionals.h" +# if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE +# define EMULATE_PTHREAD_THREAD_LOCAL +# endif +#endif + +// Force symbol visibility to hidden unless explicity exported +#ifndef LIBASYNC_STATIC +#if defined(__GNUC__) && !defined(_WIN32) +# pragma GCC visibility push(hidden) +#endif +#endif + +// Include other internal headers +#include "singleton.h" +#include "task_wait_event.h" +#include "fifo_queue.h" +#include "work_steal_queue.h" diff --git a/3party/asyncplusplus/src/scheduler.cpp b/3party/asyncplusplus/src/scheduler.cpp new file mode 100644 index 0000000..9afb0cc --- /dev/null +++ b/3party/asyncplusplus/src/scheduler.cpp @@ -0,0 +1,247 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "internal.h" + +// for pthread thread_local emulation +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) +# include +#endif + +namespace async { +namespace detail { + +void* aligned_alloc(std::size_t size, std::size_t align) +{ +#ifdef _WIN32 + void* ptr = _aligned_malloc(size, align); + if (!ptr) + LIBASYNC_THROW(std::bad_alloc()); + return ptr; +#else + void* result; + if (posix_memalign(&result, align, size)) + LIBASYNC_THROW(std::bad_alloc()); + else + return result; +#endif +} + +void aligned_free(void* addr) LIBASYNC_NOEXCEPT +{ +#ifdef _WIN32 + _aligned_free(addr); +#else + free(addr); +#endif +} + +// Wait for a task to complete (for threads outside thread pool) +static void generic_wait_handler(task_wait_handle wait_task) +{ + // Create an event to wait on + task_wait_event event; + event.init(); + + // Create a continuation for the task we are waiting for + wait_task.on_finish([&event] { + // Just signal the thread event + event.signal(wait_type::task_finished); + }); + + // Wait for the event to be set + event.wait(); +} + +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) +// Wait handler function, per-thread, defaults to generic version +struct pthread_emulation_thread_wait_handler_key_initializer { + pthread_key_t key; + + pthread_emulation_thread_wait_handler_key_initializer() + { + pthread_key_create(&key, nullptr); + } + + ~pthread_emulation_thread_wait_handler_key_initializer() + { + pthread_key_delete(key); + } +}; + +static pthread_key_t get_thread_wait_handler_key() +{ + static pthread_emulation_thread_wait_handler_key_initializer initializer; + return initializer.key; +} + +#else +static THREAD_LOCAL wait_handler thread_wait_handler = generic_wait_handler; +#endif + +static void set_thread_wait_handler(wait_handler handler) +{ +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) + // we need to call this here, because the pthread initializer is lazy, + // this means the it could be null and we need to set it before trying to + // get or set it + pthread_setspecific(get_thread_wait_handler_key(), reinterpret_cast(handler)); +#else + thread_wait_handler = handler; +#endif +} + +static wait_handler get_thread_wait_handler() +{ +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) + // we need to call this here, because the pthread initializer is lazy, + // this means the it could be null and we need to set it before trying to + // get or set it + wait_handler handler = (wait_handler) pthread_getspecific(get_thread_wait_handler_key()); + if(handler == nullptr) { + return generic_wait_handler; + } + return handler; +#else + return thread_wait_handler; +#endif +} + +// Wait for a task to complete +void wait_for_task(task_base* wait_task) +{ + // Dispatch to the current thread's wait handler + wait_handler thread_wait_handler = get_thread_wait_handler(); + thread_wait_handler(task_wait_handle(wait_task)); +} + +// The default scheduler is just a thread pool which can be configured +// using environment variables. +class default_scheduler_impl: public threadpool_scheduler { + static std::size_t get_num_threads() + { + // Get the requested number of threads from the environment + // If that fails, use the number of CPUs in the system. + std::size_t num_threads; +#ifdef _MSC_VER + char* s; +# ifdef __cplusplus_winrt + // Windows store applications do not support environment variables + s = nullptr; +# else + // MSVC gives an error when trying to use getenv, work around this + // by using _dupenv_s instead. + _dupenv_s(&s, nullptr, "LIBASYNC_NUM_THREADS"); +# endif +#else + const char *s = std::getenv("LIBASYNC_NUM_THREADS"); +#endif + if (s) + num_threads = std::strtoul(s, nullptr, 10); + else + num_threads = hardware_concurrency(); + +#if defined(_MSC_VER) && !defined(__cplusplus_winrt) + // Free the string allocated by _dupenv_s + free(s); +#endif + + // Make sure the thread count is reasonable + if (num_threads < 1) + num_threads = 1; + return num_threads; + } + +public: + default_scheduler_impl() + : threadpool_scheduler(get_num_threads()) {} +}; + +// Thread scheduler implementation +void thread_scheduler_impl::schedule(task_run_handle t) +{ + // A shared_ptr is used here because not all implementations of + // std::thread support move-only objects. + std::thread([](const std::shared_ptr& t) { + t->run(); + }, std::make_shared(std::move(t))).detach(); +} + +} // namespace detail + +threadpool_scheduler& default_threadpool_scheduler() +{ + return detail::singleton::get_instance(); +} + +// FIFO scheduler implementation +struct fifo_scheduler::internal_data { + detail::fifo_queue queue; + std::mutex lock; +}; +fifo_scheduler::fifo_scheduler() + : impl(new internal_data) {} +fifo_scheduler::~fifo_scheduler() {} +void fifo_scheduler::schedule(task_run_handle t) +{ + std::lock_guard locked(impl->lock); + impl->queue.push(std::move(t)); +} +bool fifo_scheduler::try_run_one_task() +{ + task_run_handle t; + { + std::lock_guard locked(impl->lock); + t = impl->queue.pop(); + } + if (t) { + t.run(); + return true; + } + return false; +} +void fifo_scheduler::run_all_tasks() +{ + while (try_run_one_task()) {} +} + +std::size_t hardware_concurrency() LIBASYNC_NOEXCEPT +{ + // Cache the value because calculating it may be expensive + static std::size_t value = std::thread::hardware_concurrency(); + + // Always return at least 1 core + return value == 0 ? 1 : value; +} + +wait_handler set_thread_wait_handler(wait_handler handler) LIBASYNC_NOEXCEPT +{ + wait_handler old = detail::get_thread_wait_handler(); + detail::set_thread_wait_handler(handler); + return old; +} + +} // namespace async + +#ifndef LIBASYNC_STATIC +#if defined(__GNUC__) && !defined(_WIN32) +# pragma GCC visibility pop +#endif +#endif diff --git a/3party/asyncplusplus/src/singleton.h b/3party/asyncplusplus/src/singleton.h new file mode 100644 index 0000000..869800d --- /dev/null +++ b/3party/asyncplusplus/src/singleton.h @@ -0,0 +1,73 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +namespace async { +namespace detail { + +// Thread-safe singleton wrapper class +#ifdef HAVE_THREAD_SAFE_STATIC +// C++11 guarantees thread safety for static initialization +template +class singleton { +public: + static T& get_instance() + { + static T instance; + return instance; + } +}; +#else +// Some compilers don't support thread-safe static initialization, so emulate it +template +class singleton { + std::mutex lock; + std::atomic init_flag; + alignas(T) std::uint8_t storage[sizeof(T)]; + + static singleton instance; + + // Use a destructor instead of atexit() because the latter does not work + // properly when the singleton is in a library that is unloaded. + ~singleton() + { + if (init_flag.load(std::memory_order_acquire)) + reinterpret_cast(&storage)->~T(); + } + +public: + static T& get_instance() + { + T* ptr = reinterpret_cast(&instance.storage); + if (!instance.init_flag.load(std::memory_order_acquire)) { + std::lock_guard locked(instance.lock); + if (!instance.init_flag.load(std::memory_order_relaxed)) { + new(ptr) T; + instance.init_flag.store(true, std::memory_order_release); + } + } + return *ptr; + } +}; + +template singleton singleton::instance; +#endif + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/src/task_wait_event.h b/3party/asyncplusplus/src/task_wait_event.h new file mode 100644 index 0000000..2f10ff8 --- /dev/null +++ b/3party/asyncplusplus/src/task_wait_event.h @@ -0,0 +1,109 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +namespace async { +namespace detail { + +// Set of events that an task_wait_event can hold +enum wait_type { + // The task that is being waited on has completed + task_finished = 1, + + // A task is available to execute from the scheduler + task_available = 2 +}; + +// OS-supported event object which can be used to wait for either a task to +// finish or for the scheduler to have more work for the current thread. +// +// The event object is lazily initialized to avoid unnecessary API calls. +class task_wait_event { + alignas(std::mutex) std::uint8_t m[sizeof(std::mutex)]; + alignas(std::condition_variable) std::uint8_t c[sizeof(std::condition_variable)]; + int event_mask; + bool initialized; + + std::mutex& mutex() + { + return *reinterpret_cast(&m); + } + std::condition_variable& cond() + { + return *reinterpret_cast(&c); + } + +public: + task_wait_event() + : event_mask(0), initialized(false) {} + + ~task_wait_event() + { + if (initialized) { + mutex().~mutex(); + cond().~condition_variable(); + } + } + + // Initialize the event, must be done before any other functions are called. + void init() + { + if (!initialized) { + new(&m) std::mutex; + new(&c) std::condition_variable; + initialized = true; + } + } + + // Wait for an event to occur. Returns the event(s) that occurred. This also + // clears any pending events afterwards. + int wait() + { + std::unique_lock lock(mutex()); + while (event_mask == 0) + cond().wait(lock); + int result = event_mask; + event_mask = 0; + return result; + } + + // Check if a specific event is ready + bool try_wait(int event) + { + std::lock_guard lock(mutex()); + int result = event_mask & event; + event_mask &= ~event; + return result != 0; + } + + // Signal an event and wake up a sleeping thread + void signal(int event) + { + std::unique_lock lock(mutex()); + event_mask |= event; + + // This must be done while holding the lock otherwise we may end up with + // a use-after-free due to a race with wait(). + cond().notify_one(); + lock.unlock(); + } +}; + +} // namespace detail +} // namespace async diff --git a/3party/asyncplusplus/src/threadpool_scheduler.cpp b/3party/asyncplusplus/src/threadpool_scheduler.cpp new file mode 100644 index 0000000..6318c16 --- /dev/null +++ b/3party/asyncplusplus/src/threadpool_scheduler.cpp @@ -0,0 +1,448 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "internal.h" + +// For GetProcAddress and GetModuleHandle +#ifdef _WIN32 +#include +#endif + +// for pthread thread_local emulation +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) +# include +#endif + +namespace async { +namespace detail { + +// Per-thread data, aligned to cachelines to avoid false sharing +struct LIBASYNC_CACHELINE_ALIGN thread_data_t { + work_steal_queue queue; + std::minstd_rand rng; + std::thread handle; +}; + +// Internal data used by threadpool_scheduler +struct threadpool_data { + threadpool_data(std::size_t num_threads) + : thread_data(num_threads), shutdown(false), num_waiters(0), waiters(new task_wait_event*[num_threads]) {} + + threadpool_data(std::size_t num_threads, std::function&& prerun_, std::function&& postrun_) + : thread_data(num_threads), shutdown(false), num_waiters(0), waiters(new task_wait_event*[num_threads]), + prerun(std::move(prerun_)), postrun(std::move(postrun_)) {} + + // Mutex protecting everything except thread_data + std::mutex lock; + + // Array of per-thread data + aligned_array thread_data; + + // Global queue for tasks from outside the pool + fifo_queue public_queue; + + // Shutdown request indicator + bool shutdown; + + // List of threads waiting for tasks to run. num_waiters needs to be atomic + // because it is sometimes read outside the mutex. + std::atomic num_waiters; + std::unique_ptr waiters; + + // Pre/Post run functions. + std::function prerun; + std::function postrun; + +#ifdef BROKEN_JOIN_IN_DESTRUCTOR + // Shutdown complete event, used instead of thread::join() + std::size_t shutdown_num_threads; + std::condition_variable shutdown_complete_event; +#endif +}; + +// this wrapper encapsulates both the owning_threadpool pointer and the thread id. +// this is done to improve performance on the emulated thread_local reducing the number +// of calls to "pthread_getspecific" +struct threadpool_data_wrapper { + threadpool_data* owning_threadpool; + std::size_t thread_id; + + threadpool_data_wrapper(threadpool_data* owning_threadpool, std::size_t thread_id): + owning_threadpool(owning_threadpool), thread_id(thread_id) { } +}; + +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) +struct pthread_emulation_threadpool_data_initializer { + pthread_key_t key; + + pthread_emulation_threadpool_data_initializer() + { + pthread_key_create(&key, [](void* wrapper_ptr) { + threadpool_data_wrapper* wrapper = static_cast(wrapper_ptr); + delete wrapper; + }); + } + + ~pthread_emulation_threadpool_data_initializer() + { + pthread_key_delete(key); + } +}; + +static pthread_key_t get_local_threadpool_data_key() +{ + static pthread_emulation_threadpool_data_initializer initializer; + return initializer.key; +} + +#else +// Thread pool this thread belongs to, or null if not in pool +static THREAD_LOCAL threadpool_data* owning_threadpool = nullptr; + +// Current thread's index in the pool +static THREAD_LOCAL std::size_t thread_id; +#endif + +static void create_threadpool_data(threadpool_data* owning_threadpool_, std::size_t thread_id_) +{ +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) + // the memory allocated here gets deallocated by the lambda declared on the key creation + pthread_setspecific(get_local_threadpool_data_key(), new threadpool_data_wrapper(owning_threadpool_, thread_id_)); +#else + owning_threadpool = owning_threadpool_; + thread_id = thread_id_; +#endif +} + +static threadpool_data_wrapper get_threadpool_data_wrapper() +{ +#if defined(EMULATE_PTHREAD_THREAD_LOCAL) + threadpool_data_wrapper* wrapper = static_cast(pthread_getspecific(get_local_threadpool_data_key())); + if(wrapper == nullptr) { + // if, for some reason, the wrapper is not set, this won't cause a crash + return threadpool_data_wrapper(nullptr, 0); + } + return *wrapper; +#else + return threadpool_data_wrapper(owning_threadpool, thread_id); +#endif +} + +// Try to steal a task from another thread's queue +static task_run_handle steal_task(threadpool_data* impl, std::size_t thread_id) +{ + // Make a list of victim thread ids and shuffle it + std::vector victims(impl->thread_data.size()); + std::iota(victims.begin(), victims.end(), 0); + std::shuffle(victims.begin(), victims.end(), impl->thread_data[thread_id].rng); + + // Try to steal from another thread + for (std::size_t i: victims) { + // Don't try to steal from ourself + if (i == thread_id) + continue; + + if (task_run_handle t = impl->thread_data[i].queue.steal()) + return t; + } + + // No tasks found, but we might have missed one if it was just added. In + // practice this doesn't really matter since it will be handled by another + // thread. + return task_run_handle(); +} + +// Main task stealing loop which is used by worker threads when they have +// nothing to do. +static void thread_task_loop(threadpool_data* impl, std::size_t thread_id, task_wait_handle wait_task) +{ + // Get our thread's data + thread_data_t& current_thread = impl->thread_data[thread_id]; + + // Flag indicating if we have added a continuation to the task + bool added_continuation = false; + + // Event to wait on + task_wait_event event; + + // Loop while waiting for the task to complete + while (true) { + // Check if the task has finished. If we have added a continuation, we + // need to make sure the event has been signaled, otherwise the other + // thread may try to signal it after we have freed it. + if (wait_task && (added_continuation ? event.try_wait(wait_type::task_finished) : wait_task.ready())) + return; + + // Try to get a task from the local queue + if (task_run_handle t = current_thread.queue.pop()) { + t.run(); + continue; + } + + // Stealing loop + while (true) { + // Try to steal a task + if (task_run_handle t = steal_task(impl, thread_id)) { + t.run(); + break; + } + + // Try to fetch from the public queue + std::unique_lock locked(impl->lock); + if (task_run_handle t = impl->public_queue.pop()) { + // Don't hold the lock while running the task + locked.unlock(); + t.run(); + break; + } + + // If shutting down and we don't have a task to wait for, return. + if (!wait_task && impl->shutdown) { +#ifdef BROKEN_JOIN_IN_DESTRUCTOR + // Notify once all worker threads have exited + if (--impl->shutdown_num_threads == 0) + impl->shutdown_complete_event.notify_one(); +#endif + return; + } + + // Initialize the event object + event.init(); + + // No tasks found, so sleep until something happens. + // If a continuation has not been added yet, add it. + if (wait_task && !added_continuation) { + // Create a continuation for the task we are waiting for + wait_task.on_finish([&event] { + // Signal the thread's event + event.signal(wait_type::task_finished); + }); + added_continuation = true; + } + + // Add our thread to the list of waiting threads + size_t num_waiters_val = impl->num_waiters.load(std::memory_order_relaxed); + impl->waiters[num_waiters_val] = &event; + impl->num_waiters.store(num_waiters_val + 1, std::memory_order_relaxed); + + // Wait for our event to be signaled when a task is scheduled or + // the task we are waiting for has completed. + locked.unlock(); + int events = event.wait(); + locked.lock(); + + // Remove our thread from the list of waiting threads + num_waiters_val = impl->num_waiters.load(std::memory_order_relaxed); + for (std::size_t i = 0; i < num_waiters_val; i++) { + if (impl->waiters[i] == &event) { + if (i != num_waiters_val - 1) + std::swap(impl->waiters[i], impl->waiters[num_waiters_val - 1]); + impl->num_waiters.store(num_waiters_val - 1, std::memory_order_relaxed); + break; + } + } + + // Check again if the task has finished. We have added a + // continuation at this point, so we need to check that the + // continuation has finished signaling the event. + if (wait_task && (events & wait_type::task_finished)) + return; + } + } +} + +// Wait for a task to complete (for worker threads inside thread pool) +static void threadpool_wait_handler(task_wait_handle wait_task) +{ + threadpool_data_wrapper wrapper = get_threadpool_data_wrapper(); + thread_task_loop(wrapper.owning_threadpool, wrapper.thread_id, wait_task); +} + +// Worker thread main loop +static void worker_thread(threadpool_data* owning_threadpool, std::size_t thread_id) +{ + // store on the local thread data + create_threadpool_data(owning_threadpool, thread_id); + + // Set the wait handler so threads from the pool do useful work while + // waiting for another task to finish. + set_thread_wait_handler(threadpool_wait_handler); + + // Seed the random number generator with our id. This gives each thread a + // different steal order. + owning_threadpool->thread_data[thread_id].rng.seed(static_cast(thread_id)); + + // Prerun hook + if (owning_threadpool->prerun) owning_threadpool->prerun(); + + // Main loop, runs until the shutdown signal is recieved + thread_task_loop(owning_threadpool, thread_id, task_wait_handle()); + + // Postrun hook + if (owning_threadpool->postrun) owning_threadpool->postrun(); +} + +// Recursive function to spawn all worker threads in parallel +static void recursive_spawn_worker_thread(threadpool_data* impl, std::size_t index, std::size_t threads) +{ + // If we are down to one thread, go to the worker main loop + if (threads == 1) + worker_thread(impl, index); + else { + // Split thread range into 2 sub-ranges + std::size_t mid = index + threads / 2; + + // Spawn a thread for half of the range + impl->thread_data[mid].handle = std::thread(recursive_spawn_worker_thread, impl, mid, threads - threads / 2); +#ifdef BROKEN_JOIN_IN_DESTRUCTOR + impl->thread_data[mid].handle.detach(); +#endif + + // Tail-recurse to handle our half of the range + recursive_spawn_worker_thread(impl, index, threads / 2); + } +} + +} // namespace detail + +threadpool_scheduler::threadpool_scheduler(threadpool_scheduler&& other) + : impl(std::move(other.impl)) {} + +threadpool_scheduler::threadpool_scheduler(std::size_t num_threads) + : impl(new detail::threadpool_data(num_threads)) +{ + // Start worker threads + impl->thread_data[0].handle = std::thread(detail::recursive_spawn_worker_thread, impl.get(), 0, num_threads); +#ifdef BROKEN_JOIN_IN_DESTRUCTOR + impl->thread_data[0].handle.detach(); +#endif +} + +threadpool_scheduler::threadpool_scheduler(std::size_t num_threads, + std::function&& prerun, + std::function&& postrun) + : impl(new detail::threadpool_data(num_threads, std::move(prerun), std::move(postrun))) +{ + // Start worker threads + impl->thread_data[0].handle = std::thread(detail::recursive_spawn_worker_thread, impl.get(), 0, num_threads); +#ifdef BROKEN_JOIN_IN_DESTRUCTOR + impl->thread_data[0].handle.detach(); +#endif +} + + +// Wait for all currently running tasks to finish +threadpool_scheduler::~threadpool_scheduler() +{ + if (!impl) return; +#ifdef _WIN32 + // Windows kills all threads except one on process exit before calling + // global destructors in DLLs. Waiting for dead threads to exit will likely + // result in deadlocks, so we just exit early if we detect that the process + // is exiting. + auto RtlDllShutdownInProgress = reinterpret_cast(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlDllShutdownInProgress")); + if (RtlDllShutdownInProgress && RtlDllShutdownInProgress()) { +# ifndef BROKEN_JOIN_IN_DESTRUCTOR + // We still need to detach the thread handles otherwise the std::thread + // destructor will throw an exception. + for (std::size_t i = 0; i < impl->thread_data.size(); i++) { + try { + impl->thread_data[i].handle.detach(); + } catch (...) {} + } +# endif + return; + } +#endif + + { + std::unique_lock locked(impl->lock); + + // Signal shutdown + impl->shutdown = true; + + // Wake up any sleeping threads + size_t num_waiters_val = impl->num_waiters.load(std::memory_order_relaxed); + for (std::size_t i = 0; i < num_waiters_val; i++) + impl->waiters[i]->signal(detail::wait_type::task_available); + impl->num_waiters.store(0, std::memory_order_relaxed); + +#ifdef BROKEN_JOIN_IN_DESTRUCTOR + // Wait for the threads to exit + impl->shutdown_num_threads = impl->thread_data.size(); + impl->shutdown_complete_event.wait(locked); +#endif + } + +#ifndef BROKEN_JOIN_IN_DESTRUCTOR + // Wait for the threads to exit + for (std::size_t i = 0; i < impl->thread_data.size(); i++) + impl->thread_data[i].handle.join(); +#endif +} + +// Schedule a task on the thread pool +void threadpool_scheduler::schedule(task_run_handle t) +{ + detail::threadpool_data_wrapper wrapper = detail::get_threadpool_data_wrapper(); + + // Check if we are in the thread pool + if (wrapper.owning_threadpool == impl.get()) { + // Push the task onto our task queue + impl->thread_data[wrapper.thread_id].queue.push(std::move(t)); + + // If there are no sleeping threads, just return. We check outside the + // lock to avoid locking overhead in the fast path. + if (impl->num_waiters.load(std::memory_order_relaxed) == 0) + return; + + // Get a thread to wake up from the list + std::lock_guard locked(impl->lock); + + // Check again if there are waiters + size_t num_waiters_val = impl->num_waiters.load(std::memory_order_relaxed); + if (num_waiters_val == 0) + return; + + // Pop a thread from the list and wake it up + impl->waiters[num_waiters_val - 1]->signal(detail::wait_type::task_available); + impl->num_waiters.store(num_waiters_val - 1, std::memory_order_relaxed); + } else { + std::lock_guard locked(impl->lock); + + // Push task onto the public queue + impl->public_queue.push(std::move(t)); + + // Wake up a sleeping thread + size_t num_waiters_val = impl->num_waiters.load(std::memory_order_relaxed); + if (num_waiters_val == 0) + return; + impl->waiters[num_waiters_val - 1]->signal(detail::wait_type::task_available); + impl->num_waiters.store(num_waiters_val - 1, std::memory_order_relaxed); + } +} + +} // namespace async + +#ifndef LIBASYNC_STATIC +#if defined(__GNUC__) && !defined(_WIN32) +# pragma GCC visibility pop +#endif +#endif diff --git a/3party/asyncplusplus/src/work_steal_queue.h b/3party/asyncplusplus/src/work_steal_queue.h new file mode 100644 index 0000000..004ab9a --- /dev/null +++ b/3party/asyncplusplus/src/work_steal_queue.h @@ -0,0 +1,186 @@ +// Copyright (c) 2015 Amanieu d'Antras +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef ASYNCXX_H_ +# error "Do not include this header directly, include instead." +#endif + +namespace async { +namespace detail { + +// Chase-Lev work stealing deque +// +// Dynamic Circular Work-Stealing Deque +// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.170.1097&rep=rep1&type=pdf +// +// Correct and Efficient Work-Stealing for Weak Memory Models +// http://www.di.ens.fr/~zappa/readings/ppopp13.pdf +class work_steal_queue { + // Circular array of void* + class circular_array { + detail::aligned_array items; + std::unique_ptr previous; + + public: + circular_array(std::size_t n) + : items(n) {} + + std::size_t size() const + { + return items.size(); + } + + void* get(std::size_t index) + { + return items[index & (size() - 1)]; + } + + void put(std::size_t index, void* x) + { + items[index & (size() - 1)] = x; + } + + // Growing the array returns a new circular_array object and keeps a + // linked list of all previous arrays. This is done because other threads + // could still be accessing elements from the smaller arrays. + circular_array* grow(std::size_t top, std::size_t bottom) + { + circular_array* new_array = new circular_array(size() * 2); + new_array->previous.reset(this); + for (std::size_t i = top; i != bottom; i++) + new_array->put(i, get(i)); + return new_array; + } + }; + + std::atomic array; + std::atomic top, bottom; + + // Convert a 2's complement unsigned value to a signed value. We need to do + // this because (b - t) may not always be positive. + static std::ptrdiff_t to_signed(std::size_t x) + { + // Unsigned to signed conversion is implementation-defined if the value + // doesn't fit, so we convert manually. + static_assert(static_cast(PTRDIFF_MAX) + 1 == static_cast(PTRDIFF_MIN), "Wrong integer wrapping behavior"); + if (x > static_cast(PTRDIFF_MAX)) + return static_cast(x - static_cast(PTRDIFF_MIN)) + PTRDIFF_MIN; + else + return static_cast(x); + } + +public: + work_steal_queue() + : array(new circular_array(32)), top(0), bottom(0) {} + ~work_steal_queue() + { + // Free any unexecuted tasks + std::size_t b = bottom.load(std::memory_order_relaxed); + std::size_t t = top.load(std::memory_order_relaxed); + circular_array* a = array.load(std::memory_order_relaxed); + for (std::size_t i = t; i != b; i++) + task_run_handle::from_void_ptr(a->get(i)); + delete a; + } + + // Push a task to the bottom of this thread's queue + void push(task_run_handle x) + { + std::size_t b = bottom.load(std::memory_order_relaxed); + std::size_t t = top.load(std::memory_order_acquire); + circular_array* a = array.load(std::memory_order_relaxed); + + // Grow the array if it is full + if (to_signed(b - t) >= to_signed(a->size())) { + a = a->grow(t, b); + array.store(a, std::memory_order_release); + } + + // Note that we only convert to void* here in case grow throws due to + // lack of memory. + a->put(b, x.to_void_ptr()); + std::atomic_thread_fence(std::memory_order_release); + bottom.store(b + 1, std::memory_order_relaxed); + } + + // Pop a task from the bottom of this thread's queue + task_run_handle pop() + { + std::size_t b = bottom.load(std::memory_order_relaxed); + + // Early exit if queue is empty + std::size_t t = top.load(std::memory_order_relaxed); + if (to_signed(b - t) <= 0) + return task_run_handle(); + + // Make sure bottom is stored before top is read + bottom.store(--b, std::memory_order_relaxed); + std::atomic_thread_fence(std::memory_order_seq_cst); + t = top.load(std::memory_order_relaxed); + + // If the queue is empty, restore bottom and exit + if (to_signed(b - t) < 0) { + bottom.store(b + 1, std::memory_order_relaxed); + return task_run_handle(); + } + + // Fetch the element from the queue + circular_array* a = array.load(std::memory_order_relaxed); + void* x = a->get(b); + + // If this was the last element in the queue, check for races + if (b == t) { + if (!top.compare_exchange_strong(t, t + 1, std::memory_order_seq_cst, std::memory_order_relaxed)) { + bottom.store(b + 1, std::memory_order_relaxed); + return task_run_handle(); + } + bottom.store(b + 1, std::memory_order_relaxed); + } + return task_run_handle::from_void_ptr(x); + } + + // Steal a task from the top of this thread's queue + task_run_handle steal() + { + // Loop while the compare_exchange fails. This is still lock-free because + // a fail means that another thread has sucessfully stolen a task. + while (true) { + // Make sure top is read before bottom + std::size_t t = top.load(std::memory_order_acquire); + std::atomic_thread_fence(std::memory_order_seq_cst); + std::size_t b = bottom.load(std::memory_order_acquire); + + // Exit if the queue is empty + if (to_signed(b - t) <= 0) + return task_run_handle(); + + // Fetch the element from the queue + circular_array* a = array.load(std::memory_order_consume); + void* x = a->get(t); + + // Attempt to increment top + if (top.compare_exchange_weak(t, t + 1, std::memory_order_seq_cst, std::memory_order_relaxed)) + return task_run_handle::from_void_ptr(x); + } + } +}; + +} // namespace detail +} // namespace async diff --git a/3party/fmt/fmt/format.h b/3party/fmt/fmt/format.h index 431d00b..b5e78a9 100644 --- a/3party/fmt/fmt/format.h +++ b/3party/fmt/fmt/format.h @@ -38,274 +38,267 @@ #include #include #include +#include // for std::pair #include -#include // for std::pair #undef FMT_INCLUDE // The fmt library version in the form major * 10000 + minor * 100 + patch. #define FMT_VERSION 40100 #if defined(__has_include) -# define FMT_HAS_INCLUDE(x) __has_include(x) +#define FMT_HAS_INCLUDE(x) __has_include(x) #else -# define FMT_HAS_INCLUDE(x) 0 +#define FMT_HAS_INCLUDE(x) 0 #endif -#if (FMT_HAS_INCLUDE() && __cplusplus > 201402L) || \ - (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) -# include -# define FMT_HAS_STRING_VIEW 1 +#if (FMT_HAS_INCLUDE() && __cplusplus > 201402L) \ + || (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) +#include +#define FMT_HAS_STRING_VIEW 1 #else -# define FMT_HAS_STRING_VIEW 0 +#define FMT_HAS_STRING_VIEW 0 #endif #if defined _SECURE_SCL && _SECURE_SCL -# define FMT_SECURE_SCL _SECURE_SCL +#define FMT_SECURE_SCL _SECURE_SCL #else -# define FMT_SECURE_SCL 0 +#define FMT_SECURE_SCL 0 #endif #if FMT_SECURE_SCL -# include +#include #endif #ifdef _MSC_VER -# define FMT_MSC_VER _MSC_VER +#define FMT_MSC_VER _MSC_VER #else -# define FMT_MSC_VER 0 +#define FMT_MSC_VER 0 #endif #if FMT_MSC_VER && FMT_MSC_VER <= 1500 typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; -typedef __int64 intmax_t; +typedef __int64 intmax_t; #else #include #endif #if !defined(FMT_HEADER_ONLY) && defined(_WIN32) -# ifdef FMT_EXPORT -# define FMT_API __declspec(dllexport) -# elif defined(FMT_SHARED) -# define FMT_API __declspec(dllimport) -# endif +#ifdef FMT_EXPORT +#define FMT_API __declspec(dllexport) +#elif defined(FMT_SHARED) +#define FMT_API __declspec(dllimport) +#endif #endif #ifndef FMT_API -# define FMT_API +#define FMT_API #endif #ifdef __GNUC__ -# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) -# define FMT_GCC_EXTENSION __extension__ -# if FMT_GCC_VERSION >= 406 -# pragma GCC diagnostic push +#define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#define FMT_GCC_EXTENSION __extension__ +#if FMT_GCC_VERSION >= 406 +#pragma GCC diagnostic push // Disable the warning about "long long" which is sometimes reported even // when using __extension__. -# pragma GCC diagnostic ignored "-Wlong-long" +#pragma GCC diagnostic ignored "-Wlong-long" // Disable the warning about declaration shadowing because it affects too // many valid cases. -# pragma GCC diagnostic ignored "-Wshadow" +#pragma GCC diagnostic ignored "-Wshadow" // Disable the warning about implicit conversions that may change the sign of // an integer; silencing it otherwise would require many explicit casts. -# pragma GCC diagnostic ignored "-Wsign-conversion" -# endif -# if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__ -# define FMT_HAS_GXX_CXX11 1 -# endif +#pragma GCC diagnostic ignored "-Wsign-conversion" +#endif +#if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__ +#define FMT_HAS_GXX_CXX11 1 +#endif #else -# define FMT_GCC_VERSION 0 -# define FMT_GCC_EXTENSION -# define FMT_HAS_GXX_CXX11 0 +#define FMT_GCC_VERSION 0 +#define FMT_GCC_EXTENSION +#define FMT_HAS_GXX_CXX11 0 #endif #if defined(__INTEL_COMPILER) -# define FMT_ICC_VERSION __INTEL_COMPILER +#define FMT_ICC_VERSION __INTEL_COMPILER #elif defined(__ICL) -# define FMT_ICC_VERSION __ICL +#define FMT_ICC_VERSION __ICL #endif #if defined(__clang__) && !defined(FMT_ICC_VERSION) -# define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wdocumentation-unknown-command" -# pragma clang diagnostic ignored "-Wpadded" +#define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdocumentation-unknown-command" +#pragma clang diagnostic ignored "-Wpadded" #endif #ifdef __GNUC_LIBSTD__ -# define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__) +#define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__) #endif #ifdef __has_feature -# define FMT_HAS_FEATURE(x) __has_feature(x) +#define FMT_HAS_FEATURE(x) __has_feature(x) #else -# define FMT_HAS_FEATURE(x) 0 +#define FMT_HAS_FEATURE(x) 0 #endif #ifdef __has_builtin -# define FMT_HAS_BUILTIN(x) __has_builtin(x) +#define FMT_HAS_BUILTIN(x) __has_builtin(x) #else -# define FMT_HAS_BUILTIN(x) 0 +#define FMT_HAS_BUILTIN(x) 0 #endif #ifdef __has_cpp_attribute -# define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) +#define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) #else -# define FMT_HAS_CPP_ATTRIBUTE(x) 0 +#define FMT_HAS_CPP_ATTRIBUTE(x) 0 #endif #if FMT_HAS_CPP_ATTRIBUTE(maybe_unused) -# define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED +#define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED // VC++ 1910 support /std: option and that will set _MSVC_LANG macro // Clang with Microsoft CodeGen doesn't define _MSVC_LANG macro #elif defined(_MSVC_LANG) && _MSVC_LANG > 201402 && _MSC_VER >= 1910 -# define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED +#define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED #endif #ifdef FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED // # define FMT_MAYBE_UNUSED [[maybe_unused]] -# define FMT_MAYBE_UNUSED +#define FMT_MAYBE_UNUSED // g++/clang++ also support [[gnu::unused]]. However, we don't use it. #elif defined(__GNUC__) -# define FMT_MAYBE_UNUSED __attribute__((unused)) +#define FMT_MAYBE_UNUSED __attribute__((unused)) #else -# define FMT_MAYBE_UNUSED +#define FMT_MAYBE_UNUSED #endif // Use the compiler's attribute noreturn #if defined(__MINGW32__) || defined(__MINGW64__) -# define FMT_NORETURN __attribute__((noreturn)) +#define FMT_NORETURN __attribute__((noreturn)) #elif FMT_HAS_CPP_ATTRIBUTE(noreturn) && __cplusplus >= 201103L -# define FMT_NORETURN [[noreturn]] +#define FMT_NORETURN [[noreturn]] #else -# define FMT_NORETURN +#define FMT_NORETURN #endif #ifndef FMT_USE_VARIADIC_TEMPLATES // Variadic templates are available in GCC since version 4.4 // (http://gcc.gnu.org/projects/cxx0x.html) and in Visual C++ // since version 2013. -# define FMT_USE_VARIADIC_TEMPLATES \ - (FMT_HAS_FEATURE(cxx_variadic_templates) || \ - (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800) +#define FMT_USE_VARIADIC_TEMPLATES \ + (FMT_HAS_FEATURE(cxx_variadic_templates) || (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800) #endif #ifndef FMT_USE_RVALUE_REFERENCES // Don't use rvalue references when compiling with clang and an old libstdc++ // as the latter doesn't provide std::move. -# if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402 -# define FMT_USE_RVALUE_REFERENCES 0 -# else -# define FMT_USE_RVALUE_REFERENCES \ - (FMT_HAS_FEATURE(cxx_rvalue_references) || \ - (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600) -# endif +#if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402 +#define FMT_USE_RVALUE_REFERENCES 0 +#else +#define FMT_USE_RVALUE_REFERENCES \ + (FMT_HAS_FEATURE(cxx_rvalue_references) || (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600) +#endif #endif #if __cplusplus >= 201103L || FMT_MSC_VER >= 1700 -# define FMT_USE_ALLOCATOR_TRAITS 1 +#define FMT_USE_ALLOCATOR_TRAITS 1 #else -# define FMT_USE_ALLOCATOR_TRAITS 0 +#define FMT_USE_ALLOCATOR_TRAITS 0 #endif // Check if exceptions are disabled. #if defined(__GNUC__) && !defined(__EXCEPTIONS) -# define FMT_EXCEPTIONS 0 +#define FMT_EXCEPTIONS 0 #endif #if FMT_MSC_VER && !_HAS_EXCEPTIONS -# define FMT_EXCEPTIONS 0 +#define FMT_EXCEPTIONS 0 #endif #ifndef FMT_EXCEPTIONS -# define FMT_EXCEPTIONS 1 +#define FMT_EXCEPTIONS 1 #endif #ifndef FMT_THROW -# if FMT_EXCEPTIONS -# define FMT_THROW(x) throw x -# else -# define FMT_THROW(x) assert(false) -# endif +#if FMT_EXCEPTIONS +#define FMT_THROW(x) throw x +#else +#define FMT_THROW(x) assert(false) +#endif #endif // Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature). #ifndef FMT_USE_NOEXCEPT -# define FMT_USE_NOEXCEPT 0 +#define FMT_USE_NOEXCEPT 0 #endif -#if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \ - (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \ - FMT_MSC_VER >= 1900 -# define FMT_DETECTED_NOEXCEPT noexcept +#if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) \ + || FMT_MSC_VER >= 1900 +#define FMT_DETECTED_NOEXCEPT noexcept #else -# define FMT_DETECTED_NOEXCEPT throw() +#define FMT_DETECTED_NOEXCEPT throw() #endif #ifndef FMT_NOEXCEPT -# if FMT_EXCEPTIONS -# define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT -# else -# define FMT_NOEXCEPT -# endif +#if FMT_EXCEPTIONS +#define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT +#else +#define FMT_NOEXCEPT +#endif #endif // This is needed because GCC still uses throw() in its headers when exceptions // are disabled. #if FMT_GCC_VERSION -# define FMT_DTOR_NOEXCEPT FMT_DETECTED_NOEXCEPT +#define FMT_DTOR_NOEXCEPT FMT_DETECTED_NOEXCEPT #else -# define FMT_DTOR_NOEXCEPT FMT_NOEXCEPT +#define FMT_DTOR_NOEXCEPT FMT_NOEXCEPT #endif #ifndef FMT_OVERRIDE -# if (defined(FMT_USE_OVERRIDE) && FMT_USE_OVERRIDE) || FMT_HAS_FEATURE(cxx_override) || \ - (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \ - FMT_MSC_VER >= 1900 -# define FMT_OVERRIDE override -# else -# define FMT_OVERRIDE -# endif +#if (defined(FMT_USE_OVERRIDE) && FMT_USE_OVERRIDE) || FMT_HAS_FEATURE(cxx_override) \ + || (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900 +#define FMT_OVERRIDE override +#else +#define FMT_OVERRIDE +#endif #endif #ifndef FMT_NULL -# if FMT_HAS_FEATURE(cxx_nullptr) || \ - (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \ - FMT_MSC_VER >= 1600 -# define FMT_NULL nullptr -# else -# define FMT_NULL NULL -# endif +#if FMT_HAS_FEATURE(cxx_nullptr) || (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600 +#define FMT_NULL nullptr +#else +#define FMT_NULL NULL +#endif #endif // A macro to disallow the copy constructor and operator= functions // This should be used in the private: declarations for a class #ifndef FMT_USE_DELETED_FUNCTIONS -# define FMT_USE_DELETED_FUNCTIONS 0 +#define FMT_USE_DELETED_FUNCTIONS 0 #endif -#if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || \ - (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800 -# define FMT_DELETED_OR_UNDEFINED = delete -# define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ - TypeName(const TypeName&) = delete; \ - TypeName& operator=(const TypeName&) = delete +#if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) \ + || (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800 +#define FMT_DELETED_OR_UNDEFINED = delete +#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName &) = delete; \ + TypeName &operator=(const TypeName &) = delete #else -# define FMT_DELETED_OR_UNDEFINED -# define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ - TypeName(const TypeName&); \ - TypeName& operator=(const TypeName&) +#define FMT_DELETED_OR_UNDEFINED +#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName &); \ + TypeName &operator=(const TypeName &) #endif #ifndef FMT_USE_DEFAULTED_FUNCTIONS -# define FMT_USE_DEFAULTED_FUNCTIONS 0 +#define FMT_USE_DEFAULTED_FUNCTIONS 0 #endif #ifndef FMT_DEFAULTED_COPY_CTOR -# if FMT_USE_DEFAULTED_FUNCTIONS || FMT_HAS_FEATURE(cxx_defaulted_functions) || \ - (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800 -# define FMT_DEFAULTED_COPY_CTOR(TypeName) \ - TypeName(const TypeName&) = default; -# else -# define FMT_DEFAULTED_COPY_CTOR(TypeName) -# endif +#if FMT_USE_DEFAULTED_FUNCTIONS || FMT_HAS_FEATURE(cxx_defaulted_functions) \ + || (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800 +#define FMT_DEFAULTED_COPY_CTOR(TypeName) TypeName(const TypeName &) = default; +#else +#define FMT_DEFAULTED_COPY_CTOR(TypeName) +#endif #endif #ifndef FMT_USE_USER_DEFINED_LITERALS @@ -313,41 +306,39 @@ typedef __int64 intmax_t; // makes the fmt::literals implementation easier. However, an explicit check // for variadic templates is added here just in case. // For Intel's compiler both it and the system gcc/msc must support UDLs. -# if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES && \ - (FMT_HAS_FEATURE(cxx_user_literals) || \ - (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900) && \ - (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500) -# define FMT_USE_USER_DEFINED_LITERALS 1 -# else -# define FMT_USE_USER_DEFINED_LITERALS 0 -# endif +#if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES \ + && (FMT_HAS_FEATURE(cxx_user_literals) || (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900) \ + && (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500) +#define FMT_USE_USER_DEFINED_LITERALS 1 +#else +#define FMT_USE_USER_DEFINED_LITERALS 0 +#endif #endif #ifndef FMT_USE_EXTERN_TEMPLATES -# define FMT_USE_EXTERN_TEMPLATES \ - (FMT_CLANG_VERSION >= 209 || (FMT_GCC_VERSION >= 303 && FMT_HAS_GXX_CXX11)) +#define FMT_USE_EXTERN_TEMPLATES (FMT_CLANG_VERSION >= 209 || (FMT_GCC_VERSION >= 303 && FMT_HAS_GXX_CXX11)) #endif #ifdef FMT_HEADER_ONLY // If header only do not use extern templates. -# undef FMT_USE_EXTERN_TEMPLATES -# define FMT_USE_EXTERN_TEMPLATES 0 +#undef FMT_USE_EXTERN_TEMPLATES +#define FMT_USE_EXTERN_TEMPLATES 0 #endif #ifndef FMT_ASSERT -# define FMT_ASSERT(condition, message) assert((condition) && message) +#define FMT_ASSERT(condition, message) assert((condition) && message) #endif // __builtin_clz is broken in clang with Microsoft CodeGen: // https://github.com/fmtlib/fmt/issues/519 #ifndef _MSC_VER -# if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz) -# define FMT_BUILTIN_CLZ(n) __builtin_clz(n) -# endif +#if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz) +#define FMT_BUILTIN_CLZ(n) __builtin_clz(n) +#endif -# if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll) -# define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) -# endif +#if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll) +#define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) +#endif #endif // Some compilers masquerade as both MSVC and GCC-likes or @@ -355,130 +346,165 @@ typedef __int64 intmax_t; // only define FMT_BUILTIN_CLZ using the MSVC intrinsics // if the clz and clzll builtins are not available. #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && !defined(_MANAGED) -# include // _BitScanReverse, _BitScanReverse64 +#include // _BitScanReverse, _BitScanReverse64 namespace fmt { namespace internal { // avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning -# ifndef __clang__ -# pragma intrinsic(_BitScanReverse) -# endif -inline uint32_t clz(uint32_t x) { - unsigned long r = 0; - _BitScanReverse(&r, x); +#ifndef __clang__ +#pragma intrinsic(_BitScanReverse) +#endif +inline uint32_t +clz(uint32_t x) +{ + unsigned long r = 0; + _BitScanReverse(&r, x); - assert(x != 0); - // Static analysis complains about using uninitialized data - // "r", but the only way that can happen is if "x" is 0, - // which the callers guarantee to not happen. -# pragma warning(suppress: 6102) - return 31 - r; + assert(x != 0); + // Static analysis complains about using uninitialized data + // "r", but the only way that can happen is if "x" is 0, + // which the callers guarantee to not happen. +#pragma warning(suppress : 6102) + return 31 - r; } -# define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n) + +#define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n) // avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning -# if defined(_WIN64) && !defined(__clang__) -# pragma intrinsic(_BitScanReverse64) -# endif +#if defined(_WIN64) && !defined(__clang__) +#pragma intrinsic(_BitScanReverse64) +#endif -inline uint32_t clzll(uint64_t x) { - unsigned long r = 0; -# ifdef _WIN64 - _BitScanReverse64(&r, x); -# else - // Scan the high 32 bits. - if (_BitScanReverse(&r, static_cast(x >> 32))) - return 63 - (r + 32); +inline uint32_t +clzll(uint64_t x) +{ + unsigned long r = 0; +#ifdef _WIN64 + _BitScanReverse64(&r, x); +#else + // Scan the high 32 bits. + if (_BitScanReverse(&r, static_cast(x >> 32))) return 63 - (r + 32); - // Scan the low 32 bits. - _BitScanReverse(&r, static_cast(x)); -# endif + // Scan the low 32 bits. + _BitScanReverse(&r, static_cast(x)); +#endif - assert(x != 0); - // Static analysis complains about using uninitialized data - // "r", but the only way that can happen is if "x" is 0, - // which the callers guarantee to not happen. -# pragma warning(suppress: 6102) - return 63 - r; -} -# define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n) -} + assert(x != 0); + // Static analysis complains about using uninitialized data + // "r", but the only way that can happen is if "x" is 0, + // which the callers guarantee to not happen. +#pragma warning(suppress : 6102) + return 63 - r; } + +#define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n) +}// namespace internal +}// namespace fmt #endif namespace fmt { namespace internal { struct DummyInt { - int data[2]; - operator int() const { return 0; } + int data[2]; + + operator int() const { return 0; } }; + typedef std::numeric_limits FPUtil; // Dummy implementations of system functions such as signbit and ecvt called // if the latter are not available. -inline DummyInt signbit(...) { return DummyInt(); } -inline DummyInt _ecvt_s(...) { return DummyInt(); } -inline DummyInt isinf(...) { return DummyInt(); } -inline DummyInt _finite(...) { return DummyInt(); } -inline DummyInt isnan(...) { return DummyInt(); } -inline DummyInt _isnan(...) { return DummyInt(); } +inline DummyInt +signbit(...) +{ + return DummyInt(); +} + +inline DummyInt +_ecvt_s(...) +{ + return DummyInt(); +} + +inline DummyInt +isinf(...) +{ + return DummyInt(); +} + +inline DummyInt +_finite(...) +{ + return DummyInt(); +} + +inline DummyInt +isnan(...) +{ + return DummyInt(); +} + +inline DummyInt +_isnan(...) +{ + return DummyInt(); +} // A helper function to suppress bogus "conditional expression is constant" // warnings. -template -inline T const_check(T value) { return value; } +template +inline T +const_check(T value) +{ + return value; } -} // namespace fmt +}// namespace internal +}// namespace fmt namespace std { // Standard permits specialization of std::numeric_limits. This specialization // is used to resolve ambiguity between isinf and std::isinf in glibc: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891 // and the same for isnan and signbit. -template <> -class numeric_limits : - public std::numeric_limits { - public: - // Portable version of isinf. - template - static bool isinfinity(T x) { - using namespace fmt::internal; - // The resolution "priority" is: - // isinf macro > std::isinf > ::isinf > fmt::internal::isinf - if (const_check(sizeof(isinf(x)) == sizeof(bool) || - sizeof(isinf(x)) == sizeof(int))) { - return isinf(x) != 0; +template<> +class numeric_limits : public std::numeric_limits { +public: + // Portable version of isinf. + template + static bool isinfinity(T x) + { + using namespace fmt::internal; + // The resolution "priority" is: + // isinf macro > std::isinf > ::isinf > fmt::internal::isinf + if (const_check(sizeof(isinf(x)) == sizeof(bool) || sizeof(isinf(x)) == sizeof(int))) { return isinf(x) != 0; } + return !_finite(static_cast(x)); } - return !_finite(static_cast(x)); - } - // Portable version of isnan. - template - static bool isnotanumber(T x) { - using namespace fmt::internal; - if (const_check(sizeof(isnan(x)) == sizeof(bool) || - sizeof(isnan(x)) == sizeof(int))) { - return isnan(x) != 0; + // Portable version of isnan. + template + static bool isnotanumber(T x) + { + using namespace fmt::internal; + if (const_check(sizeof(isnan(x)) == sizeof(bool) || sizeof(isnan(x)) == sizeof(int))) { return isnan(x) != 0; } + return _isnan(static_cast(x)) != 0; } - return _isnan(static_cast(x)) != 0; - } - // Portable version of signbit. - static bool isnegative(double x) { - using namespace fmt::internal; - if (const_check(sizeof(signbit(x)) == sizeof(bool) || - sizeof(signbit(x)) == sizeof(int))) { - return signbit(x) != 0; + // Portable version of signbit. + static bool isnegative(double x) + { + using namespace fmt::internal; + if (const_check(sizeof(signbit(x)) == sizeof(bool) || sizeof(signbit(x)) == sizeof(int))) { + return signbit(x) != 0; + } + if (x < 0) return true; + if (!isnotanumber(x)) return false; + int dec = 0, sign = 0; + char buffer[2];// The buffer size must be >= 2 or _ecvt_s will fail. + _ecvt_s(buffer, sizeof(buffer), x, 0, &dec, &sign); + return sign != 0; } - if (x < 0) return true; - if (!isnotanumber(x)) return false; - int dec = 0, sign = 0; - char buffer[2]; // The buffer size must be >= 2 or _ecvt_s will fail. - _ecvt_s(buffer, sizeof(buffer), x, 0, &dec, &sign); - return sign != 0; - } }; -} // namespace std +}// namespace std namespace fmt { @@ -491,22 +517,21 @@ FMT_GCC_EXTENSION typedef unsigned long long ULongLong; using std::move; #endif -template +template class BasicWriter; typedef BasicWriter Writer; typedef BasicWriter WWriter; -template +template class ArgFormatter; struct FormatSpec; -template +template class BasicPrintfArgFormatter; -template > +template> class BasicFormatter; /** @@ -534,97 +559,87 @@ class BasicFormatter; format(std::string("{}"), 42); \endrst */ -template +template class BasicStringRef { - private: - const Char *data_; - std::size_t size_; +private: + const Char *data_; + std::size_t size_; - public: - /** Constructs a string reference object from a C string and a size. */ - BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {} +public: + /** Constructs a string reference object from a C string and a size. */ + BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {} - /** + /** \rst Constructs a string reference object from a C string computing the size with ``std::char_traits::length``. \endrst */ - BasicStringRef(const Char *s) - : data_(s), size_(std::char_traits::length(s)) {} + BasicStringRef(const Char *s) : data_(s), size_(std::char_traits::length(s)) {} - /** + /** \rst Constructs a string reference from a ``std::basic_string`` object. \endrst */ - template - BasicStringRef( - const std::basic_string, Allocator> &s) - : data_(s.c_str()), size_(s.size()) {} + template + BasicStringRef(const std::basic_string, Allocator> &s) + : data_(s.c_str()), + size_(s.size()) + {} #if FMT_HAS_STRING_VIEW - /** + /** \rst Constructs a string reference from a ``std::basic_string_view`` object. \endrst */ - BasicStringRef( - const std::basic_string_view> &s) - : data_(s.data()), size_(s.size()) {} + BasicStringRef(const std::basic_string_view> &s) : data_(s.data()), size_(s.size()) {} - /** + /** \rst Converts a string reference to an ``std::string_view`` object. \endrst */ - explicit operator std::basic_string_view() const FMT_NOEXCEPT { - return std::basic_string_view(data_, size_); - } + explicit operator std::basic_string_view() const FMT_NOEXCEPT + { + return std::basic_string_view(data_, size_); + } #endif - /** + /** \rst Converts a string reference to an ``std::string`` object. \endrst */ - std::basic_string to_string() const { - return std::basic_string(data_, size_); - } + std::basic_string to_string() const { return std::basic_string(data_, size_); } - /** Returns a pointer to the string data. */ - const Char *data() const { return data_; } + /** Returns a pointer to the string data. */ + const Char *data() const { return data_; } - /** Returns the string size. */ - std::size_t size() const { return size_; } + /** Returns the string size. */ + std::size_t size() const { return size_; } - // Lexicographically compare this string reference to other. - int compare(BasicStringRef other) const { - std::size_t size = size_ < other.size_ ? size_ : other.size_; - int result = std::char_traits::compare(data_, other.data_, size); - if (result == 0) - result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1); - return result; - } + // Lexicographically compare this string reference to other. + int compare(BasicStringRef other) const + { + std::size_t size = size_ < other.size_ ? size_ : other.size_; + int result = std::char_traits::compare(data_, other.data_, size); + if (result == 0) result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1); + return result; + } - friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) { - return lhs.compare(rhs) == 0; - } - friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) { - return lhs.compare(rhs) != 0; - } - friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) { - return lhs.compare(rhs) < 0; - } - friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) { - return lhs.compare(rhs) <= 0; - } - friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) { - return lhs.compare(rhs) > 0; - } - friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) { - return lhs.compare(rhs) >= 0; - } + friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) { return lhs.compare(rhs) == 0; } + + friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) { return lhs.compare(rhs) != 0; } + + friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) { return lhs.compare(rhs) < 0; } + + friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) { return lhs.compare(rhs) <= 0; } + + friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) { return lhs.compare(rhs) > 0; } + + friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) { return lhs.compare(rhs) >= 0; } }; typedef BasicStringRef StringRef; @@ -655,27 +670,26 @@ typedef BasicStringRef WStringRef; format(std::string("{}"), 42); \endrst */ -template +template class BasicCStringRef { - private: - const Char *data_; +private: + const Char *data_; - public: - /** Constructs a string reference object from a C string. */ - BasicCStringRef(const Char *s) : data_(s) {} +public: + /** Constructs a string reference object from a C string. */ + BasicCStringRef(const Char *s) : data_(s) {} - /** + /** \rst Constructs a string reference from a ``std::basic_string`` object. \endrst */ - template - BasicCStringRef( - const std::basic_string, Allocator> &s) - : data_(s.c_str()) {} + template + BasicCStringRef(const std::basic_string, Allocator> &s) : data_(s.c_str()) + {} - /** Returns the pointer to a C string. */ - const Char *c_str() const { return data_; } + /** Returns the pointer to a C string. */ + const Char *c_str() const { return data_; } }; typedef BasicCStringRef CStringRef; @@ -683,22 +697,27 @@ typedef BasicCStringRef WCStringRef; /** A formatting error such as invalid format string. */ class FormatError : public std::runtime_error { - public: - explicit FormatError(CStringRef message) - : std::runtime_error(message.c_str()) {} - FormatError(const FormatError &ferr) : std::runtime_error(ferr) {} - FMT_API ~FormatError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE; +public: + explicit FormatError(CStringRef message) : std::runtime_error(message.c_str()) {} + + FormatError(const FormatError &ferr) : std::runtime_error(ferr) {} + + FMT_API ~FormatError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE; }; namespace internal { // MakeUnsigned::Type gives an unsigned type corresponding to integer type T. -template -struct MakeUnsigned { typedef T Type; }; +template +struct MakeUnsigned { + typedef T Type; +}; -#define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \ - template <> \ - struct MakeUnsigned { typedef U Type; } +#define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \ + template<> \ + struct MakeUnsigned { \ + typedef U Type; \ + } FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char); FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char); @@ -708,10 +727,12 @@ FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long); FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong); // Casts nonnegative integer to unsigned. -template -inline typename MakeUnsigned::Type to_unsigned(Int value) { - FMT_ASSERT(value >= 0, "negative value"); - return static_cast::Type>(value); +template +inline typename MakeUnsigned::Type +to_unsigned(Int value) +{ + FMT_ASSERT(value >= 0, "negative value"); + return static_cast::Type>(value); } // The number of characters to store in the MemoryBuffer object itself @@ -720,294 +741,322 @@ enum { INLINE_BUFFER_SIZE = 500 }; #if FMT_SECURE_SCL // Use checked iterator to avoid warnings on MSVC. -template -inline stdext::checked_array_iterator make_ptr(T *ptr, std::size_t size) { - return stdext::checked_array_iterator(ptr, size); +template +inline stdext::checked_array_iterator +make_ptr(T *ptr, std::size_t size) +{ + return stdext::checked_array_iterator(ptr, size); } #else -template -inline T *make_ptr(T *ptr, std::size_t) { return ptr; } +template +inline T * +make_ptr(T *ptr, std::size_t) +{ + return ptr; +} #endif -} // namespace internal +}// namespace internal /** \rst A buffer supporting a subset of ``std::vector``'s operations. \endrst */ -template +template class Buffer { - private: - FMT_DISALLOW_COPY_AND_ASSIGN(Buffer); +private: + FMT_DISALLOW_COPY_AND_ASSIGN(Buffer); - protected: - T *ptr_; - std::size_t size_; - std::size_t capacity_; +protected: + T *ptr_; + std::size_t size_; + std::size_t capacity_; - Buffer(T *ptr = FMT_NULL, std::size_t capacity = 0) - : ptr_(ptr), size_(0), capacity_(capacity) {} + Buffer(T *ptr = FMT_NULL, std::size_t capacity = 0) : ptr_(ptr), size_(0), capacity_(capacity) {} - /** + /** \rst Increases the buffer capacity to hold at least *size* elements updating ``ptr_`` and ``capacity_``. \endrst */ - virtual void grow(std::size_t size) = 0; + virtual void grow(std::size_t size) = 0; - public: - virtual ~Buffer() {} +public: + virtual ~Buffer() {} - /** Returns the size of this buffer. */ - std::size_t size() const { return size_; } + /** Returns the size of this buffer. */ + std::size_t size() const { return size_; } - /** Returns the capacity of this buffer. */ - std::size_t capacity() const { return capacity_; } + /** Returns the capacity of this buffer. */ + std::size_t capacity() const { return capacity_; } - /** + /** Resizes the buffer. If T is a POD type new elements may not be initialized. */ - void resize(std::size_t new_size) { - if (new_size > capacity_) - grow(new_size); - size_ = new_size; - } + void resize(std::size_t new_size) + { + if (new_size > capacity_) grow(new_size); + size_ = new_size; + } - /** + /** \rst Reserves space to store at least *capacity* elements. \endrst */ - void reserve(std::size_t capacity) { - if (capacity > capacity_) - grow(capacity); - } + void reserve(std::size_t capacity) + { + if (capacity > capacity_) grow(capacity); + } - void clear() FMT_NOEXCEPT { size_ = 0; } + void clear() FMT_NOEXCEPT { size_ = 0; } - void push_back(const T &value) { - if (size_ == capacity_) - grow(size_ + 1); - ptr_[size_++] = value; - } + void push_back(const T &value) + { + if (size_ == capacity_) grow(size_ + 1); + ptr_[size_++] = value; + } - /** Appends data to the end of the buffer. */ - template - void append(const U *begin, const U *end); + /** Appends data to the end of the buffer. */ + template + void append(const U *begin, const U *end); - T &operator[](std::size_t index) { return ptr_[index]; } - const T &operator[](std::size_t index) const { return ptr_[index]; } + T &operator[](std::size_t index) { return ptr_[index]; } + + const T &operator[](std::size_t index) const { return ptr_[index]; } }; -template -template -void Buffer::append(const U *begin, const U *end) { - FMT_ASSERT(end >= begin, "negative value"); - std::size_t new_size = size_ + static_cast(end - begin); - if (new_size > capacity_) - grow(new_size); - std::uninitialized_copy(begin, end, - internal::make_ptr(ptr_, capacity_) + size_); - size_ = new_size; +template +template +void +Buffer::append(const U *begin, const U *end) +{ + FMT_ASSERT(end >= begin, "negative value"); + std::size_t new_size = size_ + static_cast(end - begin); + if (new_size > capacity_) grow(new_size); + std::uninitialized_copy(begin, end, internal::make_ptr(ptr_, capacity_) + size_); + size_ = new_size; } namespace internal { // A memory buffer for trivially copyable/constructible types with the first // SIZE elements stored in the object itself. -template > +template> class MemoryBuffer : private Allocator, public Buffer { - private: - T data_[SIZE]; +private: + T data_[SIZE]; - // Deallocate memory allocated by the buffer. - void deallocate() { - if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_); - } + // Deallocate memory allocated by the buffer. + void deallocate() + { + if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_); + } - protected: - void grow(std::size_t size) FMT_OVERRIDE; +protected: + void grow(std::size_t size) FMT_OVERRIDE; - public: - explicit MemoryBuffer(const Allocator &alloc = Allocator()) - : Allocator(alloc), Buffer(data_, SIZE) {} - ~MemoryBuffer() FMT_OVERRIDE { deallocate(); } +public: + explicit MemoryBuffer(const Allocator &alloc = Allocator()) : Allocator(alloc), Buffer(data_, SIZE) {} + + ~MemoryBuffer() FMT_OVERRIDE { deallocate(); } #if FMT_USE_RVALUE_REFERENCES - private: - // Move data from other to this buffer. - void move(MemoryBuffer &other) { - Allocator &this_alloc = *this, &other_alloc = other; - this_alloc = std::move(other_alloc); - this->size_ = other.size_; - this->capacity_ = other.capacity_; - if (other.ptr_ == other.data_) { - this->ptr_ = data_; - std::uninitialized_copy(other.data_, other.data_ + this->size_, - make_ptr(data_, this->capacity_)); - } else { - this->ptr_ = other.ptr_; - // Set pointer to the inline array so that delete is not called - // when deallocating. - other.ptr_ = other.data_; +private: + // Move data from other to this buffer. + void move(MemoryBuffer &other) + { + Allocator &this_alloc = *this, &other_alloc = other; + this_alloc = std::move(other_alloc); + this->size_ = other.size_; + this->capacity_ = other.capacity_; + if (other.ptr_ == other.data_) { + this->ptr_ = data_; + std::uninitialized_copy(other.data_, other.data_ + this->size_, make_ptr(data_, this->capacity_)); + } else { + this->ptr_ = other.ptr_; + // Set pointer to the inline array so that delete is not called + // when deallocating. + other.ptr_ = other.data_; + } } - } - public: - MemoryBuffer(MemoryBuffer &&other) { - move(other); - } +public: + MemoryBuffer(MemoryBuffer &&other) { move(other); } - MemoryBuffer &operator=(MemoryBuffer &&other) { - assert(this != &other); - deallocate(); - move(other); - return *this; - } + MemoryBuffer &operator=(MemoryBuffer &&other) + { + assert(this != &other); + deallocate(); + move(other); + return *this; + } #endif - // Returns a copy of the allocator associated with this buffer. - Allocator get_allocator() const { return *this; } + // Returns a copy of the allocator associated with this buffer. + Allocator get_allocator() const { return *this; } }; -template -void MemoryBuffer::grow(std::size_t size) { - std::size_t new_capacity = this->capacity_ + this->capacity_ / 2; - if (size > new_capacity) - new_capacity = size; +template +void +MemoryBuffer::grow(std::size_t size) +{ + std::size_t new_capacity = this->capacity_ + this->capacity_ / 2; + if (size > new_capacity) new_capacity = size; #if FMT_USE_ALLOCATOR_TRAITS - T *new_ptr = - std::allocator_traits::allocate(*this, new_capacity, FMT_NULL); + T *new_ptr = std::allocator_traits::allocate(*this, new_capacity, FMT_NULL); #else - T *new_ptr = this->allocate(new_capacity, FMT_NULL); + T *new_ptr = this->allocate(new_capacity, FMT_NULL); #endif - // The following code doesn't throw, so the raw pointer above doesn't leak. - std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_, - make_ptr(new_ptr, new_capacity)); - std::size_t old_capacity = this->capacity_; - T *old_ptr = this->ptr_; - this->capacity_ = new_capacity; - this->ptr_ = new_ptr; - // deallocate may throw (at least in principle), but it doesn't matter since - // the buffer already uses the new storage and will deallocate it in case - // of exception. - if (old_ptr != data_) - Allocator::deallocate(old_ptr, old_capacity); + // The following code doesn't throw, so the raw pointer above doesn't leak. + std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_, make_ptr(new_ptr, new_capacity)); + std::size_t old_capacity = this->capacity_; + T *old_ptr = this->ptr_; + this->capacity_ = new_capacity; + this->ptr_ = new_ptr; + // deallocate may throw (at least in principle), but it doesn't matter since + // the buffer already uses the new storage and will deallocate it in case + // of exception. + if (old_ptr != data_) Allocator::deallocate(old_ptr, old_capacity); } // A fixed-size buffer. -template +template class FixedBuffer : public fmt::Buffer { - public: - FixedBuffer(Char *array, std::size_t size) : fmt::Buffer(array, size) {} +public: + FixedBuffer(Char *array, std::size_t size) : fmt::Buffer(array, size) {} - protected: - FMT_API void grow(std::size_t size) FMT_OVERRIDE; +protected: + FMT_API void grow(std::size_t size) FMT_OVERRIDE; }; -template +template class BasicCharTraits { - public: +public: #if FMT_SECURE_SCL - typedef stdext::checked_array_iterator CharPtr; + typedef stdext::checked_array_iterator CharPtr; #else - typedef Char *CharPtr; + typedef Char *CharPtr; #endif - static Char cast(int value) { return static_cast(value); } + static Char cast(int value) { return static_cast(value); } }; -template +template class CharTraits; -template <> +template<> class CharTraits : public BasicCharTraits { - private: - // Conversion from wchar_t to char is not allowed. - static char convert(wchar_t); +private: + // Conversion from wchar_t to char is not allowed. + static char convert(wchar_t); - public: - static char convert(char value) { return value; } +public: + static char convert(char value) { return value; } - // Formats a floating-point number. - template - FMT_API static int format_float(char *buffer, std::size_t size, - const char *format, unsigned width, int precision, T value); + // Formats a floating-point number. + template + FMT_API static int + format_float(char *buffer, std::size_t size, const char *format, unsigned width, int precision, T value); }; #if FMT_USE_EXTERN_TEMPLATES -extern template int CharTraits::format_float - (char *buffer, std::size_t size, - const char* format, unsigned width, int precision, double value); -extern template int CharTraits::format_float - (char *buffer, std::size_t size, - const char* format, unsigned width, int precision, long double value); +extern template int CharTraits::format_float(char *buffer, + std::size_t size, + const char *format, + unsigned width, + int precision, + double value); +extern template int CharTraits::format_float(char *buffer, + std::size_t size, + const char *format, + unsigned width, + int precision, + long double value); #endif -template <> +template<> class CharTraits : public BasicCharTraits { - public: - static wchar_t convert(char value) { return value; } - static wchar_t convert(wchar_t value) { return value; } +public: + static wchar_t convert(char value) { return value; } - template - FMT_API static int format_float(wchar_t *buffer, std::size_t size, - const wchar_t *format, unsigned width, int precision, T value); + static wchar_t convert(wchar_t value) { return value; } + + template + FMT_API static int + format_float(wchar_t *buffer, std::size_t size, const wchar_t *format, unsigned width, int precision, T value); }; #if FMT_USE_EXTERN_TEMPLATES -extern template int CharTraits::format_float - (wchar_t *buffer, std::size_t size, - const wchar_t* format, unsigned width, int precision, double value); -extern template int CharTraits::format_float - (wchar_t *buffer, std::size_t size, - const wchar_t* format, unsigned width, int precision, long double value); +extern template int CharTraits::format_float(wchar_t *buffer, + std::size_t size, + const wchar_t *format, + unsigned width, + int precision, + double value); +extern template int CharTraits::format_float(wchar_t *buffer, + std::size_t size, + const wchar_t *format, + unsigned width, + int precision, + long double value); #endif // Checks if a number is negative - used to avoid warnings. -template +template struct SignChecker { - template - static bool is_negative(T value) { return value < 0; } + template + static bool is_negative(T value) + { + return value < 0; + } }; -template <> +template<> struct SignChecker { - template - static bool is_negative(T) { return false; } + template + static bool is_negative(T) + { + return false; + } }; // Returns true if value is negative, false otherwise. // Same as (value < 0) but doesn't produce warnings if T is an unsigned type. -template -inline bool is_negative(T value) { - return SignChecker::is_signed>::is_negative(value); +template +inline bool +is_negative(T value) +{ + return SignChecker::is_signed>::is_negative(value); } // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise. -template -struct TypeSelector { typedef uint32_t Type; }; +template +struct TypeSelector { + typedef uint32_t Type; +}; -template <> -struct TypeSelector { typedef uint64_t Type; }; +template<> +struct TypeSelector { + typedef uint64_t Type; +}; -template +template struct IntTraits { - // Smallest of uint32_t and uint64_t that is large enough to represent - // all values of T. - typedef typename - TypeSelector::digits <= 32>::Type MainType; + // Smallest of uint32_t and uint64_t that is large enough to represent + // all values of T. + typedef typename TypeSelector::digits <= 32>::Type MainType; }; FMT_API FMT_NORETURN void report_unknown_type(char code, const char *type); // Static data is placed in this class template to allow header-only // configuration. -template +template struct FMT_API BasicData { - static const uint32_t POWERS_OF_10_32[]; - static const uint64_t POWERS_OF_10_64[]; - static const char DIGITS[]; + static const uint32_t POWERS_OF_10_32[]; + static const uint64_t POWERS_OF_10_64[]; + static const char DIGITS[]; }; #if FMT_USE_EXTERN_TEMPLATES @@ -1019,103 +1068,112 @@ typedef BasicData<> Data; #ifdef FMT_BUILTIN_CLZLL // Returns the number of decimal digits in n. Leading zeros are not counted // except for n == 0 in which case count_digits returns 1. -inline unsigned count_digits(uint64_t n) { - // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 - // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits. - int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12; - return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1; +inline unsigned +count_digits(uint64_t n) +{ + // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 + // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits. + int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12; + return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1; } #else // Fallback version of count_digits used when __builtin_clz is not available. -inline unsigned count_digits(uint64_t n) { - unsigned count = 1; - for (;;) { - // Integer division is slow so do it for a group of four digits instead - // of for every digit. The idea comes from the talk by Alexandrescu - // "Three Optimization Tips for C++". See speed-test for a comparison. - if (n < 10) return count; - if (n < 100) return count + 1; - if (n < 1000) return count + 2; - if (n < 10000) return count + 3; - n /= 10000u; - count += 4; - } +inline unsigned +count_digits(uint64_t n) +{ + unsigned count = 1; + for (;;) { + // Integer division is slow so do it for a group of four digits instead + // of for every digit. The idea comes from the talk by Alexandrescu + // "Three Optimization Tips for C++". See speed-test for a comparison. + if (n < 10) return count; + if (n < 100) return count + 1; + if (n < 1000) return count + 2; + if (n < 10000) return count + 3; + n /= 10000u; + count += 4; + } } #endif #ifdef FMT_BUILTIN_CLZ // Optional version of count_digits for better performance on 32-bit platforms. -inline unsigned count_digits(uint32_t n) { - int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12; - return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1; +inline unsigned +count_digits(uint32_t n) +{ + int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12; + return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1; } #endif // A functor that doesn't add a thousands separator. struct NoThousandsSep { - template - void operator()(Char *) {} + template + void operator()(Char *) + {} }; // A functor that adds a thousands separator. class ThousandsSep { - private: - fmt::StringRef sep_; +private: + fmt::StringRef sep_; - // Index of a decimal digit with the least significant digit having index 0. - unsigned digit_index_; + // Index of a decimal digit with the least significant digit having index 0. + unsigned digit_index_; - public: - explicit ThousandsSep(fmt::StringRef sep) : sep_(sep), digit_index_(0) {} +public: + explicit ThousandsSep(fmt::StringRef sep) : sep_(sep), digit_index_(0) {} - template - void operator()(Char *&buffer) { - if (++digit_index_ % 3 != 0) - return; - buffer -= sep_.size(); - std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(), - internal::make_ptr(buffer, sep_.size())); - } + template + void operator()(Char *&buffer) + { + if (++digit_index_ % 3 != 0) return; + buffer -= sep_.size(); + std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(), internal::make_ptr(buffer, sep_.size())); + } }; // Formats a decimal unsigned integer value writing into buffer. // thousands_sep is a functor that is called after writing each char to // add a thousands separator if necessary. -template -inline void format_decimal(Char *buffer, UInt value, unsigned num_digits, - ThousandsSep thousands_sep) { - buffer += num_digits; - while (value >= 100) { - // Integer division is slow so do it for a group of two digits instead - // of for every digit. The idea comes from the talk by Alexandrescu - // "Three Optimization Tips for C++". See speed-test for a comparison. - unsigned index = static_cast((value % 100) * 2); - value /= 100; - *--buffer = Data::DIGITS[index + 1]; +template +inline void +format_decimal(Char *buffer, UInt value, unsigned num_digits, ThousandsSep thousands_sep) +{ + buffer += num_digits; + while (value >= 100) { + // Integer division is slow so do it for a group of two digits instead + // of for every digit. The idea comes from the talk by Alexandrescu + // "Three Optimization Tips for C++". See speed-test for a comparison. + unsigned index = static_cast((value % 100) * 2); + value /= 100; + *--buffer = Data::DIGITS[index + 1]; + thousands_sep(buffer); + *--buffer = Data::DIGITS[index]; + thousands_sep(buffer); + } + if (value < 10) { + *--buffer = static_cast('0' + value); + return; + } + unsigned index = static_cast(value * 2); + *--buffer = Data::DIGITS[index + 1]; thousands_sep(buffer); *--buffer = Data::DIGITS[index]; - thousands_sep(buffer); - } - if (value < 10) { - *--buffer = static_cast('0' + value); - return; - } - unsigned index = static_cast(value * 2); - *--buffer = Data::DIGITS[index + 1]; - thousands_sep(buffer); - *--buffer = Data::DIGITS[index]; } -template -inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { - format_decimal(buffer, value, num_digits, NoThousandsSep()); - return; +template +inline void +format_decimal(Char *buffer, UInt value, unsigned num_digits) +{ + format_decimal(buffer, value, num_digits, NoThousandsSep()); + return; } #ifndef _WIN32 -# define FMT_USE_WINDOWS_H 0 +#define FMT_USE_WINDOWS_H 0 #elif !defined(FMT_USE_WINDOWS_H) -# define FMT_USE_WINDOWS_H 1 +#define FMT_USE_WINDOWS_H 1 #endif // Define FMT_USE_WINDOWS_H to 0 to disable use of windows.h. @@ -1124,513 +1182,563 @@ inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { // A converter from UTF-8 to UTF-16. // It is only provided for Windows since other systems support UTF-8 natively. class UTF8ToUTF16 { - private: - MemoryBuffer buffer_; +private: + MemoryBuffer buffer_; - public: - FMT_API explicit UTF8ToUTF16(StringRef s); - operator WStringRef() const { return WStringRef(&buffer_[0], size()); } - size_t size() const { return buffer_.size() - 1; } - const wchar_t *c_str() const { return &buffer_[0]; } - std::wstring str() const { return std::wstring(&buffer_[0], size()); } +public: + FMT_API explicit UTF8ToUTF16(StringRef s); + + operator WStringRef() const { return WStringRef(&buffer_[0], size()); } + + size_t size() const { return buffer_.size() - 1; } + + const wchar_t *c_str() const { return &buffer_[0]; } + + std::wstring str() const { return std::wstring(&buffer_[0], size()); } }; // A converter from UTF-16 to UTF-8. // It is only provided for Windows since other systems support UTF-8 natively. class UTF16ToUTF8 { - private: - MemoryBuffer buffer_; +private: + MemoryBuffer buffer_; - public: - UTF16ToUTF8() {} - FMT_API explicit UTF16ToUTF8(WStringRef s); - operator StringRef() const { return StringRef(&buffer_[0], size()); } - size_t size() const { return buffer_.size() - 1; } - const char *c_str() const { return &buffer_[0]; } - std::string str() const { return std::string(&buffer_[0], size()); } +public: + UTF16ToUTF8() {} - // Performs conversion returning a system error code instead of - // throwing exception on conversion error. This method may still throw - // in case of memory allocation error. - FMT_API int convert(WStringRef s); + FMT_API explicit UTF16ToUTF8(WStringRef s); + + operator StringRef() const { return StringRef(&buffer_[0], size()); } + + size_t size() const { return buffer_.size() - 1; } + + const char *c_str() const { return &buffer_[0]; } + + std::string str() const { return std::string(&buffer_[0], size()); } + + // Performs conversion returning a system error code instead of + // throwing exception on conversion error. This method may still throw + // in case of memory allocation error. + FMT_API int convert(WStringRef s); }; -FMT_API void format_windows_error(fmt::Writer &out, int error_code, - fmt::StringRef message) FMT_NOEXCEPT; +FMT_API void format_windows_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT; #endif // A formatting argument value. struct Value { - template - struct StringValue { - const Char *value; - std::size_t size; - }; + template + struct StringValue { + const Char *value; + std::size_t size; + }; - typedef void (*FormatFunc)( - void *formatter, const void *arg, void *format_str_ptr); + typedef void (*FormatFunc)(void *formatter, const void *arg, void *format_str_ptr); - struct CustomValue { - const void *value; - FormatFunc format; - }; + struct CustomValue { + const void *value; + FormatFunc format; + }; - union { - int int_value; - unsigned uint_value; - LongLong long_long_value; - ULongLong ulong_long_value; - double double_value; - long double long_double_value; - const void *pointer; - StringValue string; - StringValue sstring; - StringValue ustring; - StringValue wstring; - CustomValue custom; - }; + union { + int int_value; + unsigned uint_value; + LongLong long_long_value; + ULongLong ulong_long_value; + double double_value; + long double long_double_value; + const void *pointer; + StringValue string; + StringValue sstring; + StringValue ustring; + StringValue wstring; + CustomValue custom; + }; - enum Type { - NONE, NAMED_ARG, - // Integer types should go first, - INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR, - // followed by floating-point types. - DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE, - CSTRING, STRING, WSTRING, POINTER, CUSTOM - }; + enum Type { + NONE, + NAMED_ARG, + // Integer types should go first, + INT, + UINT, + LONG_LONG, + ULONG_LONG, + BOOL, + CHAR, + LAST_INTEGER_TYPE = CHAR, + // followed by floating-point types. + DOUBLE, + LONG_DOUBLE, + LAST_NUMERIC_TYPE = LONG_DOUBLE, + CSTRING, + STRING, + WSTRING, + POINTER, + CUSTOM + }; }; // A formatting argument. It is a trivially copyable/constructible type to // allow storage in internal::MemoryBuffer. struct Arg : Value { - Type type; + Type type; }; -template +template struct NamedArg; -template +template struct NamedArgWithType; -template +template struct Null {}; // A helper class template to enable or disable overloads taking wide // characters and strings in MakeValue. -template +template struct WCharHelper { - typedef Null Supported; - typedef T Unsupported; + typedef Null Supported; + typedef T Unsupported; }; -template +template struct WCharHelper { - typedef T Supported; - typedef Null Unsupported; + typedef T Supported; + typedef Null Unsupported; }; typedef char Yes[1]; typedef char No[2]; -template +template T &get(); // These are non-members to workaround an overload resolution bug in bcc32. Yes &convert(fmt::ULongLong); No &convert(...); -template +template struct ConvertToIntImpl { - enum { value = ENABLE_CONVERSION }; + enum { value = ENABLE_CONVERSION }; }; -template +template struct ConvertToIntImpl2 { - enum { value = false }; + enum { value = false }; }; -template +template struct ConvertToIntImpl2 { - enum { - // Don't convert numeric types. - value = ConvertToIntImpl::is_specialized>::value - }; + enum { + // Don't convert numeric types. + value = ConvertToIntImpl::is_specialized>::value + }; }; -template +template struct ConvertToInt { - enum { - enable_conversion = sizeof(fmt::internal::convert(get())) == sizeof(Yes) - }; - enum { value = ConvertToIntImpl2::value }; + enum { enable_conversion = sizeof(fmt::internal::convert(get())) == sizeof(Yes) }; + + enum { value = ConvertToIntImpl2::value }; }; -#define FMT_DISABLE_CONVERSION_TO_INT(Type) \ - template <> \ - struct ConvertToInt { enum { value = 0 }; } +#define FMT_DISABLE_CONVERSION_TO_INT(Type) \ + template<> \ + struct ConvertToInt { \ + enum { value = 0 }; \ + } // Silence warnings about convering float to int. FMT_DISABLE_CONVERSION_TO_INT(float); FMT_DISABLE_CONVERSION_TO_INT(double); FMT_DISABLE_CONVERSION_TO_INT(long double); -template +template struct EnableIf {}; -template -struct EnableIf { typedef T type; }; +template +struct EnableIf { + typedef T type; +}; -template -struct Conditional { typedef T type; }; +template +struct Conditional { + typedef T type; +}; -template -struct Conditional { typedef F type; }; +template +struct Conditional { + typedef F type; +}; // For bcc32 which doesn't understand ! in template arguments. -template -struct Not { enum { value = 0 }; }; +template +struct Not { + enum { value = 0 }; +}; -template <> -struct Not { enum { value = 1 }; }; +template<> +struct Not { + enum { value = 1 }; +}; -template -struct FalseType { enum { value = 0 }; }; +template +struct FalseType { + enum { value = 0 }; +}; -template struct LConvCheck { - LConvCheck(int) {} +template +struct LConvCheck { + LConvCheck(int) {} }; // Returns the thousands separator for the current locale. // We check if ``lconv`` contains ``thousands_sep`` because on Android // ``lconv`` is stubbed as an empty struct. -template -inline StringRef thousands_sep( - LConv *lc, LConvCheck = 0) { - return lc->thousands_sep; +template +inline StringRef +thousands_sep(LConv *lc, LConvCheck = 0) +{ + return lc->thousands_sep; } -inline fmt::StringRef thousands_sep(...) { return ""; } +inline fmt::StringRef +thousands_sep(...) +{ + return ""; +} #define FMT_CONCAT(a, b) a##b #if FMT_GCC_VERSION >= 303 -# define FMT_UNUSED __attribute__((unused)) +#define FMT_UNUSED __attribute__((unused)) #else -# define FMT_UNUSED +#define FMT_UNUSED #endif #ifndef FMT_USE_STATIC_ASSERT -# define FMT_USE_STATIC_ASSERT 0 +#define FMT_USE_STATIC_ASSERT 0 #endif -#if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \ - (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600 -# define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message) +#if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) \ + || _MSC_VER >= 1600 +#define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message) #else -# define FMT_CONCAT_(a, b) FMT_CONCAT(a, b) -# define FMT_STATIC_ASSERT(cond, message) \ - typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED +#define FMT_CONCAT_(a, b) FMT_CONCAT(a, b) +#define FMT_STATIC_ASSERT(cond, message) typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED #endif -template -void format_arg(Formatter&, ...) { - FMT_STATIC_ASSERT(FalseType::value, - "Cannot format argument. To enable the use of ostream " - "operator<< include fmt/ostream.h. Otherwise provide " - "an overload of format_arg."); +template +void +format_arg(Formatter &, ...) +{ + FMT_STATIC_ASSERT(FalseType::value, + "Cannot format argument. To enable the use of ostream " + "operator<< include fmt/ostream.h. Otherwise provide " + "an overload of format_arg."); } // Makes an Arg object from any type. -template +template class MakeValue : public Arg { - public: - typedef typename Formatter::Char Char; +public: + typedef typename Formatter::Char Char; - private: - // The following two methods are private to disallow formatting of - // arbitrary pointers. If you want to output a pointer cast it to - // "void *" or "const void *". In particular, this forbids formatting - // of "[const] volatile char *" which is printed as bool by iostreams. - // Do not implement! - template - MakeValue(const T *value); - template - MakeValue(T *value); +private: + // The following two methods are private to disallow formatting of + // arbitrary pointers. If you want to output a pointer cast it to + // "void *" or "const void *". In particular, this forbids formatting + // of "[const] volatile char *" which is printed as bool by iostreams. + // Do not implement! + template + MakeValue(const T *value); + template + MakeValue(T *value); - // The following methods are private to disallow formatting of wide - // characters and strings into narrow strings as in - // fmt::format("{}", L"test"); - // To fix this, use a wide format string: fmt::format(L"{}", L"test"). + // The following methods are private to disallow formatting of wide + // characters and strings into narrow strings as in + // fmt::format("{}", L"test"); + // To fix this, use a wide format string: fmt::format(L"{}", L"test"). #if !FMT_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED) - MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); #endif - MakeValue(typename WCharHelper::Unsupported); - MakeValue(typename WCharHelper::Unsupported); - MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); #if FMT_HAS_STRING_VIEW - MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); #endif - MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); - void set_string(StringRef str) { - string.value = str.data(); - string.size = str.size(); - } + void set_string(StringRef str) + { + string.value = str.data(); + string.size = str.size(); + } - void set_string(WStringRef str) { - wstring.value = str.data(); - wstring.size = str.size(); - } + void set_string(WStringRef str) + { + wstring.value = str.data(); + wstring.size = str.size(); + } - // Formats an argument of a custom type, such as a user-defined class. - template - static void format_custom_arg( - void *formatter, const void *arg, void *format_str_ptr) { - format_arg(*static_cast(formatter), - *static_cast(format_str_ptr), - *static_cast(arg)); - } + // Formats an argument of a custom type, such as a user-defined class. + template + static void format_custom_arg(void *formatter, const void *arg, void *format_str_ptr) + { + format_arg(*static_cast(formatter), + *static_cast(format_str_ptr), + *static_cast(arg)); + } - public: - MakeValue() {} +public: + MakeValue() {} -#define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \ - MakeValue(Type value) { field = rhs; } \ - static uint64_t type(Type) { return Arg::TYPE; } +#define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \ + MakeValue(Type value) { field = rhs; } \ + static uint64_t type(Type) { return Arg::TYPE; } -#define FMT_MAKE_VALUE(Type, field, TYPE) \ - FMT_MAKE_VALUE_(Type, field, TYPE, value) +#define FMT_MAKE_VALUE(Type, field, TYPE) FMT_MAKE_VALUE_(Type, field, TYPE, value) - FMT_MAKE_VALUE(bool, int_value, BOOL) - FMT_MAKE_VALUE(short, int_value, INT) - FMT_MAKE_VALUE(unsigned short, uint_value, UINT) - FMT_MAKE_VALUE(int, int_value, INT) - FMT_MAKE_VALUE(unsigned, uint_value, UINT) + FMT_MAKE_VALUE(bool, int_value, BOOL) + FMT_MAKE_VALUE(short, int_value, INT) + FMT_MAKE_VALUE(unsigned short, uint_value, UINT) + FMT_MAKE_VALUE(int, int_value, INT) + FMT_MAKE_VALUE(unsigned, uint_value, UINT) - MakeValue(long value) { - // To minimize the number of types we need to deal with, long is - // translated either to int or to long long depending on its size. - if (const_check(sizeof(long) == sizeof(int))) - int_value = static_cast(value); - else - long_long_value = value; - } - static uint64_t type(long) { - return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG; - } + MakeValue(long value) + { + // To minimize the number of types we need to deal with, long is + // translated either to int or to long long depending on its size. + if (const_check(sizeof(long) == sizeof(int))) + int_value = static_cast(value); + else + long_long_value = value; + } - MakeValue(unsigned long value) { - if (const_check(sizeof(unsigned long) == sizeof(unsigned))) - uint_value = static_cast(value); - else - ulong_long_value = value; - } - static uint64_t type(unsigned long) { - return sizeof(unsigned long) == sizeof(unsigned) ? - Arg::UINT : Arg::ULONG_LONG; - } + static uint64_t type(long) { return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG; } - FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG) - FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG) - FMT_MAKE_VALUE(float, double_value, DOUBLE) - FMT_MAKE_VALUE(double, double_value, DOUBLE) - FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE) - FMT_MAKE_VALUE(signed char, int_value, INT) - FMT_MAKE_VALUE(unsigned char, uint_value, UINT) - FMT_MAKE_VALUE(char, int_value, CHAR) + MakeValue(unsigned long value) + { + if (const_check(sizeof(unsigned long) == sizeof(unsigned))) + uint_value = static_cast(value); + else + ulong_long_value = value; + } + + static uint64_t type(unsigned long) + { + return sizeof(unsigned long) == sizeof(unsigned) ? Arg::UINT : Arg::ULONG_LONG; + } + + FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG) + FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG) + FMT_MAKE_VALUE(float, double_value, DOUBLE) + FMT_MAKE_VALUE(double, double_value, DOUBLE) + FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE) + FMT_MAKE_VALUE(signed char, int_value, INT) + FMT_MAKE_VALUE(unsigned char, uint_value, UINT) + FMT_MAKE_VALUE(char, int_value, CHAR) #if __cplusplus >= 201103L - template < - typename T, - typename = typename std::enable_if< - std::is_enum::value && ConvertToInt::value>::type> - MakeValue(T value) { int_value = value; } + template::value && ConvertToInt::value>::type> + MakeValue(T value) + { + int_value = value; + } - template < - typename T, - typename = typename std::enable_if< - std::is_enum::value && ConvertToInt::value>::type> - static uint64_t type(T) { return Arg::INT; } + template::value && ConvertToInt::value>::type> + static uint64_t type(T) + { + return Arg::INT; + } #endif #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) - MakeValue(typename WCharHelper::Supported value) { - int_value = value; - } - static uint64_t type(wchar_t) { return Arg::CHAR; } + MakeValue(typename WCharHelper::Supported value) { int_value = value; } + + static uint64_t type(wchar_t) { return Arg::CHAR; } #endif -#define FMT_MAKE_STR_VALUE(Type, TYPE) \ - MakeValue(Type value) { set_string(value); } \ - static uint64_t type(Type) { return Arg::TYPE; } +#define FMT_MAKE_STR_VALUE(Type, TYPE) \ + MakeValue(Type value) { set_string(value); } \ + static uint64_t type(Type) { return Arg::TYPE; } - FMT_MAKE_VALUE(char *, string.value, CSTRING) - FMT_MAKE_VALUE(const char *, string.value, CSTRING) - FMT_MAKE_VALUE(signed char *, sstring.value, CSTRING) - FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING) - FMT_MAKE_VALUE(unsigned char *, ustring.value, CSTRING) - FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING) - FMT_MAKE_STR_VALUE(const std::string &, STRING) + FMT_MAKE_VALUE(char *, string.value, CSTRING) + FMT_MAKE_VALUE(const char *, string.value, CSTRING) + FMT_MAKE_VALUE(signed char *, sstring.value, CSTRING) + FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING) + FMT_MAKE_VALUE(unsigned char *, ustring.value, CSTRING) + FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING) + FMT_MAKE_STR_VALUE(const std::string &, STRING) #if FMT_HAS_STRING_VIEW - FMT_MAKE_STR_VALUE(const std::string_view &, STRING) + FMT_MAKE_STR_VALUE(const std::string_view &, STRING) #endif - FMT_MAKE_STR_VALUE(StringRef, STRING) - FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str()) + FMT_MAKE_STR_VALUE(StringRef, STRING) + FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str()) -#define FMT_MAKE_WSTR_VALUE(Type, TYPE) \ - MakeValue(typename WCharHelper::Supported value) { \ - set_string(value); \ - } \ - static uint64_t type(Type) { return Arg::TYPE; } +#define FMT_MAKE_WSTR_VALUE(Type, TYPE) \ + MakeValue(typename WCharHelper::Supported value) { set_string(value); } \ + static uint64_t type(Type) { return Arg::TYPE; } - FMT_MAKE_WSTR_VALUE(wchar_t *, WSTRING) - FMT_MAKE_WSTR_VALUE(const wchar_t *, WSTRING) - FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING) + FMT_MAKE_WSTR_VALUE(wchar_t *, WSTRING) + FMT_MAKE_WSTR_VALUE(const wchar_t *, WSTRING) + FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING) #if FMT_HAS_STRING_VIEW - FMT_MAKE_WSTR_VALUE(const std::wstring_view &, WSTRING) + FMT_MAKE_WSTR_VALUE(const std::wstring_view &, WSTRING) #endif - FMT_MAKE_WSTR_VALUE(WStringRef, WSTRING) + FMT_MAKE_WSTR_VALUE(WStringRef, WSTRING) - FMT_MAKE_VALUE(void *, pointer, POINTER) - FMT_MAKE_VALUE(const void *, pointer, POINTER) + FMT_MAKE_VALUE(void *, pointer, POINTER) + FMT_MAKE_VALUE(const void *, pointer, POINTER) - template - MakeValue(const T &value, - typename EnableIf::value>::value, int>::type = 0) { - custom.value = &value; - custom.format = &format_custom_arg; - } + template + MakeValue(const T &value, typename EnableIf::value>::value, int>::type = 0) + { + custom.value = &value; + custom.format = &format_custom_arg; + } - template - static typename EnableIf::value>::value, uint64_t>::type - type(const T &) { - return Arg::CUSTOM; - } + template + static typename EnableIf::value>::value, uint64_t>::type type(const T &) + { + return Arg::CUSTOM; + } - // Additional template param `Char_` is needed here because make_type always - // uses char. - template - MakeValue(const NamedArg &value) { pointer = &value; } - template - MakeValue(const NamedArgWithType &value) { pointer = &value; } + // Additional template param `Char_` is needed here because make_type always + // uses char. + template + MakeValue(const NamedArg &value) + { + pointer = &value; + } - template - static uint64_t type(const NamedArg &) { return Arg::NAMED_ARG; } - template - static uint64_t type(const NamedArgWithType &) { return Arg::NAMED_ARG; } + template + MakeValue(const NamedArgWithType &value) + { + pointer = &value; + } + + template + static uint64_t type(const NamedArg &) + { + return Arg::NAMED_ARG; + } + + template + static uint64_t type(const NamedArgWithType &) + { + return Arg::NAMED_ARG; + } }; -template +template class MakeArg : public Arg { public: - MakeArg() { - type = Arg::NONE; - } + MakeArg() { type = Arg::NONE; } - template - MakeArg(const T &value) - : Arg(MakeValue(value)) { - type = static_cast(MakeValue::type(value)); - } + template + MakeArg(const T &value) : Arg(MakeValue(value)) + { + type = static_cast(MakeValue::type(value)); + } }; -template +template struct NamedArg : Arg { - BasicStringRef name; + BasicStringRef name; - template - NamedArg(BasicStringRef argname, const T &value) - : Arg(MakeArg< BasicFormatter >(value)), name(argname) {} + template + NamedArg(BasicStringRef argname, const T &value) : Arg(MakeArg>(value)), + name(argname) + {} }; -template +template struct NamedArgWithType : NamedArg { - NamedArgWithType(BasicStringRef argname, const T &value) - : NamedArg(argname, value) {} + NamedArgWithType(BasicStringRef argname, const T &value) : NamedArg(argname, value) {} }; class RuntimeError : public std::runtime_error { - protected: - RuntimeError() : std::runtime_error("") {} - RuntimeError(const RuntimeError &rerr) : std::runtime_error(rerr) {} - FMT_API ~RuntimeError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE; +protected: + RuntimeError() : std::runtime_error("") {} + + RuntimeError(const RuntimeError &rerr) : std::runtime_error(rerr) {} + + FMT_API ~RuntimeError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE; }; -template +template class ArgMap; -} // namespace internal +}// namespace internal /** An argument list. */ class ArgList { - private: - // To reduce compiled code size per formatting function call, types of first - // MAX_PACKED_ARGS arguments are passed in the types_ field. - uint64_t types_; - union { - // If the number of arguments is less than MAX_PACKED_ARGS, the argument - // values are stored in values_, otherwise they are stored in args_. - // This is done to reduce compiled code size as storing larger objects - // may require more code (at least on x86-64) even if the same amount of - // data is actually copied to stack. It saves ~10% on the bloat test. - const internal::Value *values_; - const internal::Arg *args_; - }; +private: + // To reduce compiled code size per formatting function call, types of first + // MAX_PACKED_ARGS arguments are passed in the types_ field. + uint64_t types_; - internal::Arg::Type type(unsigned index) const { - return type(types_, index); - } + union { + // If the number of arguments is less than MAX_PACKED_ARGS, the argument + // values are stored in values_, otherwise they are stored in args_. + // This is done to reduce compiled code size as storing larger objects + // may require more code (at least on x86-64) even if the same amount of + // data is actually copied to stack. It saves ~10% on the bloat test. + const internal::Value *values_; + const internal::Arg *args_; + }; - template - friend class internal::ArgMap; + internal::Arg::Type type(unsigned index) const { return type(types_, index); } - public: - // Maximum number of arguments with packed types. - enum { MAX_PACKED_ARGS = 16 }; + template + friend class internal::ArgMap; - ArgList() : types_(0) {} +public: + // Maximum number of arguments with packed types. + enum { MAX_PACKED_ARGS = 16 }; - ArgList(ULongLong types, const internal::Value *values) - : types_(types), values_(values) {} - ArgList(ULongLong types, const internal::Arg *args) - : types_(types), args_(args) {} + ArgList() : types_(0) {} - uint64_t types() const { return types_; } + ArgList(ULongLong types, const internal::Value *values) : types_(types), values_(values) {} - /** Returns the argument at specified index. */ - internal::Arg operator[](unsigned index) const { - using internal::Arg; - Arg arg; - bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE; - if (index < MAX_PACKED_ARGS) { - Arg::Type arg_type = type(index); - internal::Value &val = arg; - if (arg_type != Arg::NONE) - val = use_values ? values_[index] : args_[index]; - arg.type = arg_type; - return arg; + ArgList(ULongLong types, const internal::Arg *args) : types_(types), args_(args) {} + + uint64_t types() const { return types_; } + + /** Returns the argument at specified index. */ + internal::Arg operator[](unsigned index) const + { + using internal::Arg; + Arg arg; + bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE; + if (index < MAX_PACKED_ARGS) { + Arg::Type arg_type = type(index); + internal::Value &val = arg; + if (arg_type != Arg::NONE) val = use_values ? values_[index] : args_[index]; + arg.type = arg_type; + return arg; + } + if (use_values) { + // The index is greater than the number of arguments that can be stored + // in values, so return a "none" argument. + arg.type = Arg::NONE; + return arg; + } + for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) { + if (args_[i].type == Arg::NONE) return args_[i]; + } + return args_[index]; } - if (use_values) { - // The index is greater than the number of arguments that can be stored - // in values, so return a "none" argument. - arg.type = Arg::NONE; - return arg; - } - for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) { - if (args_[i].type == Arg::NONE) - return args_[i]; - } - return args_[index]; - } - static internal::Arg::Type type(uint64_t types, unsigned index) { - unsigned shift = index * 4; - uint64_t mask = 0xf; - return static_cast( - (types & (mask << shift)) >> shift); - } + static internal::Arg::Type type(uint64_t types, unsigned index) + { + unsigned shift = index * 4; + uint64_t mask = 0xf; + return static_cast((types & (mask << shift)) >> shift); + } }; -#define FMT_DISPATCH(call) static_cast(this)->call +#define FMT_DISPATCH(call) static_cast(this)->call /** \rst @@ -1656,97 +1764,74 @@ class ArgList { }; \endrst */ -template +template class ArgVisitor { - private: - typedef internal::Arg Arg; +private: + typedef internal::Arg Arg; - public: - void report_unhandled_arg() {} +public: + void report_unhandled_arg() {} - Result visit_unhandled_arg() { - FMT_DISPATCH(report_unhandled_arg()); - return Result(); - } + Result visit_unhandled_arg() + { + FMT_DISPATCH(report_unhandled_arg()); + return Result(); + } - /** Visits an ``int`` argument. **/ - Result visit_int(int value) { - return FMT_DISPATCH(visit_any_int(value)); - } + /** Visits an ``int`` argument. **/ + Result visit_int(int value) { return FMT_DISPATCH(visit_any_int(value)); } - /** Visits a ``long long`` argument. **/ - Result visit_long_long(LongLong value) { - return FMT_DISPATCH(visit_any_int(value)); - } + /** Visits a ``long long`` argument. **/ + Result visit_long_long(LongLong value) { return FMT_DISPATCH(visit_any_int(value)); } - /** Visits an ``unsigned`` argument. **/ - Result visit_uint(unsigned value) { - return FMT_DISPATCH(visit_any_int(value)); - } + /** Visits an ``unsigned`` argument. **/ + Result visit_uint(unsigned value) { return FMT_DISPATCH(visit_any_int(value)); } - /** Visits an ``unsigned long long`` argument. **/ - Result visit_ulong_long(ULongLong value) { - return FMT_DISPATCH(visit_any_int(value)); - } + /** Visits an ``unsigned long long`` argument. **/ + Result visit_ulong_long(ULongLong value) { return FMT_DISPATCH(visit_any_int(value)); } - /** Visits a ``bool`` argument. **/ - Result visit_bool(bool value) { - return FMT_DISPATCH(visit_any_int(value)); - } + /** Visits a ``bool`` argument. **/ + Result visit_bool(bool value) { return FMT_DISPATCH(visit_any_int(value)); } - /** Visits a ``char`` or ``wchar_t`` argument. **/ - Result visit_char(int value) { - return FMT_DISPATCH(visit_any_int(value)); - } + /** Visits a ``char`` or ``wchar_t`` argument. **/ + Result visit_char(int value) { return FMT_DISPATCH(visit_any_int(value)); } - /** Visits an argument of any integral type. **/ - template - Result visit_any_int(T) { - return FMT_DISPATCH(visit_unhandled_arg()); - } + /** Visits an argument of any integral type. **/ + template + Result visit_any_int(T) + { + return FMT_DISPATCH(visit_unhandled_arg()); + } - /** Visits a ``double`` argument. **/ - Result visit_double(double value) { - return FMT_DISPATCH(visit_any_double(value)); - } + /** Visits a ``double`` argument. **/ + Result visit_double(double value) { return FMT_DISPATCH(visit_any_double(value)); } - /** Visits a ``long double`` argument. **/ - Result visit_long_double(long double value) { - return FMT_DISPATCH(visit_any_double(value)); - } + /** Visits a ``long double`` argument. **/ + Result visit_long_double(long double value) { return FMT_DISPATCH(visit_any_double(value)); } - /** Visits a ``double`` or ``long double`` argument. **/ - template - Result visit_any_double(T) { - return FMT_DISPATCH(visit_unhandled_arg()); - } + /** Visits a ``double`` or ``long double`` argument. **/ + template + Result visit_any_double(T) + { + return FMT_DISPATCH(visit_unhandled_arg()); + } - /** Visits a null-terminated C string (``const char *``) argument. **/ - Result visit_cstring(const char *) { - return FMT_DISPATCH(visit_unhandled_arg()); - } + /** Visits a null-terminated C string (``const char *``) argument. **/ + Result visit_cstring(const char *) { return FMT_DISPATCH(visit_unhandled_arg()); } - /** Visits a string argument. **/ - Result visit_string(Arg::StringValue) { - return FMT_DISPATCH(visit_unhandled_arg()); - } + /** Visits a string argument. **/ + Result visit_string(Arg::StringValue) { return FMT_DISPATCH(visit_unhandled_arg()); } - /** Visits a wide string argument. **/ - Result visit_wstring(Arg::StringValue) { - return FMT_DISPATCH(visit_unhandled_arg()); - } + /** Visits a wide string argument. **/ + Result visit_wstring(Arg::StringValue) { return FMT_DISPATCH(visit_unhandled_arg()); } - /** Visits a pointer argument. **/ - Result visit_pointer(const void *) { - return FMT_DISPATCH(visit_unhandled_arg()); - } + /** Visits a pointer argument. **/ + Result visit_pointer(const void *) { return FMT_DISPATCH(visit_unhandled_arg()); } - /** Visits an argument of a custom (user-defined) type. **/ - Result visit_custom(Arg::CustomValue) { - return FMT_DISPATCH(visit_unhandled_arg()); - } + /** Visits an argument of a custom (user-defined) type. **/ + Result visit_custom(Arg::CustomValue) { return FMT_DISPATCH(visit_unhandled_arg()); } - /** + /** \rst Visits an argument dispatching to the appropriate visit method based on the argument type. For example, if the argument type is ``double`` then @@ -1754,169 +1839,185 @@ class ArgVisitor { called. \endrst */ - Result visit(const Arg &arg) { - switch (arg.type) { - case Arg::NONE: - case Arg::NAMED_ARG: - FMT_ASSERT(false, "invalid argument type"); - break; - case Arg::INT: - return FMT_DISPATCH(visit_int(arg.int_value)); - case Arg::UINT: - return FMT_DISPATCH(visit_uint(arg.uint_value)); - case Arg::LONG_LONG: - return FMT_DISPATCH(visit_long_long(arg.long_long_value)); - case Arg::ULONG_LONG: - return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value)); - case Arg::BOOL: - return FMT_DISPATCH(visit_bool(arg.int_value != 0)); - case Arg::CHAR: - return FMT_DISPATCH(visit_char(arg.int_value)); - case Arg::DOUBLE: - return FMT_DISPATCH(visit_double(arg.double_value)); - case Arg::LONG_DOUBLE: - return FMT_DISPATCH(visit_long_double(arg.long_double_value)); - case Arg::CSTRING: - return FMT_DISPATCH(visit_cstring(arg.string.value)); - case Arg::STRING: - return FMT_DISPATCH(visit_string(arg.string)); - case Arg::WSTRING: - return FMT_DISPATCH(visit_wstring(arg.wstring)); - case Arg::POINTER: - return FMT_DISPATCH(visit_pointer(arg.pointer)); - case Arg::CUSTOM: - return FMT_DISPATCH(visit_custom(arg.custom)); + Result visit(const Arg &arg) + { + switch (arg.type) { + case Arg::NONE: + case Arg::NAMED_ARG: + FMT_ASSERT(false, "invalid argument type"); + break; + case Arg::INT: + return FMT_DISPATCH(visit_int(arg.int_value)); + case Arg::UINT: + return FMT_DISPATCH(visit_uint(arg.uint_value)); + case Arg::LONG_LONG: + return FMT_DISPATCH(visit_long_long(arg.long_long_value)); + case Arg::ULONG_LONG: + return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value)); + case Arg::BOOL: + return FMT_DISPATCH(visit_bool(arg.int_value != 0)); + case Arg::CHAR: + return FMT_DISPATCH(visit_char(arg.int_value)); + case Arg::DOUBLE: + return FMT_DISPATCH(visit_double(arg.double_value)); + case Arg::LONG_DOUBLE: + return FMT_DISPATCH(visit_long_double(arg.long_double_value)); + case Arg::CSTRING: + return FMT_DISPATCH(visit_cstring(arg.string.value)); + case Arg::STRING: + return FMT_DISPATCH(visit_string(arg.string)); + case Arg::WSTRING: + return FMT_DISPATCH(visit_wstring(arg.wstring)); + case Arg::POINTER: + return FMT_DISPATCH(visit_pointer(arg.pointer)); + case Arg::CUSTOM: + return FMT_DISPATCH(visit_custom(arg.custom)); + } + return Result(); } - return Result(); - } }; -enum Alignment { - ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC -}; +enum Alignment { ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC }; // Flags. enum { - SIGN_FLAG = 1, PLUS_FLAG = 2, MINUS_FLAG = 4, HASH_FLAG = 8, - CHAR_FLAG = 0x10 // Argument has char type - used in error reporting. + SIGN_FLAG = 1, + PLUS_FLAG = 2, + MINUS_FLAG = 4, + HASH_FLAG = 8, + CHAR_FLAG = 0x10// Argument has char type - used in error reporting. }; // An empty format specifier. struct EmptySpec {}; // A type specifier. -template +template struct TypeSpec : EmptySpec { - Alignment align() const { return ALIGN_DEFAULT; } - unsigned width() const { return 0; } - int precision() const { return -1; } - bool flag(unsigned) const { return false; } - char type() const { return TYPE; } - char type_prefix() const { return TYPE; } - char fill() const { return ' '; } + Alignment align() const { return ALIGN_DEFAULT; } + + unsigned width() const { return 0; } + + int precision() const { return -1; } + + bool flag(unsigned) const { return false; } + + char type() const { return TYPE; } + + char type_prefix() const { return TYPE; } + + char fill() const { return ' '; } }; // A width specifier. struct WidthSpec { - unsigned width_; - // Fill is always wchar_t and cast to char if necessary to avoid having - // two specialization of WidthSpec and its subclasses. - wchar_t fill_; + unsigned width_; + // Fill is always wchar_t and cast to char if necessary to avoid having + // two specialization of WidthSpec and its subclasses. + wchar_t fill_; - WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {} + WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {} - unsigned width() const { return width_; } - wchar_t fill() const { return fill_; } + unsigned width() const { return width_; } + + wchar_t fill() const { return fill_; } }; // An alignment specifier. struct AlignSpec : WidthSpec { - Alignment align_; + Alignment align_; - AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT) - : WidthSpec(width, fill), align_(align) {} + AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT) : WidthSpec(width, fill), align_(align) {} - Alignment align() const { return align_; } + Alignment align() const { return align_; } - int precision() const { return -1; } + int precision() const { return -1; } }; // An alignment and type specifier. -template +template struct AlignTypeSpec : AlignSpec { - AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {} + AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {} - bool flag(unsigned) const { return false; } - char type() const { return TYPE; } - char type_prefix() const { return TYPE; } + bool flag(unsigned) const { return false; } + + char type() const { return TYPE; } + + char type_prefix() const { return TYPE; } }; // A full format specifier. struct FormatSpec : AlignSpec { - unsigned flags_; - int precision_; - char type_; + unsigned flags_; + int precision_; + char type_; - FormatSpec( - unsigned width = 0, char type = 0, wchar_t fill = ' ') - : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {} + FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ') + : AlignSpec(width, fill), + flags_(0), + precision_(-1), + type_(type) + {} - bool flag(unsigned f) const { return (flags_ & f) != 0; } - int precision() const { return precision_; } - char type() const { return type_; } - char type_prefix() const { return type_; } + bool flag(unsigned f) const { return (flags_ & f) != 0; } + + int precision() const { return precision_; } + + char type() const { return type_; } + + char type_prefix() const { return type_; } }; // An integer format specifier. -template , typename Char = char> +template, typename Char = char> class IntFormatSpec : public SpecT { - private: - T value_; +private: + T value_; - public: - IntFormatSpec(T val, const SpecT &spec = SpecT()) - : SpecT(spec), value_(val) {} +public: + IntFormatSpec(T val, const SpecT &spec = SpecT()) : SpecT(spec), value_(val) {} - T value() const { return value_; } + T value() const { return value_; } }; // A string format specifier. -template +template class StrFormatSpec : public AlignSpec { - private: - const Char *str_; +private: + const Char *str_; - public: - template - StrFormatSpec(const Char *str, unsigned width, FillChar fill) - : AlignSpec(width, fill), str_(str) { - internal::CharTraits::convert(FillChar()); - } +public: + template + StrFormatSpec(const Char *str, unsigned width, FillChar fill) : AlignSpec(width, fill), + str_(str) + { + internal::CharTraits::convert(FillChar()); + } - const Char *str() const { return str_; } + const Char *str() const { return str_; } }; /** Returns an integer format specifier to format the value in base 2. */ -IntFormatSpec > bin(int value); +IntFormatSpec> bin(int value); /** Returns an integer format specifier to format the value in base 8. */ -IntFormatSpec > oct(int value); +IntFormatSpec> oct(int value); /** Returns an integer format specifier to format the value in base 16 using lower-case letters for the digits above 9. */ -IntFormatSpec > hex(int value); +IntFormatSpec> hex(int value); /** Returns an integer formatter format specifier to format in base 16 using upper-case letters for the digits above 9. */ -IntFormatSpec > hexu(int value); +IntFormatSpec> hexu(int value); /** \rst @@ -1932,58 +2033,59 @@ IntFormatSpec > hexu(int value); \endrst */ -template -IntFormatSpec, Char> pad( - int value, unsigned width, Char fill = ' '); +template +IntFormatSpec, Char> pad(int value, unsigned width, Char fill = ' '); -#define FMT_DEFINE_INT_FORMATTERS(TYPE) \ -inline IntFormatSpec > bin(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'b'>()); \ -} \ - \ -inline IntFormatSpec > oct(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'o'>()); \ -} \ - \ -inline IntFormatSpec > hex(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'x'>()); \ -} \ - \ -inline IntFormatSpec > hexu(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'X'>()); \ -} \ - \ -template \ -inline IntFormatSpec > pad( \ - IntFormatSpec > f, unsigned width) { \ - return IntFormatSpec >( \ - f.value(), AlignTypeSpec(width, ' ')); \ -} \ - \ -/* For compatibility with older compilers we provide two overloads for pad, */ \ -/* one that takes a fill character and one that doesn't. In the future this */ \ -/* can be replaced with one overload making the template argument Char */ \ -/* default to char (C++11). */ \ -template \ -inline IntFormatSpec, Char> pad( \ - IntFormatSpec, Char> f, \ - unsigned width, Char fill) { \ - return IntFormatSpec, Char>( \ - f.value(), AlignTypeSpec(width, fill)); \ -} \ - \ -inline IntFormatSpec > pad( \ - TYPE value, unsigned width) { \ - return IntFormatSpec >( \ - value, AlignTypeSpec<0>(width, ' ')); \ -} \ - \ -template \ -inline IntFormatSpec, Char> pad( \ - TYPE value, unsigned width, Char fill) { \ - return IntFormatSpec, Char>( \ - value, AlignTypeSpec<0>(width, fill)); \ -} +#define FMT_DEFINE_INT_FORMATTERS(TYPE) \ + inline IntFormatSpec> bin(TYPE value) \ + { \ + return IntFormatSpec>(value, TypeSpec<'b'>()); \ + } \ + \ + inline IntFormatSpec> oct(TYPE value) \ + { \ + return IntFormatSpec>(value, TypeSpec<'o'>()); \ + } \ + \ + inline IntFormatSpec> hex(TYPE value) \ + { \ + return IntFormatSpec>(value, TypeSpec<'x'>()); \ + } \ + \ + inline IntFormatSpec> hexu(TYPE value) \ + { \ + return IntFormatSpec>(value, TypeSpec<'X'>()); \ + } \ + \ + template \ + inline IntFormatSpec> pad(IntFormatSpec> f, \ + unsigned width) \ + { \ + return IntFormatSpec>(f.value(), AlignTypeSpec(width, ' ')); \ + } \ + \ + /* For compatibility with older compilers we provide two overloads for pad, */ \ + /* one that takes a fill character and one that doesn't. In the future this */ \ + /* can be replaced with one overload making the template argument Char */ \ + /* default to char (C++11). */ \ + template \ + inline IntFormatSpec, Char> pad(IntFormatSpec, Char> f, \ + unsigned width, \ + Char fill) \ + { \ + return IntFormatSpec, Char>(f.value(), AlignTypeSpec(width, fill)); \ + } \ + \ + inline IntFormatSpec> pad(TYPE value, unsigned width) \ + { \ + return IntFormatSpec>(value, AlignTypeSpec<0>(width, ' ')); \ + } \ + \ + template \ + inline IntFormatSpec, Char> pad(TYPE value, unsigned width, Char fill) \ + { \ + return IntFormatSpec, Char>(value, AlignTypeSpec<0>(width, fill)); \ + } FMT_DEFINE_INT_FORMATTERS(int) FMT_DEFINE_INT_FORMATTERS(long) @@ -2004,239 +2106,252 @@ FMT_DEFINE_INT_FORMATTERS(ULongLong) \endrst */ -template -inline StrFormatSpec pad( - const Char *str, unsigned width, Char fill = ' ') { - return StrFormatSpec(str, width, fill); +template +inline StrFormatSpec +pad(const Char *str, unsigned width, Char fill = ' ') +{ + return StrFormatSpec(str, width, fill); } -inline StrFormatSpec pad( - const wchar_t *str, unsigned width, char fill = ' ') { - return StrFormatSpec(str, width, fill); +inline StrFormatSpec +pad(const wchar_t *str, unsigned width, char fill = ' ') +{ + return StrFormatSpec(str, width, fill); } namespace internal { -template +template class ArgMap { - private: - typedef std::vector< - std::pair, internal::Arg> > MapType; - typedef typename MapType::value_type Pair; +private: + typedef std::vector, internal::Arg>> MapType; + typedef typename MapType::value_type Pair; - MapType map_; + MapType map_; - public: - void init(const ArgList &args); +public: + void init(const ArgList &args); - const internal::Arg *find(const fmt::BasicStringRef &name) const { - // The list is unsorted, so just return the first matching name. - for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); - it != end; ++it) { - if (it->first == name) - return &it->second; + const internal::Arg *find(const fmt::BasicStringRef &name) const + { + // The list is unsorted, so just return the first matching name. + for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); it != end; ++it) { + if (it->first == name) return &it->second; + } + return FMT_NULL; } - return FMT_NULL; - } }; -template -void ArgMap::init(const ArgList &args) { - if (!map_.empty()) - return; - typedef internal::NamedArg NamedArg; - const NamedArg *named_arg = FMT_NULL; - bool use_values = - args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE; - if (use_values) { - for (unsigned i = 0;/*nothing*/; ++i) { - internal::Arg::Type arg_type = args.type(i); - switch (arg_type) { - case internal::Arg::NONE: +template +void +ArgMap::init(const ArgList &args) +{ + if (!map_.empty()) return; + typedef internal::NamedArg NamedArg; + const NamedArg *named_arg = FMT_NULL; + bool use_values = args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE; + if (use_values) { + for (unsigned i = 0; /*nothing*/; ++i) { + internal::Arg::Type arg_type = args.type(i); + switch (arg_type) { + case internal::Arg::NONE: + return; + case internal::Arg::NAMED_ARG: + named_arg = static_cast(args.values_[i].pointer); + map_.push_back(Pair(named_arg->name, *named_arg)); + break; + default: + /*nothing*/; + } + } return; - case internal::Arg::NAMED_ARG: - named_arg = static_cast(args.values_[i].pointer); - map_.push_back(Pair(named_arg->name, *named_arg)); - break; - default: - /*nothing*/; - } } - return; - } - for (unsigned i = 0; i != ArgList::MAX_PACKED_ARGS; ++i) { - internal::Arg::Type arg_type = args.type(i); - if (arg_type == internal::Arg::NAMED_ARG) { - named_arg = static_cast(args.args_[i].pointer); - map_.push_back(Pair(named_arg->name, *named_arg)); + for (unsigned i = 0; i != ArgList::MAX_PACKED_ARGS; ++i) { + internal::Arg::Type arg_type = args.type(i); + if (arg_type == internal::Arg::NAMED_ARG) { + named_arg = static_cast(args.args_[i].pointer); + map_.push_back(Pair(named_arg->name, *named_arg)); + } } - } - for (unsigned i = ArgList::MAX_PACKED_ARGS;/*nothing*/; ++i) { - switch (args.args_[i].type) { - case internal::Arg::NONE: - return; - case internal::Arg::NAMED_ARG: - named_arg = static_cast(args.args_[i].pointer); - map_.push_back(Pair(named_arg->name, *named_arg)); - break; - default: - /*nothing*/; + for (unsigned i = ArgList::MAX_PACKED_ARGS; /*nothing*/; ++i) { + switch (args.args_[i].type) { + case internal::Arg::NONE: + return; + case internal::Arg::NAMED_ARG: + named_arg = static_cast(args.args_[i].pointer); + map_.push_back(Pair(named_arg->name, *named_arg)); + break; + default: + /*nothing*/; + } } - } } -template +template class ArgFormatterBase : public ArgVisitor { - private: - BasicWriter &writer_; - Spec &spec_; +private: + BasicWriter &writer_; + Spec &spec_; - FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase); + FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase); - void write_pointer(const void *p) { - spec_.flags_ = HASH_FLAG; - spec_.type_ = 'x'; - writer_.write_int(reinterpret_cast(p), spec_); - } - - // workaround MSVC two-phase lookup issue - typedef internal::Arg Arg; - - protected: - BasicWriter &writer() { return writer_; } - Spec &spec() { return spec_; } - - void write(bool value) { - const char *str_value = value ? "true" : "false"; - Arg::StringValue str = { str_value, std::strlen(str_value) }; - writer_.write_str(str, spec_); - } - - void write(const char *value) { - Arg::StringValue str = {value, value ? std::strlen(value) : 0}; - writer_.write_str(str, spec_); - } - - public: - typedef Spec SpecType; - - ArgFormatterBase(BasicWriter &w, Spec &s) - : writer_(w), spec_(s) {} - - template - void visit_any_int(T value) { writer_.write_int(value, spec_); } - - template - void visit_any_double(T value) { writer_.write_double(value, spec_); } - - void visit_bool(bool value) { - if (spec_.type_) { - visit_any_int(value); - return; + void write_pointer(const void *p) + { + //FIXME: @tqcq cast_pointer + spec_.flags_ = HASH_FLAG; + spec_.type_ = 'x'; + // #if (sizeof(uintptr_t) < sizeof(void *)) + writer_.write_int(reinterpret_cast(p), spec_); + // #else + // writer_.write_int(reinterpret_cast(p), spec_); + // #endif } - write(value); - } - void visit_char(int value) { - if (spec_.type_ && spec_.type_ != 'c') { - spec_.flags_ |= CHAR_FLAG; - writer_.write_int(value, spec_); - return; + // workaround MSVC two-phase lookup issue + typedef internal::Arg Arg; + +protected: + BasicWriter &writer() { return writer_; } + + Spec &spec() { return spec_; } + + void write(bool value) + { + const char *str_value = value ? "true" : "false"; + Arg::StringValue str = {str_value, std::strlen(str_value)}; + writer_.write_str(str, spec_); } - if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0) - FMT_THROW(FormatError("invalid format specifier for char")); - typedef typename BasicWriter::CharPtr CharPtr; - Char fill = internal::CharTraits::cast(spec_.fill()); - CharPtr out = CharPtr(); - const unsigned CHAR_SIZE = 1; - if (spec_.width_ > CHAR_SIZE) { - out = writer_.grow_buffer(spec_.width_); - if (spec_.align_ == ALIGN_RIGHT) { - std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill); - out += spec_.width_ - CHAR_SIZE; - } else if (spec_.align_ == ALIGN_CENTER) { - out = writer_.fill_padding(out, spec_.width_, - internal::const_check(CHAR_SIZE), fill); - } else { - std::uninitialized_fill_n(out + CHAR_SIZE, - spec_.width_ - CHAR_SIZE, fill); - } - } else { - out = writer_.grow_buffer(CHAR_SIZE); + + void write(const char *value) + { + Arg::StringValue str = {value, value ? std::strlen(value) : 0}; + writer_.write_str(str, spec_); } - *out = internal::CharTraits::cast(value); - } - void visit_cstring(const char *value) { - if (spec_.type_ == 'p') - return write_pointer(value); - write(value); - } +public: + typedef Spec SpecType; - // Qualification with "internal" here and below is a workaround for nvcc. - void visit_string(internal::Arg::StringValue value) { - writer_.write_str(value, spec_); - } + ArgFormatterBase(BasicWriter &w, Spec &s) : writer_(w), spec_(s) {} - using ArgVisitor::visit_wstring; + template + void visit_any_int(T value) + { + writer_.write_int(value, spec_); + } - void visit_wstring(internal::Arg::StringValue value) { - writer_.write_str(value, spec_); - } + template + void visit_any_double(T value) + { + writer_.write_double(value, spec_); + } - void visit_pointer(const void *value) { - if (spec_.type_ && spec_.type_ != 'p') - report_unknown_type(spec_.type_, "pointer"); - write_pointer(value); - } + void visit_bool(bool value) + { + if (spec_.type_) { + visit_any_int(value); + return; + } + write(value); + } + + void visit_char(int value) + { + if (spec_.type_ && spec_.type_ != 'c') { + spec_.flags_ |= CHAR_FLAG; + writer_.write_int(value, spec_); + return; + } + if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0) + FMT_THROW(FormatError("invalid format specifier for char")); + typedef typename BasicWriter::CharPtr CharPtr; + Char fill = internal::CharTraits::cast(spec_.fill()); + CharPtr out = CharPtr(); + const unsigned CHAR_SIZE = 1; + if (spec_.width_ > CHAR_SIZE) { + out = writer_.grow_buffer(spec_.width_); + if (spec_.align_ == ALIGN_RIGHT) { + std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill); + out += spec_.width_ - CHAR_SIZE; + } else if (spec_.align_ == ALIGN_CENTER) { + out = writer_.fill_padding(out, spec_.width_, internal::const_check(CHAR_SIZE), fill); + } else { + std::uninitialized_fill_n(out + CHAR_SIZE, spec_.width_ - CHAR_SIZE, fill); + } + } else { + out = writer_.grow_buffer(CHAR_SIZE); + } + *out = internal::CharTraits::cast(value); + } + + void visit_cstring(const char *value) + { + if (spec_.type_ == 'p') return write_pointer(value); + write(value); + } + + // Qualification with "internal" here and below is a workaround for nvcc. + void visit_string(internal::Arg::StringValue value) { writer_.write_str(value, spec_); } + + using ArgVisitor::visit_wstring; + + void visit_wstring(internal::Arg::StringValue value) { writer_.write_str(value, spec_); } + + void visit_pointer(const void *value) + { + if (spec_.type_ && spec_.type_ != 'p') report_unknown_type(spec_.type_, "pointer"); + write_pointer(value); + } }; class FormatterBase { - private: - ArgList args_; - int next_arg_index_; +private: + ArgList args_; + int next_arg_index_; - // Returns the argument with specified index. - FMT_API Arg do_get_arg(unsigned arg_index, const char *&error); + // Returns the argument with specified index. + FMT_API Arg do_get_arg(unsigned arg_index, const char *&error); - protected: - const ArgList &args() const { return args_; } +protected: + const ArgList &args() const { return args_; } - explicit FormatterBase(const ArgList &args) { - args_ = args; - next_arg_index_ = 0; - } - - // Returns the next argument. - Arg next_arg(const char *&error) { - if (next_arg_index_ >= 0) - return do_get_arg(internal::to_unsigned(next_arg_index_++), error); - error = "cannot switch from manual to automatic argument indexing"; - return Arg(); - } - - // Checks if manual indexing is used and returns the argument with - // specified index. - Arg get_arg(unsigned arg_index, const char *&error) { - return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg(); - } - - bool check_no_auto_index(const char *&error) { - if (next_arg_index_ > 0) { - error = "cannot switch from automatic to manual argument indexing"; - return false; + explicit FormatterBase(const ArgList &args) + { + args_ = args; + next_arg_index_ = 0; } - next_arg_index_ = -1; - return true; - } - template - void write(BasicWriter &w, const Char *start, const Char *end) { - if (start != end) - w << BasicStringRef(start, internal::to_unsigned(end - start)); - } + // Returns the next argument. + Arg next_arg(const char *&error) + { + if (next_arg_index_ >= 0) return do_get_arg(internal::to_unsigned(next_arg_index_++), error); + error = "cannot switch from manual to automatic argument indexing"; + return Arg(); + } + + // Checks if manual indexing is used and returns the argument with + // specified index. + Arg get_arg(unsigned arg_index, const char *&error) + { + return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg(); + } + + bool check_no_auto_index(const char *&error) + { + if (next_arg_index_ > 0) { + error = "cannot switch from automatic to manual argument indexing"; + return false; + } + next_arg_index_ = -1; + return true; + } + + template + void write(BasicWriter &w, const Char *start, const Char *end) + { + if (start != end) w << BasicStringRef(start, internal::to_unsigned(end - start)); + } }; -} // namespace internal +}// namespace internal /** \rst @@ -2255,14 +2370,14 @@ class FormatterBase { will be called. \endrst */ -template +template class BasicArgFormatter : public internal::ArgFormatterBase { - private: - BasicFormatter &formatter_; - const Char *format_; +private: + BasicFormatter &formatter_; + const Char *format_; - public: - /** +public: + /** \rst Constructs an argument formatter object. *formatter* is a reference to the main formatter object, *spec* contains @@ -2270,275 +2385,275 @@ class BasicArgFormatter : public internal::ArgFormatterBase { to the part of the format string being parsed for custom argument types. \endrst */ - BasicArgFormatter(BasicFormatter &formatter, - Spec &spec, const Char *fmt) - : internal::ArgFormatterBase(formatter.writer(), spec), - formatter_(formatter), format_(fmt) {} + BasicArgFormatter(BasicFormatter &formatter, Spec &spec, const Char *fmt) + : internal::ArgFormatterBase(formatter.writer(), spec), + formatter_(formatter), + format_(fmt) + {} - /** Formats an argument of a custom (user-defined) type. */ - void visit_custom(internal::Arg::CustomValue c) { - c.format(&formatter_, c.value, &format_); - } + /** Formats an argument of a custom (user-defined) type. */ + void visit_custom(internal::Arg::CustomValue c) { c.format(&formatter_, c.value, &format_); } }; /** The default argument formatter. */ -template -class ArgFormatter : - public BasicArgFormatter, Char, FormatSpec> { - public: - /** Constructs an argument formatter object. */ - ArgFormatter(BasicFormatter &formatter, - FormatSpec &spec, const Char *fmt) - : BasicArgFormatter, - Char, FormatSpec>(formatter, spec, fmt) {} +template +class ArgFormatter : public BasicArgFormatter, Char, FormatSpec> { +public: + /** Constructs an argument formatter object. */ + ArgFormatter(BasicFormatter &formatter, FormatSpec &spec, const Char *fmt) + : BasicArgFormatter, Char, FormatSpec>(formatter, spec, fmt) + {} }; /** This template formats data and writes the output to a writer. */ -template +template class BasicFormatter : private internal::FormatterBase { - public: - /** The character type for the output. */ - typedef CharType Char; +public: + /** The character type for the output. */ + typedef CharType Char; - private: - BasicWriter &writer_; - internal::ArgMap map_; +private: + BasicWriter &writer_; + internal::ArgMap map_; - FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter); + FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter); - using internal::FormatterBase::get_arg; + using internal::FormatterBase::get_arg; - // Checks if manual indexing is used and returns the argument with - // specified name. - internal::Arg get_arg(BasicStringRef arg_name, const char *&error); + // Checks if manual indexing is used and returns the argument with + // specified name. + internal::Arg get_arg(BasicStringRef arg_name, const char *&error); - // Parses argument index and returns corresponding argument. - internal::Arg parse_arg_index(const Char *&s); + // Parses argument index and returns corresponding argument. + internal::Arg parse_arg_index(const Char *&s); - // Parses argument name and returns corresponding argument. - internal::Arg parse_arg_name(const Char *&s); + // Parses argument name and returns corresponding argument. + internal::Arg parse_arg_name(const Char *&s); - public: - /** +public: + /** \rst Constructs a ``BasicFormatter`` object. References to the arguments and the writer are stored in the formatter object so make sure they have appropriate lifetimes. \endrst */ - BasicFormatter(const ArgList &args, BasicWriter &w) - : internal::FormatterBase(args), writer_(w) {} + BasicFormatter(const ArgList &args, BasicWriter &w) : internal::FormatterBase(args), writer_(w) {} - /** Returns a reference to the writer associated with this formatter. */ - BasicWriter &writer() { return writer_; } + /** Returns a reference to the writer associated with this formatter. */ + BasicWriter &writer() { return writer_; } - /** Formats stored arguments and writes the output to the writer. */ - void format(BasicCStringRef format_str); + /** Formats stored arguments and writes the output to the writer. */ + void format(BasicCStringRef format_str); - // Formats a single argument and advances format_str, a format string pointer. - const Char *format(const Char *&format_str, const internal::Arg &arg); + // Formats a single argument and advances format_str, a format string pointer. + const Char *format(const Char *&format_str, const internal::Arg &arg); }; // Generates a comma-separated list with results of applying f to // numbers 0..n-1. -# define FMT_GEN(n, f) FMT_GEN##n(f) -# define FMT_GEN1(f) f(0) -# define FMT_GEN2(f) FMT_GEN1(f), f(1) -# define FMT_GEN3(f) FMT_GEN2(f), f(2) -# define FMT_GEN4(f) FMT_GEN3(f), f(3) -# define FMT_GEN5(f) FMT_GEN4(f), f(4) -# define FMT_GEN6(f) FMT_GEN5(f), f(5) -# define FMT_GEN7(f) FMT_GEN6(f), f(6) -# define FMT_GEN8(f) FMT_GEN7(f), f(7) -# define FMT_GEN9(f) FMT_GEN8(f), f(8) -# define FMT_GEN10(f) FMT_GEN9(f), f(9) -# define FMT_GEN11(f) FMT_GEN10(f), f(10) -# define FMT_GEN12(f) FMT_GEN11(f), f(11) -# define FMT_GEN13(f) FMT_GEN12(f), f(12) -# define FMT_GEN14(f) FMT_GEN13(f), f(13) -# define FMT_GEN15(f) FMT_GEN14(f), f(14) +#define FMT_GEN(n, f) FMT_GEN##n(f) +#define FMT_GEN1(f) f(0) +#define FMT_GEN2(f) FMT_GEN1(f), f(1) +#define FMT_GEN3(f) FMT_GEN2(f), f(2) +#define FMT_GEN4(f) FMT_GEN3(f), f(3) +#define FMT_GEN5(f) FMT_GEN4(f), f(4) +#define FMT_GEN6(f) FMT_GEN5(f), f(5) +#define FMT_GEN7(f) FMT_GEN6(f), f(6) +#define FMT_GEN8(f) FMT_GEN7(f), f(7) +#define FMT_GEN9(f) FMT_GEN8(f), f(8) +#define FMT_GEN10(f) FMT_GEN9(f), f(9) +#define FMT_GEN11(f) FMT_GEN10(f), f(10) +#define FMT_GEN12(f) FMT_GEN11(f), f(11) +#define FMT_GEN13(f) FMT_GEN12(f), f(12) +#define FMT_GEN14(f) FMT_GEN13(f), f(13) +#define FMT_GEN15(f) FMT_GEN14(f), f(14) namespace internal { -inline uint64_t make_type() { return 0; } - -template -inline uint64_t make_type(const T &arg) { - return MakeValue< BasicFormatter >::type(arg); +inline uint64_t +make_type() +{ + return 0; } -template +template +inline uint64_t +make_type(const T &arg) +{ + return MakeValue>::type(arg); +} + +template struct ArgArray; -template -struct ArgArray { - // '+' is used to silence GCC -Wduplicated-branches warning. - typedef Value Type[N > 0 ? N : +1]; +template +struct ArgArray { + // '+' is used to silence GCC -Wduplicated-branches warning. + typedef Value Type[N > 0 ? N : +1]; - template - static Value make(const T &value) { + template + static Value make(const T &value) + { #ifdef __clang__ - Value result = MakeValue(value); - // Workaround a bug in Apple LLVM version 4.2 (clang-425.0.28) of clang: - // https://github.com/fmtlib/fmt/issues/276 - (void)result.custom.format; - return result; + Value result = MakeValue(value); + // Workaround a bug in Apple LLVM version 4.2 (clang-425.0.28) of clang: + // https://github.com/fmtlib/fmt/issues/276 + (void) result.custom.format; + return result; #else - return MakeValue(value); + return MakeValue(value); #endif - } + } }; -template -struct ArgArray { - typedef Arg Type[N + 1]; // +1 for the list end Arg::NONE +template +struct ArgArray { + typedef Arg Type[N + 1];// +1 for the list end Arg::NONE - template - static Arg make(const T &value) { return MakeArg(value); } + template + static Arg make(const T &value) + { + return MakeArg(value); + } }; #if FMT_USE_VARIADIC_TEMPLATES -template -inline uint64_t make_type(const Arg &first, const Args & ... tail) { - return make_type(first) | (make_type(tail...) << 4); +template +inline uint64_t +make_type(const Arg &first, const Args &...tail) +{ + return make_type(first) | (make_type(tail...) << 4); } #else struct ArgType { - uint64_t type; + uint64_t type; - ArgType() : type(0) {} + ArgType() : type(0) {} - template - ArgType(const T &arg) : type(make_type(arg)) {} + template + ArgType(const T &arg) : type(make_type(arg)) + {} }; -# define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType() +#define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType() -inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { - return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) | - (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) | - (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) | - (t12.type << 48) | (t13.type << 52) | (t14.type << 56); +inline uint64_t +make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) +{ + return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) | (t4.type << 16) | (t5.type << 20) + | (t6.type << 24) | (t7.type << 28) | (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) + | (t12.type << 48) | (t13.type << 52) | (t14.type << 56); } #endif -} // namespace internal +}// namespace internal -# define FMT_MAKE_TEMPLATE_ARG(n) typename T##n -# define FMT_MAKE_ARG_TYPE(n) T##n -# define FMT_MAKE_ARG(n) const T##n &v##n -# define FMT_ASSIGN_char(n) \ - arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) -# define FMT_ASSIGN_wchar_t(n) \ - arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) +#define FMT_MAKE_TEMPLATE_ARG(n) typename T##n +#define FMT_MAKE_ARG_TYPE(n) T##n +#define FMT_MAKE_ARG(n) const T##n &v##n +#define FMT_ASSIGN_char(n) arr[n] = fmt::internal::MakeValue>(v##n) +#define FMT_ASSIGN_wchar_t(n) arr[n] = fmt::internal::MakeValue>(v##n) #if FMT_USE_VARIADIC_TEMPLATES // Defines a variadic function returning void. -# define FMT_VARIADIC_VOID(func, arg_type) \ - template \ - void func(arg_type arg0, const Args & ... args) { \ - typedef fmt::internal::ArgArray ArgArray; \ - typename ArgArray::Type array{ \ - ArgArray::template make >(args)...}; \ - func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \ - } +#define FMT_VARIADIC_VOID(func, arg_type) \ + template \ + void func(arg_type arg0, const Args &...args) \ + { \ + typedef fmt::internal::ArgArray ArgArray; \ + typename ArgArray::Type array{ArgArray::template make>(args)...}; \ + func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \ + } // Defines a variadic constructor. -# define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \ - template \ - ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \ - typedef fmt::internal::ArgArray ArgArray; \ - typename ArgArray::Type array{ \ - ArgArray::template make >(args)...}; \ - func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \ - } +#define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \ + template \ + ctor(arg0_type arg0, arg1_type arg1, const Args &...args) \ + { \ + typedef fmt::internal::ArgArray ArgArray; \ + typename ArgArray::Type array{ArgArray::template make>(args)...}; \ + func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \ + } #else -# define FMT_MAKE_REF(n) \ - fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) -# define FMT_MAKE_REF2(n) v##n +#define FMT_MAKE_REF(n) fmt::internal::MakeValue>(v##n) +#define FMT_MAKE_REF2(n) v##n // Defines a wrapper for a function taking one argument of type arg_type // and n additional arguments of arbitrary types. -# define FMT_WRAP1(func, arg_type, n) \ - template \ - inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \ - const fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ - func(arg1, fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ - } +#define FMT_WRAP1(func, arg_type, n) \ + template \ + inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) \ + { \ + const fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ + func(arg1, fmt::ArgList(fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ + } // Emulates a variadic function returning void on a pre-C++11 compiler. -# define FMT_VARIADIC_VOID(func, arg_type) \ - inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \ - FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \ - FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \ - FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \ - FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \ - FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10) +#define FMT_VARIADIC_VOID(func, arg_type) \ + inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \ + FMT_WRAP1(func, arg_type, 1) \ + FMT_WRAP1(func, arg_type, 2) \ + FMT_WRAP1(func, arg_type, 3) \ + FMT_WRAP1(func, arg_type, 4) FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \ + FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) FMT_WRAP1(func, arg_type, 9) \ + FMT_WRAP1(func, arg_type, 10) -# define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \ - template \ - ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \ - const fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ - func(arg0, arg1, fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ - } +#define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \ + template \ + ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) \ + { \ + const fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ + func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ + } // Emulates a variadic constructor on a pre-C++11 compiler. -# define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \ - FMT_CTOR(ctor, func, arg0_type, arg1_type, 10) +#define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \ + FMT_CTOR(ctor, func, arg0_type, arg1_type, 10) #endif // Generates a comma-separated list with results of applying f to pairs // (argument, index). #define FMT_FOR_EACH1(f, x0) f(x0, 0) -#define FMT_FOR_EACH2(f, x0, x1) \ - FMT_FOR_EACH1(f, x0), f(x1, 1) -#define FMT_FOR_EACH3(f, x0, x1, x2) \ - FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2) -#define FMT_FOR_EACH4(f, x0, x1, x2, x3) \ - FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3) -#define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \ - FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4) -#define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \ - FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5) -#define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \ - FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6) -#define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \ - FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7) -#define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \ - FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8) -#define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \ - FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9) +#define FMT_FOR_EACH2(f, x0, x1) FMT_FOR_EACH1(f, x0), f(x1, 1) +#define FMT_FOR_EACH3(f, x0, x1, x2) FMT_FOR_EACH2(f, x0, x1), f(x2, 2) +#define FMT_FOR_EACH4(f, x0, x1, x2, x3) FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3) +#define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4) +#define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5) +#define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6) +#define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7) +#define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8) +#define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \ + FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9) /** An error returned by an operating system or a language runtime, for example a file opening error. */ class SystemError : public internal::RuntimeError { - private: - FMT_API void init(int err_code, CStringRef format_str, ArgList args); +private: + FMT_API void init(int err_code, CStringRef format_str, ArgList args); - protected: - int error_code_; +protected: + int error_code_; - typedef char Char; // For FMT_VARIADIC_CTOR. + typedef char Char;// For FMT_VARIADIC_CTOR. - SystemError() {} + SystemError() {} - public: - /** +public: + /** \rst Constructs a :class:`fmt::SystemError` object with a description formatted with `fmt::format_system_error`. *message* and additional @@ -2556,15 +2671,13 @@ class SystemError : public internal::RuntimeError { throw fmt::SystemError(errno, "cannot open file '{}'", filename); \endrst */ - SystemError(int error_code, CStringRef message) { - init(error_code, message, ArgList()); - } - FMT_DEFAULTED_COPY_CTOR(SystemError) - FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef) + SystemError(int error_code, CStringRef message) { init(error_code, message, ArgList()); } + FMT_DEFAULTED_COPY_CTOR(SystemError) + FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef) - FMT_API ~SystemError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE; + FMT_API ~SystemError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE; - int error_code() const { return error_code_; } + int error_code() const { return error_code_; } }; /** @@ -2583,8 +2696,7 @@ class SystemError : public internal::RuntimeError { may look like "Unknown error -1" and is platform-dependent. \endrst */ -FMT_API void format_system_error(fmt::Writer &out, int error_code, - fmt::StringRef message) FMT_NOEXCEPT; +FMT_API void format_system_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT; /** \rst @@ -2604,156 +2716,153 @@ FMT_API void format_system_error(fmt::Writer &out, int error_code, \endrst */ -template +template class BasicWriter { - private: - // Output buffer. - Buffer &buffer_; +private: + // Output buffer. + Buffer &buffer_; - FMT_DISALLOW_COPY_AND_ASSIGN(BasicWriter); + FMT_DISALLOW_COPY_AND_ASSIGN(BasicWriter); - typedef typename internal::CharTraits::CharPtr CharPtr; + typedef typename internal::CharTraits::CharPtr CharPtr; #if FMT_SECURE_SCL - // Returns pointer value. - static Char *get(CharPtr p) { return p.base(); } + // Returns pointer value. + static Char *get(CharPtr p) { return p.base(); } #else - static Char *get(Char *p) { return p; } + static Char *get(Char *p) { return p; } #endif - // Fills the padding around the content and returns the pointer to the - // content area. - static CharPtr fill_padding(CharPtr buffer, - unsigned total_size, std::size_t content_size, wchar_t fill); + // Fills the padding around the content and returns the pointer to the + // content area. + static CharPtr fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill); - // Grows the buffer by n characters and returns a pointer to the newly - // allocated area. - CharPtr grow_buffer(std::size_t n) { - std::size_t size = buffer_.size(); - buffer_.resize(size + n); - return internal::make_ptr(&buffer_[size], n); - } - - // Writes an unsigned decimal integer. - template - Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) { - unsigned num_digits = internal::count_digits(value); - Char *ptr = get(grow_buffer(prefix_size + num_digits)); - internal::format_decimal(ptr + prefix_size, value, num_digits); - return ptr; - } - - // Writes a decimal integer. - template - void write_decimal(Int value) { - typedef typename internal::IntTraits::MainType MainType; - MainType abs_value = static_cast(value); - if (internal::is_negative(value)) { - abs_value = 0 - abs_value; - *write_unsigned_decimal(abs_value, 1) = '-'; - } else { - write_unsigned_decimal(abs_value, 0); + // Grows the buffer by n characters and returns a pointer to the newly + // allocated area. + CharPtr grow_buffer(std::size_t n) + { + std::size_t size = buffer_.size(); + buffer_.resize(size + n); + return internal::make_ptr(&buffer_[size], n); } - } - // Prepare a buffer for integer formatting. - CharPtr prepare_int_buffer(unsigned num_digits, - const EmptySpec &, const char *prefix, unsigned prefix_size) { - unsigned size = prefix_size + num_digits; - CharPtr p = grow_buffer(size); - std::uninitialized_copy(prefix, prefix + prefix_size, p); - return p + size - 1; - } + // Writes an unsigned decimal integer. + template + Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) + { + unsigned num_digits = internal::count_digits(value); + Char *ptr = get(grow_buffer(prefix_size + num_digits)); + internal::format_decimal(ptr + prefix_size, value, num_digits); + return ptr; + } - template - CharPtr prepare_int_buffer(unsigned num_digits, - const Spec &spec, const char *prefix, unsigned prefix_size); + // Writes a decimal integer. + template + void write_decimal(Int value) + { + typedef typename internal::IntTraits::MainType MainType; + MainType abs_value = static_cast(value); + if (internal::is_negative(value)) { + abs_value = 0 - abs_value; + *write_unsigned_decimal(abs_value, 1) = '-'; + } else { + write_unsigned_decimal(abs_value, 0); + } + } - // Formats an integer. - template - void write_int(T value, Spec spec); + // Prepare a buffer for integer formatting. + CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size) + { + unsigned size = prefix_size + num_digits; + CharPtr p = grow_buffer(size); + std::uninitialized_copy(prefix, prefix + prefix_size, p); + return p + size - 1; + } - // Formats a floating-point number (double or long double). - template - void write_double(T value, const Spec &spec); + template + CharPtr prepare_int_buffer(unsigned num_digits, const Spec &spec, const char *prefix, unsigned prefix_size); - // Writes a formatted string. - template - CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec); + // Formats an integer. + template + void write_int(T value, Spec spec); - template - void write_str(const internal::Arg::StringValue &str, - const Spec &spec); + // Formats a floating-point number (double or long double). + template + void write_double(T value, const Spec &spec); - // This following methods are private to disallow writing wide characters - // and strings to a char stream. If you want to print a wide string as a - // pointer as std::ostream does, cast it to const void*. - // Do not implement! - void operator<<(typename internal::WCharHelper::Unsupported); - void operator<<( - typename internal::WCharHelper::Unsupported); + // Writes a formatted string. + template + CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec); - // Appends floating-point length specifier to the format string. - // The second argument is only used for overload resolution. - void append_float_length(Char *&format_ptr, long double) { - *format_ptr++ = 'L'; - } + template + void write_str(const internal::Arg::StringValue &str, const Spec &spec); - template - void append_float_length(Char *&, T) {} + // This following methods are private to disallow writing wide characters + // and strings to a char stream. If you want to print a wide string as a + // pointer as std::ostream does, cast it to const void*. + // Do not implement! + void operator<<(typename internal::WCharHelper::Unsupported); + void operator<<(typename internal::WCharHelper::Unsupported); - template - friend class internal::ArgFormatterBase; + // Appends floating-point length specifier to the format string. + // The second argument is only used for overload resolution. + void append_float_length(Char *&format_ptr, long double) { *format_ptr++ = 'L'; } - template - friend class BasicPrintfArgFormatter; + template + void append_float_length(Char *&, T) + {} - protected: - /** + template + friend class internal::ArgFormatterBase; + + template + friend class BasicPrintfArgFormatter; + +protected: + /** Constructs a ``BasicWriter`` object. */ - explicit BasicWriter(Buffer &b) : buffer_(b) {} + explicit BasicWriter(Buffer &b) : buffer_(b) {} - public: - /** +public: + /** \rst Destroys a ``BasicWriter`` object. \endrst */ - virtual ~BasicWriter() {} + virtual ~BasicWriter() {} - /** + /** Returns the total number of characters written. */ - std::size_t size() const { return buffer_.size(); } + std::size_t size() const { return buffer_.size(); } - /** + /** Returns a pointer to the output buffer content. No terminating null character is appended. */ - const Char *data() const FMT_NOEXCEPT { return &buffer_[0]; } + const Char *data() const FMT_NOEXCEPT { return &buffer_[0]; } - /** + /** Returns a pointer to the output buffer content with terminating null character appended. */ - const Char *c_str() const { - std::size_t size = buffer_.size(); - buffer_.reserve(size + 1); - buffer_[size] = '\0'; - return &buffer_[0]; - } + const Char *c_str() const + { + std::size_t size = buffer_.size(); + buffer_.reserve(size + 1); + buffer_[size] = '\0'; + return &buffer_[0]; + } - /** + /** \rst Returns the content of the output buffer as an `std::string`. \endrst */ - std::basic_string str() const { - return std::basic_string(&buffer_[0], buffer_.size()); - } + std::basic_string str() const { return std::basic_string(&buffer_[0], buffer_.size()); } - /** + /** \rst Writes formatted data. @@ -2778,476 +2887,474 @@ class BasicWriter { See also :ref:`syntax`. \endrst */ - void write(BasicCStringRef format, ArgList args) { - BasicFormatter(args, *this).format(format); - } - FMT_VARIADIC_VOID(write, BasicCStringRef) + void write(BasicCStringRef format, ArgList args) { BasicFormatter(args, *this).format(format); } + FMT_VARIADIC_VOID(write, BasicCStringRef) - BasicWriter &operator<<(int value) { - write_decimal(value); - return *this; - } - BasicWriter &operator<<(unsigned value) { - return *this << IntFormatSpec(value); - } - BasicWriter &operator<<(long value) { - write_decimal(value); - return *this; - } - BasicWriter &operator<<(unsigned long value) { - return *this << IntFormatSpec(value); - } - BasicWriter &operator<<(LongLong value) { - write_decimal(value); - return *this; - } + BasicWriter &operator<<(int value) + { + write_decimal(value); + return *this; + } - /** + BasicWriter &operator<<(unsigned value) { return *this << IntFormatSpec(value); } + + BasicWriter &operator<<(long value) + { + write_decimal(value); + return *this; + } + + BasicWriter &operator<<(unsigned long value) { return *this << IntFormatSpec(value); } + + BasicWriter &operator<<(LongLong value) + { + write_decimal(value); + return *this; + } + + /** \rst Formats *value* and writes it to the stream. \endrst */ - BasicWriter &operator<<(ULongLong value) { - return *this << IntFormatSpec(value); - } + BasicWriter &operator<<(ULongLong value) { return *this << IntFormatSpec(value); } - BasicWriter &operator<<(double value) { - write_double(value, FormatSpec()); - return *this; - } + BasicWriter &operator<<(double value) + { + write_double(value, FormatSpec()); + return *this; + } - /** + /** \rst Formats *value* using the general format for floating-point numbers (``'g'``) and writes it to the stream. \endrst */ - BasicWriter &operator<<(long double value) { - write_double(value, FormatSpec()); - return *this; - } + BasicWriter &operator<<(long double value) + { + write_double(value, FormatSpec()); + return *this; + } - /** + /** Writes a character to the stream. */ - BasicWriter &operator<<(char value) { - buffer_.push_back(value); - return *this; - } + BasicWriter &operator<<(char value) + { + buffer_.push_back(value); + return *this; + } - BasicWriter &operator<<( - typename internal::WCharHelper::Supported value) { - buffer_.push_back(value); - return *this; - } + BasicWriter &operator<<(typename internal::WCharHelper::Supported value) + { + buffer_.push_back(value); + return *this; + } - /** + /** \rst Writes *value* to the stream. \endrst */ - BasicWriter &operator<<(fmt::BasicStringRef value) { - const Char *str = value.data(); - buffer_.append(str, str + value.size()); - return *this; - } + BasicWriter &operator<<(fmt::BasicStringRef value) + { + const Char *str = value.data(); + buffer_.append(str, str + value.size()); + return *this; + } - BasicWriter &operator<<( - typename internal::WCharHelper::Supported value) { - const char *str = value.data(); - buffer_.append(str, str + value.size()); - return *this; - } + BasicWriter &operator<<(typename internal::WCharHelper::Supported value) + { + const char *str = value.data(); + buffer_.append(str, str + value.size()); + return *this; + } - template - BasicWriter &operator<<(IntFormatSpec spec) { - internal::CharTraits::convert(FillChar()); - write_int(spec.value(), spec); - return *this; - } + template + BasicWriter &operator<<(IntFormatSpec spec) + { + internal::CharTraits::convert(FillChar()); + write_int(spec.value(), spec); + return *this; + } - template - BasicWriter &operator<<(const StrFormatSpec &spec) { - const StrChar *s = spec.str(); - write_str(s, std::char_traits::length(s), spec); - return *this; - } + template + BasicWriter &operator<<(const StrFormatSpec &spec) + { + const StrChar *s = spec.str(); + write_str(s, std::char_traits::length(s), spec); + return *this; + } - void clear() FMT_NOEXCEPT { buffer_.clear(); } + void clear() FMT_NOEXCEPT { buffer_.clear(); } - Buffer &buffer() FMT_NOEXCEPT { return buffer_; } + Buffer &buffer() FMT_NOEXCEPT { return buffer_; } }; -template -template -typename BasicWriter::CharPtr BasicWriter::write_str( - const StrChar *s, std::size_t size, const AlignSpec &spec) { - CharPtr out = CharPtr(); - if (spec.width() > size) { - out = grow_buffer(spec.width()); - Char fill = internal::CharTraits::cast(spec.fill()); - if (spec.align() == ALIGN_RIGHT) { - std::uninitialized_fill_n(out, spec.width() - size, fill); - out += spec.width() - size; - } else if (spec.align() == ALIGN_CENTER) { - out = fill_padding(out, spec.width(), size, fill); +template +template +typename BasicWriter::CharPtr +BasicWriter::write_str(const StrChar *s, std::size_t size, const AlignSpec &spec) +{ + CharPtr out = CharPtr(); + if (spec.width() > size) { + out = grow_buffer(spec.width()); + Char fill = internal::CharTraits::cast(spec.fill()); + if (spec.align() == ALIGN_RIGHT) { + std::uninitialized_fill_n(out, spec.width() - size, fill); + out += spec.width() - size; + } else if (spec.align() == ALIGN_CENTER) { + out = fill_padding(out, spec.width(), size, fill); + } else { + std::uninitialized_fill_n(out + size, spec.width() - size, fill); + } } else { - std::uninitialized_fill_n(out + size, spec.width() - size, fill); + out = grow_buffer(size); } - } else { - out = grow_buffer(size); - } - std::uninitialized_copy(s, s + size, out); - return out; + std::uninitialized_copy(s, s + size, out); + return out; } -template -template -void BasicWriter::write_str( - const internal::Arg::StringValue &s, const Spec &spec) { - // Check if StrChar is convertible to Char. - internal::CharTraits::convert(StrChar()); - if (spec.type_ && spec.type_ != 's') - internal::report_unknown_type(spec.type_, "string"); - const StrChar *str_value = s.value; - std::size_t str_size = s.size; - if (str_size == 0) { - if (!str_value) { - FMT_THROW(FormatError("string pointer is null")); +template +template +void +BasicWriter::write_str(const internal::Arg::StringValue &s, const Spec &spec) +{ + // Check if StrChar is convertible to Char. + internal::CharTraits::convert(StrChar()); + if (spec.type_ && spec.type_ != 's') internal::report_unknown_type(spec.type_, "string"); + const StrChar *str_value = s.value; + std::size_t str_size = s.size; + if (str_size == 0) { + if (!str_value) { FMT_THROW(FormatError("string pointer is null")); } } - } - std::size_t precision = static_cast(spec.precision_); - if (spec.precision_ >= 0 && precision < str_size) - str_size = precision; - write_str(str_value, str_size, spec); + std::size_t precision = static_cast(spec.precision_); + if (spec.precision_ >= 0 && precision < str_size) str_size = precision; + write_str(str_value, str_size, spec); } -template +template typename BasicWriter::CharPtr - BasicWriter::fill_padding( - CharPtr buffer, unsigned total_size, - std::size_t content_size, wchar_t fill) { - std::size_t padding = total_size - content_size; - std::size_t left_padding = padding / 2; - Char fill_char = internal::CharTraits::cast(fill); - std::uninitialized_fill_n(buffer, left_padding, fill_char); - buffer += left_padding; - CharPtr content = buffer; - std::uninitialized_fill_n(buffer + content_size, - padding - left_padding, fill_char); - return content; +BasicWriter::fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill) +{ + std::size_t padding = total_size - content_size; + std::size_t left_padding = padding / 2; + Char fill_char = internal::CharTraits::cast(fill); + std::uninitialized_fill_n(buffer, left_padding, fill_char); + buffer += left_padding; + CharPtr content = buffer; + std::uninitialized_fill_n(buffer + content_size, padding - left_padding, fill_char); + return content; } -template -template +template +template typename BasicWriter::CharPtr - BasicWriter::prepare_int_buffer( - unsigned num_digits, const Spec &spec, - const char *prefix, unsigned prefix_size) { - unsigned width = spec.width(); - Alignment align = spec.align(); - Char fill = internal::CharTraits::cast(spec.fill()); - if (spec.precision() > static_cast(num_digits)) { - // Octal prefix '0' is counted as a digit, so ignore it if precision - // is specified. - if (prefix_size > 0 && prefix[prefix_size - 1] == '0') - --prefix_size; - unsigned number_size = - prefix_size + internal::to_unsigned(spec.precision()); - AlignSpec subspec(number_size, '0', ALIGN_NUMERIC); - if (number_size >= width) - return prepare_int_buffer(num_digits, subspec, prefix, prefix_size); - buffer_.reserve(width); - unsigned fill_size = width - number_size; - if (align != ALIGN_LEFT) { - CharPtr p = grow_buffer(fill_size); - std::uninitialized_fill(p, p + fill_size, fill); +BasicWriter::prepare_int_buffer(unsigned num_digits, const Spec &spec, const char *prefix, unsigned prefix_size) +{ + unsigned width = spec.width(); + Alignment align = spec.align(); + Char fill = internal::CharTraits::cast(spec.fill()); + if (spec.precision() > static_cast(num_digits)) { + // Octal prefix '0' is counted as a digit, so ignore it if precision + // is specified. + if (prefix_size > 0 && prefix[prefix_size - 1] == '0') --prefix_size; + unsigned number_size = prefix_size + internal::to_unsigned(spec.precision()); + AlignSpec subspec(number_size, '0', ALIGN_NUMERIC); + if (number_size >= width) return prepare_int_buffer(num_digits, subspec, prefix, prefix_size); + buffer_.reserve(width); + unsigned fill_size = width - number_size; + if (align != ALIGN_LEFT) { + CharPtr p = grow_buffer(fill_size); + std::uninitialized_fill(p, p + fill_size, fill); + } + CharPtr result = prepare_int_buffer(num_digits, subspec, prefix, prefix_size); + if (align == ALIGN_LEFT) { + CharPtr p = grow_buffer(fill_size); + std::uninitialized_fill(p, p + fill_size, fill); + } + return result; } - CharPtr result = prepare_int_buffer( - num_digits, subspec, prefix, prefix_size); + unsigned size = prefix_size + num_digits; + if (width <= size) { + CharPtr p = grow_buffer(size); + std::uninitialized_copy(prefix, prefix + prefix_size, p); + return p + size - 1; + } + CharPtr p = grow_buffer(width); + CharPtr end = p + width; if (align == ALIGN_LEFT) { - CharPtr p = grow_buffer(fill_size); - std::uninitialized_fill(p, p + fill_size, fill); - } - return result; - } - unsigned size = prefix_size + num_digits; - if (width <= size) { - CharPtr p = grow_buffer(size); - std::uninitialized_copy(prefix, prefix + prefix_size, p); - return p + size - 1; - } - CharPtr p = grow_buffer(width); - CharPtr end = p + width; - if (align == ALIGN_LEFT) { - std::uninitialized_copy(prefix, prefix + prefix_size, p); - p += size; - std::uninitialized_fill(p, end, fill); - } else if (align == ALIGN_CENTER) { - p = fill_padding(p, width, size, fill); - std::uninitialized_copy(prefix, prefix + prefix_size, p); - p += size; - } else { - if (align == ALIGN_NUMERIC) { - if (prefix_size != 0) { - p = std::uninitialized_copy(prefix, prefix + prefix_size, p); - size -= prefix_size; - } + std::uninitialized_copy(prefix, prefix + prefix_size, p); + p += size; + std::uninitialized_fill(p, end, fill); + } else if (align == ALIGN_CENTER) { + p = fill_padding(p, width, size, fill); + std::uninitialized_copy(prefix, prefix + prefix_size, p); + p += size; } else { - std::uninitialized_copy(prefix, prefix + prefix_size, end - size); + if (align == ALIGN_NUMERIC) { + if (prefix_size != 0) { + p = std::uninitialized_copy(prefix, prefix + prefix_size, p); + size -= prefix_size; + } + } else { + std::uninitialized_copy(prefix, prefix + prefix_size, end - size); + } + std::uninitialized_fill(p, end - size, fill); + p = end; } - std::uninitialized_fill(p, end - size, fill); - p = end; - } - return p - 1; + return p - 1; } -template -template -void BasicWriter::write_int(T value, Spec spec) { - unsigned prefix_size = 0; - typedef typename internal::IntTraits::MainType UnsignedType; - UnsignedType abs_value = static_cast(value); - char prefix[4] = ""; - if (internal::is_negative(value)) { - prefix[0] = '-'; - ++prefix_size; - abs_value = 0 - abs_value; - } else if (spec.flag(SIGN_FLAG)) { - prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' '; - ++prefix_size; - } - switch (spec.type()) { - case 0: case 'd': { - unsigned num_digits = internal::count_digits(abs_value); - CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1; - internal::format_decimal(get(p), abs_value, 0); - break; - } - case 'x': case 'X': { - UnsignedType n = abs_value; - if (spec.flag(HASH_FLAG)) { - prefix[prefix_size++] = '0'; - prefix[prefix_size++] = spec.type_prefix(); +template +template +void +BasicWriter::write_int(T value, Spec spec) +{ + unsigned prefix_size = 0; + typedef typename internal::IntTraits::MainType UnsignedType; + UnsignedType abs_value = static_cast(value); + char prefix[4] = ""; + if (internal::is_negative(value)) { + prefix[0] = '-'; + ++prefix_size; + abs_value = 0 - abs_value; + } else if (spec.flag(SIGN_FLAG)) { + prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' '; + ++prefix_size; } - unsigned num_digits = 0; - do { - ++num_digits; - } while ((n >>= 4) != 0); - Char *p = get(prepare_int_buffer( - num_digits, spec, prefix, prefix_size)); - n = abs_value; - const char *digits = spec.type() == 'x' ? - "0123456789abcdef" : "0123456789ABCDEF"; - do { - *p-- = digits[n & 0xf]; - } while ((n >>= 4) != 0); - break; - } - case 'b': case 'B': { - UnsignedType n = abs_value; - if (spec.flag(HASH_FLAG)) { - prefix[prefix_size++] = '0'; - prefix[prefix_size++] = spec.type_prefix(); + switch (spec.type()) { + case 0: + case 'd': { + unsigned num_digits = internal::count_digits(abs_value); + CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1; + internal::format_decimal(get(p), abs_value, 0); + break; } - unsigned num_digits = 0; - do { - ++num_digits; - } while ((n >>= 1) != 0); - Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); - n = abs_value; - do { - *p-- = static_cast('0' + (n & 1)); - } while ((n >>= 1) != 0); - break; - } - case 'o': { - UnsignedType n = abs_value; - if (spec.flag(HASH_FLAG)) - prefix[prefix_size++] = '0'; - unsigned num_digits = 0; - do { - ++num_digits; - } while ((n >>= 3) != 0); - Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); - n = abs_value; - do { - *p-- = static_cast('0' + (n & 7)); - } while ((n >>= 3) != 0); - break; - } - case 'n': { - unsigned num_digits = internal::count_digits(abs_value); - fmt::StringRef sep = ""; + case 'x': + case 'X': { + UnsignedType n = abs_value; + if (spec.flag(HASH_FLAG)) { + prefix[prefix_size++] = '0'; + prefix[prefix_size++] = spec.type_prefix(); + } + unsigned num_digits = 0; + do { + ++num_digits; + } while ((n >>= 4) != 0); + Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); + n = abs_value; + const char *digits = spec.type() == 'x' ? "0123456789abcdef" : "0123456789ABCDEF"; + do { + *p-- = digits[n & 0xf]; + } while ((n >>= 4) != 0); + break; + } + case 'b': + case 'B': { + UnsignedType n = abs_value; + if (spec.flag(HASH_FLAG)) { + prefix[prefix_size++] = '0'; + prefix[prefix_size++] = spec.type_prefix(); + } + unsigned num_digits = 0; + do { + ++num_digits; + } while ((n >>= 1) != 0); + Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); + n = abs_value; + do { + *p-- = static_cast('0' + (n & 1)); + } while ((n >>= 1) != 0); + break; + } + case 'o': { + UnsignedType n = abs_value; + if (spec.flag(HASH_FLAG)) prefix[prefix_size++] = '0'; + unsigned num_digits = 0; + do { + ++num_digits; + } while ((n >>= 3) != 0); + Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); + n = abs_value; + do { + *p-- = static_cast('0' + (n & 7)); + } while ((n >>= 3) != 0); + break; + } + case 'n': { + unsigned num_digits = internal::count_digits(abs_value); + fmt::StringRef sep = ""; #if !(defined(ANDROID) || defined(__ANDROID__)) - sep = internal::thousands_sep(std::localeconv()); + sep = internal::thousands_sep(std::localeconv()); #endif - unsigned size = static_cast( - num_digits + sep.size() * ((num_digits - 1) / 3)); - CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1; - internal::format_decimal(get(p), abs_value, 0, internal::ThousandsSep(sep)); - break; - } - default: - internal::report_unknown_type( - spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer"); - break; - } + unsigned size = static_cast(num_digits + sep.size() * ((num_digits - 1) / 3)); + CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1; + internal::format_decimal(get(p), abs_value, 0, internal::ThousandsSep(sep)); + break; + } + default: + internal::report_unknown_type(spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer"); + break; + } } -template -template -void BasicWriter::write_double(T value, const Spec &spec) { - // Check type. - char type = spec.type(); - bool upper = false; - switch (type) { - case 0: - type = 'g'; - break; - case 'e': case 'f': case 'g': case 'a': - break; - case 'F': +template +template +void +BasicWriter::write_double(T value, const Spec &spec) +{ + // Check type. + char type = spec.type(); + bool upper = false; + switch (type) { + case 0: + type = 'g'; + break; + case 'e': + case 'f': + case 'g': + case 'a': + break; + case 'F': #if FMT_MSC_VER - // MSVC's printf doesn't support 'F'. - type = 'f'; + // MSVC's printf doesn't support 'F'. + type = 'f'; #endif - // Fall through. - case 'E': case 'G': case 'A': - upper = true; - break; - default: - internal::report_unknown_type(type, "double"); - break; - } - - char sign = 0; - // Use isnegative instead of value < 0 because the latter is always - // false for NaN. - if (internal::FPUtil::isnegative(static_cast(value))) { - sign = '-'; - value = -value; - } else if (spec.flag(SIGN_FLAG)) { - sign = spec.flag(PLUS_FLAG) ? '+' : ' '; - } - - if (internal::FPUtil::isnotanumber(value)) { - // Format NaN ourselves because sprintf's output is not consistent - // across platforms. - std::size_t nan_size = 4; - const char *nan = upper ? " NAN" : " nan"; - if (!sign) { - --nan_size; - ++nan; + // Fall through. + case 'E': + case 'G': + case 'A': + upper = true; + break; + default: + internal::report_unknown_type(type, "double"); + break; } - CharPtr out = write_str(nan, nan_size, spec); - if (sign) - *out = sign; - return; - } - if (internal::FPUtil::isinfinity(value)) { - // Format infinity ourselves because sprintf's output is not consistent - // across platforms. - std::size_t inf_size = 4; - const char *inf = upper ? " INF" : " inf"; - if (!sign) { - --inf_size; - ++inf; + char sign = 0; + // Use isnegative instead of value < 0 because the latter is always + // false for NaN. + if (internal::FPUtil::isnegative(static_cast(value))) { + sign = '-'; + value = -value; + } else if (spec.flag(SIGN_FLAG)) { + sign = spec.flag(PLUS_FLAG) ? '+' : ' '; } - CharPtr out = write_str(inf, inf_size, spec); - if (sign) - *out = sign; - return; - } - std::size_t offset = buffer_.size(); - unsigned width = spec.width(); - if (sign) { - buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u)); - if (width > 0) - --width; - ++offset; - } + if (internal::FPUtil::isnotanumber(value)) { + // Format NaN ourselves because sprintf's output is not consistent + // across platforms. + std::size_t nan_size = 4; + const char *nan = upper ? " NAN" : " nan"; + if (!sign) { + --nan_size; + ++nan; + } + CharPtr out = write_str(nan, nan_size, spec); + if (sign) *out = sign; + return; + } - // Build format string. - enum { MAX_FORMAT_SIZE = 10}; // longest format: %#-*.*Lg - Char format[MAX_FORMAT_SIZE]; - Char *format_ptr = format; - *format_ptr++ = '%'; - unsigned width_for_sprintf = width; - if (spec.flag(HASH_FLAG)) - *format_ptr++ = '#'; - if (spec.align() == ALIGN_CENTER) { - width_for_sprintf = 0; - } else { - if (spec.align() == ALIGN_LEFT) - *format_ptr++ = '-'; - if (width != 0) - *format_ptr++ = '*'; - } - if (spec.precision() >= 0) { - *format_ptr++ = '.'; - *format_ptr++ = '*'; - } + if (internal::FPUtil::isinfinity(value)) { + // Format infinity ourselves because sprintf's output is not consistent + // across platforms. + std::size_t inf_size = 4; + const char *inf = upper ? " INF" : " inf"; + if (!sign) { + --inf_size; + ++inf; + } + CharPtr out = write_str(inf, inf_size, spec); + if (sign) *out = sign; + return; + } - append_float_length(format_ptr, value); - *format_ptr++ = type; - *format_ptr = '\0'; + std::size_t offset = buffer_.size(); + unsigned width = spec.width(); + if (sign) { + buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u)); + if (width > 0) --width; + ++offset; + } - // Format using snprintf. - Char fill = internal::CharTraits::cast(spec.fill()); - unsigned n = 0; - Char *start = FMT_NULL; - for (;;) { - std::size_t buffer_size = buffer_.capacity() - offset; + // Build format string. + enum { MAX_FORMAT_SIZE = 10 };// longest format: %#-*.*Lg + + Char format[MAX_FORMAT_SIZE]; + Char *format_ptr = format; + *format_ptr++ = '%'; + unsigned width_for_sprintf = width; + if (spec.flag(HASH_FLAG)) *format_ptr++ = '#'; + if (spec.align() == ALIGN_CENTER) { + width_for_sprintf = 0; + } else { + if (spec.align() == ALIGN_LEFT) *format_ptr++ = '-'; + if (width != 0) *format_ptr++ = '*'; + } + if (spec.precision() >= 0) { + *format_ptr++ = '.'; + *format_ptr++ = '*'; + } + + append_float_length(format_ptr, value); + *format_ptr++ = type; + *format_ptr = '\0'; + + // Format using snprintf. + Char fill = internal::CharTraits::cast(spec.fill()); + unsigned n = 0; + Char *start = FMT_NULL; + for (;;) { + std::size_t buffer_size = buffer_.capacity() - offset; #if FMT_MSC_VER - // MSVC's vsnprintf_s doesn't work with zero size, so reserve - // space for at least one extra character to make the size non-zero. - // Note that the buffer's capacity will increase by more than 1. - if (buffer_size == 0) { - buffer_.reserve(offset + 1); - buffer_size = buffer_.capacity() - offset; - } + // MSVC's vsnprintf_s doesn't work with zero size, so reserve + // space for at least one extra character to make the size non-zero. + // Note that the buffer's capacity will increase by more than 1. + if (buffer_size == 0) { + buffer_.reserve(offset + 1); + buffer_size = buffer_.capacity() - offset; + } #endif - start = &buffer_[offset]; - int result = internal::CharTraits::format_float( - start, buffer_size, format, width_for_sprintf, spec.precision(), value); - if (result >= 0) { - n = internal::to_unsigned(result); - if (offset + n < buffer_.capacity()) - break; // The buffer is large enough - continue with formatting. - buffer_.reserve(offset + n + 1); - } else { - // If result is negative we ask to increase the capacity by at least 1, - // but as std::vector, the buffer grows exponentially. - buffer_.reserve(buffer_.capacity() + 1); + start = &buffer_[offset]; + int result = internal::CharTraits::format_float(start, + buffer_size, + format, + width_for_sprintf, + spec.precision(), + value); + if (result >= 0) { + n = internal::to_unsigned(result); + if (offset + n < buffer_.capacity()) break;// The buffer is large enough - continue with formatting. + buffer_.reserve(offset + n + 1); + } else { + // If result is negative we ask to increase the capacity by at least 1, + // but as std::vector, the buffer grows exponentially. + buffer_.reserve(buffer_.capacity() + 1); + } } - } - if (sign) { - if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) || - *start != ' ') { - *(start - 1) = sign; - sign = 0; - } else { - *(start - 1) = fill; + if (sign) { + if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) || *start != ' ') { + *(start - 1) = sign; + sign = 0; + } else { + *(start - 1) = fill; + } + ++n; } - ++n; - } - if (spec.align() == ALIGN_CENTER && spec.width() > n) { - width = spec.width(); - CharPtr p = grow_buffer(width); - std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char)); - fill_padding(p, spec.width(), n, fill); - return; - } - if (spec.fill() != ' ' || sign) { - while (*start == ' ') - *start++ = fill; - if (sign) - *(start - 1) = sign; - } - grow_buffer(n); + if (spec.align() == ALIGN_CENTER && spec.width() > n) { + width = spec.width(); + CharPtr p = grow_buffer(width); + std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char)); + fill_padding(p, spec.width(), n, fill); + return; + } + if (spec.fill() != ' ' || sign) { + while (*start == ' ') *start++ = fill; + if (sign) *(start - 1) = sign; + } + grow_buffer(n); } /** @@ -3284,35 +3391,33 @@ void BasicWriter::write_double(T value, const Spec &spec) { accessed as a C string with ``out.c_str()``. \endrst */ -template > +template> class BasicMemoryWriter : public BasicWriter { - private: - internal::MemoryBuffer buffer_; +private: + internal::MemoryBuffer buffer_; - public: - explicit BasicMemoryWriter(const Allocator& alloc = Allocator()) - : BasicWriter(buffer_), buffer_(alloc) {} +public: + explicit BasicMemoryWriter(const Allocator &alloc = Allocator()) : BasicWriter(buffer_), buffer_(alloc) {} #if FMT_USE_RVALUE_REFERENCES - /** + /** \rst Constructs a :class:`fmt::BasicMemoryWriter` object moving the content of the other object to it. \endrst */ - BasicMemoryWriter(BasicMemoryWriter &&other) - : BasicWriter(buffer_), buffer_(std::move(other.buffer_)) { - } + BasicMemoryWriter(BasicMemoryWriter &&other) : BasicWriter(buffer_), buffer_(std::move(other.buffer_)) {} - /** + /** \rst Moves the content of the other ``BasicMemoryWriter`` object to this one. \endrst */ - BasicMemoryWriter &operator=(BasicMemoryWriter &&other) { - buffer_ = std::move(other.buffer_); - return *this; - } + BasicMemoryWriter &operator=(BasicMemoryWriter &&other) + { + buffer_ = std::move(other.buffer_); + return *this; + } #endif }; @@ -3339,30 +3444,30 @@ typedef BasicMemoryWriter WMemoryWriter; +--------------+---------------------------+ \endrst */ -template +template class BasicArrayWriter : public BasicWriter { - private: - internal::FixedBuffer buffer_; +private: + internal::FixedBuffer buffer_; - public: - /** +public: + /** \rst Constructs a :class:`fmt::BasicArrayWriter` object for *array* of the given size. \endrst */ - BasicArrayWriter(Char *array, std::size_t size) - : BasicWriter(buffer_), buffer_(array, size) {} + BasicArrayWriter(Char *array, std::size_t size) : BasicWriter(buffer_), buffer_(array, size) {} - /** + /** \rst Constructs a :class:`fmt::BasicArrayWriter` object for *array* of the size known at compile time. \endrst */ - template - explicit BasicArrayWriter(Char (&array)[SIZE]) - : BasicWriter(buffer_), buffer_(array, SIZE) {} + template + explicit BasicArrayWriter(Char (&array)[SIZE]) : BasicWriter(buffer_), + buffer_(array, SIZE) + {} }; typedef BasicArrayWriter ArrayWriter; @@ -3370,18 +3475,17 @@ typedef BasicArrayWriter WArrayWriter; // Reports a system error without throwing an exception. // Can be used to report errors from destructors. -FMT_API void report_system_error(int error_code, - StringRef message) FMT_NOEXCEPT; +FMT_API void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT; #if FMT_USE_WINDOWS_H /** A Windows error. */ class WindowsError : public SystemError { - private: - FMT_API void init(int error_code, CStringRef format_str, ArgList args); +private: + FMT_API void init(int error_code, CStringRef format_str, ArgList args); - public: - /** +public: + /** \rst Constructs a :class:`fmt::WindowsError` object with the description of the form @@ -3409,16 +3513,13 @@ class WindowsError : public SystemError { } \endrst */ - WindowsError(int error_code, CStringRef message) { - init(error_code, message, ArgList()); - } - FMT_VARIADIC_CTOR(WindowsError, init, int, CStringRef) + WindowsError(int error_code, CStringRef message) { init(error_code, message, ArgList()); } + FMT_VARIADIC_CTOR(WindowsError, init, int, CStringRef) }; // Reports a Windows error without throwing an exception. // Can be used to report errors from destructors. -FMT_API void report_windows_error(int error_code, - StringRef message) FMT_NOEXCEPT; +FMT_API void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT; #endif @@ -3441,16 +3542,20 @@ FMT_API void print_colored(Color c, CStringRef format, ArgList args); std::string message = format("The answer is {}", 42); \endrst */ -inline std::string format(CStringRef format_str, ArgList args) { - MemoryWriter w; - w.write(format_str, args); - return w.str(); +inline std::string +format(CStringRef format_str, ArgList args) +{ + MemoryWriter w; + w.write(format_str, args); + return w.str(); } -inline std::wstring format(WCStringRef format_str, ArgList args) { - WMemoryWriter w; - w.write(format_str, args); - return w.str(); +inline std::wstring +format(WCStringRef format_str, ArgList args) +{ + WMemoryWriter w; + w.write(format_str, args); + return w.str(); } /** @@ -3479,105 +3584,112 @@ FMT_API void print(CStringRef format_str, ArgList args); Fast integer formatter. */ class FormatInt { - private: - // Buffer should be large enough to hold all digits (digits10 + 1), - // a sign and a null character. - enum {BUFFER_SIZE = std::numeric_limits::digits10 + 3}; - mutable char buffer_[BUFFER_SIZE]; - char *str_; +private: + // Buffer should be large enough to hold all digits (digits10 + 1), + // a sign and a null character. + enum { BUFFER_SIZE = std::numeric_limits::digits10 + 3 }; - // Formats value in reverse and returns the number of digits. - char *format_decimal(ULongLong value) { - char *buffer_end = buffer_ + BUFFER_SIZE - 1; - while (value >= 100) { - // Integer division is slow so do it for a group of two digits instead - // of for every digit. The idea comes from the talk by Alexandrescu - // "Three Optimization Tips for C++". See speed-test for a comparison. - unsigned index = static_cast((value % 100) * 2); - value /= 100; - *--buffer_end = internal::Data::DIGITS[index + 1]; - *--buffer_end = internal::Data::DIGITS[index]; + mutable char buffer_[BUFFER_SIZE]; + char *str_; + + // Formats value in reverse and returns the number of digits. + char *format_decimal(ULongLong value) + { + char *buffer_end = buffer_ + BUFFER_SIZE - 1; + while (value >= 100) { + // Integer division is slow so do it for a group of two digits instead + // of for every digit. The idea comes from the talk by Alexandrescu + // "Three Optimization Tips for C++". See speed-test for a comparison. + unsigned index = static_cast((value % 100) * 2); + value /= 100; + *--buffer_end = internal::Data::DIGITS[index + 1]; + *--buffer_end = internal::Data::DIGITS[index]; + } + if (value < 10) { + *--buffer_end = static_cast('0' + value); + return buffer_end; + } + unsigned index = static_cast(value * 2); + *--buffer_end = internal::Data::DIGITS[index + 1]; + *--buffer_end = internal::Data::DIGITS[index]; + return buffer_end; } - if (value < 10) { - *--buffer_end = static_cast('0' + value); - return buffer_end; + + void FormatSigned(LongLong value) + { + ULongLong abs_value = static_cast(value); + bool negative = value < 0; + if (negative) abs_value = 0 - abs_value; + str_ = format_decimal(abs_value); + if (negative) *--str_ = '-'; } - unsigned index = static_cast(value * 2); - *--buffer_end = internal::Data::DIGITS[index + 1]; - *--buffer_end = internal::Data::DIGITS[index]; - return buffer_end; - } - void FormatSigned(LongLong value) { - ULongLong abs_value = static_cast(value); - bool negative = value < 0; - if (negative) - abs_value = 0 - abs_value; - str_ = format_decimal(abs_value); - if (negative) - *--str_ = '-'; - } +public: + explicit FormatInt(int value) { FormatSigned(value); } - public: - explicit FormatInt(int value) { FormatSigned(value); } - explicit FormatInt(long value) { FormatSigned(value); } - explicit FormatInt(LongLong value) { FormatSigned(value); } - explicit FormatInt(unsigned value) : str_(format_decimal(value)) {} - explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {} - explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {} + explicit FormatInt(long value) { FormatSigned(value); } - /** Returns the number of characters written to the output buffer. */ - std::size_t size() const { - return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1); - } + explicit FormatInt(LongLong value) { FormatSigned(value); } - /** + explicit FormatInt(unsigned value) : str_(format_decimal(value)) {} + + explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {} + + explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {} + + /** Returns the number of characters written to the output buffer. */ + std::size_t size() const { return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1); } + + /** Returns a pointer to the output buffer content. No terminating null character is appended. */ - const char *data() const { return str_; } + const char *data() const { return str_; } - /** + /** Returns a pointer to the output buffer content with terminating null character appended. */ - const char *c_str() const { - buffer_[BUFFER_SIZE - 1] = '\0'; - return str_; - } + const char *c_str() const + { + buffer_[BUFFER_SIZE - 1] = '\0'; + return str_; + } - /** + /** \rst Returns the content of the output buffer as an ``std::string``. \endrst */ - std::string str() const { return std::string(str_, size()); } + std::string str() const { return std::string(str_, size()); } }; // Formats a decimal integer value writing into buffer and returns // a pointer to the end of the formatted string. This function doesn't // write a terminating null character. -template -inline void format_decimal(char *&buffer, T value) { - typedef typename internal::IntTraits::MainType MainType; - MainType abs_value = static_cast(value); - if (internal::is_negative(value)) { - *buffer++ = '-'; - abs_value = 0 - abs_value; - } - if (abs_value < 100) { - if (abs_value < 10) { - *buffer++ = static_cast('0' + abs_value); - return; +template +inline void +format_decimal(char *&buffer, T value) +{ + typedef typename internal::IntTraits::MainType MainType; + MainType abs_value = static_cast(value); + if (internal::is_negative(value)) { + *buffer++ = '-'; + abs_value = 0 - abs_value; } - unsigned index = static_cast(abs_value * 2); - *buffer++ = internal::Data::DIGITS[index]; - *buffer++ = internal::Data::DIGITS[index + 1]; - return; - } - unsigned num_digits = internal::count_digits(abs_value); - internal::format_decimal(buffer, abs_value, num_digits); - buffer += num_digits; + if (abs_value < 100) { + if (abs_value < 10) { + *buffer++ = static_cast('0' + abs_value); + return; + } + unsigned index = static_cast(abs_value * 2); + *buffer++ = internal::Data::DIGITS[index]; + *buffer++ = internal::Data::DIGITS[index + 1]; + return; + } + unsigned num_digits = internal::count_digits(abs_value); + internal::format_decimal(buffer, abs_value, num_digits); + buffer += num_digits; } /** @@ -3590,30 +3702,34 @@ inline void format_decimal(char *&buffer, T value) { \endrst */ -template -inline internal::NamedArgWithType arg(StringRef name, const T &arg) { - return internal::NamedArgWithType(name, arg); +template +inline internal::NamedArgWithType +arg(StringRef name, const T &arg) +{ + return internal::NamedArgWithType(name, arg); } -template -inline internal::NamedArgWithType arg(WStringRef name, const T &arg) { - return internal::NamedArgWithType(name, arg); +template +inline internal::NamedArgWithType +arg(WStringRef name, const T &arg) +{ + return internal::NamedArgWithType(name, arg); } // The following two functions are deleted intentionally to disable // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``. -template -void arg(StringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; -template -void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; -} +template +void arg(StringRef, const internal::NamedArg &) FMT_DELETED_OR_UNDEFINED; +template +void arg(WStringRef, const internal::NamedArg &) FMT_DELETED_OR_UNDEFINED; +}// namespace fmt #if FMT_GCC_VERSION // Use the system_header pragma to suppress warnings about variadic macros // because suppressing -Wvariadic-macros with the diagnostic pragma doesn't // work. It is used at the end because we want to suppress as little warnings // as possible. -# pragma GCC system_header +#pragma GCC system_header #endif // This is used to work around VC++ bugs in handling variadic macros. @@ -3626,58 +3742,55 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 -#define FMT_FOR_EACH_(N, f, ...) \ - FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__)) -#define FMT_FOR_EACH(f, ...) \ - FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__)) +#define FMT_FOR_EACH_(N, f, ...) FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__)) +#define FMT_FOR_EACH(f, ...) FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__)) #define FMT_ADD_ARG_NAME(type, index) type arg##index #define FMT_GET_ARG_NAME(type, index) arg##index #if FMT_USE_VARIADIC_TEMPLATES -# define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \ - template \ - ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ - const Args & ... args) Const { \ - typedef fmt::internal::ArgArray ArgArray; \ - typename ArgArray::Type array{ \ - ArgArray::template make >(args)...}; \ - call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \ - fmt::ArgList(fmt::internal::make_type(args...), array)); \ - } +#define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \ + template \ + ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), const Args &...args) Const \ + { \ + typedef fmt::internal::ArgArray ArgArray; \ + typename ArgArray::Type array{ArgArray::template make>(args)...}; \ + call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList(fmt::internal::make_type(args...), array)); \ + } #else // Defines a wrapper for a function taking __VA_ARGS__ arguments // and n additional arguments of arbitrary types. -# define FMT_WRAP(Const, Char, ReturnType, func, call, n, ...) \ - template \ - inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ - FMT_GEN(n, FMT_MAKE_ARG)) Const { \ - fmt::internal::ArgArray::Type arr; \ - FMT_GEN(n, FMT_ASSIGN_##Char); \ - call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \ - } +#define FMT_WRAP(Const, Char, ReturnType, func, call, n, ...) \ + template \ + inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), FMT_GEN(n, FMT_MAKE_ARG)) Const \ + { \ + fmt::internal::ArgArray::Type arr; \ + FMT_GEN(n, FMT_ASSIGN_##Char); \ + call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \ + fmt::ArgList(fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \ + } -# define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \ - inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) Const { \ - call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \ - } \ - FMT_WRAP(Const, Char, ReturnType, func, call, 1, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 2, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 3, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 4, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 5, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 6, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 7, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 8, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 9, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 10, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 11, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 12, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 13, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 14, __VA_ARGS__) \ - FMT_WRAP(Const, Char, ReturnType, func, call, 15, __VA_ARGS__) -#endif // FMT_USE_VARIADIC_TEMPLATES +#define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \ + inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) Const \ + { \ + call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \ + } \ + FMT_WRAP(Const, Char, ReturnType, func, call, 1, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 2, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 3, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 4, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 5, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 6, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 7, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 8, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 9, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 10, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 11, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 12, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 13, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 14, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 15, __VA_ARGS__) +#endif// FMT_USE_VARIADIC_TEMPLATES /** \rst @@ -3706,21 +3819,18 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; } \endrst */ -#define FMT_VARIADIC(ReturnType, func, ...) \ - FMT_VARIADIC_(, char, ReturnType, func, return func, __VA_ARGS__) +#define FMT_VARIADIC(ReturnType, func, ...) FMT_VARIADIC_(, char, ReturnType, func, return func, __VA_ARGS__) -#define FMT_VARIADIC_CONST(ReturnType, func, ...) \ - FMT_VARIADIC_(const, char, ReturnType, func, return func, __VA_ARGS__) +#define FMT_VARIADIC_CONST(ReturnType, func, ...) FMT_VARIADIC_(const, char, ReturnType, func, return func, __VA_ARGS__) -#define FMT_VARIADIC_W(ReturnType, func, ...) \ - FMT_VARIADIC_(, wchar_t, ReturnType, func, return func, __VA_ARGS__) +#define FMT_VARIADIC_W(ReturnType, func, ...) FMT_VARIADIC_(, wchar_t, ReturnType, func, return func, __VA_ARGS__) -#define FMT_VARIADIC_CONST_W(ReturnType, func, ...) \ - FMT_VARIADIC_(const, wchar_t, ReturnType, func, return func, __VA_ARGS__) +#define FMT_VARIADIC_CONST_W(ReturnType, func, ...) \ + FMT_VARIADIC_(const, wchar_t, ReturnType, func, return func, __VA_ARGS__) #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id) -#define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id) +#define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L## #id, id) /** \rst @@ -3748,376 +3858,369 @@ FMT_VARIADIC(void, print, std::FILE *, CStringRef) FMT_VARIADIC(void, print_colored, Color, CStringRef) namespace internal { -template -inline bool is_name_start(Char c) { - return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c; +template +inline bool +is_name_start(Char c) +{ + return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c; } // Parses an unsigned integer advancing s to the end of the parsed input. // This function assumes that the first character of s is a digit. -template -unsigned parse_nonnegative_int(const Char *&s) { - assert('0' <= *s && *s <= '9'); - unsigned value = 0; - // Convert to unsigned to prevent a warning. - unsigned max_int = (std::numeric_limits::max)(); - unsigned big = max_int / 10; - do { - // Check for overflow. - if (value > big) { - value = max_int + 1; - break; - } - value = value * 10 + (*s - '0'); - ++s; - } while ('0' <= *s && *s <= '9'); - // Convert to unsigned to prevent a warning. - if (value > max_int) - FMT_THROW(FormatError("number is too big")); - return value; -} - -inline void require_numeric_argument(const Arg &arg, char spec) { - if (arg.type > Arg::LAST_NUMERIC_TYPE) { - std::string message = - fmt::format("format specifier '{}' requires numeric argument", spec); - FMT_THROW(fmt::FormatError(message)); - } -} - -template -void check_sign(const Char *&s, const Arg &arg) { - char sign = static_cast(*s); - require_numeric_argument(arg, sign); - if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) { - FMT_THROW(FormatError(fmt::format( - "format specifier '{}' requires signed argument", sign))); - } - ++s; -} -} // namespace internal - -template -inline internal::Arg BasicFormatter::get_arg( - BasicStringRef arg_name, const char *&error) { - if (check_no_auto_index(error)) { - map_.init(args()); - const internal::Arg *arg = map_.find(arg_name); - if (arg) - return *arg; - error = "argument not found"; - } - return internal::Arg(); -} - -template -inline internal::Arg BasicFormatter::parse_arg_index(const Char *&s) { - const char *error = FMT_NULL; - internal::Arg arg = *s < '0' || *s > '9' ? - next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error); - if (error) { - FMT_THROW(FormatError( - *s != '}' && *s != ':' ? "invalid format string" : error)); - } - return arg; -} - -template -inline internal::Arg BasicFormatter::parse_arg_name(const Char *&s) { - assert(internal::is_name_start(*s)); - const Char *start = s; - Char c; - do { - c = *++s; - } while (internal::is_name_start(c) || ('0' <= c && c <= '9')); - const char *error = FMT_NULL; - internal::Arg arg = get_arg(BasicStringRef(start, s - start), error); - if (error) - FMT_THROW(FormatError(error)); - return arg; -} - -template -const Char *BasicFormatter::format( - const Char *&format_str, const internal::Arg &arg) { - using internal::Arg; - const Char *s = format_str; - typename ArgFormatter::SpecType spec; - if (*s == ':') { - if (arg.type == Arg::CUSTOM) { - arg.custom.format(this, arg.custom.value, &s); - return s; - } - ++s; - // Parse fill and alignment. - if (Char c = *s) { - const Char *p = s + 1; - spec.align_ = ALIGN_DEFAULT; - do { - switch (*p) { - case '<': - spec.align_ = ALIGN_LEFT; - break; - case '>': - spec.align_ = ALIGN_RIGHT; - break; - case '=': - spec.align_ = ALIGN_NUMERIC; - break; - case '^': - spec.align_ = ALIGN_CENTER; +template +unsigned +parse_nonnegative_int(const Char *&s) +{ + assert('0' <= *s && *s <= '9'); + unsigned value = 0; + // Convert to unsigned to prevent a warning. + unsigned max_int = (std::numeric_limits::max)(); + unsigned big = max_int / 10; + do { + // Check for overflow. + if (value > big) { + value = max_int + 1; break; } - if (spec.align_ != ALIGN_DEFAULT) { - if (p != s) { - if (c == '}') break; - if (c == '{') - FMT_THROW(FormatError("invalid fill character '{'")); - s += 2; - spec.fill_ = c; - } else ++s; - if (spec.align_ == ALIGN_NUMERIC) - require_numeric_argument(arg, '='); - break; - } - } while (--p >= s); - } - - // Parse sign. - switch (*s) { - case '+': - check_sign(s, arg); - spec.flags_ |= SIGN_FLAG | PLUS_FLAG; - break; - case '-': - check_sign(s, arg); - spec.flags_ |= MINUS_FLAG; - break; - case ' ': - check_sign(s, arg); - spec.flags_ |= SIGN_FLAG; - break; - } - - if (*s == '#') { - require_numeric_argument(arg, '#'); - spec.flags_ |= HASH_FLAG; - ++s; - } - - // Parse zero flag. - if (*s == '0') { - require_numeric_argument(arg, '0'); - spec.align_ = ALIGN_NUMERIC; - spec.fill_ = '0'; - ++s; - } - - // Parse width. - if ('0' <= *s && *s <= '9') { - spec.width_ = internal::parse_nonnegative_int(s); - } else if (*s == '{') { - ++s; - Arg width_arg = internal::is_name_start(*s) ? - parse_arg_name(s) : parse_arg_index(s); - if (*s++ != '}') - FMT_THROW(FormatError("invalid format string")); - ULongLong value = 0; - switch (width_arg.type) { - case Arg::INT: - if (width_arg.int_value < 0) - FMT_THROW(FormatError("negative width")); - value = width_arg.int_value; - break; - case Arg::UINT: - value = width_arg.uint_value; - break; - case Arg::LONG_LONG: - if (width_arg.long_long_value < 0) - FMT_THROW(FormatError("negative width")); - value = width_arg.long_long_value; - break; - case Arg::ULONG_LONG: - value = width_arg.ulong_long_value; - break; - default: - FMT_THROW(FormatError("width is not integer")); - } - unsigned max_int = (std::numeric_limits::max)(); - if (value > max_int) - FMT_THROW(FormatError("number is too big")); - spec.width_ = static_cast(value); - } - - // Parse precision. - if (*s == '.') { - ++s; - spec.precision_ = 0; - if ('0' <= *s && *s <= '9') { - spec.precision_ = internal::parse_nonnegative_int(s); - } else if (*s == '{') { + value = value * 10 + (*s - '0'); ++s; - Arg precision_arg = internal::is_name_start(*s) ? - parse_arg_name(s) : parse_arg_index(s); - if (*s++ != '}') - FMT_THROW(FormatError("invalid format string")); - ULongLong value = 0; - switch (precision_arg.type) { - case Arg::INT: - if (precision_arg.int_value < 0) - FMT_THROW(FormatError("negative precision")); - value = precision_arg.int_value; - break; - case Arg::UINT: - value = precision_arg.uint_value; - break; - case Arg::LONG_LONG: - if (precision_arg.long_long_value < 0) - FMT_THROW(FormatError("negative precision")); - value = precision_arg.long_long_value; - break; - case Arg::ULONG_LONG: - value = precision_arg.ulong_long_value; - break; - default: - FMT_THROW(FormatError("precision is not integer")); + } while ('0' <= *s && *s <= '9'); + // Convert to unsigned to prevent a warning. + if (value > max_int) FMT_THROW(FormatError("number is too big")); + return value; +} + +inline void +require_numeric_argument(const Arg &arg, char spec) +{ + if (arg.type > Arg::LAST_NUMERIC_TYPE) { + std::string message = fmt::format("format specifier '{}' requires numeric argument", spec); + FMT_THROW(fmt::FormatError(message)); + } +} + +template +void +check_sign(const Char *&s, const Arg &arg) +{ + char sign = static_cast(*s); + require_numeric_argument(arg, sign); + if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) { + FMT_THROW(FormatError(fmt::format("format specifier '{}' requires signed argument", sign))); + } + ++s; +} +}// namespace internal + +template +inline internal::Arg +BasicFormatter::get_arg(BasicStringRef arg_name, const char *&error) +{ + if (check_no_auto_index(error)) { + map_.init(args()); + const internal::Arg *arg = map_.find(arg_name); + if (arg) return *arg; + error = "argument not found"; + } + return internal::Arg(); +} + +template +inline internal::Arg +BasicFormatter::parse_arg_index(const Char *&s) +{ + const char *error = FMT_NULL; + internal::Arg arg = *s < '0' || *s > '9' ? next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error); + if (error) { FMT_THROW(FormatError(*s != '}' && *s != ':' ? "invalid format string" : error)); } + return arg; +} + +template +inline internal::Arg +BasicFormatter::parse_arg_name(const Char *&s) +{ + assert(internal::is_name_start(*s)); + const Char *start = s; + Char c; + do { + c = *++s; + } while (internal::is_name_start(c) || ('0' <= c && c <= '9')); + const char *error = FMT_NULL; + internal::Arg arg = get_arg(BasicStringRef(start, s - start), error); + if (error) FMT_THROW(FormatError(error)); + return arg; +} + +template +const Char * +BasicFormatter::format(const Char *&format_str, const internal::Arg &arg) +{ + using internal::Arg; + const Char *s = format_str; + typename ArgFormatter::SpecType spec; + if (*s == ':') { + if (arg.type == Arg::CUSTOM) { + arg.custom.format(this, arg.custom.value, &s); + return s; } - unsigned max_int = (std::numeric_limits::max)(); - if (value > max_int) - FMT_THROW(FormatError("number is too big")); - spec.precision_ = static_cast(value); - } else { - FMT_THROW(FormatError("missing precision specifier")); - } - if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) { - FMT_THROW(FormatError( - fmt::format("precision not allowed in {} format specifier", - arg.type == Arg::POINTER ? "pointer" : "integer"))); - } + ++s; + // Parse fill and alignment. + if (Char c = *s) { + const Char *p = s + 1; + spec.align_ = ALIGN_DEFAULT; + do { + switch (*p) { + case '<': + spec.align_ = ALIGN_LEFT; + break; + case '>': + spec.align_ = ALIGN_RIGHT; + break; + case '=': + spec.align_ = ALIGN_NUMERIC; + break; + case '^': + spec.align_ = ALIGN_CENTER; + break; + } + if (spec.align_ != ALIGN_DEFAULT) { + if (p != s) { + if (c == '}') break; + if (c == '{') FMT_THROW(FormatError("invalid fill character '{'")); + s += 2; + spec.fill_ = c; + } else + ++s; + if (spec.align_ == ALIGN_NUMERIC) require_numeric_argument(arg, '='); + break; + } + } while (--p >= s); + } + + // Parse sign. + switch (*s) { + case '+': + check_sign(s, arg); + spec.flags_ |= SIGN_FLAG | PLUS_FLAG; + break; + case '-': + check_sign(s, arg); + spec.flags_ |= MINUS_FLAG; + break; + case ' ': + check_sign(s, arg); + spec.flags_ |= SIGN_FLAG; + break; + } + + if (*s == '#') { + require_numeric_argument(arg, '#'); + spec.flags_ |= HASH_FLAG; + ++s; + } + + // Parse zero flag. + if (*s == '0') { + require_numeric_argument(arg, '0'); + spec.align_ = ALIGN_NUMERIC; + spec.fill_ = '0'; + ++s; + } + + // Parse width. + if ('0' <= *s && *s <= '9') { + spec.width_ = internal::parse_nonnegative_int(s); + } else if (*s == '{') { + ++s; + Arg width_arg = internal::is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s); + if (*s++ != '}') FMT_THROW(FormatError("invalid format string")); + ULongLong value = 0; + switch (width_arg.type) { + case Arg::INT: + if (width_arg.int_value < 0) FMT_THROW(FormatError("negative width")); + value = width_arg.int_value; + break; + case Arg::UINT: + value = width_arg.uint_value; + break; + case Arg::LONG_LONG: + if (width_arg.long_long_value < 0) FMT_THROW(FormatError("negative width")); + value = width_arg.long_long_value; + break; + case Arg::ULONG_LONG: + value = width_arg.ulong_long_value; + break; + default: + FMT_THROW(FormatError("width is not integer")); + } + unsigned max_int = (std::numeric_limits::max)(); + if (value > max_int) FMT_THROW(FormatError("number is too big")); + spec.width_ = static_cast(value); + } + + // Parse precision. + if (*s == '.') { + ++s; + spec.precision_ = 0; + if ('0' <= *s && *s <= '9') { + spec.precision_ = internal::parse_nonnegative_int(s); + } else if (*s == '{') { + ++s; + Arg precision_arg = internal::is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s); + if (*s++ != '}') FMT_THROW(FormatError("invalid format string")); + ULongLong value = 0; + switch (precision_arg.type) { + case Arg::INT: + if (precision_arg.int_value < 0) FMT_THROW(FormatError("negative precision")); + value = precision_arg.int_value; + break; + case Arg::UINT: + value = precision_arg.uint_value; + break; + case Arg::LONG_LONG: + if (precision_arg.long_long_value < 0) FMT_THROW(FormatError("negative precision")); + value = precision_arg.long_long_value; + break; + case Arg::ULONG_LONG: + value = precision_arg.ulong_long_value; + break; + default: + FMT_THROW(FormatError("precision is not integer")); + } + unsigned max_int = (std::numeric_limits::max)(); + if (value > max_int) FMT_THROW(FormatError("number is too big")); + spec.precision_ = static_cast(value); + } else { + FMT_THROW(FormatError("missing precision specifier")); + } + if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) { + FMT_THROW(FormatError(fmt::format("precision not allowed in {} format specifier", + arg.type == Arg::POINTER ? "pointer" : "integer"))); + } + } + + // Parse type. + if (*s != '}' && *s) spec.type_ = static_cast(*s++); } - // Parse type. - if (*s != '}' && *s) - spec.type_ = static_cast(*s++); - } + if (*s++ != '}') FMT_THROW(FormatError("missing '}' in format string")); - if (*s++ != '}') - FMT_THROW(FormatError("missing '}' in format string")); - - // Format argument. - ArgFormatter(*this, spec, s - 1).visit(arg); - return s; + // Format argument. + ArgFormatter(*this, spec, s - 1).visit(arg); + return s; } -template -void BasicFormatter::format(BasicCStringRef format_str) { - const Char *s = format_str.c_str(); - const Char *start = s; - while (*s) { - Char c = *s++; - if (c != '{' && c != '}') continue; - if (*s == c) { - write(writer_, start, s); - start = ++s; - continue; +template +void +BasicFormatter::format(BasicCStringRef format_str) +{ + const Char *s = format_str.c_str(); + const Char *start = s; + while (*s) { + Char c = *s++; + if (c != '{' && c != '}') continue; + if (*s == c) { + write(writer_, start, s); + start = ++s; + continue; + } + if (c == '}') FMT_THROW(FormatError("unmatched '}' in format string")); + write(writer_, start, s - 1); + internal::Arg arg = internal::is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s); + start = s = format(s, arg); } - if (c == '}') - FMT_THROW(FormatError("unmatched '}' in format string")); - write(writer_, start, s - 1); - internal::Arg arg = internal::is_name_start(*s) ? - parse_arg_name(s) : parse_arg_index(s); - start = s = format(s, arg); - } - write(writer_, start, s); + write(writer_, start, s); } -template +template struct ArgJoin { - It first; - It last; - BasicCStringRef sep; + It first; + It last; + BasicCStringRef sep; - ArgJoin(It first, It last, const BasicCStringRef& sep) : - first(first), - last(last), - sep(sep) {} + ArgJoin(It first, It last, const BasicCStringRef &sep) : first(first), last(last), sep(sep) {} }; -template -ArgJoin join(It first, It last, const BasicCStringRef& sep) { - return ArgJoin(first, last, sep); +template +ArgJoin +join(It first, It last, const BasicCStringRef &sep) +{ + return ArgJoin(first, last, sep); } -template -ArgJoin join(It first, It last, const BasicCStringRef& sep) { - return ArgJoin(first, last, sep); +template +ArgJoin +join(It first, It last, const BasicCStringRef &sep) +{ + return ArgJoin(first, last, sep); } #if FMT_HAS_GXX_CXX11 -template -auto join(const Range& range, const BasicCStringRef& sep) - -> ArgJoin { - return join(std::begin(range), std::end(range), sep); +template +auto +join(const Range &range, const BasicCStringRef &sep) -> ArgJoin +{ + return join(std::begin(range), std::end(range), sep); } -template -auto join(const Range& range, const BasicCStringRef& sep) - -> ArgJoin { - return join(std::begin(range), std::end(range), sep); +template +auto +join(const Range &range, const BasicCStringRef &sep) -> ArgJoin +{ + return join(std::begin(range), std::end(range), sep); } #endif -template -void format_arg(fmt::BasicFormatter &f, - const Char *&format_str, const ArgJoin& e) { - const Char* end = format_str; - if (*end == ':') - ++end; - while (*end && *end != '}') - ++end; - if (*end != '}') - FMT_THROW(FormatError("missing '}' in format string")); +template +void +format_arg(fmt::BasicFormatter &f, const Char *&format_str, const ArgJoin &e) +{ + const Char *end = format_str; + if (*end == ':') ++end; + while (*end && *end != '}') ++end; + if (*end != '}') FMT_THROW(FormatError("missing '}' in format string")); - It it = e.first; - if (it != e.last) { - const Char* save = format_str; - f.format(format_str, internal::MakeArg >(*it++)); - while (it != e.last) { - f.writer().write(e.sep); - format_str = save; - f.format(format_str, internal::MakeArg >(*it++)); + It it = e.first; + if (it != e.last) { + const Char *save = format_str; + f.format(format_str, internal::MakeArg>(*it++)); + while (it != e.last) { + f.writer().write(e.sep); + format_str = save; + f.format(format_str, internal::MakeArg>(*it++)); + } } - } - format_str = end + 1; + format_str = end + 1; } -} // namespace fmt +}// namespace fmt #if FMT_USE_USER_DEFINED_LITERALS namespace fmt { namespace internal { -template +template struct UdlFormat { - const Char *str; + const Char *str; - template - auto operator()(Args && ... args) const - -> decltype(format(str, std::forward(args)...)) { - return format(str, std::forward(args)...); - } + template + auto operator()(Args &&...args) const -> decltype(format(str, std::forward(args)...)) + { + return format(str, std::forward(args)...); + } }; -template +template struct UdlArg { - const Char *str; + const Char *str; - template - NamedArgWithType operator=(T &&value) const { - return {str, std::forward(value)}; - } + template + NamedArgWithType operator=(T &&value) const + { + return {str, std::forward(value)}; + } }; -} // namespace internal +}// namespace internal inline namespace literals { @@ -4131,10 +4234,9 @@ inline namespace literals { std::string message = "The answer is {}"_format(42); \endrst */ -inline internal::UdlFormat -operator"" _format(const char *s, std::size_t) { return {s}; } -inline internal::UdlFormat -operator"" _format(const wchar_t *s, std::size_t) { return {s}; } +inline internal::UdlFormat operator"" _format(const char *s, std::size_t) { return {s}; } + +inline internal::UdlFormat operator"" _format(const wchar_t * s, std::size_t) { return {s}; } /** \rst @@ -4146,29 +4248,28 @@ operator"" _format(const wchar_t *s, std::size_t) { return {s}; } print("Elapsed time: {s:.2f} seconds", "s"_a=1.23); \endrst */ -inline internal::UdlArg -operator"" _a(const char *s, std::size_t) { return {s}; } -inline internal::UdlArg -operator"" _a(const wchar_t *s, std::size_t) { return {s}; } +inline internal::UdlArg operator"" _a(const char *s, std::size_t) { return {s}; } -} // inline namespace literals -} // namespace fmt -#endif // FMT_USE_USER_DEFINED_LITERALS +inline internal::UdlArg operator"" _a(const wchar_t * s, std::size_t) { return {s}; } + +}// namespace literals +}// namespace fmt +#endif// FMT_USE_USER_DEFINED_LITERALS // Restore warnings. #if FMT_GCC_VERSION >= 406 -# pragma GCC diagnostic pop +#pragma GCC diagnostic pop #endif #if defined(__clang__) && !defined(FMT_ICC_VERSION) -# pragma clang diagnostic pop +#pragma clang diagnostic pop #endif #ifdef FMT_HEADER_ONLY -# define FMT_FUNC inline -# include "format.cc" +#define FMT_FUNC inline +#include "format.cc" #else -# define FMT_FUNC +#define FMT_FUNC #endif -#endif // FMT_FORMAT_H_ +#endif// FMT_FORMAT_H_ diff --git a/CMakeLists.txt b/CMakeLists.txt index d8bfb40..89fea01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,22 +20,23 @@ set(BUILD_EXAMPLES OFF) add_library(sled STATIC "") add_subdirectory(3party/gperftools EXCLUDE_FROM_ALL) +add_subdirectory(3party/asyncplusplus EXCLUDE_FROM_ALL) # add_subdirectory(3party/cppuprofile EXCLUDE_FROM_ALL) -add_subdirectory(3party/protobuf-3.21.12 EXCLUDE_FROM_ALL) +# add_subdirectory(3party/protobuf-3.21.12 EXCLUDE_FROM_ALL) if(NOT TARGET marl) - add_subdirectory(3party/marl EXCLUDE_FROM_ALL) + add_subdirectory(3party/marl EXCLUDE_FROM_ALL) endif() if(NOT TARGET rpc_core) - add_subdirectory(3party/rpc_core EXCLUDE_FROM_ALL) + add_subdirectory(3party/rpc_core EXCLUDE_FROM_ALL) endif() if(NOT TARGET fmt) - add_subdirectory(3party/fmt EXCLUDE_FROM_ALL) + add_subdirectory(3party/fmt EXCLUDE_FROM_ALL) endif() if(SLED_LOCATION_PATH) - target_compile_definitions( + target_compile_definitions( sled PRIVATE __SLED_LOCATION_PATH="${SLED_LOCATION_PATH}") endif() # add_subdirectory(3party/eigen EXCLUDE_FROM_ALL) @@ -43,7 +44,9 @@ target_include_directories(sled PUBLIC include 3party/eigen 3party/inja 3party/rxcpp) target_sources( sled - PRIVATE src/filesystem/path.cc + PRIVATE + src/async/async.cc + src/filesystem/path.cc src/log/log.cc src/network/async_resolver.cc src/network/ip_address.cc @@ -81,13 +84,15 @@ target_sources( include(CheckCCompilerFlag) check_c_compiler_flag("-Wl,--whole-archive" SUPPORT_COMPILE_WHOLE_ARCHIVE) if(SUPPORT_COMPILE_WHOLE_ARCHIVE) - set(WHOLE_ARCHIVE_WRAPPER_START "-Wl,--whole-archive") - set(WHOLE_ARCHIVE_WRAPPER_END "-Wl,--no-whole-archive") + set(WHOLE_ARCHIVE_WRAPPER_START "-Wl,--whole-archive") + set(WHOLE_ARCHIVE_WRAPPER_END "-Wl,--no-whole-archive") endif() target_link_libraries( sled - PUBLIC rpc_core fmt marl protobuf::libprotobuf # cppuprofile + PUBLIC rpc_core fmt marl + Async++ + # protobuf::libprotobuf # ${WHOLE_ARCHIVE_WRAPPER_START} tcmalloc_and_profiler_static # ${WHOLE_ARCHIVE_WRAPPER_END} @@ -97,32 +102,34 @@ target_link_libraries( set_target_properties(sled PROPERTIES POSITION_INDEPENDENT_CODE ON) if(SLED_BUILD_BENCHMARK) - if(NOT TARGET benchmark) - find_package(benchmark REQUIRED) - endif() + if(NOT TARGET benchmark) + find_package(benchmark REQUIRED) + endif() - add_executable( + add_executable( sled_benchmark src/random_bench.cc src/strings/base64_bench.cc src/system/fiber/fiber_bench.cc src/system/thread_pool_bench.cc src/system_time_bench.cc) - target_link_libraries(sled_benchmark PRIVATE sled benchmark::benchmark + target_link_libraries(sled_benchmark PRIVATE sled benchmark::benchmark benchmark::benchmark_main) endif(SLED_BUILD_BENCHMARK) if(SLED_BUILD_TESTS) - include(FetchContent) - FetchContent_Declare( + include(FetchContent) + FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip ) - FetchContent_MakeAvailable(googletest) - add_executable( + FetchContent_MakeAvailable(googletest) + add_executable( sled_tests # src/exec/just_test.cc + src/async/async_test.cc src/any_test.cc src/filesystem/path_test.cc src/futures/promise_test.cc + src/futures/detail/just_test.cc src/log/fmt_test.cc # src/profiling/profiling_test.cc src/strings/base64_test.cc @@ -134,20 +141,23 @@ if(SLED_BUILD_TESTS) src/system/thread_pool_test.cc src/rx_test.cc src/uri_test.cc) - target_link_libraries(sled_tests PRIVATE sled GTest::gtest GTest::gtest_main) - add_test(NAME sled_tests COMMAND sled_tests) + if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(sled_tests PRIVATE -Wthread-safety) + endif() + target_link_libraries(sled_tests PRIVATE sled GTest::gtest GTest::gtest_main) + add_test(NAME sled_tests COMMAND sled_tests) endif(SLED_BUILD_TESTS) if(SLED_BUILD_FUZZ) - macro(add_fuzz_test name sources) - add_executable(${name} ${sources}) - target_link_libraries(${name} PRIVATE sled) - target_compile_options(${name} PRIVATE -g -O1 -fsanitize=fuzzer,address + macro(add_fuzz_test name sources) + add_executable(${name} ${sources}) + target_link_libraries(${name} PRIVATE sled) + target_compile_options(${name} PRIVATE -g -O1 -fsanitize=fuzzer,address -fsanitize-coverage=trace-cmp) - target_link_options(${name} PRIVATE -fsanitize=fuzzer,address + target_link_options(${name} PRIVATE -fsanitize=fuzzer,address -fsanitize-coverage=trace-cmp) - endmacro() + endmacro() - add_fuzz_test(base64_fuzz src/strings/base64_fuzz.cc) + add_fuzz_test(base64_fuzz src/strings/base64_fuzz.cc) endif(SLED_BUILD_FUZZ) diff --git a/include/sled/async/async.h b/include/sled/async/async.h new file mode 100644 index 0000000..d566fe6 --- /dev/null +++ b/include/sled/async/async.h @@ -0,0 +1,33 @@ +#ifndef SLED_ASYNC_ASYNC_H +#define SLED_ASYNC_ASYNC_H + +namespace sled { +class FiberScheduler; + +} + +namespace async { +sled::FiberScheduler &default_scheduler(); +class task_base; + +namespace detail { +void wait_for_task(task_base *wait_task); +} +}// namespace async + +#define LIBASYNC_CUSTON_EVENT +#define LIBASYNC_CUSTOM_DEFAULT_SCHEDULER +#include + +namespace sled { +void SleepWaitHandler(async::task_wait_handle t); + +class FiberScheduler { +public: + FiberScheduler(); + void schedule(async::task_run_handle t); +}; + +}// namespace sled + +#endif// SLED_ASYNC_ASYNC_H diff --git a/include/sled/exec/detail/just.h b/include/sled/exec/detail/just.h deleted file mode 100644 index a75c370..0000000 --- a/include/sled/exec/detail/just.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef SLED_EXEC_DETAIL_JUST_H -#define SLED_EXEC_DETAIL_JUST_H -#pragma once - -#include -#include - -namespace sled { - -template -struct JustOperation { - TReceiver receiver; - T value; - - void Start() { receiver.SetValue(value); } -}; - -template -struct JustSender { - using result_t = T; - T value; - - template - JustOperation Connect(TReceiver &&receiver) - { - return {std::forward(receiver), std::forward(value)}; - } -}; - -template -JustSender -Just(T &&t) -{ - return {std::forward(t)}; -} - -}// namespace sled - -#endif// SLED_EXEC_DETAIL_JUST_H diff --git a/include/sled/exec/detail/retry.h b/include/sled/exec/detail/retry.h deleted file mode 100644 index ec16a18..0000000 --- a/include/sled/exec/detail/retry.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef SLED_EXEC_DETAIL_RETRY_H -#define SLED_EXEC_DETAIL_RETRY_H -#include -#include -#include -#pragma once - -#include "traits.h" - -namespace sled { - -struct RetryState { - int retry_count; - bool need_retry; -}; - -template -struct RetryReceiver { - TReceiver receiver; - std::shared_ptr state; - - template - void SetValue(T &&value) - { - receiver.SetValue(value); - } - - void SetError(std::exception_ptr err) - { - if (state->retry_count < 0) {} - } - - void SetStopped() { receiver.SetStopped(); } -}; - -template -struct RetryOperation { - ConnectResultT> op; - std::shared_ptr state; - - void Start() {} -}; - -template -struct RetrySender { - using S = typename std::remove_cv::type>::type; - using result_t = SenderResultT; - S sender; - int retry_count; - - template - RetryOperation Connect(TReceiver &&receiver) - { - auto retry_state = std::make_shared(new RetryState{retry_count, false}); - return {sender.Connect(RetryReceiver{receiver, retry_state}), retry_state}; - } -}; - -template -RetrySender -Retry(TSender &&sender, int retry_count) -{ - return {std::forward(sender), retry_count}; -} - -}// namespace sled -#endif// SLED_EXEC_DETAIL_RETRY_H diff --git a/include/sled/exec/detail/sync_wait.h b/include/sled/exec/detail/sync_wait.h deleted file mode 100644 index 63dbace..0000000 --- a/include/sled/exec/detail/sync_wait.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef SLED_EXEC_DETAIL_SYNC_WAIT_H -#define SLED_EXEC_DETAIL_SYNC_WAIT_H -#pragma once - -#include "sled/optional.h" -#include "sled/synchronization/mutex.h" -#include "traits.h" -#include - -namespace sled { -struct SyncWaitState { - sled::Mutex lock; - sled::ConditionVariable cv; - std::exception_ptr err; - bool done = false; -}; - -template -struct SyncWaitReceiver { - SyncWaitState &data; - sled::optional &value; - - void SetValue(T &&val) - { - sled::MutexLock lock(&data.lock); - value.emplace(val); - data.done = true; - data.cv.NotifyOne(); - } - - void SetError(std::exception_ptr err) - { - sled::MutexLock lock(&data.lock); - data.err = err; - data.done = true; - data.cv.NotifyOne(); - } - - void SetStopped(std::exception_ptr err) - { - sled::MutexLock lock(&data.lock); - data.done = true; - data.cv.NotifyOne(); - } -}; - -template -sled::optional> -SyncWait(TSender sender) -{ - using T = SenderResultT; - SyncWaitState data; - sled::optional value; - - auto op = sender.Connect(SyncWaitReceiver{data, value}); - op.Start(); - - sled::MutexLock lock(&data.lock); - data.cv.Wait(lock, [&data] { return data.done; }); - - if (data.err) { std::rethrow_exception(data.err); } - - return value; -} - -}// namespace sled - -#endif// SLED_EXEC_DETAIL_SYNC_WAIT_H diff --git a/include/sled/exec/detail/then.h b/include/sled/exec/detail/then.h deleted file mode 100644 index 77b403b..0000000 --- a/include/sled/exec/detail/then.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef SLED_EXEC_DETAIL_THEN_H -#define SLED_EXEC_DETAIL_THEN_H -#pragma once - -#include "traits.h" -#include -#include - -namespace sled { -template -struct ThenReceiver { - TReceiver receiver; - F func; - - template - void SetValue(T &&value) - { - try { - receiver.SetValue(func(std::forward(value))); - } catch (...) { - receiver.SetError(std::current_exception()); - } - } - - void SetError(std::exception_ptr err) { receiver.SetError(err); } - - void SetStopped() { receiver.SetStopped(); } -}; - -template -struct ThenOperation { - ConnectResultT> op; - - void Start() { op.Start(); } -}; - -template -struct ThenSender { - using S = typename std::remove_cv::type>::type; - using result_t = typename eggs::invoke_result_t>; - S sender; - F func; - - template - ThenOperation Connect(TReceiver &&receiver) - { - return {sender.Connect(ThenReceiver{std::forward(receiver), func})}; - } -}; - -template -ThenSender -Then(TSender &&sender, F &&func) -{ - return {std::forward(sender), std::forward(func)}; -} - -}// namespace sled -#endif// SLED_EXEC_DETAIL_THEN_H diff --git a/include/sled/exec/detail/traits.h b/include/sled/exec/detail/traits.h deleted file mode 100644 index 47cfe03..0000000 --- a/include/sled/exec/detail/traits.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef SLED_EXEC_DETAIL_TRAITS_H -#define SLED_EXEC_DETAIL_TRAITS_H -#pragma once -#include "invoke_result.h" -#include - -namespace sled { -template -using ConnectResultT = decltype(std::declval().Connect(std::declval())); - -template -using SenderResultT = typename TSender::result_t; - -}// namespace sled - -#endif// SLED_EXEC_DETAIL_TRAITS_H diff --git a/include/sled/exec/exec.h b/include/sled/exec/exec.h deleted file mode 100644 index 9a9ea19..0000000 --- a/include/sled/exec/exec.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef SLED_EXEC_EXEC_H -#define SLED_EXEC_EXEC_H -#pragma once -#include "detail/just.h" -#include "detail/sync_wait.h" -#include "detail/then.h" - -namespace sled {} - -#endif// SLED_EXEC_EXEC_H diff --git a/include/sled/futures/future.h b/include/sled/futures/future.h new file mode 100644 index 0000000..a21378b --- /dev/null +++ b/include/sled/futures/future.h @@ -0,0 +1,6 @@ +#ifndef SLED_FUTURES_FUTURE_H +#define SLED_FUTURES_FUTURE_H + +namespace sled {} + +#endif// SLED_FUTURES_FUTURE_H diff --git a/include/sled/sled.h b/include/sled/sled.h index d3a06d0..71aac9f 100644 --- a/include/sled/sled.h +++ b/include/sled/sled.h @@ -2,7 +2,10 @@ #ifndef SLED_SLED_H #define SLED_SLED_H +namespace async {} + // thrid_party +#include "async/async.h" #include "inja.hpp" #include "rx.h" diff --git a/include/sled/synchronization/mutex.h b/include/sled/synchronization/mutex.h index 6c01f1d..1b029a6 100644 --- a/include/sled/synchronization/mutex.h +++ b/include/sled/synchronization/mutex.h @@ -22,7 +22,7 @@ namespace internal { template struct HasLockAndUnlock { template().Lock()) * = nullptr, + decltype(std::declval().Lock()) * = nullptr, decltype(std::declval().Unlock()) * = nullptr> static int Test(int); @@ -33,31 +33,32 @@ struct HasLockAndUnlock { }; }// namespace internal -using Mutex = marl::mutex; +// using Mutex = marl::mutex; -// class Mutex final { -// public: -// Mutex() = default; -// Mutex(const Mutex &) = delete; -// Mutex &operator=(const Mutex &) = delete; -// -// inline void Lock() { impl_.lock(); }; -// -// inline bool TryLock() { return impl_.try_lock(); } -// -// inline void AssertHeld() {} -// -// inline void Unlock() { impl_.unlock(); } -// -// private: -// std::mutex impl_; -// friend class ConditionVariable; -// }; - -class RecursiveMutex final { +class SLED_LOCKABLE Mutex final { public: - RecursiveMutex() = default; - RecursiveMutex(const RecursiveMutex &) = delete; + Mutex() = default; + Mutex(const Mutex &) = delete; + Mutex &operator=(const Mutex &) = delete; + + inline void Lock() SLED_EXCLUSIVE_LOCK_FUNCTION(impl_) { impl_.lock(); }; + + inline bool TryLock() SLED_EXCLUSIVE_TRYLOCK_FUNCTION(true) { return impl_.try_lock(); } + + inline void AssertHeld() SLED_ASSERT_EXCLUSIVE_LOCK(impl_) {} + + inline void Unlock() SLED_UNLOCK_FUNCTION(impl_) { impl_.unlock(); } + +private: + marl::mutex impl_; + friend class ConditionVariable; + friend class MutexLock; +}; + +class SLED_LOCKABLE RecursiveMutex final { +public: + RecursiveMutex() = default; + RecursiveMutex(const RecursiveMutex &) = delete; RecursiveMutex &operator=(const RecursiveMutex &) = delete; inline void Lock() SLED_SHARED_LOCK_FUNCTION() { impl_.lock(); } @@ -72,17 +73,14 @@ private: std::recursive_mutex impl_; }; -class RecursiveMutexLock final { +class SLED_SCOPED_CAPABILITY RecursiveMutexLock final { public: - RecursiveMutexLock(const RecursiveMutexLock &) = delete; + RecursiveMutexLock(const RecursiveMutexLock &) = delete; RecursiveMutexLock &operator=(const RecursiveMutexLock &) = delete; - explicit RecursiveMutexLock(RecursiveMutex *mutex) SLED_EXCLUSIVE_LOCK_FUNCTION(mutex) : mutex_(mutex) - { - mutex->Lock(); - } + explicit RecursiveMutexLock(RecursiveMutex *mutex) SLED_ACQUIRE_SHARED(mutex) : mutex_(mutex) { mutex->Lock(); } - ~RecursiveMutexLock() SLED_UNLOCK_FUNCTION() { mutex_->Unlock(); } + ~RecursiveMutexLock() SLED_RELEASE_SHARED(mutex_) { mutex_->Unlock(); } private: RecursiveMutex *mutex_; @@ -101,13 +99,13 @@ private: // friend class ConditionVariable; // }; // -class MutexLock final { +class SLED_SCOPED_CAPABILITY MutexLock final { public: - MutexLock(Mutex *mutex) SLED_EXCLUSIVE_LOCK_FUNCTION(mutex) : lock_(*mutex) {} + MutexLock(Mutex *mutex) SLED_ACQUIRE(mutex) : lock_(mutex->impl_) {} - ~MutexLock() SLED_UNLOCK_FUNCTION() = default; + ~MutexLock() SLED_RELEASE() = default; - MutexLock(const MutexLock &) = delete; + MutexLock(const MutexLock &) = delete; MutexLock &operator=(const MutexLock &) = delete; private: diff --git a/include/sled/uri.h b/include/sled/uri.h index 695e740..46bd8b9 100644 --- a/include/sled/uri.h +++ b/include/sled/uri.h @@ -20,36 +20,36 @@ public: // using ParamType = std::pair; using ParamMap = std::map; // http://xxx.com/index.html?field=value - static URI ParseAbsoluteURI(const std::string &uri_str); + // static URI ParseAbsoluteURI(const std::string &uri_str); + // http://xxx.com/index.html?field=value#download static URI ParseURI(const std::string &uri_str); + // http://xxx.com/index.html - static URI ParseURIReference(const std::string &uri_str); + // static URI ParseURIReference(const std::string &uri_str); URI() = default; URI(const std::string &uri_str); // setter getter __SLED_URI_GETTER_AND_SETTER(std::string, scheme) + __SLED_URI_GETTER_AND_SETTER(std::string, content) __SLED_URI_GETTER_AND_SETTER(std::string, username) __SLED_URI_GETTER_AND_SETTER(std::string, password) __SLED_URI_GETTER_AND_SETTER(std::string, host) - __SLED_URI_GETTER_AND_SETTER(std::string, port) + __SLED_URI_GETTER_AND_SETTER(unsigned long, port) __SLED_URI_GETTER_AND_SETTER(std::string, path) - __SLED_URI_GETTER_AND_SETTER(std::string, file_name) - __SLED_URI_GETTER_AND_SETTER(std::string, file_extension) __SLED_URI_GETTER_AND_SETTER(ParamMap, query) __SLED_URI_GETTER_AND_SETTER(std::string, anchor) private: std::string scheme_; + std::string content_; std::string username_; std::string password_; std::string host_; - std::string port_; + unsigned long port_; std::string path_; - std::string file_name_; - std::string file_extension_; ParamMap query_; std::string anchor_; }; diff --git a/include/sled/utility/move_on_copy.h b/include/sled/utility/move_on_copy.h new file mode 100644 index 0000000..a195e3c --- /dev/null +++ b/include/sled/utility/move_on_copy.h @@ -0,0 +1,31 @@ +#ifndef SLED_UTILITY_MOVE_ON_COPY_H +#define SLED_UTILITY_MOVE_ON_COPY_H +#include +#include + +namespace sled { + +template +struct MoveOnCopy { + using type = typename std::remove_reference::type; + + MoveOnCopy(type &&value) : value(std::move(value)) {} + + MoveOnCopy(const MoveOnCopy &other) : value(std::move(other.value)) {} + + MoveOnCopy(MoveOnCopy &&other) : value(std::move(other.value)) {} + + // MoveOnCopy(MoveOnCopy &&) = delete; + MoveOnCopy &operator=(const MoveOnCopy &) = delete; + + mutable type value; +}; + +template +auto +MakeMoveOnCopy(T &&value) -> MoveOnCopy +{ + return {std::move(value)}; +} +}// namespace sled +#endif// SLED_UTILITY_MOVE_ON_COPY_H diff --git a/src/async/async.cc b/src/async/async.cc new file mode 100644 index 0000000..44bed72 --- /dev/null +++ b/src/async/async.cc @@ -0,0 +1,48 @@ +#include "sled/async/async.h" +#include "sled/synchronization/event.h" +#include "sled/system/thread_pool.h" +#include "sled/utility/move_on_copy.h" +// clang-format off +#include + + +namespace sled { + +void +SleepWaitHandler(async::task_wait_handle t) +{ + sled::Event event; + t.on_finish([&] { event.Set(); }); + event.Wait(sled::Event::kForever); +} + +FiberScheduler::FiberScheduler() +{ +} + +void +FiberScheduler::schedule(async::task_run_handle t) +{ + static ThreadPool thread_pool; + auto move_on_copy = sled::MakeMoveOnCopy(t); + thread_pool.submit([move_on_copy] { move_on_copy.value.run_with_wait_handler(SleepWaitHandler); }); + // thread_pool.submit([move_on_copy] { move_on_copy.value.run(); }); +} + +}// namespace sled + +// clang-format on +namespace async { +sled::FiberScheduler & +default_scheduler() +{ + static sled::FiberScheduler scheduler; + return scheduler; +} + +void +detail::wait_for_task(task_base *wait_task) +{ + sled::SleepWaitHandler(task_wait_handle(wait_task)); +} +}// namespace async diff --git a/src/async/async_test.cc b/src/async/async_test.cc new file mode 100644 index 0000000..14c1d50 --- /dev/null +++ b/src/async/async_test.cc @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +TEST(Async, task) +{ + auto task1 = async::spawn([] { return 42; }).then([](int value) { return value * 3; }).then([](int value) { + EXPECT_EQ(value, 126); + return value; + }); + task1.wait(); + EXPECT_EQ(126, task1.get()); +} + +TEST(Async, parallel_for) +{ + const int count = 1000; + std::vector values(count); + async::parallel_for(async::irange(0, count), [&values](int x) { + EXPECT_FALSE(values[x]); + values[x] = true; + }); + for (int i = 0; i < count; i++) { EXPECT_TRUE(values[i]) << i; } +} + +TEST(Async, parallel_reduce) +{ + auto r = async::parallel_reduce(async::irange(1, 5), 0, [](int x, int y) { + LOGD("", "{},{}", x, y); + return x + y; + }); + LOGD("", "{}", r); +} diff --git a/src/futures/detail/just_test.cc b/src/futures/detail/just_test.cc new file mode 100644 index 0000000..1687169 --- /dev/null +++ b/src/futures/detail/just_test.cc @@ -0,0 +1,4 @@ +#include +#include + +TEST(Just, basic) { auto s1 = sled::detail::Just(42); } diff --git a/src/log/log.cc b/src/log/log.cc index 3f80e0e..60311eb 100644 --- a/src/log/log.cc +++ b/src/log/log.cc @@ -1,4 +1,5 @@ #include "sled/log/log.h" +#include "sled/synchronization/mutex.h" #include "sled/time_utils.h" #include #include @@ -60,7 +61,7 @@ class ScopedAtomicWaiter { public: ScopedAtomicWaiter(std::atomic_bool &flag) : flag_(flag) { - bool old = flag_.load(); + bool old = true; while (!flag_.compare_exchange_weak(old, false)) { continue; } } @@ -79,7 +80,7 @@ GetCurrentUTCTime() // int64_t now = tp.tv_sec * kNumNanosecsPerSec + tp.tv_nsec; // std::time_t t = tp.tv_sec; const int64_t now = TimeUTCNanos(); - std::time_t t = now / kNumNanosecsPerSec; + std::time_t t = now / kNumNanosecsPerSec; #else std::time_t t = std::time(nullptr); #endif @@ -103,18 +104,40 @@ SetLogLevel(LogLevel level) g_log_level = level; } +static std::atomic g_current_id(0); +static std::atomic g_request_id(0); + +struct Waiter { + Waiter(uint32_t id, std::atomic ¤t_id) : id_(id), current_id_(current_id) {} + + ~Waiter() { current_id_.fetch_add(1); } + + uint32_t id() { return id_; } + + void wait() + { + while (id_ > current_id_.load()) {} + } + +private: + uint32_t id_; + std::atomic ¤t_id_; +}; + void Log(LogLevel level, const char *tag, const char *fmt, const char *file_name, int line, const char *func_name, ...) { if (level < g_log_level) { return; } - static std::atomic_bool allow(true); - ScopedAtomicWaiter waiter(allow); - int len = file_name ? strlen(file_name) : 0; + auto utc_time = GetCurrentUTCTime(); + auto level_str = ToShortString(level); + int len = file_name ? strlen(file_name) : 0; while (len > 0 && file_name[len - 1] != '/') { len--; } - auto msg = fmt::format("{} {} {} {}:{}@{}: {}", GetCurrentUTCTime(), ToShortString(level), tag, file_name + len, - line, func_name, fmt); + auto msg = fmt::format("{} {} {} {}:{}@{}: {}", utc_time, level_str, tag, file_name + len, line, func_name, fmt); + + Waiter waiter(g_request_id.fetch_add(1), g_current_id); + waiter.wait(); std::cout << GetConsoleColorPrefix(level) << msg << GetConsoleColorSuffix() << std::endl; } diff --git a/src/uri.cc b/src/uri.cc index b27b793..49c6f9c 100644 --- a/src/uri.cc +++ b/src/uri.cc @@ -1,15 +1,525 @@ #include "sled/uri.h" #include "sled/strings/utils.h" +#include +#include +#include +#include +#include + +namespace detail { +class uri { + /* URIs are broadly divided into two categories: hierarchical and + * non-hierarchical. Both hierarchical URIs and non-hierarchical URIs have a + * few elements in common; all URIs have a scheme of one or more alphanumeric + * characters followed by a colon, and they all may optionally have a query + * component preceded by a question mark, and a fragment component preceded by + * an octothorpe (hash mark: '#'). The query consists of stanzas separated by + * either ampersands ('&') or semicolons (';') (but only one or the other), + * and each stanza consists of a key and an optional value; if the value + * exists, the key and value must be divided by an equals sign. + * + * The following is an example from Wikipedia of a hierarchical URI: + * scheme:[//[user:password@]domain[:port]][/]path[?query][#fragment] + */ + +public: + enum class scheme_category { Hierarchical, NonHierarchical }; + + enum class component { Scheme, Content, Username, Password, Host, Port, Path, Query, Fragment }; + + enum class query_argument_separator { ampersand, semicolon }; + + uri(char const *uri_text, + scheme_category category = scheme_category::Hierarchical, + query_argument_separator separator = query_argument_separator::ampersand) + : m_category(category), + m_port(0), + m_path_is_rooted(false), + m_separator(separator) + { + setup(std::string(uri_text), category); + }; + + uri(std::string const &uri_text, + scheme_category category = scheme_category::Hierarchical, + query_argument_separator separator = query_argument_separator::ampersand) + : m_category(category), + m_port(0), + m_path_is_rooted(false), + m_separator(separator) + { + setup(uri_text, category); + }; + + uri(std::map const &components, + scheme_category category, + bool rooted_path, + query_argument_separator separator = query_argument_separator::ampersand) + : m_category(category), + m_path_is_rooted(rooted_path), + m_separator(separator) + { + if (components.count(component::Scheme)) { + if (components.at(component::Scheme).length() == 0) { + throw std::invalid_argument("Scheme cannot be empty."); + } + m_scheme = components.at(component::Scheme); + } else { + throw std::invalid_argument("A URI must have a scheme."); + } + + if (category == scheme_category::Hierarchical) { + if (components.count(component::Content)) { + throw std::invalid_argument("The content component is only for use in non-hierarchical URIs."); + } + + bool has_username = components.count(component::Username); + bool has_password = components.count(component::Password); + if (has_username && has_password) { + m_username = components.at(component::Username); + m_password = components.at(component::Password); + } else if ((has_username && !has_password) || (!has_username && has_password)) { + throw std::invalid_argument("If a username or password is supplied, both must be provided."); + } + + if (components.count(component::Host)) { m_host = components.at(component::Host); } + + if (components.count(component::Port)) { m_port = std::stoul(components.at(component::Port)); } + + if (components.count(component::Path)) { + m_path = components.at(component::Path); + } else { + throw std::invalid_argument("A path is required on a hierarchical URI, even an empty path."); + } + } else { + if (components.count(component::Username) || components.count(component::Password) + || components.count(component::Host) || components.count(component::Port) + || components.count(component::Path)) { + throw std::invalid_argument( + "None of the hierarchical components are allowed in a non-hierarchical URI."); + } + + if (components.count(component::Content)) { + m_content = components.at(component::Content); + } else { + throw std::invalid_argument( + "Content is a required component for a non-hierarchical URI, even an empty string."); + } + } + + if (components.count(component::Query)) { m_query = components.at(component::Query); } + + if (components.count(component::Fragment)) { m_fragment = components.at(component::Fragment); } + } + + uri(uri const &other, std::map const &replacements) + : m_category(other.m_category), + m_path_is_rooted(other.m_path_is_rooted), + m_separator(other.m_separator) + { + m_scheme = (replacements.count(component::Scheme)) ? replacements.at(component::Scheme) : other.m_scheme; + + if (m_category == scheme_category::Hierarchical) { + m_username + = (replacements.count(component::Username)) ? replacements.at(component::Username) : other.m_username; + + m_password + = (replacements.count(component::Password)) ? replacements.at(component::Password) : other.m_password; + + m_host = (replacements.count(component::Host)) ? replacements.at(component::Host) : other.m_host; + + m_port + = (replacements.count(component::Port)) ? std::stoul(replacements.at(component::Port)) : other.m_port; + + m_path = (replacements.count(component::Path)) ? replacements.at(component::Path) : other.m_path; + } else { + m_content + = (replacements.count(component::Content)) ? replacements.at(component::Content) : other.m_content; + } + + m_query = (replacements.count(component::Query)) ? replacements.at(component::Query) : other.m_query; + + m_fragment + = (replacements.count(component::Fragment)) ? replacements.at(component::Fragment) : other.m_fragment; + } + + // Copy constructor; just use the copy assignment operator internally. + uri(uri const &other) { *this = other; }; + + // Copy assignment operator + uri &operator=(uri const &other) + { + if (this != &other) { + m_scheme = other.m_scheme; + m_content = other.m_content; + m_username = other.m_username; + m_password = other.m_password; + m_host = other.m_host; + m_path = other.m_path; + m_query = other.m_query; + m_fragment = other.m_fragment; + m_query_dict = other.m_query_dict; + m_category = other.m_category; + m_port = other.m_port; + m_path_is_rooted = other.m_path_is_rooted; + m_separator = other.m_separator; + } + return *this; + } + + ~uri(){}; + + std::string const &get_scheme() const { return m_scheme; }; + + scheme_category get_scheme_category() const { return m_category; }; + + std::string const &get_content() const + { + if (m_category != scheme_category::NonHierarchical) { + throw std::domain_error("The content component is only valid for non-hierarchical URIs."); + } + return m_content; + }; + + std::string const &get_username() const + { + if (m_category != scheme_category::Hierarchical) { + throw std::domain_error("The username component is only valid for hierarchical URIs."); + } + return m_username; + }; + + std::string const &get_password() const + { + if (m_category != scheme_category::Hierarchical) { + throw std::domain_error("The password component is only valid for hierarchical URIs."); + } + return m_password; + }; + + std::string const &get_host() const + { + if (m_category != scheme_category::Hierarchical) { + throw std::domain_error("The host component is only valid for hierarchical URIs."); + } + return m_host; + }; + + unsigned long get_port() const + { + if (m_category != scheme_category::Hierarchical) { + throw std::domain_error("The port component is only valid for hierarchical URIs."); + } + return m_port; + }; + + std::string const &get_path() const + { + if (m_category != scheme_category::Hierarchical) { + throw std::domain_error("The path component is only valid for hierarchical URIs."); + } + return m_path; + }; + + std::string const &get_query() const { return m_query; }; + + std::map const &get_query_dictionary() const { return m_query_dict; }; + + std::string const &get_fragment() const { return m_fragment; }; + + std::string to_string() const + { + std::string full_uri; + full_uri.append(m_scheme); + full_uri.append(":"); + + if (m_content.length() > m_path.length()) { + full_uri.append("//"); + if (!(m_username.empty() || m_password.empty())) { + full_uri.append(m_username); + full_uri.append(":"); + full_uri.append(m_password); + full_uri.append("@"); + } + + full_uri.append(m_host); + + if (m_port != 0) { + full_uri.append(":"); + full_uri.append(std::to_string(m_port)); + } + } + + if (m_path_is_rooted) { full_uri.append("/"); } + full_uri.append(m_path); + + if (!m_query.empty()) { + full_uri.append("?"); + full_uri.append(m_query); + } + + if (!m_fragment.empty()) { + full_uri.append("#"); + full_uri.append(m_fragment); + } + + return full_uri; + }; + +private: + void setup(std::string const &uri_text, scheme_category category) + { + size_t const uri_length = uri_text.length(); + + if (uri_length == 0) { throw std::invalid_argument("URIs cannot be of zero length."); } + + std::string::const_iterator cursor = parse_scheme(uri_text, uri_text.begin()); + // After calling parse_scheme, *cursor == ':'; none of the following parsers + // expect a separator character, so we advance the cursor upon calling them. + cursor = parse_content(uri_text, (cursor + 1)); + + if ((cursor != uri_text.end()) && (*cursor == '?')) { cursor = parse_query(uri_text, (cursor + 1)); } + + if ((cursor != uri_text.end()) && (*cursor == '#')) { cursor = parse_fragment(uri_text, (cursor + 1)); } + + init_query_dictionary();// If the query string is empty, this will be empty too. + }; + + std::string::const_iterator parse_scheme(std::string const &uri_text, std::string::const_iterator scheme_start) + { + std::string::const_iterator scheme_end = scheme_start; + while ((scheme_end != uri_text.end()) && (*scheme_end != ':')) { + if (!(std::isalnum(*scheme_end) || (*scheme_end == '-') || (*scheme_end == '+') || (*scheme_end == '.'))) { + throw std::invalid_argument( + "Invalid character found in the scheme component. Supplied URI was: \"" + uri_text + "\"."); + } + ++scheme_end; + } + + if (scheme_end == uri_text.end()) { + throw std::invalid_argument( + "End of URI found while parsing the scheme. Supplied URI was: \"" + uri_text + "\"."); + } + + if (scheme_start == scheme_end) { + throw std::invalid_argument( + "Scheme component cannot be zero-length. Supplied URI was: \"" + uri_text + "\"."); + } + + m_scheme = std::string(scheme_start, scheme_end); + return scheme_end; + }; + + std::string::const_iterator parse_content(std::string const &uri_text, std::string::const_iterator content_start) + { + std::string::const_iterator content_end = content_start; + while ((content_end != uri_text.end()) && (*content_end != '?') && (*content_end != '#')) { ++content_end; } + + m_content = std::string(content_start, content_end); + + if ((m_category == scheme_category::Hierarchical) && (m_content.length() > 0)) { + // If it's a hierarchical URI, the content should be parsed for the hierarchical components. + std::string::const_iterator path_start = m_content.begin(); + std::string::const_iterator path_end = m_content.end(); + if (!m_content.compare(0, 2, "//")) { + // In this case an authority component is present. + std::string::const_iterator authority_cursor = (m_content.begin() + 2); + if (m_content.find_first_of('@') != std::string::npos) { + std::string::const_iterator userpass_divider + = parse_username(uri_text, m_content, authority_cursor); + authority_cursor = parse_password(uri_text, m_content, (userpass_divider + 1)); + // After this call, *authority_cursor == '@', so we skip over it. + ++authority_cursor; + } + + authority_cursor = parse_host(uri_text, m_content, authority_cursor); + + if ((authority_cursor != m_content.end()) && (*authority_cursor == ':')) { + authority_cursor = parse_port(uri_text, m_content, (authority_cursor + 1)); + } + + if ((authority_cursor != m_content.end()) && (*authority_cursor == '/')) { + // Then the path is rooted, and we should note this. + m_path_is_rooted = true; + path_start = authority_cursor + 1; + } + + // If we've reached the end and no path is present then set path_start + // to the end. + if (authority_cursor == m_content.end()) { path_start = m_content.end(); } + } else if (!m_content.compare(0, 1, "/")) { + m_path_is_rooted = true; + ++path_start; + } + + // We can now build the path based on what remains in the content string, + // since that's all that exists after the host and optional port component. + m_path = std::string(path_start, path_end); + } + return content_end; + }; + + std::string::const_iterator + parse_username(std::string const &uri_text, std::string const &content, std::string::const_iterator username_start) + { + std::string::const_iterator username_end = username_start; + // Since this is only reachable when '@' was in the content string, we can + // ignore the end-of-string case. + while (*username_end != ':') { + if (*username_end == '@') { + throw std::invalid_argument( + "Username must be followed by a password. Supplied URI was: \"" + uri_text + "\"."); + } + ++username_end; + } + m_username = std::string(username_start, username_end); + return username_end; + }; + + std::string::const_iterator + parse_password(std::string const &uri_text, std::string const &content, std::string::const_iterator password_start) + { + std::string::const_iterator password_end = password_start; + while (*password_end != '@') { ++password_end; } + + m_password = std::string(password_start, password_end); + return password_end; + }; + + std::string::const_iterator + parse_host(std::string const &uri_text, std::string const &content, std::string::const_iterator host_start) + { + std::string::const_iterator host_end = host_start; + // So, the host can contain a few things. It can be a domain, it can be an + // IPv4 address, it can be an IPv6 address, or an IPvFuture literal. In the + // case of those last two, it's of the form [...] where what's between the + // brackets is a matter of which IPv?? version it is. + while (host_end != content.end()) { + if (*host_end == '[') { + // We're parsing an IPv6 or IPvFuture address, so we should handle that + // instead of the normal procedure. + while ((host_end != content.end()) && (*host_end != ']')) { ++host_end; } + + if (host_end == content.end()) { + throw std::invalid_argument( + "End of content component encountered " + "while parsing the host component. " + "Supplied URI was: \"" + + uri_text + "\"."); + } + + ++host_end; + break; + // We can stop looping, we found the end of the IP literal, which is the + // whole of the host component when one's in use. + } else if ((*host_end == ':') || (*host_end == '/')) { + break; + } else { + ++host_end; + } + } + + m_host = std::string(host_start, host_end); + return host_end; + }; + + std::string::const_iterator + parse_port(std::string const &uri_text, std::string const &content, std::string::const_iterator port_start) + { + std::string::const_iterator port_end = port_start; + while ((port_end != content.end()) && (*port_end != '/')) { + if (!std::isdigit(*port_end)) { + throw std::invalid_argument( + "Invalid character while parsing the port. " + "Supplied URI was: \"" + + uri_text + "\"."); + } + + ++port_end; + } + + m_port = std::stoul(std::string(port_start, port_end)); + return port_end; + }; + + std::string::const_iterator parse_query(std::string const &uri_text, std::string::const_iterator query_start) + { + std::string::const_iterator query_end = query_start; + while ((query_end != uri_text.end()) && (*query_end != '#')) { + // Queries can contain almost any character except hash, which is reserved + // for the start of the fragment. + ++query_end; + } + m_query = std::string(query_start, query_end); + return query_end; + }; + + std::string::const_iterator parse_fragment(std::string const &uri_text, std::string::const_iterator fragment_start) + { + m_fragment = std::string(fragment_start, uri_text.end()); + return uri_text.end(); + }; + + void init_query_dictionary() + { + if (!m_query.empty()) { + // Loop over the query string looking for '&'s, then check each one for + // an '=' to find keys and values; if there's not an '=' then the key + // will have an empty value in the map. + char separator = (m_separator == query_argument_separator::ampersand) ? '&' : ';'; + size_t carat = 0; + size_t stanza_end = m_query.find_first_of(separator); + do { + std::string stanza + = m_query.substr(carat, + ((stanza_end != std::string::npos) ? (stanza_end - carat) : std::string::npos)); + size_t key_value_divider = stanza.find_first_of('='); + std::string key = stanza.substr(0, key_value_divider); + std::string value; + if (key_value_divider != std::string::npos) { value = stanza.substr((key_value_divider + 1)); } + + if (m_query_dict.count(key) != 0) { throw std::invalid_argument("Bad key in the query string!"); } + + m_query_dict.emplace(key, value); + carat = ((stanza_end != std::string::npos) ? (stanza_end + 1) : std::string::npos); + stanza_end = m_query.find_first_of(separator, carat); + } while ((stanza_end != std::string::npos) || (carat != std::string::npos)); + } + } + + std::string m_scheme; + std::string m_content; + std::string m_username; + std::string m_password; + std::string m_host; + std::string m_path; + std::string m_query; + std::string m_fragment; + + std::map m_query_dict; + + scheme_category m_category; + unsigned long m_port; + bool m_path_is_rooted; + query_argument_separator m_separator; +}; +}// namespace detail namespace sled { URI URI::ParseURI(const std::string &uri_str) { - if (uri_str.empty()) { return {}; } URI uri; - // TODO: decode before - auto start_pos = uri_str.find_first_not_of(" "); - auto end_pos = uri_str.find(':'); + detail::uri uri_impl(uri_str.c_str()); + uri.set_scheme(uri_impl.get_scheme()); + uri.set_content(uri_impl.get_content()); + uri.set_username(uri_impl.get_username()); + uri.set_password(uri_impl.get_password()); + uri.set_host(uri_impl.get_host()); + uri.set_port(uri_impl.get_port()); + uri.set_path(uri_impl.get_path()); + uri.set_query(uri_impl.get_query_dictionary()); + uri.set_anchor(uri_impl.get_fragment()); return std::move(uri); }