diff --git a/.gitignore b/.gitignore index 2df50c1..1d48d5b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # -# AperLambda ignore file +# LambdAurora's ignore file # -# v0.8 +# v0.12 # JetBrains .idea/ @@ -41,6 +41,10 @@ node_modules/ logs/ # Languages +## Java +classes/ +## Python +__pycache__/ ## Rust **/*.rs.bk @@ -54,10 +58,11 @@ desktop.ini *.dylib *.lib lib*.a +*.png~ # Common bin/ build/ dist/ run/ -target/ \ No newline at end of file +target/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bf3b82..29c7c13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,18 +12,18 @@ set(CMAKE_CXX_STANDARD 17) # Options option(LAMBDACOMMON_BUILD_STATIC "Build static libraries" OFF) option(LAMBDACOMMON_INSTALL "Generate installation target" ON) -option(LAMBDACOMMON_BUILD_C_WRAPPER "Build the λcommon C wrapper" ON) +option(LAMBDACOMMON_BUILD_C_WRAPPER "Build the λcommon C wrapper" OFF) option(LAMBDACOMMON_BUILD_TESTS "Build the λcommon test programs" ON) # Version set(LAMBDACOMMON_VERSION_MAJOR 1) set(LAMBDACOMMON_VERSION_MINOR 10) set(LAMBDACOMMON_VERSION_PATCH 0) -set(LAMBDACOMMON_VERSION_TYPE "indev") +set(LAMBDACOMMON_VERSION_TYPE "Release") # Generate compile flags. generate_flags(LAMBDACOMMON_COMPILE_FLAGS "native" 2 true) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${LAMBDACOMMON_COMPILE_FLAGS}") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${LAMBDACOMMON_COMPILE_FLAGS} -Wno-attributes") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework CoreFoundation -framework ApplicationServices -Wno-unused-command-line-argument") diff --git a/README.md b/README.md index f8f32a0..c936f30 100644 --- a/README.md +++ b/README.md @@ -129,4 +129,4 @@ Build the sources with CMake and make and install with `make install`, and keep ## Use in CMake -Use `Findlambdacommon.cmake` in [LambdaCMakeModules](https://github.com/LambdAurora/lcmm.git) to find λcommon on your computer. \ No newline at end of file +Use `Findlambdacommon.cmake` in [LambdaCMakeModules](https://github.com/LambdAurora/lcmm.git) to find λcommon on your computer. diff --git a/build.sh b/build.sh index ceb2ace..6dfb794 100644 --- a/build.sh +++ b/build.sh @@ -11,7 +11,7 @@ echo "cd to build directory" mkdir -p build/ cd build/ echo "Building project..." -cmake .. +cmake -DLAMBDACOMMON_BUILD_C_WRAPPER=ON .. if [ $? -ne 0 ]; then echo "${RED}Error: CMake doesn't exit with success! Cleaning...${NC}" cd .. diff --git a/c_wrapper/CMakeLists.txt b/c_wrapper/CMakeLists.txt index a76120a..0e2d3ec 100644 --- a/c_wrapper/CMakeLists.txt +++ b/c_wrapper/CMakeLists.txt @@ -32,7 +32,7 @@ endfunction() set(C_HEADERS_GRAPHICS include/clambdacommon/graphics/color.h) set(C_HEADERS_MATHS include/clambdacommon/maths/geometry/point.h include/clambdacommon/maths/geometry/vector.h) set(C_HEADERS_SYSTEM include/clambdacommon/system/os.h include/clambdacommon/system/terminal.h include/clambdacommon/system/system.h include/clambdacommon/system/time.h) -set(C_HEADERS_BASE include/clambdacommon/clambdacommon.h include/clambdacommon/lstring.h include/clambdacommon/maths.h include/clambdacommon/resources.h) +set(C_HEADERS_BASE include/clambdacommon/clambdacommon.h include/clambdacommon/lstring.h include/clambdacommon/maths.h include/clambdacommon/resources.h include/clambdacommon/types.h) set(C_HEADERS_FILES ${C_HEADERS_GRAPHICS} ${C_HEADERS_MATHS} ${C_HEADERS_SYSTEM} ${C_HEADERS_BASE}) # This is the C sources files. set(C_SOURCES_GRAPHICS src/graphics/color.cpp) @@ -40,32 +40,33 @@ set(C_SOURCES_SYSTEM src/system/os.cpp src/system/terminal.cpp src/system/system set(C_SOURCES_BASE src/clambdacommon.cpp src/lstring.cpp src/maths.cpp src/resources.cpp) set(C_SOURCES_FILES ${C_SOURCES_GRAPHICS} ${C_SOURCES_SYSTEM} ${C_SOURCES_BASE}) -generate_template_wrapper(point int8_t) -generate_template_wrapper(point uint8_t) -generate_template_wrapper(point int16_t) -generate_template_wrapper(point uint16_t) -generate_template_wrapper(point int32_t) -generate_template_wrapper(point uint32_t) -generate_template_wrapper(point int64_t) -generate_template_wrapper(point uint64_t) -generate_template_wrapper(point float) -generate_template_wrapper(point double) -generate_template_wrapper(vector int8_t) -generate_template_wrapper(vector uint8_t) -generate_template_wrapper(vector int16_t) -generate_template_wrapper(vector uint16_t) -generate_template_wrapper(vector int32_t) -generate_template_wrapper(vector uint32_t) -generate_template_wrapper(vector int64_t) -generate_template_wrapper(vector uint64_t) -generate_template_wrapper(vector float) -generate_template_wrapper(vector double) +generate_template_wrapper(point i8) +generate_template_wrapper(point u8) +generate_template_wrapper(point i16) +generate_template_wrapper(point u16) +generate_template_wrapper(point i32) +generate_template_wrapper(point u32) +generate_template_wrapper(point i64) +generate_template_wrapper(point u64) +generate_template_wrapper(point f32) +generate_template_wrapper(point f64) +generate_template_wrapper(vector i8) +generate_template_wrapper(vector u8) +generate_template_wrapper(vector i16) +generate_template_wrapper(vector u16) +generate_template_wrapper(vector i32) +generate_template_wrapper(vector u32) +generate_template_wrapper(vector i64) +generate_template_wrapper(vector u64) +generate_template_wrapper(vector f32) +generate_template_wrapper(vector f64) # Now build the library # Build the shared library add_library(clambdacommon ${C_HEADERS_FILES} ${C_SOURCES_FILES}) target_link_libraries(clambdacommon lambdacommon) +target_include_directories(clambdacommon PUBLIC "include") target_include_directories(clambdacommon PUBLIC "${CMAKE_BINARY_DIR}/c_wrapper/exports") # Install if the option is on. diff --git a/c_wrapper/include/clambdacommon/graphics/color.h b/c_wrapper/include/clambdacommon/graphics/color.h index 61a2f6a..9146a57 100644 --- a/c_wrapper/include/clambdacommon/graphics/color.h +++ b/c_wrapper/include/clambdacommon/graphics/color.h @@ -11,7 +11,7 @@ #define CLAMBDACOMMON_COLOR_H #include "../clambdacommon.h" -#include +#include "../types.h" #ifdef __cplusplus extern "C" { @@ -19,25 +19,25 @@ extern "C" { typedef struct lcColor lcColor; -lcColor* lc_new_Color(float red, float green, float blue, float alpha); +lcColor* lc_new_Color(f32 red, f32 green, f32 blue, f32 alpha); void lc_delete_Color(lcColor* color); -float lc_Color_get_red(const lcColor* color); +f32 lc_Color_get_red(const lcColor* color); -float lc_Color_get_green(const lcColor* color); +f32 lc_Color_get_green(const lcColor* color); -float lc_Color_get_blue(const lcColor* color); +f32 lc_Color_get_blue(const lcColor* color); -float lc_Color_get_alpha(const lcColor* color); +f32 lc_Color_get_alpha(const lcColor* color); -uint8_t lc_Color_get_red_as_int(const lcColor* color); +u8 lc_Color_get_red_as_int(const lcColor* color); -uint8_t lc_Color_get_green_as_int(const lcColor* color); +u8 lc_Color_get_green_as_int(const lcColor* color); -uint8_t lc_Color_get_blue_as_int(const lcColor* color); +u8 lc_Color_get_blue_as_int(const lcColor* color); -uint8_t lc_Color_get_alpha_as_int(const lcColor* color); +u8 lc_Color_get_alpha_as_int(const lcColor* color); const char* lc_Color_to_string(const lcColor* color, bool hex); @@ -47,11 +47,11 @@ lcColor* lc_color_blend(const lcColor* bg, const lcColor* fg); lcColor* lc_color_mix(const lcColor* a, const lcColor* b, float ratio); -lcColor* lc_color_from_hex(uint64_t hex_color, bool has_alpha); +lcColor* lc_color_from_hex(u64 hex_color, bool has_alpha); lcColor* lc_color_from_hex_str(const char* hex_color); -lcColor* lc_color_from_int_rgba(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha); +lcColor* lc_color_from_int_rgba(u8 red, u8 green, u8 blue, u8 alpha); #ifdef __cplusplus } diff --git a/c_wrapper/include/clambdacommon/maths.h b/c_wrapper/include/clambdacommon/maths.h index e2cd098..d440ca7 100644 --- a/c_wrapper/include/clambdacommon/maths.h +++ b/c_wrapper/include/clambdacommon/maths.h @@ -10,6 +10,8 @@ #ifndef CLAMBDACOMMON_MATHS_H #define CLAMBDACOMMON_MATHS_H +#include "types.h" + #define LCOMMON_PI 3.14159265359 #define LCOMMON_TAU 6.28318530718 @@ -24,13 +26,13 @@ extern "C" { #endif -double lc_maths_radians(double degrees); +f64 lc_maths_radians(f64 degrees); -float lc_maths_fradians(float degrees); +f32 lc_maths_fradians(f32 degrees); -double lc_maths_degrees(double radians); +f64 lc_maths_degrees(f64 radians); -float lc_maths_fdegrees(float radians); +f32 lc_maths_fdegrees(f32 radians); #ifdef __cplusplus } diff --git a/c_wrapper/include/clambdacommon/maths/geometry/point.h b/c_wrapper/include/clambdacommon/maths/geometry/point.h index 5449dc8..0ad321c 100644 --- a/c_wrapper/include/clambdacommon/maths/geometry/point.h +++ b/c_wrapper/include/clambdacommon/maths/geometry/point.h @@ -10,15 +10,15 @@ #ifndef CLAMBDACOMMON_POINT_H #define CLAMBDACOMMON_POINT_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #endif //CLAMBDACOMMON_POINT_H diff --git a/c_wrapper/include/clambdacommon/maths/geometry/point.h.in b/c_wrapper/include/clambdacommon/maths/geometry/point.h.in index 1cbe3cc..9a63476 100644 --- a/c_wrapper/include/clambdacommon/maths/geometry/point.h.in +++ b/c_wrapper/include/clambdacommon/maths/geometry/point.h.in @@ -10,7 +10,7 @@ #ifndef CLAMBDACOMMON_POINT_@LC_C_TYPE@_H #define CLAMBDACOMMON_POINT_@LC_C_TYPE@_H -#include +#include #ifdef __cplusplus extern "C" { diff --git a/c_wrapper/include/clambdacommon/maths/geometry/vector.h b/c_wrapper/include/clambdacommon/maths/geometry/vector.h index 1dc7df9..278406e 100644 --- a/c_wrapper/include/clambdacommon/maths/geometry/vector.h +++ b/c_wrapper/include/clambdacommon/maths/geometry/vector.h @@ -10,15 +10,15 @@ #ifndef CLAMBDACOMMON_VECTOR_H #define CLAMBDACOMMON_VECTOR_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #endif //CLAMBDACOMMON_VECTOR_H diff --git a/c_wrapper/include/clambdacommon/maths/geometry/vector.h.in b/c_wrapper/include/clambdacommon/maths/geometry/vector.h.in index 928939b..0cfc94e 100644 --- a/c_wrapper/include/clambdacommon/maths/geometry/vector.h.in +++ b/c_wrapper/include/clambdacommon/maths/geometry/vector.h.in @@ -10,7 +10,7 @@ #ifndef CLAMBDACOMMON_VECTOR_@LC_C_TYPE@_H #define CLAMBDACOMMON_VECTOR_@LC_C_TYPE@_H -#include +#include #ifdef __cplusplus extern "C" { diff --git a/c_wrapper/include/clambdacommon/resources.h b/c_wrapper/include/clambdacommon/resources.h index 2a45c7d..5aca9dc 100644 --- a/c_wrapper/include/clambdacommon/resources.h +++ b/c_wrapper/include/clambdacommon/resources.h @@ -17,36 +17,36 @@ extern "C" { #endif -typedef struct lc_ResourceName lc_ResourceName; +typedef struct lc_Identifier lc_Identifier; -lc_ResourceName* lc_new_ResourceName(const char* name); +lc_Identifier* lc_new_Identifier(const char* name); -lc_ResourceName* lc_new_ResourceName_DP(const char* domain, const char* path); +lc_Identifier* lc_new_Identifier_DP(const char* domain, const char* path); -void lc_delete_ResourceName(lc_ResourceName* resourceName); +void lc_delete_Identifier(lc_Identifier* resourceName); /*! - * Gets the domain of the resource. + * Gets the namespace of the resource. * @param resourceName The resource name instance. - * @return The domain of the resource as a dynamically allocated string. + * @return The namespace of the resource as a dynamically allocated string. */ -const char* lc_ResourceName_get_domain(const lc_ResourceName* resourceName); +const char* lc_Identifier_get_namespace(const lc_Identifier* resourceName); /*! * Gets the name of the resource. * @param resourceName The resource name instance. * @return The name of the resource as a dynamically allocated string. */ -const char* lc_ResourceName_get_name(const lc_ResourceName* resourceName); +const char* lc_Identifier_get_name(const lc_Identifier* resourceName); /*! * Gets the resource name as a string. * @param resourceName The resource name instance. * @return The resource name as a dynamically allocated string. */ -const char* lc_ResourceName_to_string(const lc_ResourceName* resourceName); +const char* lc_Identifier_to_string(const lc_Identifier* resourceName); -bool lc_ResourceName_equals(const lc_ResourceName* resourceName, const lc_ResourceName* other); +bool lc_Identifier_equals(const lc_Identifier* resourceName, const lc_Identifier* other); #ifdef __cplusplus } diff --git a/c_wrapper/include/clambdacommon/types.h b/c_wrapper/include/clambdacommon/types.h new file mode 100644 index 0000000..9c70bed --- /dev/null +++ b/c_wrapper/include/clambdacommon/types.h @@ -0,0 +1,33 @@ +/* + * Copyright © 2019 LambdAurora + * + * This file is part of λcommon. + * + * Licensed under the MIT license. For more information, + * see the LICENSE file. + */ + +#ifndef CLAMBDACOMMON_TYPES_H +#define CLAMBDACOMMON_TYPES_H + +#include + +/* + * Integers + */ +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +/* + * Floats + */ +typedef float f32; +typedef double f64; + +#endif //CLAMBDACOMMON_TYPES_H diff --git a/c_wrapper/src/resources.cpp b/c_wrapper/src/resources.cpp index 2f1ac88..825a6c2 100644 --- a/c_wrapper/src/resources.cpp +++ b/c_wrapper/src/resources.cpp @@ -10,42 +10,42 @@ #include "../include/clambdacommon/resources.h" #include -#define RESOURCENAME_CPP(RESOURCENAME) reinterpret_cast(RESOURCENAME) -#define CONST_RESOURCENAME_CPP(RESOURCENAME) reinterpret_cast(RESOURCENAME) +#define RESOURCENAME_CPP(RESOURCENAME) reinterpret_cast(RESOURCENAME) +#define CONST_RESOURCENAME_CPP(RESOURCENAME) reinterpret_cast(RESOURCENAME) -lc_ResourceName* lc_new_ResourceName(const char* name) { - return reinterpret_cast(new lambdacommon::ResourceName(std::string(name))); +lc_Identifier* lc_new_Identifier(const char* name) { + return reinterpret_cast(new lambdacommon::Identifier(std::string(name))); } -lc_ResourceName* lc_new_ResourceName_DP(const char* domain, const char* path) { - return reinterpret_cast(new lambdacommon::ResourceName({domain}, {path})); +lc_Identifier* lc_new_Identifier_DP(const char* domain, const char* path) { + return reinterpret_cast(new lambdacommon::Identifier({domain}, {path})); } -void lc_delete_ResourceName(lc_ResourceName* resourceName) { +void lc_delete_Identifier(lc_Identifier* resourceName) { delete RESOURCENAME_CPP(resourceName); } -const char* lc_ResourceName_get_domain(const lc_ResourceName* resourceName) { +const char* lc_Identifier_get_namespace(const lc_Identifier* resourceName) { auto domain_cpp = CONST_RESOURCENAME_CPP(resourceName)->get_domain(); char* domain = new char[domain_cpp.size()]; domain_cpp.copy(domain, domain_cpp.size()); return domain; } -const char* lc_ResourceName_get_name(const lc_ResourceName* resourceName) { +const char* lc_Identifier_get_name(const lc_Identifier* resourceName) { auto name_cpp = CONST_RESOURCENAME_CPP(resourceName)->get_name(); char* name = new char[name_cpp.size()]; name_cpp.copy(name, name_cpp.size()); return name; } -const char* lc_ResourceName_to_string(const lc_ResourceName* resourceName) { +const char* lc_Identifier_to_string(const lc_Identifier* resourceName) { auto to_string_cpp = CONST_RESOURCENAME_CPP(resourceName)->to_string(); char* toString = new char[to_string_cpp.size()]; to_string_cpp.copy(toString, to_string_cpp.size()); return toString; } -bool lc_ResourceName_equals(const lc_ResourceName* resourceName, const lc_ResourceName* other) { +bool lc_Identifier_equals(const lc_Identifier* resourceName, const lc_Identifier* other) { return *CONST_RESOURCENAME_CPP(resourceName) == *CONST_RESOURCENAME_CPP(other); } diff --git a/include/lambdacommon/exceptions/exceptions.h b/include/lambdacommon/exceptions/exceptions.h index 06222b5..300601a 100644 --- a/include/lambdacommon/exceptions/exceptions.h +++ b/include/lambdacommon/exceptions/exceptions.h @@ -24,7 +24,7 @@ namespace lambdacommon public: explicit ParseException(std::string msg) : _msg(std::move(msg)) {} - const char* what() const noexcept override { + [[nodiscard]] const char* what() const noexcept override { return _msg.c_str(); } }; diff --git a/include/lambdacommon/graphics/color.h b/include/lambdacommon/graphics/color.h index b5c9d97..a5167b2 100644 --- a/include/lambdacommon/graphics/color.h +++ b/include/lambdacommon/graphics/color.h @@ -10,7 +10,7 @@ #ifndef LAMBDACOMMON_COLOR_H #define LAMBDACOMMON_COLOR_H -#include "../object.h" +#include "../types.h" #include namespace lambdacommon @@ -20,13 +20,13 @@ namespace lambdacommon class LAMBDACOMMON_API Color : public Object { private: - float _red; - float _green; - float _blue; - float _alpha; + f32 _red; + f32 _green; + f32 _blue; + f32 _alpha; public: - Color(float red, float green, float blue, float alpha = 1.f); + Color(f32 red, f32 green, f32 blue, f32 alpha = 1.f); /*! @brief Gets the red value of the color. * @@ -34,7 +34,7 @@ namespace lambdacommon * * @return The red value (between 0 and 1). */ - float red() const; + f32 red() const; /*! @brief Gets the green value of the color. * @@ -42,7 +42,7 @@ namespace lambdacommon * * @return The green value (between 0 and 1). */ - float green() const; + f32 green() const; /*! @brief Gets the blue value of the color. * @@ -50,7 +50,7 @@ namespace lambdacommon * * @return The blue value (between 0 and 1). */ - float blue() const; + f32 blue() const; /*! @brief Gets the alpha value of the color. * @@ -58,7 +58,7 @@ namespace lambdacommon * * @return The alpha value (between 0 and 1). */ - float alpha() const; + f32 alpha() const; /*! @brief Gets the red value of the color. * @@ -66,7 +66,7 @@ namespace lambdacommon * * @return The red value (between 0 and 255). */ - uint8_t red_as_int() const; + u8 red_as_int() const; /*! @brief Gets the green value of the color. * @@ -74,7 +74,7 @@ namespace lambdacommon * * @return The green value (between 0 and 255). */ - uint8_t green_as_int() const; + u8 green_as_int() const; /*! @brief Gets the blue value of the color. * @@ -82,7 +82,7 @@ namespace lambdacommon * * @return The blue value (between 0 and 255). */ - uint8_t blue_as_int() const; + u8 blue_as_int() const; /*! @brief Gets the alpha value of the color. * @@ -90,7 +90,7 @@ namespace lambdacommon * * @return The alpha value (between 0 and 255). */ - uint8_t alpha_as_int() const; + u8 alpha_as_int() const; /*! * Blends this color with a background color. @@ -105,13 +105,13 @@ namespace lambdacommon * @param ratio The mix ratio. * @return The mixed color. */ - const Color mix(const Color& b, float ratio) const; + const Color mix(const Color& b, f32 ratio) const; /*! * Gets the color as an hexadecimal color. * @return The hexadecimal color. */ - //uint64_t to_hex() const; + //u64 to_hex() const; /*! * Gets the color as a string. @@ -139,7 +139,7 @@ namespace lambdacommon Color& operator*=(const Color& other); - Color& operator*=(float coefficient); + Color& operator*=(f32 coefficient); /*! * Adds the specified color to the current color. @@ -168,7 +168,7 @@ namespace lambdacommon * @param coefficient The multiplication coefficient. * @return The multiplied color. */ - friend const Color operator*(Color self, float coefficient); + friend const Color operator*(Color self, f32 coefficient); /** * Represents the black color. @@ -213,7 +213,7 @@ namespace lambdacommon * @param ratio The mix ratio. * @return The mixed color. */ - extern Color LAMBDACOMMON_API mix(const Color& a, const Color& b, float ratio); + extern Color LAMBDACOMMON_API mix(const Color& a, const Color& b, f32 ratio); /*! * Makes a new Color instance from the given hexadecimal color value. @@ -221,7 +221,7 @@ namespace lambdacommon * @param has_alpha True if the hexadecimal color value includes the alpha channel, else false. * @return A new Color instance. */ - extern Color LAMBDACOMMON_API from_hex(uint64_t hex_color, bool has_alpha = true); + extern Color LAMBDACOMMON_API from_hex(u64 hex_color, bool has_alpha = true); /*! * Makes a new Color instance from the given hexadecimal color value string. @@ -238,7 +238,7 @@ namespace lambdacommon * @param alpha Alpha channel. * @return A new Color instance. */ - extern Color LAMBDACOMMON_API from_int_rgba(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255); + extern Color LAMBDACOMMON_API from_int_rgba(u8 red, u8 green, u8 blue, u8 alpha = 255); } } diff --git a/include/lambdacommon/graphics/scene.h b/include/lambdacommon/graphics/scene.h index eb0f17c..7edc758 100644 --- a/include/lambdacommon/graphics/scene.h +++ b/include/lambdacommon/graphics/scene.h @@ -22,16 +22,16 @@ namespace lambdacommon class Scene { protected: - const uint32_t id; + const u32 id; public: - Scene(uint32_t id); + Scene(u32 id); /*! * Gets the ID which identify the Scene. * @return The ID. */ - uint32_t get_id() const; + u32 get_id() const; /*! * Updates every component of the Scene. diff --git a/include/lambdacommon/maths.h b/include/lambdacommon/maths.h index 9f19976..2a07cd3 100644 --- a/include/lambdacommon/maths.h +++ b/include/lambdacommon/maths.h @@ -16,7 +16,7 @@ * Note: Never use `using namespace maths;` with this header, else the std and this will be conflicting. */ -#include "lambdacommon.h" +#include "types.h" #define LCOMMON_PI 3.14159265359 #define LCOMMON_TAU 6.28318530718 @@ -30,7 +30,7 @@ namespace lambdacommon::maths * @return The absolute value. */ template - N abs(N number) { + inline N abs(N number) { return number < 0 ? -number : number; } @@ -42,7 +42,7 @@ namespace lambdacommon::maths * @return The smallest number. */ template - N min(N a, N b) { + inline N min(N a, N b) { return a < b ? a : b; } @@ -76,7 +76,7 @@ namespace lambdacommon::maths * @return The largest number. */ template - N max(N a, N b) { + inline N max(N a, N b) { return a > b ? a : b; } @@ -111,7 +111,7 @@ namespace lambdacommon::maths * @return The clamped value. */ template - N clamp(N number, N min, N max) { + inline N clamp(N number, N min, N max) { return maths::min(maths::max(number, min), max); } @@ -143,14 +143,14 @@ namespace lambdacommon::maths * @param degrees Value representing an angle, expressed in degrees. * @return The radian value. */ - extern double LAMBDACOMMON_API radians(double degrees); + extern f64 LAMBDACOMMON_API radians(f64 degrees); /*! * Converts a degree value to a radian value. * @param degrees Value representing an angle, expressed in degrees. * @return The radian value. */ - extern float LAMBDACOMMON_API radians(float degrees); + extern f32 LAMBDACOMMON_API radians(f32 degrees); /*! * Converts a degree value to a radian value. @@ -159,8 +159,8 @@ namespace lambdacommon::maths * @return The radian value. */ template - double radians(N degrees) { - return radians(static_cast(degrees)); + f64 radians(N degrees) { + return radians(static_cast(degrees)); } /*! @@ -168,14 +168,14 @@ namespace lambdacommon::maths * @param degrees Value representing an angle, expressed in radians. * @return The degree value. */ - extern double LAMBDACOMMON_API degrees(double radians); + extern f64 LAMBDACOMMON_API degrees(f64 radians); /*! * Converts a radian value to a radian value. * @param degrees Value representing an angle, expressed in radians. * @return The degree value. */ - extern float LAMBDACOMMON_API degrees(float radians); + extern f32 LAMBDACOMMON_API degrees(f32 radians); /*! * Converts a radian value to a radian value. @@ -184,8 +184,8 @@ namespace lambdacommon::maths * @return The degree value. */ template - double degrees(N radians) { - return degrees(static_cast(radians)); + f64 degrees(N radians) { + return degrees(static_cast(radians)); } } diff --git a/include/lambdacommon/resources.h b/include/lambdacommon/resources.h index e790588..e00e689 100644 --- a/include/lambdacommon/resources.h +++ b/include/lambdacommon/resources.h @@ -24,24 +24,24 @@ namespace lambdacommon using namespace std::rel_ops; /*! - * ResourceName + * Identifier * * Represents a resource identifier. */ - class LAMBDACOMMON_API ResourceName : public Object + class LAMBDACOMMON_API Identifier : public Object { private: std::string _namespace; std::string _name; public: - ResourceName(const std::string& name); + Identifier(const std::string& name); - ResourceName(std::string domain, std::string name) noexcept; + Identifier(std::string domain, std::string name) noexcept; - ResourceName(const ResourceName& other); + Identifier(const Identifier& other); - ResourceName(ResourceName&& other) noexcept; + Identifier(Identifier&& other) noexcept; /*! * Gets the namespace of the resource. @@ -56,11 +56,11 @@ namespace lambdacommon const std::string& get_name() const; /*! - * Creates a new {@code ResourceName} from this resource name. + * Creates a new {@code Identifier} from this resource name. * @param name The path to append. - * @return The new {@code ResourceName} with the appended path. + * @return The new {@code Identifier} with the appended path. */ - ResourceName sub(const std::string& name) const; + Identifier sub(const std::string& name) const; /*! * Gets the resource name as a string. @@ -69,21 +69,21 @@ namespace lambdacommon std::string to_string() const override; /*! - * Creates a new {@code ResourceName} from this resource name. + * Creates a new {@code Identifier} from this resource name. * @param path The path to append. - * @return The new {@code ResourceName} with the appended path. + * @return The new {@code Identifier} with the appended path. */ - inline ResourceName operator/(const std::string& path) const { + inline Identifier operator/(const std::string& path) const { return sub(path); } - ResourceName& operator=(const ResourceName& other); + Identifier& operator=(const Identifier& other); - ResourceName& operator=(ResourceName&& other) noexcept; + Identifier& operator=(Identifier&& other) noexcept; - bool operator==(const ResourceName& other) const; + bool operator==(const Identifier& other) const; - bool operator<(const ResourceName& other) const; + bool operator<(const Identifier& other) const; template decltype(auto) get() const { @@ -100,7 +100,7 @@ namespace lambdacommon class LAMBDACOMMON_API ResourcesManager { protected: - uint32_t _id; + u32 _id; public: ResourcesManager(); @@ -113,14 +113,14 @@ namespace lambdacommon * Gets the identifier of the resources manager. * @return The identifier. */ - uint32_t get_id() const; + u32 get_id() const; /*! @brief Checks whether the resources exists or not. * * @param resource The resource to check. * @return True of the resource exists, else false. */ - virtual bool has_resource(const ResourceName& resource) const = 0; + [[nodiscard]] virtual bool has_resource(const Identifier& resource) const = 0; /*! @brief Checks whether the resource exists or not. * @@ -128,14 +128,7 @@ namespace lambdacommon * @param extension The extension of the resource file. * @return True if the resource exists, else false. */ - virtual bool has_resource(const ResourceName& resource, const std::string& extension) const = 0; - - /*! - * Loads the resource content into a string value. - * @param resource The resource to load. - * @return The resource content if successfully loaded, else an empty string. - */ - virtual std::string load_resource(const ResourceName& resource) const = 0; + [[nodiscard]] virtual bool has_resource(const Identifier& resource, const std::string& extension) const = 0; /*! @brief Loads the resource content into a string value. * @@ -143,9 +136,16 @@ namespace lambdacommon * @param extension The extension of the resource file. * @return The resource content if successfully loaded,X else an empty string. */ - virtual std::string load_resource(const ResourceName& resource, const std::string& extension) const = 0; + [[nodiscard]] virtual std::string load_resource(const Identifier& resource, const std::string& extension) const = 0; bool operator==(const ResourcesManager& other) const; + + /*! + * Loads the resource content into a string value. + * @param resource The resource to load. + * @return The resource content if successfully loaded, else an empty string. + */ + [[nodiscard]] virtual std::string load_resource(const Identifier& resource) const = 0; }; class LAMBDACOMMON_API FileResourcesManager : public ResourcesManager @@ -166,16 +166,16 @@ namespace lambdacommon */ const lambdacommon::fs::path& get_working_directory() const; - bool has_resource(const ResourceName& resource) const override; + bool has_resource(const Identifier& resource) const override; - bool has_resource(const ResourceName& resource, const std::string& extension) const override; + bool has_resource(const Identifier& resource, const std::string& extension) const override; lambdacommon::fs::path - get_resource_path(const ResourceName& resource, const std::string& extension) const; + get_resource_path(const Identifier& resource, const std::string& extension) const; - std::string load_resource(const ResourceName& resource) const override; + std::string load_resource(const Identifier& resource) const override; - std::string load_resource(const ResourceName& resource, const std::string& extension) const override; + std::string load_resource(const Identifier& resource, const std::string& extension) const override; FileResourcesManager& operator=(const FileResourcesManager& other); @@ -185,18 +185,18 @@ namespace lambdacommon }; } -// Structured bindings for lambdacommon::ResourceName. +// Structured bindings for lambdacommon::Identifier. namespace std { template<> - struct tuple_size : std::integral_constant + struct tuple_size : std::integral_constant { }; template - struct tuple_element + struct tuple_element { - using type = decltype(std::declval().get()); + using type = decltype(std::declval().get()); }; } diff --git a/include/lambdacommon/system/fs.h b/include/lambdacommon/system/fs.h index c60160d..7813cd4 100644 --- a/include/lambdacommon/system/fs.h +++ b/include/lambdacommon/system/fs.h @@ -14,6 +14,7 @@ #include "../types.h" #include #include +#include #ifdef LAMBDA_WINDOWS # pragma warning(push) @@ -28,7 +29,7 @@ namespace lambdacommon * * file_type defines constants that indicate a type of a file or directory a path refers to. The value of the enumerators are distinct. */ - enum class file_type : int8_t + enum class file_type : i8 { none = 0, not_found = -1, @@ -46,7 +47,7 @@ namespace lambdacommon * * This type represents file access permissions. perms satisfies the requirements of `BitmaskType`. */ - enum class perms : uint16_t + enum class perms : u16 { none = 0, owner_read = 0400, @@ -94,7 +95,7 @@ namespace lambdacommon inline perms& operator^=(perms& self, perms other) noexcept { return self = self ^ other; } - enum class perm_options : uint16_t + enum class perm_options : u16 { replace = 3, add = 1, @@ -198,7 +199,7 @@ namespace lambdacommon path(std::string path); - path(std::wstring path); + explicit path(std::wstring path); path(const path& other); @@ -222,52 +223,52 @@ namespace lambdacommon * Returns the root name of the generic-format path. If the path (in generic format) does not include root name, returns `FilePath()`. * @return The root name of the path. */ - path root_name() const; + [[nodiscard]] path root_name() const; /*! * Returns the root directory of the generic-format path. If the path (in generic format) does not include root directory, returns `path()`. * @return The root directory of the path. */ - path root_directory() const; + [[nodiscard]] path root_directory() const; /*! * Returns the root path of the path. If the path foes not include root path, returns `path()`. * Effectively, returns the following `root_name() / root_directory()`. * @return The root path of the path. */ - path root_path() const; + [[nodiscard]] path root_path() const; - path relative_path() const; + [[nodiscard]] path relative_path() const; - bool empty() const override; + [[nodiscard]] bool empty() const override; /*! * Checks whether `root_name()` is empty. * @return True if the root name is not empty, false otherwise. */ - bool has_root_name() const; + [[nodiscard]] bool has_root_name() const; /*! * Checks whether `root_directory()` is empty. * @return True if the root directory is not empty, false otherwise. */ - bool has_root_directory() const; + [[nodiscard]] bool has_root_directory() const; /*! * Checks whether `root_path()` is empty. * @return True if the root path is not empty, false otherwise. */ - bool has_root_path() const; + [[nodiscard]] bool has_root_path() const; /*! * Checks whether `relative_path()` is empty. * @return True if the relative path is not empty, false otherwise. */ - bool has_relative_path() const; + [[nodiscard]] bool has_relative_path() const; - bool has_filename() const; + [[nodiscard]] bool has_filename() const; - bool is_absolute() const override; + [[nodiscard]] bool is_absolute() const override; class LAMBDACOMMON_API iterator; @@ -277,37 +278,37 @@ namespace lambdacommon * Returns an iterator to the first element of the path. If the path is empty, the returned iterator is equal to `end()`. * @return Iterator to the first element of the path. */ - iterator begin() const; + [[nodiscard]] iterator begin() const; /*! * Returns an iterator one past the last element of the path. * @return Iterator one past the end of the path. */ - iterator end() const; + [[nodiscard]] iterator end() const; - std::string to_string() const override; + [[nodiscard]] std::string to_string() const override; - std::wstring to_wstring() const override; + [[nodiscard]] std::wstring to_wstring() const override; - std::string to_generic_string() const; + [[nodiscard]] std::string to_generic_string() const; /*! * Gets the path as the native string type. * @return The path as the native string type. */ - const string_type& native() const noexcept; + [[nodiscard]] const string_type& native() const noexcept; /*! * Gets the path as a C value. * @return The path as a C value. */ - const value_type* c_str() const noexcept; + [[nodiscard]] const value_type* c_str() const noexcept; /*! * Makes a new instance of FilePath with the absolute path. * @return The absolute path. */ - path to_absolute() const; + [[nodiscard]] path to_absolute() const; /*! * Makes a new instance of FilePath with the absolute path. @@ -320,25 +321,25 @@ namespace lambdacommon * Checks whether the path exists or not. * @return True if the path exists else false. */ - bool exists() const; + [[nodiscard]] bool exists() const; /*! * Gets the generic-format filename component of the path. * @return The filename identified by the path. */ - path get_filename() const; + [[nodiscard]] path get_filename() const; /*! * Gets the file extension. * @return The file extension. */ - path get_extension() const; + [[nodiscard]] path get_extension() const; /*! * Determines the type and attributes of the filesystem object. * @return The file status. */ - file_status status() const; + [[nodiscard]] file_status status() const; /*! * Determines the type and attributes of the filesystem object. @@ -351,7 +352,7 @@ namespace lambdacommon * Determines the type and attributes of the filesystem object. * @return The file status. */ - file_status symlink_status() const; + [[nodiscard]] file_status symlink_status() const; /*! * Determines the type and attributes of the filesystem object. @@ -364,7 +365,7 @@ namespace lambdacommon * Gets the file type. * @return The file type. */ - file_type get_file_type() const; + [[nodiscard]] file_type get_file_type() const; /*! * Gets the file type. @@ -379,7 +380,7 @@ namespace lambdacommon * The result of attempting to determine the size of a directory (as well as any other file that is not a regular file or a symlink) is implementation-defined. * @return The size of the file, in bytes. */ - uintmax_t file_size() const; + [[nodiscard]] uintmax_t file_size() const; /*! @brief Returns the size of a file. * @@ -395,7 +396,7 @@ namespace lambdacommon * Returns the time of the last modification of this path (symlinks are followed). * @return The time of the last modification. */ - file_time_type last_write_time() const; + [[nodiscard]] file_time_type last_write_time() const; /*! @brief Get the time of the last data modification. * @@ -439,7 +440,7 @@ namespace lambdacommon * The non-throwing overload returns an empty path on errors. * @return The target of the symlink (which may not necessarily exist). */ - path read_symlink() const; + [[nodiscard]] path read_symlink() const; /*! @brief Obtains the target of a symbolic link. * @@ -502,25 +503,25 @@ namespace lambdacommon * Checks whether the path points to a directory. * @return True if the path points to a directory, else false. */ - bool is_directory() const; + [[nodiscard]] bool is_directory() const; /*! * Checks whether the path points to a regular file. * @return True if the path points to a regular file, else false. */ - bool is_file() const; + [[nodiscard]] bool is_file() const; /*! * Checks whether the path points to a symlink. * @return True if the path points to a symlink, else false. */ - bool is_symlink() const; + [[nodiscard]] bool is_symlink() const; /*! * Gets the size of the file in bytes. * @return The size of the file. */ - size_t get_size() const; + [[nodiscard]] size_t get_size() const; /*! @brief Returns the number of hard links referring to the specific file. * @@ -528,7 +529,7 @@ namespace lambdacommon * * @return The number of hard links. */ - size_t hard_link_count() const; + [[nodiscard]] size_t hard_link_count() const; /*! @brief Returns the number of hard links referring to the specific file. * @@ -560,7 +561,7 @@ namespace lambdacommon path& operator=(string_type source); - operator string_type() const; + explicit operator string_type() const; path& operator/=(const path& other); @@ -634,9 +635,9 @@ namespace lambdacommon filesystem_error(const std::string& msg, const path& p1, const path& p2, std::error_code ec); - const path& path1() const noexcept; + [[nodiscard]] const path& path1() const noexcept; - const path& path2() const noexcept; + [[nodiscard]] const path& path2() const noexcept; }; class LAMBDACOMMON_API directory_entry @@ -659,15 +660,15 @@ namespace lambdacommon void assign(path p); // Observers - const path& get_path() const noexcept; + [[nodiscard]] const path& get_path() const noexcept; operator const path&() const noexcept; - file_status status() const; + [[nodiscard]] file_status status() const; file_status status(std::error_code& ec) const noexcept; - file_status symlink_status() const; + [[nodiscard]] file_status symlink_status() const; file_status symlink_status(std::error_code& ec) const noexcept; @@ -699,7 +700,7 @@ namespace lambdacommon friend class directory_iterator; - explicit proxy(const directory_entry& dir_entry) : _dir_entry(dir_entry) {} + explicit proxy(directory_entry dir_entry) : _dir_entry(std::move(dir_entry)) {} public: const directory_entry& operator*() const& noexcept { return _dir_entry; } diff --git a/include/lambdacommon/system/system.h b/include/lambdacommon/system/system.h index 25210d2..82c829c 100644 --- a/include/lambdacommon/system/system.h +++ b/include/lambdacommon/system/system.h @@ -61,25 +61,25 @@ namespace lambdacommon * Returns the count of the processor (CPU) cores as an int. * @return The CPU cores' count */ - extern uint32_t LAMBDACOMMON_API get_cpu_cores(); + extern u32 LAMBDACOMMON_API get_cpu_cores(); /*! * Returns the size of the physical memory (RAM) in bytes. * @return The physical memory in bytes. */ - extern uint64_t LAMBDACOMMON_API get_memory_total(); + extern u64 LAMBDACOMMON_API get_memory_total(); /*! * Returns the size of the available physical memory (RAM) in bytes. * @return The available physical memory in bytes. */ - extern uint64_t LAMBDACOMMON_API get_memory_available(); + extern u64 LAMBDACOMMON_API get_memory_available(); /*! * Returns the size of the used physical memory (RAM) in bytes. * @return The used physical memory in bytes. */ - extern uint64_t LAMBDACOMMON_API get_memory_used(); + extern u64 LAMBDACOMMON_API get_memory_used(); /* * Computer diff --git a/include/lambdacommon/system/terminal.h b/include/lambdacommon/system/terminal.h index 4dd0b52..5066900 100644 --- a/include/lambdacommon/system/terminal.h +++ b/include/lambdacommon/system/terminal.h @@ -74,11 +74,11 @@ namespace lambdacommon B_WHITE = 107 }; - extern std::ostream LAMBDACOMMON_API& operator<<(std::ostream& stream, TermFormatting term_formatting); + extern std::ostream& LAMBDACOMMON_API operator<<(std::ostream& stream, TermFormatting term_formatting); - extern std::ostream LAMBDACOMMON_API& operator<<(std::ostream& stream, const std::vector& term_formatting); + extern std::ostream& LAMBDACOMMON_API operator<<(std::ostream& stream, const std::vector& term_formatting); - extern std::ostream LAMBDACOMMON_API& operator<<(std::ostream& stream, const std::vector& string_vector); + extern std::ostream& LAMBDACOMMON_API operator<<(std::ostream& stream, const std::vector& string_vector); inline std::vector formats(std::initializer_list format_list) { return format_list; @@ -90,14 +90,14 @@ namespace lambdacommon * @param stream Stream will be affect. * @return The current stream. */ - extern std::ostream LAMBDACOMMON_API& erase_current_line(std::ostream& stream = std::cout); + extern std::ostream& LAMBDACOMMON_API erase_current_line(std::ostream& stream = std::cout); /*! * Clears the specified stream. * @param stream The stream to affect. * @return The affected stream. */ - extern std::ostream LAMBDACOMMON_API& clear(std::ostream& stream = std::cout); + extern std::ostream& LAMBDACOMMON_API clear(std::ostream& stream = std::cout); /*! * Gets the cursor position in the tty. @@ -141,7 +141,7 @@ namespace lambdacommon * @param stream The stream to affect. * @return The affected stream. */ - extern std::ostream LAMBDACOMMON_API& bell(std::ostream& stream = std::cout); + extern std::ostream& LAMBDACOMMON_API bell(std::ostream& stream = std::cout); /* * Terminal manipulations diff --git a/include/lambdacommon/system/time.h b/include/lambdacommon/system/time.h index 69d0063..225e840 100644 --- a/include/lambdacommon/system/time.h +++ b/include/lambdacommon/system/time.h @@ -13,16 +13,13 @@ #include "../lambdacommon.h" #include -namespace lambdacommon +namespace lambdacommon::time { - namespace time - { - /*! - * Gets the current time in milliseconds. - * @return The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. - */ - extern time_t LAMBDACOMMON_API get_time_millis(); - } + /*! + * Gets the current time in milliseconds. + * @return The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. + */ + extern time_t LAMBDACOMMON_API get_time_millis(); } #endif //LAMBDACOMMON_TIME_H diff --git a/include/lambdacommon/types.h b/include/lambdacommon/types.h index 2fc17bf..b08094d 100644 --- a/include/lambdacommon/types.h +++ b/include/lambdacommon/types.h @@ -16,36 +16,54 @@ namespace lambdacommon { + /* + * Integers + */ + typedef int8_t i8; + typedef int16_t i16; + typedef int32_t i32; + typedef int64_t i64; + typedef uint8_t u8; + typedef uint16_t u16; + typedef uint32_t u32; + typedef uint64_t u64; + + /* + * Floats + */ + typedef float f32; + typedef double f64; + /* * Sizes */ - typedef Size2D Size2D_u16; - typedef Size2D Size2D_i16; - typedef Size2D Size2D_u32; - typedef Size2D Size2D_i32; + typedef Size2D Size2D_u16; + typedef Size2D Size2D_i16; + typedef Size2D Size2D_u32; + typedef Size2D Size2D_i32; - typedef Size3D Size3D_u16; - typedef Size3D Size3D_i16; - typedef Size3D Size3D_u32; - typedef Size3D Size3D_i32; + typedef Size3D Size3D_u16; + typedef Size3D Size3D_i16; + typedef Size3D Size3D_u32; + typedef Size3D Size3D_i32; /* * Points */ - typedef Point2D Point2D_u16; - typedef Point2D Point2D_i16; - typedef Point2D Point2D_u32; - typedef Point2D Point2D_i32; + typedef Point2D Point2D_u16; + typedef Point2D Point2D_i16; + typedef Point2D Point2D_u32; + typedef Point2D Point2D_i32; /* * Network */ - typedef uint16_t port_t; + typedef u16 port_t; /* * Time */ - typedef uint64_t utime_t; + typedef u64 utime_t; #if __cplusplus > 201703L using file_time_type = std::chrono::time_point; #else diff --git a/src/lc_info.cpp b/src/lc_info.cpp index 1982126..5bcd37d 100644 --- a/src/lc_info.cpp +++ b/src/lc_info.cpp @@ -12,7 +12,7 @@ namespace sys = lambdacommon::system; namespace term = lambdacommon::terminal; -int main(int argc, char** argv) { +auto main(int argc, char** argv) -> int { term::setup(); if (argc > 1) { term::set_title("λcommon_info - Help"); diff --git a/src/maths.cpp b/src/maths.cpp index 74348ca..c58850c 100644 --- a/src/maths.cpp +++ b/src/maths.cpp @@ -18,19 +18,19 @@ namespace lambdacommon::maths #define RADIANS(TYPE, VALUE) VALUE * (static_cast(LCOMMON_PI) / static_cast(180.0)) #define DEGREES(TYPE, VALUE) VALUE * (static_cast(180.0) / static_cast(LCOMMON_PI)) - double LAMBDACOMMON_API radians(double degrees) { - return RADIANS(double, degrees); + f64 LAMBDACOMMON_API radians(f64 degrees) { + return RADIANS(f64, degrees); } - float LAMBDACOMMON_API radians(float degrees) { - return RADIANS(float, degrees); + f32 LAMBDACOMMON_API radians(f32 degrees) { + return RADIANS(f32, degrees); } - double LAMBDACOMMON_API degrees(double radians) { - return DEGREES(double, radians); + f64 LAMBDACOMMON_API degrees(f64 radians) { + return DEGREES(f64, radians); } - float LAMBDACOMMON_API degrees(float radians) { - return DEGREES(float, radians); + f32 LAMBDACOMMON_API degrees(f32 radians) { + return DEGREES(f32, radians); } } diff --git a/src/resources.cpp b/src/resources.cpp index bdebc12..a917248 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -21,7 +21,7 @@ namespace lambdacommon { - ResourceName::ResourceName(const std::string& name) { + Identifier::Identifier(const std::string& name) { auto separator = name.find_first_of(':'); if (separator <= 0) throw std::invalid_argument("The resource name '" + name + "' is invalid."); @@ -29,29 +29,29 @@ namespace lambdacommon _name = name.substr(separator + 1); } - ResourceName::ResourceName(std::string domain, std::string name) noexcept : _namespace(std::move(domain)), _name(std::move(name)) {} + Identifier::Identifier(std::string domain, std::string name) noexcept : _namespace(std::move(domain)), _name(std::move(name)) {} - ResourceName::ResourceName(const ResourceName& other) = default; + Identifier::Identifier(const Identifier& other) = default; - ResourceName::ResourceName(ResourceName&& other) noexcept : _namespace(std::move(other._namespace)), _name(std::move(other._name)) {} + Identifier::Identifier(Identifier&& other) noexcept : _namespace(std::move(other._namespace)), _name(std::move(other._name)) {} - const std::string& ResourceName::get_domain() const { + const std::string& Identifier::get_domain() const { return _namespace; } - const std::string& ResourceName::get_name() const { + const std::string& Identifier::get_name() const { return _name; } - ResourceName ResourceName::sub(const std::string& name) const { - return ResourceName(_namespace, lstring::merge_path(_name, name)); + Identifier Identifier::sub(const std::string& name) const { + return Identifier(_namespace, lstring::merge_path(_name, name)); } - std::string ResourceName::to_string() const { + std::string Identifier::to_string() const { return _namespace + ":" + _name; } - ResourceName& ResourceName::operator=(const ResourceName& other) { + Identifier& Identifier::operator=(const Identifier& other) { if (this != &other) { if (other._namespace != _namespace) _namespace = other._namespace; @@ -61,7 +61,7 @@ namespace lambdacommon return *this; } - ResourceName& ResourceName::operator=(ResourceName&& other) noexcept { + Identifier& Identifier::operator=(Identifier&& other) noexcept { if (this != &other) { _namespace = std::move(other._namespace); _name = std::move(other._name); @@ -69,18 +69,18 @@ namespace lambdacommon return *this; } - bool ResourceName::operator==(const ResourceName& other) const { + bool Identifier::operator==(const Identifier& other) const { return _namespace == other._namespace && _name == other._name; } - bool ResourceName::operator<(const ResourceName& other) const { + bool Identifier::operator<(const Identifier& other) const { return std::tie(_namespace, _name) < std::tie(other._namespace, other._name); } /* * ResourcesManager */ - uint32_t last_id = 0; + u32 last_id = 0; ResourcesManager::ResourcesManager() : _id(++last_id) {} @@ -88,7 +88,7 @@ namespace lambdacommon ResourcesManager::ResourcesManager(ResourcesManager&& other) noexcept : _id(other._id) {} - uint32_t ResourcesManager::get_id() const { + u32 ResourcesManager::get_id() const { return _id; } @@ -112,26 +112,26 @@ namespace lambdacommon return _working_directory; } - bool FileResourcesManager::has_resource(const ResourceName& resource) const { + bool FileResourcesManager::has_resource(const Identifier& resource) const { return this->has_resource(resource, ""); } - bool FileResourcesManager::has_resource(const ResourceName& resource, const std::string& extension) const { + bool FileResourcesManager::has_resource(const Identifier& resource, const std::string& extension) const { return get_resource_path(resource, extension).exists(); } - lambdacommon::fs::path FileResourcesManager::get_resource_path(const ResourceName& resource, const std::string& extension) const { + lambdacommon::fs::path FileResourcesManager::get_resource_path(const Identifier& resource, const std::string& extension) const { auto file = resource.get_name(); if (!extension.empty()) file += "." + extension; return (_working_directory / resource.get_domain()) / file; } - std::string FileResourcesManager::load_resource(const ResourceName& resource) const { + std::string FileResourcesManager::load_resource(const Identifier& resource) const { return this->load_resource(resource, ""); } - std::string FileResourcesManager::load_resource(const ResourceName& resource, const std::string& extension) const { + std::string FileResourcesManager::load_resource(const Identifier& resource, const std::string& extension) const { auto resource_path = get_resource_path(resource, extension); if (!resource_path.exists()) return ""; diff --git a/src/serializable.cpp b/src/serializable.cpp index 3fce6df..9aaa32b 100644 --- a/src/serializable.cpp +++ b/src/serializable.cpp @@ -14,7 +14,8 @@ namespace lambdacommon namespace serializable { std::vector LAMBDACOMMON_API tokenize(const std::string& _string, const std::string& delim) { - std::string::size_type last_pos = 0, pos = _string.find_first_of(delim, last_pos); + std::string::size_type last_pos = 0; + std::string::size_type pos = _string.find_first_of(delim, last_pos); std::vector tokens; while (last_pos != std::string::npos) { diff --git a/src/system/fs.cpp b/src/system/fs.cpp index 58f4eaf..980ddb3 100644 --- a/src/system/fs.cpp +++ b/src/system/fs.cpp @@ -23,7 +23,7 @@ using LPFN_CreateSymbolicLinkW = BOOLEAN(WINAPI *)(LPCWSTR, LPCWSTR, DWORD); using LPFN_CreateHardLinkW = BOOLEAN(WINAPI *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES); #else -# include +# include # define ERROR_INVALID_PARAMETER EINVAL # define ERROR_PATH_NOT_FOUND ENOENT # include @@ -235,7 +235,7 @@ namespace lambdacommon path path::root_name() const { #ifdef LAMBDA_WINDOWS - if (_path.length() >= 2 && std::toupper(static_cast(_path[0])) >= 'A' && std::toupper(static_cast(_path[0])) <= 'Z' && _path[1] == L':') + if (_path.length() >= 2 && std::toupper(static_cast(_path[0])) >= 'A' && std::toupper(static_cast(_path[0])) <= 'Z' && _path[1] == L':') return _path.substr(0, 2); #endif if (_path.length() > 2 && _path[0] == '/' && _path[1] == '/' && _path[2] != '/' && std::isprint(_path[2])) { @@ -361,11 +361,11 @@ namespace lambdacommon if (this->empty()) return (current_path() / "").to_absolute(ec); // Gets the size of the absolute path. - uint32_t size = ::GetFullPathNameW(this->c_str(), 0, nullptr, nullptr); + u32 size = ::GetFullPathNameW(this->c_str(), 0, nullptr, nullptr); if (size) { // Allocate the buffer to get the absolute path. std::vector buffer(size, 0); - uint32_t a = ::GetFullPathNameW(this->c_str(), size, buffer.data(), nullptr); + u32 a = ::GetFullPathNameW(this->c_str(), size, buffer.data(), nullptr); // We check that everything is correct and we can return the result. if (a && a < size) { path result(std::wstring(buffer.data(), a)); @@ -404,7 +404,7 @@ namespace lambdacommon path path::get_extension() const { // As the filename contains the extension, we get the filename and fetch the string after the last dot character if there is one, else we returns an empty string. - string_type file_name = std::move(get_filename()); + string_type file_name = this->get_filename().native(); auto pos = file_name.find_last_of('.'); if (pos == std::string::npos || pos == 0) return {""}; @@ -923,7 +923,7 @@ namespace lambdacommon update_current(); // Find the position of a potential root directory. #ifdef LAMBDA_WINDOWS - if (_last - _first >= 3 && std::toupper(static_cast(*first)) >= 'A' && std::toupper(static_cast(*first)) <= 'Z' && *(first + 1) == L':' && + if (_last - _first >= 3 && std::toupper(static_cast(*first)) >= 'A' && std::toupper(static_cast(*first)) <= 'Z' && *(first + 1) == L':' && *(first + 2) == preferred_separator) _root = _first + 2; else diff --git a/src/system/system.cpp b/src/system/system.cpp index 7857ca7..4690ea4 100644 --- a/src/system/system.cpp +++ b/src/system/system.cpp @@ -88,7 +88,7 @@ namespace lambdacommon::system return arch == ARM || arch == ARM64 || arch == ARMv7 || arch == ARMv8_64; } - uint32_t LAMBDACOMMON_API get_cpu_cores() { + u32 LAMBDACOMMON_API get_cpu_cores() { return std::thread::hardware_concurrency(); } @@ -323,7 +323,7 @@ namespace lambdacommon::system return get_processor_arch_enum_str(); } - uint64_t LAMBDACOMMON_API get_memory_total() + u64 LAMBDACOMMON_API get_memory_total() { MEMORYSTATUSEX statex; statex.dwLength = sizeof(statex); @@ -331,7 +331,7 @@ namespace lambdacommon::system return statex.ullTotalPhys; } - uint64_t LAMBDACOMMON_API get_memory_available() + u64 LAMBDACOMMON_API get_memory_available() { MEMORYSTATUSEX statex; statex.dwLength = sizeof(statex); @@ -339,7 +339,7 @@ namespace lambdacommon::system return statex.ullAvailPhys; } - uint64_t LAMBDACOMMON_API get_memory_used() + u64 LAMBDACOMMON_API get_memory_used() { return get_memory_total() - get_memory_available(); } @@ -495,28 +495,28 @@ namespace lambdacommon::system return u_name.machine; } - uint64_t LAMBDACOMMON_API get_memory_total() { + u64 LAMBDACOMMON_API get_memory_total() { #ifdef _SC_PHYS_PAGES long pages = sysconf(_SC_PHYS_PAGES); long page_size = sysconf(_SC_PAGESIZE); - return static_cast(pages * page_size); + return static_cast(pages * page_size); #elif defined(LAMBDA_MEM_TOTAL_KEY) - uint64_t mem; + u64 mem; size_t len = sizeof(mem); int mib[2]; mib[0] = CTL_HW; mib[1] = LAMBDA_MEM_TOTAL_KEY; sysctl(mib, 2, &size, &len, nullptr, 0); #else - uint64_t mem; + u64 mem; size_t len = sizeof(mem); sysctlbyname("hw.memsize", &mem, &len, nullptr, 0); return mem / sysconf(_SC_PAGE_SIZE); #endif } - uint64_t LAMBDACOMMON_API get_memory_available() { - uint64_t mem_free = 0; + u64 LAMBDACOMMON_API get_memory_available() { + u64 mem_free = 0; #ifdef LAMBDA_MAC_OSX vm_size_t page_size; mach_port_t mach_port; @@ -526,15 +526,15 @@ namespace lambdacommon::system mach_port = mach_host_self(); count = sizeof(vm_stats) / sizeof(natural_t); if (KERN_SUCCESS == host_page_size(mach_port, &page_size) && KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO, (host_info64_t) &vm_stats, &count)) - mem_free = static_cast(vm_stats.free_count * page_size); + mem_free = static_cast(vm_stats.free_count * page_size); #elif defined(LAMBDA_FREEBSD) || defined(LAMBDA_DRAGONFLY) long page_size = sysconf(_SC_PAGESIZE); - uint32_t mem_inactive, mem_unused, mem_cache; + u32 mem_inactive, mem_unused, mem_cache; size_t mem_inactive_len = sizeof(mem_inactive), mem_unused_len = sizeof(mem_unused), mem_cache_len = sizeof(mem_cache); sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &mem_inactive_len, nullptr, 0); sysctlbyname("vm.stats.vm.v_free_count", &mem_unused, &mem_unused_len, nullptr, 0); sysctlbyname("vm.stats.vm.v_cache_count", &mem_cache, &mem_cache_len, nullptr, 0); - mem_free = static_cast(page_size * (mem_inactive + mem_unused + mem_cache)); + mem_free = static_cast(page_size * (mem_inactive + mem_unused + mem_cache)); #elif !defined(LAMBDA_WASM) std::ifstream meminfo; meminfo.open("/proc/meminfo", std::ios::in); @@ -559,11 +559,11 @@ namespace lambdacommon::system return mem_free; } - uint64_t LAMBDACOMMON_API get_memory_used() { + u64 LAMBDACOMMON_API get_memory_used() { std::ifstream meminfo; meminfo.open("/proc/meminfo", std::ios::in); - uint64_t mem_used = 0; + u64 mem_used = 0; if (meminfo.is_open()) { std::string last; diff --git a/src/system/terminal.cpp b/src/system/terminal.cpp index 6c2b612..8f5a8d7 100644 --- a/src/system/terminal.cpp +++ b/src/system/terminal.cpp @@ -23,546 +23,543 @@ #else # include # include -# include +# include # include #endif -namespace lambdacommon +namespace lambdacommon::terminal { - namespace terminal - { - bool _use_ansi = false; - bool _has_utf8 = false; - - /* - * INTERNAL - */ - inline FILE* get_standard_stream(const std::ostream& stream) { - if (&stream == &std::cout) - return stdout; - else if ((&stream == &std::cerr) || (&stream == &std::clog)) - return stderr; - - return nullptr; - } + bool _use_ansi = false; + bool _has_utf8 = false; + + /* + * INTERNAL + */ + inline FILE* get_standard_stream(const std::ostream& stream) { + if (&stream == &std::cout) + return stdout; + else if ((&stream == &std::cerr) || (&stream == &std::clog)) + return stderr; + + return nullptr; + } #ifdef WIN_FRIENDLY - HANDLE get_term_handle(const std::ostream &stream) - { - // Get terminal handle. - HANDLE h_terminal = INVALID_HANDLE_VALUE; - if (&stream == &std::cout) - h_terminal = GetStdHandle(STD_OUTPUT_HANDLE); - else if (&stream == &std::cerr) - h_terminal = GetStdHandle(STD_ERROR_HANDLE); - return h_terminal; - } - - void cls(HANDLE h_console) - { - COORD coord_screen{0, 0}; // Home for the cursor. - DWORD c_chars_written; - CONSOLE_SCREEN_BUFFER_INFO csbi; - DWORD dw_con_size; - - // Get the number of character cells in the current buffer. - if (!GetConsoleScreenBufferInfo(h_console, &csbi)) - return; - - dw_con_size = static_cast(csbi.dwSize.X * csbi.dwSize.Y); - - // Fill the entire screen with blanks. - if (!FillConsoleOutputCharacter(h_console, // Handle to console screen buffer. - (TCHAR) ' ', // Character to write to the buffer. - dw_con_size, // Number of cells to write. - coord_screen, // Coordinates of first cell. - &c_chars_written)) // Receive number of characters written. - return; - - // Get the current text attribute. - if (!GetConsoleScreenBufferInfo(h_console, &csbi)) - return; - - // Set the buffer's attributes accordingly. - if (!FillConsoleOutputAttribute(h_console, // Handle to console screen buffer - csbi.wAttributes, // Character attributes to use - dw_con_size, // Number of cells to set attribute - coord_screen, // Coordinates of first cell - &c_chars_written)) // Receive number of characters written - return; - - // Put the cursor at its home coordinates. - SetConsoleCursorPosition(h_console, coord_screen); - } + HANDLE get_term_handle(const std::ostream &stream) + { + // Get terminal handle. + HANDLE h_terminal = INVALID_HANDLE_VALUE; + if (&stream == &std::cout) + h_terminal = GetStdHandle(STD_OUTPUT_HANDLE); + else if (&stream == &std::cerr) + h_terminal = GetStdHandle(STD_ERROR_HANDLE); + return h_terminal; + } - inline void win_change_attributes(std::ostream &stream, int foreground, int background) - { - static WORD default_attributes = 0; + void cls(HANDLE h_console) + { + COORD coord_screen{0, 0}; // Home for the cursor. + DWORD c_chars_written; + CONSOLE_SCREEN_BUFFER_INFO csbi; + DWORD dw_con_size; + + // Get the number of character cells in the current buffer. + if (!GetConsoleScreenBufferInfo(h_console, &csbi)) + return; + + dw_con_size = static_cast(csbi.dwSize.X * csbi.dwSize.Y); + + // Fill the entire screen with blanks. + if (!FillConsoleOutputCharacter(h_console, // Handle to console screen buffer. + (TCHAR) ' ', // Character to write to the buffer. + dw_con_size, // Number of cells to write. + coord_screen, // Coordinates of first cell. + &c_chars_written)) // Receive number of characters written. + return; + + // Get the current text attribute. + if (!GetConsoleScreenBufferInfo(h_console, &csbi)) + return; + + // Set the buffer's attributes accordingly. + if (!FillConsoleOutputAttribute(h_console, // Handle to console screen buffer + csbi.wAttributes, // Character attributes to use + dw_con_size, // Number of cells to set attribute + coord_screen, // Coordinates of first cell + &c_chars_written)) // Receive number of characters written + return; + + // Put the cursor at its home coordinates. + SetConsoleCursorPosition(h_console, coord_screen); + } - // Get terminal handle - auto h_terminal = get_term_handle(stream); + inline void win_change_attributes(std::ostream &stream, int foreground, int background) + { + static WORD default_attributes = 0; - // Save default terminal attributes if it unsaved - if (!default_attributes) { - CONSOLE_SCREEN_BUFFER_INFO info; - if (!GetConsoleScreenBufferInfo(h_terminal, &info)) - return; - default_attributes = info.wAttributes; - } + // Get terminal handle + auto h_terminal = get_term_handle(stream); - // Restore all default settings - if (foreground == -1 && background == -1) { - SetConsoleTextAttribute(h_terminal, default_attributes); - return; - } - - // Get current settings + // Save default terminal attributes if it unsaved + if (!default_attributes) { CONSOLE_SCREEN_BUFFER_INFO info; if (!GetConsoleScreenBufferInfo(h_terminal, &info)) return; + default_attributes = info.wAttributes; + } - if (foreground != -1) { - info.wAttributes &= ~(info.wAttributes & 0x0F); - info.wAttributes |= static_cast(foreground); - } + // Restore all default settings + if (foreground == -1 && background == -1) { + SetConsoleTextAttribute(h_terminal, default_attributes); + return; + } - if (background != -1) { - info.wAttributes &= ~(info.wAttributes & 0xF0); - info.wAttributes |= static_cast(background); - } + // Get current settings + CONSOLE_SCREEN_BUFFER_INFO info; + if (!GetConsoleScreenBufferInfo(h_terminal, &info)) + return; + + if (foreground != -1) { + info.wAttributes &= ~(info.wAttributes & 0x0F); + info.wAttributes |= static_cast(foreground); + } - SetConsoleTextAttribute(h_terminal, info.wAttributes); + if (background != -1) { + info.wAttributes &= ~(info.wAttributes & 0xF0); + info.wAttributes |= static_cast(background); } + SetConsoleTextAttribute(h_terminal, info.wAttributes); + } + #else #define RD_EOF -1 #define RD_EIO -2 - static inline int rd(const int fd) { - unsigned char buffer[4]; - - while (1) { - ssize_t n = read(fd, buffer, 1); - if (n != (ssize_t) -1) - buffer[n] = '\0'; - - if (n > (ssize_t) 0) - return buffer[0]; - else if (n == (ssize_t) 0) - return RD_EOF; - else if (n != (ssize_t) -1) - return RD_EIO; - else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) - return RD_EIO; - } + static inline int rd(const int fd) { + unsigned char buffer[4]; + + while (1) { + ssize_t n = read(fd, buffer, 1); + if (n != (ssize_t) -1) + buffer[n] = '\0'; + + if (n > (ssize_t) 0) + return buffer[0]; + else if (n == (ssize_t) 0) + return RD_EOF; + else if (n != (ssize_t) -1) + return RD_EIO; + else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) + return RD_EIO; } + } - static inline int wr(const int fd, const char* const data, const size_t bytes) { - const char* head = data; - const char* const tail = data + bytes; - - while (head < tail) { - ssize_t n = write(fd, head, (size_t) (tail - head)); - if (n > (ssize_t) 0) - head += n; - else if (n != (ssize_t) -1) - return EIO; - else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) - return errno; - } - - return 0; + static inline int wr(const int fd, const char* const data, const size_t bytes) { + const char* head = data; + const char* const tail = data + bytes; + + while (head < tail) { + ssize_t n = write(fd, head, (size_t) (tail - head)); + if (n > (ssize_t) 0) + head += n; + else if (n != (ssize_t) -1) + return EIO; + else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) + return errno; } + return 0; + } + #endif // WIN_FRIENDLY - /* - * IMPLEMENTATION - */ + /* + * IMPLEMENTATION + */ - std::ostream LAMBDACOMMON_API& operator<<(std::ostream& stream, TermFormatting term_formatting) { - std::vector formats{term_formatting}; - return stream << formats; - } + std::ostream& LAMBDACOMMON_API operator<<(std::ostream& stream, TermFormatting term_formatting) { + std::vector formats{term_formatting}; + return stream << formats; + } - std::ostream LAMBDACOMMON_API& operator<<(std::ostream& stream, const std::vector& term_formatting) { - if (is_tty(stream)) { + std::ostream& LAMBDACOMMON_API operator<<(std::ostream& stream, const std::vector& term_formatting) { + if (is_tty(stream)) { #ifdef WIN_FRIENDLY - if (!_use_ansi) { - for (auto format : term_formatting) { - switch (format) { - case RESET: - win_change_attributes(stream, -1, -1); - break; - case BLACK: - win_change_attributes(stream, 0x0, -1); - break; - case BLUE: - win_change_attributes(stream, FOREGROUND_BLUE, -1); - break; - case GREEN: - win_change_attributes(stream, FOREGROUND_GREEN, -1); - break; - case CYAN: - win_change_attributes(stream, FOREGROUND_BLUE | FOREGROUND_GREEN, -1); - break; - case RED: - win_change_attributes(stream, FOREGROUND_RED, -1); - break; - case MAGENTA: - win_change_attributes(stream, 0x5, -1); - break; - case YELLOW: - win_change_attributes(stream, 0x6, -1); - break; - case LIGHT_GRAY: - win_change_attributes(stream, 0x7, -1); - break; - case DARK_GRAY: - win_change_attributes(stream, 0x8, -1); - break; - case LIGHT_BLUE: - win_change_attributes(stream, FOREGROUND_BLUE | FOREGROUND_INTENSITY, -1); - break; - case LIGHT_GREEN: - win_change_attributes(stream, FOREGROUND_GREEN | FOREGROUND_INTENSITY, -1); - break; - case LIGHT_CYAN: - win_change_attributes(stream, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY, -1); - break; - case LIGHT_RED: - win_change_attributes(stream, FOREGROUND_RED | FOREGROUND_INTENSITY, -1); - break; - case LIGHT_MAGENTA: - win_change_attributes(stream, 0xD, -1); - break; - case LIGHT_YELLOW: - win_change_attributes(stream, 0xE, -1); - break; - case WHITE: - win_change_attributes(stream, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, -1); - break; - case B_BLACK: - win_change_attributes(stream, -1, 0x00); - break; - case B_BLUE: - win_change_attributes(stream, -1, BACKGROUND_BLUE); - break; - case B_GREEN: - win_change_attributes(stream, -1, BACKGROUND_GREEN); - break; - case B_CYAN: - win_change_attributes(stream, -1, BACKGROUND_BLUE | BACKGROUND_GREEN); - break; - case B_RED: - win_change_attributes(stream, -1, BACKGROUND_RED); - break; - case B_MAGENTA: - win_change_attributes(stream, -1, (0x5 << 4)); - break; - case B_YELLOW: - win_change_attributes(stream, -1, (0x6 << 4)); - break; - case B_LIGHT_GRAY: - win_change_attributes(stream, -1, (0x7 << 4)); - break; - case B_DARK_GRAY: - win_change_attributes(stream, -1, (0x8 << 4)); - break; - case B_LIGHT_BLUE: - win_change_attributes(stream, -1, (0x9 << 4)); - break; - case B_LIGHT_GREEN: - win_change_attributes(stream, -1, (0xA << 4)); - break; - case B_LIGHT_CYAN: - win_change_attributes(stream, -1, (0xB << 4)); - break; - case B_LIGHT_RED: - win_change_attributes(stream, -1, (0xC << 4)); - break; - case B_LIGHT_MAGENTA: - win_change_attributes(stream, -1, (0xD << 4)); - break; - case B_LIGHT_YELLOW: - win_change_attributes(stream, -1, (0xE << 4)); - break; - case B_WHITE: - win_change_attributes(stream, -1, (0xF << 4)); - break; - default: - break; - } + if (!_use_ansi) { + for (auto format : term_formatting) { + switch (format) { + case RESET: + win_change_attributes(stream, -1, -1); + break; + case BLACK: + win_change_attributes(stream, 0x0, -1); + break; + case BLUE: + win_change_attributes(stream, FOREGROUND_BLUE, -1); + break; + case GREEN: + win_change_attributes(stream, FOREGROUND_GREEN, -1); + break; + case CYAN: + win_change_attributes(stream, FOREGROUND_BLUE | FOREGROUND_GREEN, -1); + break; + case RED: + win_change_attributes(stream, FOREGROUND_RED, -1); + break; + case MAGENTA: + win_change_attributes(stream, 0x5, -1); + break; + case YELLOW: + win_change_attributes(stream, 0x6, -1); + break; + case LIGHT_GRAY: + win_change_attributes(stream, 0x7, -1); + break; + case DARK_GRAY: + win_change_attributes(stream, 0x8, -1); + break; + case LIGHT_BLUE: + win_change_attributes(stream, FOREGROUND_BLUE | FOREGROUND_INTENSITY, -1); + break; + case LIGHT_GREEN: + win_change_attributes(stream, FOREGROUND_GREEN | FOREGROUND_INTENSITY, -1); + break; + case LIGHT_CYAN: + win_change_attributes(stream, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY, -1); + break; + case LIGHT_RED: + win_change_attributes(stream, FOREGROUND_RED | FOREGROUND_INTENSITY, -1); + break; + case LIGHT_MAGENTA: + win_change_attributes(stream, 0xD, -1); + break; + case LIGHT_YELLOW: + win_change_attributes(stream, 0xE, -1); + break; + case WHITE: + win_change_attributes(stream, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, -1); + break; + case B_BLACK: + win_change_attributes(stream, -1, 0x00); + break; + case B_BLUE: + win_change_attributes(stream, -1, BACKGROUND_BLUE); + break; + case B_GREEN: + win_change_attributes(stream, -1, BACKGROUND_GREEN); + break; + case B_CYAN: + win_change_attributes(stream, -1, BACKGROUND_BLUE | BACKGROUND_GREEN); + break; + case B_RED: + win_change_attributes(stream, -1, BACKGROUND_RED); + break; + case B_MAGENTA: + win_change_attributes(stream, -1, (0x5 << 4)); + break; + case B_YELLOW: + win_change_attributes(stream, -1, (0x6 << 4)); + break; + case B_LIGHT_GRAY: + win_change_attributes(stream, -1, (0x7 << 4)); + break; + case B_DARK_GRAY: + win_change_attributes(stream, -1, (0x8 << 4)); + break; + case B_LIGHT_BLUE: + win_change_attributes(stream, -1, (0x9 << 4)); + break; + case B_LIGHT_GREEN: + win_change_attributes(stream, -1, (0xA << 4)); + break; + case B_LIGHT_CYAN: + win_change_attributes(stream, -1, (0xB << 4)); + break; + case B_LIGHT_RED: + win_change_attributes(stream, -1, (0xC << 4)); + break; + case B_LIGHT_MAGENTA: + win_change_attributes(stream, -1, (0xD << 4)); + break; + case B_LIGHT_YELLOW: + win_change_attributes(stream, -1, (0xE << 4)); + break; + case B_WHITE: + win_change_attributes(stream, -1, (0xF << 4)); + break; + default: + break; } - return stream; - } else - goto write_ansi; -#else - goto write_ansi; -#endif + } + return stream; } else goto write_ansi; - write_ansi: - if (!_use_ansi) - return stream; - std::string ansi_sequence{((char) 0x1B)}; - ansi_sequence += "["; - auto formattings = term_formatting.size(); - for (size_t i = 0; i < formattings; i++) { - auto str = std::to_string(static_cast(term_formatting[i])); - if (i != formattings - 1) - ansi_sequence += (str + ";"); - else - ansi_sequence += str; - } - ansi_sequence += "m"; - stream << ansi_sequence; +#else + goto write_ansi; +#endif + } else + goto write_ansi; + write_ansi: + if (!_use_ansi) return stream; + std::string ansi_sequence{((char) 0x1B)}; + ansi_sequence += "["; + auto formattings = term_formatting.size(); + for (size_t i = 0; i < formattings; i++) { + auto str = std::to_string(static_cast(term_formatting[i])); + if (i != formattings - 1) + ansi_sequence += (str + ";"); + else + ansi_sequence += str; } + ansi_sequence += "m"; + stream << ansi_sequence; + return stream; + } - std::ostream LAMBDACOMMON_API& operator<<(std::ostream& stream, const std::vector& string_vector) { - stream << lstring::to_string(string_vector); - return stream; - } + std::ostream& LAMBDACOMMON_API operator<<(std::ostream& stream, const std::vector& string_vector) { + stream << lstring::to_string(string_vector); + return stream; + } - void erase_current_line_ansi(std::ostream& stream) { - if (_use_ansi) - stream << "\033[2K"; - } + void erase_current_line_ansi(std::ostream& stream) { + if (_use_ansi) + stream << "\033[2K"; + } - std::ostream LAMBDACOMMON_API& erase_current_line(std::ostream& stream) { + std::ostream& LAMBDACOMMON_API erase_current_line(std::ostream& stream) { #ifdef WIN_FRIENDLY - if (_use_ansi) { - erase_current_line_ansi(stream); - stream << '\r'; - } else { - auto current_pos = get_cursor_position(stream); + if (_use_ansi) { + erase_current_line_ansi(stream); + stream << '\r'; + } else { + auto current_pos = get_cursor_position(stream); + set_cursor_position(0, current_pos.get_y(), stream); + if (is_tty(stream)) { + auto size = get_size(stream); + for (size_t i = 0; i < size.get_width(); i++) + stream << ' '; set_cursor_position(0, current_pos.get_y(), stream); - if (is_tty(stream)) { - auto size = get_size(stream); - for (size_t i = 0; i < size.get_width(); i++) - stream << ' '; - set_cursor_position(0, current_pos.get_y(), stream); - } } + } #else - erase_current_line_ansi(stream); - stream << '\r'; + erase_current_line_ansi(stream); + stream << '\r'; #endif - return stream; - } + return stream; + } - std::ostream LAMBDACOMMON_API& clear(std::ostream& stream) { + std::ostream& LAMBDACOMMON_API clear(std::ostream& stream) { #ifdef WIN_FRIENDLY - if (is_tty(stream)) { - cls(get_term_handle(stream)); - return stream; - } -#endif - if (_use_ansi) - stream << "\033[2J"; + if (is_tty(stream)) { + cls(get_term_handle(stream)); return stream; } +#endif + if (_use_ansi) + stream << "\033[2J"; + return stream; + } - Point2D_u16 LAMBDACOMMON_API get_cursor_position(std::ostream& stream) { - if (is_tty(stream)) { + Point2D_u16 LAMBDACOMMON_API get_cursor_position(std::ostream& stream) { + if (is_tty(stream)) { #ifdef WIN_FRIENDLY - CONSOLE_SCREEN_BUFFER_INFO csbi; - if (GetConsoleScreenBufferInfo(get_term_handle(stream), &csbi)) - return {static_cast(csbi.dwCursorPosition.X), static_cast(csbi.dwCursorPosition.Y)}; + CONSOLE_SCREEN_BUFFER_INFO csbi; + if (GetConsoleScreenBufferInfo(get_term_handle(stream), &csbi)) + return {static_cast(csbi.dwCursorPosition.X), static_cast(csbi.dwCursorPosition.Y)}; #else - // Based on https://www.linuxquestions.org/questions/programming-9/get-cursor-position-in-c-947833/#post4693254. - struct termios saved{}, temp{}; - int result; - auto tty = fileno(get_standard_stream(stream)); - Point2D_u16 cursor_pos(0, 0); - - // Save current terminal settings. + // Based on https://www.linuxquestions.org/questions/programming-9/get-cursor-position-in-c-947833/#post4693254. + struct termios saved{}, temp{}; + int result; + auto tty = fileno(get_standard_stream(stream)); + Point2D_u16 cursor_pos(0, 0); + + // Save current terminal settings. + do { + result = tcgetattr(tty, &saved); + } while (result == -1 && errno == EINTR); + if (result == -1) return cursor_pos; + + // Get current terminal settings. + do { + result = tcgetattr(tty, &temp); + } while (result == -1 && errno == EINTR); + if (result == -1) return cursor_pos; + + // Disables ICANON, ECHO and CREAD. + temp.c_lflag &= ~ICANON; + temp.c_lflag &= ~ECHO; + temp.c_lflag &= ~CREAD; + + do { do { - result = tcgetattr(tty, &saved); + result = tcsetattr(tty, TCSANOW, &temp); } while (result == -1 && errno == EINTR); - if (result == -1) return cursor_pos; + if (result == -1) break; - // Get current terminal settings. - do { - result = tcgetattr(tty, &temp); - } while (result == -1 && errno == EINTR); - if (result == -1) return cursor_pos; - - // Disables ICANON, ECHO and CREAD. - temp.c_lflag &= ~ICANON; - temp.c_lflag &= ~ECHO; - temp.c_lflag &= ~CREAD; - - do { - do { - result = tcsetattr(tty, TCSANOW, &temp); - } while (result == -1 && errno == EINTR); - if (result == -1) break; - - // Request cursor coordinates from the terminal. - int retval = wr(tty, "\033[6n", 4); - if (retval) - break; + // Request cursor coordinates from the terminal. + int retval = wr(tty, "\033[6n", 4); + if (retval) + break; - // Expect an ESC - result = rd(tty); - if (result != 27) - break; + // Expect an ESC + result = rd(tty); + if (result != 27) + break; - // Expect [ - result = rd(tty); - if (result != '[') - break; + // Expect [ + result = rd(tty); + if (result != '[') + break; - // Parse rows. - uint16_t rows = 0; + // Parse rows. + u16 rows = 0; + result = rd(tty); + while (result >= '0' && result <= '9') { + rows = static_cast(10 * rows + result - '0'); result = rd(tty); - while (result >= '0' && result <= '9') { - rows = static_cast(10 * rows + result - '0'); - result = rd(tty); - } + } - if (result != ';') - break; + if (result != ';') + break; - // Parse columns. - uint16_t columns = 0; + // Parse columns. + u16 columns = 0; + result = rd(tty); + while (result >= '0' && result <= '9') { + columns = static_cast(10 * columns + result - '0'); result = rd(tty); - while (result >= '0' && result <= '9') { - columns = static_cast(10 * columns + result - '0'); - result = rd(tty); - } - - if (result != 'R') - break; + } - cursor_pos.set_x(static_cast(columns - 1)); - cursor_pos.set_y(static_cast(rows - 1)); + if (result != 'R') break; - } while (true); - do { - result = tcsetattr(tty, TCSANOW, &saved); - } while (result == -1 && errno == EINTR); + cursor_pos.set_x(static_cast(columns - 1)); + cursor_pos.set_y(static_cast(rows - 1)); + break; + } while (true); + + do { + result = tcsetattr(tty, TCSANOW, &saved); + } while (result == -1 && errno == EINTR); - return cursor_pos; + return cursor_pos; #endif - } - return {0, 0}; } + return {0, 0}; + } - void LAMBDACOMMON_API set_cursor_position(unsigned short x, unsigned short y, std::ostream& stream) { + void LAMBDACOMMON_API set_cursor_position(unsigned short x, unsigned short y, std::ostream& stream) { #ifdef WIN_FRIENDLY - if (is_tty(stream)) { - COORD coord; - coord.X = x; - coord.Y = y; - SetConsoleCursorPosition(get_term_handle(stream), coord); - return; - } -#endif - if (_use_ansi) - stream << ("\033[" + std::to_string(y) + ';' + std::to_string(x) + "H"); + if (is_tty(stream)) { + COORD coord; + coord.X = x; + coord.Y = y; + SetConsoleCursorPosition(get_term_handle(stream), coord); + return; } +#endif + if (_use_ansi) + stream << ("\033[" + std::to_string(y) + ';' + std::to_string(x) + "H"); + } - /* - * Sound manipulations - */ + /* + * Sound manipulations + */ - std::ostream LAMBDACOMMON_API& bell(std::ostream& stream) { - stream << ((char) 0x7); - return stream; - } + std::ostream& LAMBDACOMMON_API bell(std::ostream& stream) { + stream << ((char) 0x7); + return stream; + } - /* - * Terminal manipulations - */ + /* + * Terminal manipulations + */ - bool LAMBDACOMMON_API setup(bool force_ansi) { - bool ok = true; + bool LAMBDACOMMON_API setup(bool force_ansi) { + bool ok = true; #ifdef WIN_FRIENDLY - if (HANDLE h_out = GetStdHandle(STD_OUTPUT_HANDLE); h_out != INVALID_HANDLE_VALUE) { - if (DWORD dw_mode = 0; GetConsoleMode(h_out, &dw_mode)) { - dw_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; - if (SetConsoleMode(h_out, dw_mode)) - _use_ansi = true; - } + if (HANDLE h_out = GetStdHandle(STD_OUTPUT_HANDLE); h_out != INVALID_HANDLE_VALUE) { + if (DWORD dw_mode = 0; GetConsoleMode(h_out, &dw_mode)) { + dw_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if (SetConsoleMode(h_out, dw_mode)) + _use_ansi = true; } - if (!_use_ansi) - ok = false; + } + if (!_use_ansi) + ok = false; #elif !defined(LAMBDA_WASM) - _use_ansi = true; + _use_ansi = true; #endif - if (!_use_ansi && force_ansi) - _use_ansi = force_ansi; - if (!use_utf8()) - ok = false; - return ok; - } + if (!_use_ansi && force_ansi) + _use_ansi = force_ansi; + if (!use_utf8()) + ok = false; + return ok; + } - bool LAMBDACOMMON_API use_utf8() { + bool LAMBDACOMMON_API use_utf8() { #ifdef WIN_FRIENDLY - if (SetConsoleCP(CP_UTF8) && SetConsoleOutputCP(CP_UTF8)) - _has_utf8 = true; + if (SetConsoleCP(CP_UTF8) && SetConsoleOutputCP(CP_UTF8)) + _has_utf8 = true; #else - if (setlocale(LC_ALL, "en_US.UTF-8") != nullptr) - _has_utf8 = true; + if (setlocale(LC_ALL, "en_US.UTF-8") != nullptr) + _has_utf8 = true; #endif - return _has_utf8; - } + return _has_utf8; + } - bool LAMBDACOMMON_API has_utf8() { - return _has_utf8; - } + bool LAMBDACOMMON_API has_utf8() { + return _has_utf8; + } - bool LAMBDACOMMON_API is_tty(const std::ostream& stream) { - FILE* std_stream = get_standard_stream(stream); + bool LAMBDACOMMON_API is_tty(const std::ostream& stream) { + FILE* std_stream = get_standard_stream(stream); #ifdef LAMBDA_WINDOWS - return ::_isatty(_fileno(std_stream)) != 0; + return ::_isatty(_fileno(std_stream)) != 0; #else - return static_cast(::isatty(fileno(std_stream))); + return static_cast(::isatty(fileno(std_stream))); #endif - } + } - std::string LAMBDACOMMON_API get_title() { + std::string LAMBDACOMMON_API get_title() { #ifdef WIN_FRIENDLY - if (TCHAR current_title[MAX_PATH]; GetConsoleTitle(current_title, MAX_PATH)) - return {current_title}; - else return ""; + if (TCHAR current_title[MAX_PATH]; GetConsoleTitle(current_title, MAX_PATH)) + return {current_title}; + else return ""; #else - return ""; + return ""; #endif - } + } - bool LAMBDACOMMON_API set_title(const std::string& title, std::ostream& stream) { + bool LAMBDACOMMON_API set_title(const std::string& title, std::ostream& stream) { #ifdef WIN_FRIENDLY - if (is_tty(stream)) - return (bool) SetConsoleTitle(TEXT(title.c_str())); + if (is_tty(stream)) + return (bool) SetConsoleTitle(TEXT(title.c_str())); #endif - if (_use_ansi) - stream << ("\033]0;" + title + "\007"); - return true; - } + if (_use_ansi) + stream << ("\033]0;" + title + "\007"); + return true; + } - const Size2D_u16 get_size(const std::ostream& stream) { - Size2D_u16 size{}; + const Size2D_u16 get_size(const std::ostream& stream) { + Size2D_u16 size{}; #ifdef WIN_FRIENDLY - CONSOLE_SCREEN_BUFFER_INFO csbi; + CONSOLE_SCREEN_BUFFER_INFO csbi; - GetConsoleScreenBufferInfo(get_term_handle(stream), &csbi); - size.set_width(static_cast(csbi.srWindow.Right - csbi.srWindow.Left + 1)); - size.set_height(static_cast(csbi.srWindow.Bottom - csbi.srWindow.Top + 1)); + GetConsoleScreenBufferInfo(get_term_handle(stream), &csbi); + size.set_width(static_cast(csbi.srWindow.Right - csbi.srWindow.Left + 1)); + size.set_height(static_cast(csbi.srWindow.Bottom - csbi.srWindow.Top + 1)); #else - struct winsize w{}; - ioctl(fileno(get_standard_stream(stream)), TIOCGWINSZ, &w); - size.set_width(w.ws_col); - size.set_height(w.ws_row); + struct winsize w{}; + ioctl(fileno(get_standard_stream(stream)), TIOCGWINSZ, &w); + size.set_width(w.ws_col); + size.set_height(w.ws_row); #endif - return size; - } + return size; } } diff --git a/src/system/time.cpp b/src/system/time.cpp index c1c0b11..820e6fb 100644 --- a/src/system/time.cpp +++ b/src/system/time.cpp @@ -9,12 +9,9 @@ #include "../../include/lambdacommon/system/time.h" -namespace lambdacommon +namespace lambdacommon::time { - namespace time - { - time_t LAMBDACOMMON_API get_time_millis() { - return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - } + time_t LAMBDACOMMON_API get_time_millis() { + return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); } } diff --git a/tests/test.cpp b/tests/test.cpp index 267698f..b6484be 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -15,9 +15,9 @@ using namespace lstring::stream; using namespace terminal; using namespace std; -const ResourceName BASE_RESOURCENAME{"tests", "value/path"}; +const Identifier BASE_RESOURCENAME{"tests", "value/path"}; -bool test(const string& test_name, const std::function& func) { +auto test(const string& test_name, const std::function& func) -> bool { if (test_name.find('"') != string::npos) { cout << "TESTING "; bool state = false; @@ -165,13 +165,13 @@ LC_TEST_SECTION(Size) LC_TEST_SECTION(ResourceNames) { - LC_TEST(rsc_name, "ResourceName(const std::string &resource_name)") { - REQUIRE(ResourceName("tests:value/path").to_string() == "tests:value/path"); + LC_TEST(rsc_name, "Identifier(const std::string &resource_name)") { + REQUIRE(Identifier("tests:value/path").to_string() == "tests:value/path"); } - LC_TEST(rsc_operators, "ResourceName operators") { - REQUIRE(ResourceName("tests:value/path") != ResourceName("tests", "value/OwO")); - REQUIRE(ResourceName("tests:value/path") / "owo" == ResourceName("tests", "value/path/owo")); + LC_TEST(rsc_operators, "Identifier operators") { + REQUIRE(Identifier("tests:value/path") != Identifier("tests", "value/OwO")); + REQUIRE(Identifier("tests:value/path") / "owo" == Identifier("tests", "value/path/owo")); } } @@ -182,7 +182,7 @@ LC_TEST_SECTION(Color) } } -int main() { +auto main() -> int { setup(); set_title("λcommon - tests"); cout << "Starting lambdacommon-tests with" << CYAN << " lambdacommon" << RESET << " v"