Skip to content

A cross-platform C++20 system information library providing memory information, platform detection, and system utilities.

License

Notifications You must be signed in to change notification settings

moehoshio/NekoSystem

Repository files navigation

NekoSystem

A cross-platform C++20 system information library providing memory information, platform detection, and system utilities.

License Require CMake Module Support CI Status

Features

  • Platform-consistent paths: All paths use forward slashes / to avoid backslashes, ensuring cross-platform consistency
  • Memory Information: Get system memory statistics (total, free memory)
  • Platform Detection: Detect operating system (Windows, macOS, Linux) and architecture (x64, x86, ARM64, ARM)
  • System Utilities: Work with temporary directories, working paths, and home directories
  • Cross-platform: Supports Windows, macOS, and Linux
  • Modern C++: Built with C++20 features and best practices

Quick Start

Configure: CMake | Vcpkg | Conan | Test

Example: Memory Info | Platform Detection | System Utilities

Prerequisites

  • C++20 compatible compiler
  • CMake 3.14 or higher

Integration

CMake

include(FetchContent)
FetchContent_Declare(
    NekoSystem
    GIT_REPOSITORY https://github.com/moehoshio/NekoSystem.git
    GIT_TAG        main
)
FetchContent_MakeAvailable(NekoSystem)

# Add your target and link NekoSystem
add_executable(your_target main.cpp)

target_link_libraries(your_target PRIVATE Neko::System)

C++20 Module Support

NekoSystem supports C++20 modules. To use NekoSystem as a module, ensure your compiler supports C++20 modules and configure your project accordingly.

...
# Add NekoSystem to your CMake project
FetchContent_Declare(
    NekoSystem
    GIT_REPOSITORY https://github.com/moehoshio/NekoSystem.git
    GIT_TAG        main
)
 

# Set Variables Before Building
set(NEKO_SYSTEM_ENABLE_MODULE ON CACHE BOOL "Enable Module support" FORCE)
FetchContent_MakeAvailable(NekoSystem)

target_link_libraries(your_target PRIVATE Neko::System::Module)

Import the module in your C++ code:

import neko.system;

Vcpkg

To install NekoSystem using vcpkg, run the following command:

vcpkg install neko-system

Or add it to your vcpkg.json:

{
  "dependencies": ["neko-system"]
}

Then in your CMakeLists.txt:

find_package(NekoSystem CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE Neko::System)

When configuring your project, specify the vcpkg toolchain file:

cmake -B build -DCMAKE_PREFIX_PATH=/path/to/vcpkg/installed/x64-windows
cmake --build build --config Debug

Note: Installing via vcpkg does not support modules.

Conan

To install NekoSystem using Conan, add the following to your conanfile.txt:

[requires]
neko-system/[*]

[generators]
CMakeDeps
CMakeToolchain

Or use it in your conanfile.py:

from conan import ConanFile

class YourProject(ConanFile):
    requires = "neko-system/[*]"
    generators = "CMakeDeps", "CMakeToolchain"

Then install and use:

conan install . --build=missing
cmake -B build -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake
cmake --build build

In your CMakeLists.txt:

find_package(NekoSystem CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE Neko::System)

Conan with C++20 Module Support

To enable C++20 module support with Conan, use the enable_module option:

conan install . --build=missing -o neko-system/[*]:enable_module=True

Or specify it in your conanfile.txt:

[requires]
neko-system/[*]

