some docs
This commit is contained in:
parent
55819ebfe0
commit
c9ea3821b6
7 changed files with 342 additions and 244 deletions
|
@ -201,7 +201,11 @@ template <typename... Args>
|
|||
fn LogImpl(const LogLevel level, const std::source_location& loc, std::format_string<Args...> fmt, Args&&... args) {
|
||||
using namespace std::chrono;
|
||||
using namespace term;
|
||||
#ifdef _WIN32
|
||||
using enum term::Color;
|
||||
#else
|
||||
using enum Color;
|
||||
#endif
|
||||
|
||||
const auto [color, levelStr] = [&] {
|
||||
switch (level) {
|
||||
|
@ -213,32 +217,66 @@ fn LogImpl(const LogLevel level, const std::source_location& loc, std::format_st
|
|||
}
|
||||
}();
|
||||
|
||||
const String filename = std::filesystem::path(loc.file_name()).lexically_normal().string();
|
||||
|
||||
Print(BrightWhite, "[{:%X}] ", zoned_time { current_zone(), std::chrono::floor<seconds>(system_clock::now()) });
|
||||
Print(Emphasis::Bold | color, "{} ", levelStr);
|
||||
Print(fmt, std::forward<Args>(args)...);
|
||||
|
||||
#ifndef NDEBUG
|
||||
Print(BrightWhite, "\n{:>14} ", "╰──");
|
||||
Print(Emphasis::Italic | BrightWhite, "{}:{}", filename, loc.line());
|
||||
Print(
|
||||
Emphasis::Italic | BrightWhite,
|
||||
"{}:{}",
|
||||
std::filesystem::path(loc.file_name()).lexically_normal().string(),
|
||||
loc.line()
|
||||
);
|
||||
#endif
|
||||
|
||||
Print("\n");
|
||||
}
|
||||
|
||||
// Suppress unused macro warnings in Clang
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-macros"
|
||||
#endif
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define DEBUG_LOG(...) static_cast<void>(0)
|
||||
#else
|
||||
/**
|
||||
* @def DEBUG_LOG
|
||||
* @brief Logs a message at the DEBUG level.
|
||||
* @details Only active in non-release builds (when NDEBUG is not defined).
|
||||
* Includes timestamp, level, message, and source location.
|
||||
* @param ... Format string and arguments for the log message.
|
||||
*/
|
||||
#define DEBUG_LOG(...) LogImpl(LogLevel::DEBUG, std::source_location::current(), __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def INFO_LOG(...)
|
||||
* @brief Logs a message at the INFO level.
|
||||
* @details Includes timestamp, level, message, and source location (in debug builds).
|
||||
* @param ... Format string and arguments for the log message.
|
||||
*/
|
||||
#define INFO_LOG(...) LogImpl(LogLevel::INFO, std::source_location::current(), __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* @def WARN_LOG(...)
|
||||
* @brief Logs a message at the WARN level.
|
||||
* @details Includes timestamp, level, message, and source location (in debug builds).
|
||||
* @param ... Format string and arguments for the log message.
|
||||
*/
|
||||
#define WARN_LOG(...) LogImpl(LogLevel::WARN, std::source_location::current(), __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* @def ERROR_LOG(...)
|
||||
* @brief Logs a message at the ERROR level.
|
||||
* @details Includes timestamp, level, message, and source location (in debug builds).
|
||||
* @param ... Format string and arguments for the log message.
|
||||
*/
|
||||
#define ERROR_LOG(...) LogImpl(LogLevel::ERROR, std::source_location::current(), __VA_ARGS__)
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
|
308
src/util/types.h
308
src/util/types.h
|
@ -1,253 +1,170 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <expected>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <array> // std::array alias (Array)
|
||||
#include <cstdlib> // std::getenv, std::free
|
||||
#include <expected> // std::expected alias (Result)
|
||||
#include <map> // std::map alias (Map)
|
||||
#include <memory> // std::shared_ptr and std::unique_ptr aliases (SharedPointer, UniquePointer)
|
||||
#include <optional> // std::optional alias (Option)
|
||||
#include <string> // std::string and std::string_view aliases (String, StringView)
|
||||
#include <utility> // std::pair alias (Pair)
|
||||
#include <variant> // std::variant alias (NowPlayingError)
|
||||
#include <vector> // std::vector alias (Vec)
|
||||
|
||||
#ifdef _WIN32
|
||||
// ReSharper disable once CppUnusedIncludeDirective
|
||||
#include <guiddef.h>
|
||||
#include <variant>
|
||||
#include <winrt/base.h>
|
||||
#else
|
||||
#include <variant>
|
||||
#include <winrt/base.h> // winrt::hresult_error (WindowsError)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @typedef u8
|
||||
* @brief Represents an 8-bit unsigned integer.
|
||||
*
|
||||
* This type alias is used for 8-bit unsigned integers, ranging from 0 to 255.
|
||||
* It is based on the std::uint8_t type.
|
||||
*/
|
||||
using u8 = std::uint8_t;
|
||||
//----------------------------------------------------------------//
|
||||
// Integer Type Aliases //
|
||||
// Provides concise names for standard fixed-width integer types. //
|
||||
//----------------------------------------------------------------//
|
||||
|
||||
using u8 = std::uint8_t; ///< 8-bit unsigned integer.
|
||||
using u16 = std::uint16_t; ///< 16-bit unsigned integer.
|
||||
using u32 = std::uint32_t; ///< 32-bit unsigned integer.
|
||||
using u64 = std::uint64_t; ///< 64-bit unsigned integer.
|
||||
|
||||
using i8 = std::int8_t; ///< 8-bit signed integer.
|
||||
using i16 = std::int16_t; ///< 16-bit signed integer.
|
||||
using i32 = std::int32_t; ///< 32-bit signed integer.
|
||||
using i64 = std::int64_t; ///< 64-bit signed integer.
|
||||
|
||||
//-----------------------------------------------------------//
|
||||
// Floating-Point Type Aliases //
|
||||
// Provides concise names for standard floating-point types. //
|
||||
//-----------------------------------------------------------//
|
||||
|
||||
using f32 = std::float_t; ///< 32-bit floating-point number.
|
||||
using f64 = std::double_t; ///< 64-bit floating-point number.
|
||||
|
||||
//-------------------------------------------------//
|
||||
// Size Type Aliases //
|
||||
// Provides concise names for standard size types. //
|
||||
//-------------------------------------------------//
|
||||
|
||||
using usize = std::size_t; ///< Unsigned size type (result of sizeof).
|
||||
using isize = std::ptrdiff_t; ///< Signed size type (result of pointer subtraction).
|
||||
|
||||
//---------------------------------------------------//
|
||||
// String Type Aliases //
|
||||
// Provides concise names for standard string types. //
|
||||
//---------------------------------------------------//
|
||||
|
||||
using String = std::string; ///< Owning, mutable string.
|
||||
using StringView = std::string_view; ///< Non-owning view of a string.
|
||||
using CStr = const char*; ///< Pointer to a null-terminated C-style string.
|
||||
|
||||
//----------------------------------------------------//
|
||||
// Standard Library Type Aliases //
|
||||
// Provides concise names for standard library types. //
|
||||
//----------------------------------------------------//
|
||||
|
||||
using Exception = std::exception; ///< Standard exception type.
|
||||
|
||||
/**
|
||||
* @typedef u16
|
||||
* @brief Represents a 16-bit unsigned integer.
|
||||
*
|
||||
* This type alias is used for 16-bit unsigned integers, ranging from 0 to 65,535.
|
||||
* It is based on the std::uint16_t type.
|
||||
*/
|
||||
using u16 = std::uint16_t;
|
||||
|
||||
/**
|
||||
* @typedef u32
|
||||
* @brief Represents a 32-bit unsigned integer.
|
||||
*
|
||||
* This type alias is used for 32-bit unsigned integers, ranging from 0 to 4,294,967,295.
|
||||
* It is based on the std::uint32_t type.
|
||||
*/
|
||||
using u32 = std::uint32_t;
|
||||
|
||||
/**
|
||||
* @typedef u64
|
||||
* @brief Represents a 64-bit unsigned integer.
|
||||
*
|
||||
* This type alias is used for 64-bit unsigned integers, ranging from 0 to
|
||||
* 18,446,744,073,709,551,615. It is based on the std::uint64_t type.
|
||||
*/
|
||||
using u64 = std::uint64_t;
|
||||
|
||||
// Type Aliases for Signed Integers
|
||||
|
||||
/**
|
||||
* @typedef i8
|
||||
* @brief Represents an 8-bit signed integer.
|
||||
*
|
||||
* This type alias is used for 8-bit signed integers, ranging from -128 to 127.
|
||||
* It is based on the std::int8_t type.
|
||||
*/
|
||||
using i8 = std::int8_t;
|
||||
|
||||
/**
|
||||
* @typedef i16
|
||||
* @brief Represents a 16-bit signed integer.
|
||||
*
|
||||
* This type alias is used for 16-bit signed integers, ranging from -32,768 to 32,767.
|
||||
* It is based on the std::int16_t type.
|
||||
*/
|
||||
using i16 = std::int16_t;
|
||||
|
||||
/**
|
||||
* @typedef i32
|
||||
* @brief Represents a 32-bit signed integer.
|
||||
*
|
||||
* This type alias is used for 32-bit signed integers, ranging from -2,147,483,648 to 2,147,483,647.
|
||||
* It is based on the std::int32_t type.
|
||||
*/
|
||||
using i32 = std::int32_t;
|
||||
|
||||
/**
|
||||
* @typedef i64
|
||||
* @brief Represents a 64-bit signed integer.
|
||||
*
|
||||
* This type alias is used for 64-bit signed integers, ranging from -9,223,372,036,854,775,808 to
|
||||
* 9,223,372,036,854,775,807. It is based on the std::int64_t type.
|
||||
*/
|
||||
using i64 = std::int64_t;
|
||||
|
||||
// Type Aliases for Floating-Point Numbers
|
||||
|
||||
/**
|
||||
* @typedef f32
|
||||
* @brief Represents a 32-bit floating-point number.
|
||||
*
|
||||
* This type alias is used for 32-bit floating-point numbers, which follow the IEEE 754 standard.
|
||||
* It is based on the float type.
|
||||
*/
|
||||
using f32 = float;
|
||||
|
||||
/**
|
||||
* @typedef f64
|
||||
* @brief Represents a 64-bit floating-point number.
|
||||
*
|
||||
* This type alias is used for 64-bit floating-point numbers, which follow the IEEE 754 standard.
|
||||
* It is based on the double type.
|
||||
*/
|
||||
using f64 = double;
|
||||
|
||||
// Type Aliases for Size Types
|
||||
|
||||
/**
|
||||
* @typedef usize
|
||||
* @brief Represents an unsigned size type.
|
||||
*
|
||||
* This type alias is used for representing the size of objects in bytes.
|
||||
* It is based on the std::size_t type, which is the result type of the sizeof operator.
|
||||
*/
|
||||
using usize = std::size_t;
|
||||
|
||||
/**
|
||||
* @typedef isize
|
||||
* @brief Represents a signed size type.
|
||||
*
|
||||
* This type alias is used for representing pointer differences.
|
||||
* It is based on the std::ptrdiff_t type, which is the signed integer type returned when
|
||||
* subtracting two pointers.
|
||||
*/
|
||||
using isize = std::ptrdiff_t;
|
||||
|
||||
/**
|
||||
* @typedef String
|
||||
* @brief Represents a string.
|
||||
*/
|
||||
using String = std::string;
|
||||
|
||||
/**
|
||||
* @typedef StringView
|
||||
* @brief Represents a string view.
|
||||
*
|
||||
* This type alias is used for non-owning views of strings, allowing for efficient string manipulation
|
||||
* without copying the underlying data.
|
||||
*/
|
||||
using StringView = std::string_view;
|
||||
|
||||
/**
|
||||
* @typedef Exception
|
||||
* @brief Represents a generic exception type.
|
||||
*/
|
||||
using Exception = std::exception;
|
||||
|
||||
/**
|
||||
* @typedef Expected
|
||||
* @brief Represents an expected value or an error.
|
||||
* @typedef Result
|
||||
* @brief Alias for std::expected<Tp, Er>. Represents a value that can either be
|
||||
* a success value of type Tp or an error value of type Er.
|
||||
* @tparam Tp The type of the success value.
|
||||
* @tparam Er The type of the error value.
|
||||
*/
|
||||
template <typename Tp, typename Er>
|
||||
using Result = std::expected<Tp, Er>;
|
||||
|
||||
/**
|
||||
* @typedef Unexpected
|
||||
* @brief Represents an unexpected error.
|
||||
* @typedef Err
|
||||
* @brief Alias for std::unexpected<Er>. Used to construct a Result in an error state.
|
||||
* @tparam Er The type of the error value.
|
||||
*/
|
||||
template <typename Er>
|
||||
using Err = std::unexpected<Er>;
|
||||
|
||||
/**
|
||||
* @typedef Optional
|
||||
* @brief Represents an optional value.
|
||||
* @typedef Option
|
||||
* @brief Alias for std::optional<Tp>. Represents a value that may or may not be present.
|
||||
* @tparam Tp The type of the potential value.
|
||||
*/
|
||||
template <typename Tp>
|
||||
using Option = std::optional<Tp>;
|
||||
|
||||
/**
|
||||
* @typedef Array
|
||||
* @brief Represents a fixed-size array.
|
||||
* @brief Alias for std::array<Tp, sz>. Represents a fixed-size array.
|
||||
* @tparam Tp The element type.
|
||||
* @tparam sz The size of the array.
|
||||
*/
|
||||
template <typename Tp, usize sz>
|
||||
using Array = std::array<Tp, sz>;
|
||||
|
||||
/**
|
||||
* @typedef Vec
|
||||
* @brief Represents a dynamic array (vector).
|
||||
* @brief Alias for std::vector<Tp>. Represents a dynamic-size array (vector).
|
||||
* @tparam Tp The element type.
|
||||
*/
|
||||
template <typename Tp>
|
||||
using Vec = std::vector<Tp>;
|
||||
|
||||
/**
|
||||
* @typedef Pair
|
||||
* @brief Represents a pair of values.
|
||||
* @brief Alias for std::pair<T1, T2>. Represents a pair of values.
|
||||
* @tparam T1 The type of the first element.
|
||||
* @tparam T2 The type of the second element.
|
||||
*/
|
||||
template <typename T1, typename T2>
|
||||
using Pair = std::pair<T1, T2>;
|
||||
|
||||
/**
|
||||
* @typedef Map
|
||||
* @brief Represents a map (dictionary) of key-value pairs.
|
||||
* @brief Alias for std::map<Key, Val>. Represents an ordered map (dictionary).
|
||||
* @tparam Key The key type.
|
||||
* @tparam Val The value type.
|
||||
*/
|
||||
template <typename Key, typename Val>
|
||||
using Map = std::map<Key, Val>;
|
||||
|
||||
/**
|
||||
* @typedef SharedPointer
|
||||
* @brief Represents a shared pointer.
|
||||
*
|
||||
* This type alias is used for shared ownership of dynamically allocated objects.
|
||||
* @brief Alias for std::shared_ptr<Tp>. Manages shared ownership of a dynamically allocated object.
|
||||
* @tparam Tp The type of the managed object.
|
||||
*/
|
||||
template <typename Tp>
|
||||
using SharedPointer = std::shared_ptr<Tp>;
|
||||
|
||||
/**
|
||||
* @typedef UniquePointer
|
||||
* @brief Represents a unique pointer.
|
||||
*
|
||||
* This type alias is used for unique ownership of dynamically allocated objects.
|
||||
* @brief Alias for std::unique_ptr<Tp, Dp>. Manages unique ownership of a dynamically allocated object.
|
||||
* @tparam Tp The type of the managed object.
|
||||
* @tparam Dp The deleter type (defaults to std::default_delete<Tp>).
|
||||
*/
|
||||
template <typename Tp, typename Dp>
|
||||
template <typename Tp, typename Dp = std::default_delete<Tp>>
|
||||
using UniquePointer = std::unique_ptr<Tp, Dp>;
|
||||
|
||||
/**
|
||||
* @typedef CStr
|
||||
* @brief Represents a C string (const char*).
|
||||
*
|
||||
* This type alias is used for C-style strings, which are null-terminated arrays of characters.
|
||||
*/
|
||||
using CStr = const char*;
|
||||
//--------------------------------------------------------//
|
||||
// Application-Specific Type Aliases //
|
||||
// Provides concise names for application-specific types. //
|
||||
//--------------------------------------------------------//
|
||||
|
||||
/**
|
||||
* @enum NowPlayingCode
|
||||
* @brief Represents error codes for Now Playing functionality.
|
||||
* @brief Error codes specific to the Now Playing feature.
|
||||
*/
|
||||
enum class NowPlayingCode : u8 {
|
||||
NoPlayers,
|
||||
NoActivePlayer,
|
||||
NoPlayers, ///< No media players were found (e.g., no MPRIS services on Linux).
|
||||
NoActivePlayer, ///< Players were found, but none are currently active or playing.
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* @typedef WindowsError
|
||||
* @brief Represents a Windows-specific error.
|
||||
*/
|
||||
using WindowsError = winrt::hresult_error;
|
||||
using WindowsError = winrt::hresult_error; ///< Alias for WinRT HRESULT error type.
|
||||
#endif
|
||||
|
||||
// Unified error type
|
||||
/**
|
||||
* @typedef NowPlayingError
|
||||
* @brief Represents the possible errors returned by "Now Playing" functions.
|
||||
* It's a variant that can hold either a generic NowPlayingCode,
|
||||
* a platform-specific error (WindowsError on Windows, String on others),
|
||||
* or potentially other error types if extended.
|
||||
*/
|
||||
using NowPlayingError = std::variant<
|
||||
NowPlayingCode,
|
||||
#ifdef _WIN32
|
||||
|
@ -257,30 +174,45 @@ using NowPlayingError = std::variant<
|
|||
#endif
|
||||
>;
|
||||
|
||||
enum class EnvError : u8 { NotFound, AccessError };
|
||||
/**
|
||||
* @enum EnvError
|
||||
* @brief Error codes for environment variable retrieval.
|
||||
*/
|
||||
enum class EnvError : u8 {
|
||||
NotFound, ///< Environment variable not found.
|
||||
AccessError, ///< Access error when trying to retrieve the variable.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Safely retrieves an environment variable.
|
||||
* @param name The name of the environment variable to retrieve.
|
||||
* @return A Result containing the value of the environment variable as a String,
|
||||
* or an EnvError if an error occurred.
|
||||
*/
|
||||
inline auto GetEnv(CStr name) -> Result<String, EnvError> {
|
||||
#ifdef _WIN32
|
||||
char* rawPtr = nullptr;
|
||||
usize bufferSize = 0;
|
||||
|
||||
// Use _dupenv_s to safely retrieve environment variables on Windows
|
||||
const i32 err = _dupenv_s(&rawPtr, &bufferSize, name);
|
||||
|
||||
const UniquePointer<char, decltype(&free)> ptrManager(rawPtr, free);
|
||||
|
||||
if (err != 0)
|
||||
return Err(EnvError::AccessError);
|
||||
return Err(EnvError::AccessError); // Error retrieving environment variable
|
||||
|
||||
if (!ptrManager)
|
||||
return Err(EnvError::NotFound);
|
||||
return Err(EnvError::NotFound); // Environment variable not found
|
||||
|
||||
return ptrManager.get();
|
||||
#else
|
||||
// Use std::getenv to retrieve environment variables on POSIX systems
|
||||
const CStr value = std::getenv(name);
|
||||
|
||||
if (!value)
|
||||
return Err(EnvError::NotFound);
|
||||
return Err(EnvError::NotFound); // Environment variable not found
|
||||
|
||||
return String(value);
|
||||
return value;
|
||||
#endif
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue