From 6bb976e47fe42817756244a8799b65a4cf347d4c Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Fri, 11 Mar 2022 12:16:19 -0600 Subject: [PATCH] external: Add some headers from my util-headers project. --- src/external/CMakeLists.txt | 6 + src/external/util-headers/util/Finally.h | 117 ++++++++++++++++++ .../util/FixedLengthStringFunctions.h | 84 +++++++++++++ src/external/util-headers/util/Stride.h | 87 +++++++++++++ 4 files changed, 294 insertions(+) create mode 100644 src/external/util-headers/util/Finally.h create mode 100644 src/external/util-headers/util/FixedLengthStringFunctions.h create mode 100644 src/external/util-headers/util/Stride.h diff --git a/src/external/CMakeLists.txt b/src/external/CMakeLists.txt index 5b4f06ce5..d5d6d1b7a 100644 --- a/src/external/CMakeLists.txt +++ b/src/external/CMakeLists.txt @@ -24,6 +24,12 @@ endif() add_library(xrt-external-flexkalman INTERFACE) target_include_directories(xrt-external-flexkalman INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/flexkalman) +# FlexKalman +add_library(xrt-external-util-headers INTERFACE) +target_include_directories( + xrt-external-util-headers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/util-headers + ) + # Glad add_library(xrt-external-glad INTERFACE) target_include_directories(xrt-external-glad INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/glad/include) diff --git a/src/external/util-headers/util/Finally.h b/src/external/util-headers/util/Finally.h new file mode 100644 index 000000000..e38335b14 --- /dev/null +++ b/src/external/util-headers/util/Finally.h @@ -0,0 +1,117 @@ +/** @file + @brief Header declaring a C++11 `finally` or "scope-guard" construct. + + Inspirations are many - Alexandrescu's original and C++11 ScopeGuard, and + the Guideline Support Library's (GSL) `final_act`/`finally` + https://github.com/Microsoft/GSL/blob/0cf947db7760bf5756e4cb0d47c72a257ed527c5/include/gsl_util.h + come to mind, but this has been written and re-written numerous times + since its introduction into the C++ global consciousness, and this + implementation was written independently after I couldn't find a + previous independent implementation I had written a few weeks earlier in + an implementation file. -- Ryan Pavlik + + See UniqueDestructionActionWrapper for a "generalized" (in some sense) + version of this. + + Originally written for use in OSVR for Sensics , + relicensed to BSL 1.0 with permission. + + This header is maintained as a part of 'util-headers' - you can always + find the latest version online at https://github.com/rpavlik/util-headers + + This GUID can help identify the project: d1dbc94e-e863-49cf-bc08-ab4d9f486613 + + This copy of the header is from the revision that Git calls + 1a8444782d15cb9458052e3d8251c4f5b8e808d5 + + Commit date: "2022-03-11 12:11:32 -0600" + + @date 2016 + + @author + Ryan Pavlik + + +*/ + +// Copyright 2016, Sensics, Inc. +// +// SPDX-License-Identifier: BSL-1.0 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef INCLUDED_Finally_h_GUID_D925FE58_9C57_448B_C0BB_19A42B3243BA +#define INCLUDED_Finally_h_GUID_D925FE58_9C57_448B_C0BB_19A42B3243BA + +// Internal Includes +// - none + +// Library/third-party includes +// - none + +// Standard includes +#include + +namespace util { +namespace detail { + /// Allows you to run a callable something at the end of a scope. + /// + /// The class that provides the scope-guard behavior. Often not referred to + /// by name because auto is useful here, and often not created by a direct + /// constructor call because of the finally() convenience functions combined + /// with lambdas. + template class FinalTask { + public: + /// Explicit constructor from something callable. + explicit FinalTask(F f) : f_(std::move(f)) {} + + /// Move constructor - cancels the moved-from task. + FinalTask(FinalTask &&other) : f_(std::move(other.f_)), do_(other.do_) { + other.cancel(); + } + + /// non-copyable + FinalTask(FinalTask const &) = delete; + + /// non-assignable + FinalTask &operator=(FinalTask const &) = delete; + + /// Destructor - if we haven't been cancelled, do our callable thing. + ~FinalTask() { + if (do_) { + f_(); + } + } + /// Cancel causes us to not do our final task on destruction. + void cancel() { do_ = false; } + + private: + /// Our callable task to do at destruction. + F f_; + /// Whether we should actually do it. + bool do_ = true; + }; +} // namespace detail + +/// Creation free function for final tasks to run on scope exit. Works great +/// when paired with lambdas (particularly with `[&]` reference capture). +/// Use like: +/// `auto f = finally([&]{ dothis(); });` to have `dothis()` called when `f` +/// goes out of scope, no matter how. +template inline detail::FinalTask finally(F &&f) { + /// Perfect forwarding version. + return detail::FinalTask(std::forward(f)); +} + +/// @overload +template inline detail::FinalTask finally(F const &f) { + // Added this overload because GSL had it and GSL is supposed to be best + // practices guidelines... + return detail::FinalTask(f); +} + +} // namespace util + +#endif // INCLUDED_Finally_h_GUID_D925FE58_9C57_448B_C0BB_19A42B3243BA diff --git a/src/external/util-headers/util/FixedLengthStringFunctions.h b/src/external/util-headers/util/FixedLengthStringFunctions.h new file mode 100644 index 000000000..a0d65e108 --- /dev/null +++ b/src/external/util-headers/util/FixedLengthStringFunctions.h @@ -0,0 +1,84 @@ +/** @file + @brief Header providing C++-enhanced versions of the various string + functions that work on fixed-length buffers, inspired by + https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ + + @date 2015 + + This header is maintained as a part of 'util-headers' - you can always + find the latest version online at https://github.com/rpavlik/util-headers + + This GUID can help identify the project: d1dbc94e-e863-49cf-bc08-ab4d9f486613 + + This copy of the header is from the revision that Git calls + 1a8444782d15cb9458052e3d8251c4f5b8e808d5 + + Commit date: "2022-03-11 12:11:32 -0600" + + @author + Sensics, Inc. + +*/ + +// Copyright 2015, Sensics, Inc. +// Copyright 2022, Collabora, Ltd. +// +// SPDX-License-Identifier: BSL-1.0 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef INCLUDED_FixedLengthStringFunctions_h_GUID_34010E53_D3F3_42BD_FB36_6D00EA79C3A9 +#define INCLUDED_FixedLengthStringFunctions_h_GUID_34010E53_D3F3_42BD_FB36_6D00EA79C3A9 + +// Internal Includes +// - none + +// Library/third-party includes +// - none + +// Standard includes +#include +#include +#include + +#ifdef _MSC_VER +#define UTILHEADERS_STR_MSRUNTIME +#else +#include +#ifndef _WIN32 +#include +#endif +#if defined(__KLIBC__) || defined(__BIONIC__) || \ + (!defined(__GLIBC__) && defined(_BSD_SOURCE)) +// strlcpy providers: +// klibc +// bionic +// musl if _GNU_SOURCE or _BSD_SOURCE defined (_BSD_SOURCE defined by default) - +// but not glibc! +#define UTILHEADERS_STR_STRLCPY +#elif defined(__DARWIN_C_LEVEL) && defined(__DARWIN_C_FULL) && \ + (__DARWIN_C_LEVEL >= __DARWIN_C_FULL) +// Also provided in cases on Darwin +#define UTILHEADERS_STR_STRLCPY +#endif +#endif + +namespace util { +/// @brief Copy a string into a char array, guaranteed to null-terminate and not +/// overrun. Does not correctly handle UTF-8 (may truncate in the middle of a +/// codepoint). +template inline void strcpy_safe(char (&dest)[N], const char *src) { +#if defined(UTILHEADERS_STR_STRLCPY) + strlcpy(dest, src, N); +#elif defined(UTILHEADERS_STR_MSRUNTIME) + strncpy_s(dest, src, _TRUNCATE); +#else + strncpy(dest, src, N); + dest[N - 1] = '\0'; +#endif +} +} // namespace util + +#endif // INCLUDED_FixedLengthStringFunctions_h_GUID_34010E53_D3F3_42BD_FB36_6D00EA79C3A9 diff --git a/src/external/util-headers/util/Stride.h b/src/external/util-headers/util/Stride.h new file mode 100644 index 000000000..8326c42a5 --- /dev/null +++ b/src/external/util-headers/util/Stride.h @@ -0,0 +1,87 @@ +/** @file + @brief A class for handling "do this every n times" + + This header is maintained as a part of 'util-headers' - you can always + find the latest version online at https://github.com/rpavlik/util-headers + + This GUID can help identify the project: d1dbc94e-e863-49cf-bc08-ab4d9f486613 + + This copy of the header is from the revision that Git calls + 1a8444782d15cb9458052e3d8251c4f5b8e808d5 + + Commit date: "2022-03-11 12:11:32 -0600" + + @date + 2009-2010 + + @author + Ryan Pavlik + and + http://academic.cleardefinition.com/ + Iowa State University Virtual Reality Applications Center + Human-Computer Interaction Graduate Program +*/ + +// Copyright 2009-2010, Iowa State University. +// +// SPDX-License-Identifier: BSL-1.0 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#pragma once +#ifndef INCLUDED_Stride_h_GUID_eaa50b9c_e526_4656_89dc_99008d82447d +#define INCLUDED_Stride_h_GUID_eaa50b9c_e526_4656_89dc_99008d82447d + +// Local includes +// - none + +// Library includes +// - none + +// Standard includes +// - none + +namespace util { + +/// @addtogroup Other Other Utility Classes +/// @{ + +/// Handle the task of "do this every n times" in an easy way. + class Stride { + public: + Stride(const unsigned int n) : + _stride(n), + _step(0) { } + + void advance() { + _step = (_step + 1) % _stride; + } + + Stride operator++() { + Stride temp = *this; + advance(); + return temp; + } + + Stride & operator++(int) { + advance(); + return *this; + } + + operator bool() const { + return _step == 0; + } + + private: + unsigned int _stride; + unsigned int _step; + }; + +/// @} + +} // end of util namespace + +#endif // INCLUDED_Stride_h_GUID_eaa50b9c_e526_4656_89dc_99008d82447d +