[options]
neko-system/*:enable_module=True

[generators]
CMakeDeps
CMakeToolchain

Or in your conanfile.py:

from conan import ConanFile

class YourProject(ConanFile):
    requires = "neko-system/[*]"
    generators = "CMakeDeps", "CMakeToolchain"
    
    def configure(self):
        self.options["neko-system"].enable_module = True

Then link against the module target in your CMakeLists.txt:

find_package(NekoSystem CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE Neko::System::Module)

Usage Examples

Memory Information

#include <neko/system/memoryinfo.hpp>
#include <iostream>

int main() {
    auto memInfo = neko::system::getSystemMemoryInfo();
    if (memInfo) {
        std::cout << "Memory Info: " << memInfo->toString() << std::endl;
        std::cout << "Total: " << memInfo->totalBytes << " bytes" << std::endl;
        std::cout << "Free: " << memInfo->freeBytes << " bytes" << std::endl;
    }
    return 0;
}

Platform Detection

#include <neko/system/platform.hpp>
#include <iostream>

int main() {
    using namespace neko::system;
    
    std::cout << "OS: " << getOsName() << std::endl;
    std::cout << "Architecture: " << getOsArch() << std::endl;
    std::cout << "OS Version: " << getOsVersion() << std::endl;
    
    // Platform-specific checks
    if (isWindows()) {
        std::cout << "Running on Windows" << std::endl;
    } else if (isMacOS()) {
        std::cout << "Running on macOS" << std::endl;
    } else if (isLinux()) {
        std::cout << "Running on Linux" << std::endl;
    }
    
    return 0;
}

System Utilities

#include <neko/system/platform.hpp>
#include <iostream>

int main() {
    using namespace neko::system;
    
    // Get system directories
    std::cout << "Temp folder: " << tempFolder() << std::endl;
    std::cout << "Work path: " << workPath() << std::endl;
    
    auto home = getHome();
    if (home) {
        std::cout << "Home directory: " << *home << std::endl;
    }
    
    return 0;
}

API Reference

Memory Information API

neko::system::MemoryInfo

struct MemoryInfo {
    neko::uint64 totalBytes;  // Total physical memory in bytes
    neko::uint64 freeBytes;   // Free physical memory in bytes
    
    std::string toString() const;  // Human-readable format
};

neko::system::getSystemMemoryInfo()

std::optional<MemoryInfo> getSystemMemoryInfo();

Returns system memory information or std::nullopt on failure.

Platform Detection API

Constants

constexpr neko::cstr osName;    // "windows", "osx", "linux", or "unknown"
constexpr neko::cstr osArch;    // "x64", "x86", "arm64", "arm", or "unknown"

Functions

constexpr neko::cstr getOsName();           // Get OS name
constexpr neko::cstr getOsArch();           // Get architecture
std::string getOsVersion();                 // Get OS version string

// Platform detection helpers
constexpr bool isWindows();
constexpr bool isMacOS();
constexpr bool isLinux();

// Architecture detection helpers
constexpr bool isArchX64();
constexpr bool isArchX86();
constexpr bool isArchArm64();
constexpr bool isArchArm();

System Utilities API

// Directory management
std::string tempFolder(const std::string &setTempDir = "");
std::string workPath(const std::string &setPath = "");
std::optional<std::string> getHome();

Test

You can run the tests to verify that everything is working correctly.

If you haven't configured the build yet, please run:

cmake -D NEKO_SYSTEM_BUILD_TESTS=ON -D NEKO_SYSTEM_AUTO_FETCH_DEPS=ON -B ./build -S .

Now, you can build the test files with:

cmake --build ./build --config Debug

Then, you can run the tests with the following commands:

cd ./build && ctest --output-on-failure

If everything is set up correctly, you should see output similar to the following:

  Test project /path/to/NekoSystem/build
        Start  1: MemoryInfoTest.GetSystemMemoryInfoReturn 
  sValidResult
   1/41 Test  #1: MemoryInfoTest.GetSystemMemoryInfoReturn
  sValidResult ...   Passed    0.02 sec

#   ......

        Start 41: NekoFunction_tests
  41/41 Test #41: NekoFunction_tests .....................
  ................   Passed    0.02 sec
  
  100% tests passed, 0 tests failed out of 41

  Total Test time (real) =   0.81 sec

Disable Tests

If you want to disable building and running tests, you can set the following CMake option when configuring your project:

cmake -B ./build -DNEKO_SYSTEM_BUILD_TESTS=OFF -S .

or

...
# Set Variables Before Building
set(NEKO_SYSTEM_BUILD_TESTS OFF CACHE BOOL "Disable building tests" FORCE)
FetchContent_MakeAvailable(NekoSystem)

This will skip test targets during the build process.

License

License MIT OR Apache-2.0

See More

  • NekoLog: An easy-to-use, modern, lightweight, and efficient C++20 logging library.
  • NekoEvent: A modern easy to use type-safe and high-performance event handling system for C++.
  • NekoSchema: A lightweight, header-only C++20 schema library.
  • NekoSystem: A modern C++20 cross-platform system utility library.
  • NekoNetwork: A modern , easy-to-use C++20 networking library via libcurl.
  • NekoFunction: A comprehensive modern C++ utility library that provides practical functions for common programming tasks.
  • NekoThreadPool: An easy to use and efficient C++ 20 thread pool that supports priorities and submission to specific threads.

About

A cross-platform C++20 system information library providing memory information, platform detection, and system utilities.

Resources

License

Stars

Watchers

Forks

Packages

No packages published