The open source OpenXR runtime

external: Add some headers from my util-headers project.

authored by

Ryan Pavlik and committed by
Jakob Bornecrantz
6bb976e4 12bb18a3

+294
+6
src/external/CMakeLists.txt
··· 24 24 add_library(xrt-external-flexkalman INTERFACE) 25 25 target_include_directories(xrt-external-flexkalman INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/flexkalman) 26 26 27 + # FlexKalman 28 + add_library(xrt-external-util-headers INTERFACE) 29 + target_include_directories( 30 + xrt-external-util-headers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/util-headers 31 + ) 32 + 27 33 # Glad 28 34 add_library(xrt-external-glad INTERFACE) 29 35 target_include_directories(xrt-external-glad INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/glad/include)
+117
src/external/util-headers/util/Finally.h
··· 1 + /** @file 2 + @brief Header declaring a C++11 `finally` or "scope-guard" construct. 3 + 4 + Inspirations are many - Alexandrescu's original and C++11 ScopeGuard, and 5 + the Guideline Support Library's (GSL) `final_act`/`finally` 6 + https://github.com/Microsoft/GSL/blob/0cf947db7760bf5756e4cb0d47c72a257ed527c5/include/gsl_util.h 7 + come to mind, but this has been written and re-written numerous times 8 + since its introduction into the C++ global consciousness, and this 9 + implementation was written independently after I couldn't find a 10 + previous independent implementation I had written a few weeks earlier in 11 + an implementation file. -- Ryan Pavlik 12 + 13 + See UniqueDestructionActionWrapper for a "generalized" (in some sense) 14 + version of this. 15 + 16 + Originally written for use in OSVR for Sensics <http://sensics.com/osvr>, 17 + relicensed to BSL 1.0 with permission. 18 + 19 + This header is maintained as a part of 'util-headers' - you can always 20 + find the latest version online at https://github.com/rpavlik/util-headers 21 + 22 + This GUID can help identify the project: d1dbc94e-e863-49cf-bc08-ab4d9f486613 23 + 24 + This copy of the header is from the revision that Git calls 25 + 1a8444782d15cb9458052e3d8251c4f5b8e808d5 26 + 27 + Commit date: "2022-03-11 12:11:32 -0600" 28 + 29 + @date 2016 30 + 31 + @author 32 + Ryan Pavlik 33 + <ryan.pavlik@gmail.com> 34 + <http://ryanpavlik.com> 35 + */ 36 + 37 + // Copyright 2016, Sensics, Inc. 38 + // 39 + // SPDX-License-Identifier: BSL-1.0 40 + // 41 + // Distributed under the Boost Software License, Version 1.0. 42 + // (See accompanying file LICENSE_1_0.txt or copy at 43 + // http://www.boost.org/LICENSE_1_0.txt) 44 + 45 + #ifndef INCLUDED_Finally_h_GUID_D925FE58_9C57_448B_C0BB_19A42B3243BA 46 + #define INCLUDED_Finally_h_GUID_D925FE58_9C57_448B_C0BB_19A42B3243BA 47 + 48 + // Internal Includes 49 + // - none 50 + 51 + // Library/third-party includes 52 + // - none 53 + 54 + // Standard includes 55 + #include <utility> 56 + 57 + namespace util { 58 + namespace detail { 59 + /// Allows you to run a callable something at the end of a scope. 60 + /// 61 + /// The class that provides the scope-guard behavior. Often not referred to 62 + /// by name because auto is useful here, and often not created by a direct 63 + /// constructor call because of the finally() convenience functions combined 64 + /// with lambdas. 65 + template <typename F> class FinalTask { 66 + public: 67 + /// Explicit constructor from something callable. 68 + explicit FinalTask(F f) : f_(std::move(f)) {} 69 + 70 + /// Move constructor - cancels the moved-from task. 71 + FinalTask(FinalTask &&other) : f_(std::move(other.f_)), do_(other.do_) { 72 + other.cancel(); 73 + } 74 + 75 + /// non-copyable 76 + FinalTask(FinalTask const &) = delete; 77 + 78 + /// non-assignable 79 + FinalTask &operator=(FinalTask const &) = delete; 80 + 81 + /// Destructor - if we haven't been cancelled, do our callable thing. 82 + ~FinalTask() { 83 + if (do_) { 84 + f_(); 85 + } 86 + } 87 + /// Cancel causes us to not do our final task on destruction. 88 + void cancel() { do_ = false; } 89 + 90 + private: 91 + /// Our callable task to do at destruction. 92 + F f_; 93 + /// Whether we should actually do it. 94 + bool do_ = true; 95 + }; 96 + } // namespace detail 97 + 98 + /// Creation free function for final tasks to run on scope exit. Works great 99 + /// when paired with lambdas (particularly with `[&]` reference capture). 100 + /// Use like: 101 + /// `auto f = finally([&]{ dothis(); });` to have `dothis()` called when `f` 102 + /// goes out of scope, no matter how. 103 + template <typename F> inline detail::FinalTask<F> finally(F &&f) { 104 + /// Perfect forwarding version. 105 + return detail::FinalTask<F>(std::forward<F>(f)); 106 + } 107 + 108 + /// @overload 109 + template <typename F> inline detail::FinalTask<F> finally(F const &f) { 110 + // Added this overload because GSL had it and GSL is supposed to be best 111 + // practices guidelines... 112 + return detail::FinalTask<F>(f); 113 + } 114 + 115 + } // namespace util 116 + 117 + #endif // INCLUDED_Finally_h_GUID_D925FE58_9C57_448B_C0BB_19A42B3243BA
+84
src/external/util-headers/util/FixedLengthStringFunctions.h
··· 1 + /** @file 2 + @brief Header providing C++-enhanced versions of the various string 3 + functions that work on fixed-length buffers, inspired by 4 + https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ 5 + 6 + @date 2015 7 + 8 + This header is maintained as a part of 'util-headers' - you can always 9 + find the latest version online at https://github.com/rpavlik/util-headers 10 + 11 + This GUID can help identify the project: d1dbc94e-e863-49cf-bc08-ab4d9f486613 12 + 13 + This copy of the header is from the revision that Git calls 14 + 1a8444782d15cb9458052e3d8251c4f5b8e808d5 15 + 16 + Commit date: "2022-03-11 12:11:32 -0600" 17 + 18 + @author 19 + Sensics, Inc. 20 + <http://sensics.com/osvr> 21 + */ 22 + 23 + // Copyright 2015, Sensics, Inc. 24 + // Copyright 2022, Collabora, Ltd. 25 + // 26 + // SPDX-License-Identifier: BSL-1.0 27 + // 28 + // Distributed under the Boost Software License, Version 1.0. 29 + // (See accompanying file LICENSE_1_0.txt or copy at 30 + // http://www.boost.org/LICENSE_1_0.txt) 31 + 32 + #ifndef INCLUDED_FixedLengthStringFunctions_h_GUID_34010E53_D3F3_42BD_FB36_6D00EA79C3A9 33 + #define INCLUDED_FixedLengthStringFunctions_h_GUID_34010E53_D3F3_42BD_FB36_6D00EA79C3A9 34 + 35 + // Internal Includes 36 + // - none 37 + 38 + // Library/third-party includes 39 + // - none 40 + 41 + // Standard includes 42 + #include <stddef.h> 43 + #include <stdexcept> 44 + #include <string.h> 45 + 46 + #ifdef _MSC_VER 47 + #define UTILHEADERS_STR_MSRUNTIME 48 + #else 49 + #include <limits.h> 50 + #ifndef _WIN32 51 + #include <sys/types.h> 52 + #endif 53 + #if defined(__KLIBC__) || defined(__BIONIC__) || \ 54 + (!defined(__GLIBC__) && defined(_BSD_SOURCE)) 55 + // strlcpy providers: 56 + // klibc 57 + // bionic 58 + // musl if _GNU_SOURCE or _BSD_SOURCE defined (_BSD_SOURCE defined by default) - 59 + // but not glibc! 60 + #define UTILHEADERS_STR_STRLCPY 61 + #elif defined(__DARWIN_C_LEVEL) && defined(__DARWIN_C_FULL) && \ 62 + (__DARWIN_C_LEVEL >= __DARWIN_C_FULL) 63 + // Also provided in cases on Darwin 64 + #define UTILHEADERS_STR_STRLCPY 65 + #endif 66 + #endif 67 + 68 + namespace util { 69 + /// @brief Copy a string into a char array, guaranteed to null-terminate and not 70 + /// overrun. Does not correctly handle UTF-8 (may truncate in the middle of a 71 + /// codepoint). 72 + template <size_t N> inline void strcpy_safe(char (&dest)[N], const char *src) { 73 + #if defined(UTILHEADERS_STR_STRLCPY) 74 + strlcpy(dest, src, N); 75 + #elif defined(UTILHEADERS_STR_MSRUNTIME) 76 + strncpy_s(dest, src, _TRUNCATE); 77 + #else 78 + strncpy(dest, src, N); 79 + dest[N - 1] = '\0'; 80 + #endif 81 + } 82 + } // namespace util 83 + 84 + #endif // INCLUDED_FixedLengthStringFunctions_h_GUID_34010E53_D3F3_42BD_FB36_6D00EA79C3A9
+87
src/external/util-headers/util/Stride.h
··· 1 + /** @file 2 + @brief A class for handling "do this every n times" 3 + 4 + This header is maintained as a part of 'util-headers' - you can always 5 + find the latest version online at https://github.com/rpavlik/util-headers 6 + 7 + This GUID can help identify the project: d1dbc94e-e863-49cf-bc08-ab4d9f486613 8 + 9 + This copy of the header is from the revision that Git calls 10 + 1a8444782d15cb9458052e3d8251c4f5b8e808d5 11 + 12 + Commit date: "2022-03-11 12:11:32 -0600" 13 + 14 + @date 15 + 2009-2010 16 + 17 + @author 18 + Ryan Pavlik 19 + <rpavlik@iastate.edu> and <abiryan@ryand.net> 20 + http://academic.cleardefinition.com/ 21 + Iowa State University Virtual Reality Applications Center 22 + Human-Computer Interaction Graduate Program 23 + */ 24 + 25 + // Copyright 2009-2010, Iowa State University. 26 + // 27 + // SPDX-License-Identifier: BSL-1.0 28 + // 29 + // Distributed under the Boost Software License, Version 1.0. 30 + // (See accompanying file LICENSE_1_0.txt or copy at 31 + // http://www.boost.org/LICENSE_1_0.txt) 32 + 33 + #pragma once 34 + #ifndef INCLUDED_Stride_h_GUID_eaa50b9c_e526_4656_89dc_99008d82447d 35 + #define INCLUDED_Stride_h_GUID_eaa50b9c_e526_4656_89dc_99008d82447d 36 + 37 + // Local includes 38 + // - none 39 + 40 + // Library includes 41 + // - none 42 + 43 + // Standard includes 44 + // - none 45 + 46 + namespace util { 47 + 48 + /// @addtogroup Other Other Utility Classes 49 + /// @{ 50 + 51 + /// Handle the task of "do this every n times" in an easy way. 52 + class Stride { 53 + public: 54 + Stride(const unsigned int n) : 55 + _stride(n), 56 + _step(0) { } 57 + 58 + void advance() { 59 + _step = (_step + 1) % _stride; 60 + } 61 + 62 + Stride operator++() { 63 + Stride temp = *this; 64 + advance(); 65 + return temp; 66 + } 67 + 68 + Stride & operator++(int) { 69 + advance(); 70 + return *this; 71 + } 72 + 73 + operator bool() const { 74 + return _step == 0; 75 + } 76 + 77 + private: 78 + unsigned int _stride; 79 + unsigned int _step; 80 + }; 81 + 82 + /// @} 83 + 84 + } // end of util namespace 85 + 86 + #endif // INCLUDED_Stride_h_GUID_eaa50b9c_e526_4656_89dc_99008d82447d 87 +