Skip to content

ZhekehZ/Cpp-resource-injector

Repository files navigation

Cpp-resource-injector [c++20]

A header only c++ library for resource injection

CMake Conan version - resource-injector/0.1@zhekehz/stable 20

Usage

CmakeLists.txt:

INJECT_RESOURCES(
    TARGET          target_name
    GENERATED_DIR   <some pah>
    RESOURCES_DIR   <resources dir>
    RESOURCES
        res.txt AS NAME1
        res2.txt AS NAME2
    COMPILE_TIME_RESOURCES
        compile_time.txt AS NAME3
)

Code.cpp:

auto stream1 = get_resource_stream<injected_resources::NAME1>(); 
// The resource "NAME1" will be copied and linked to the binary file.

// If NAME2 is not used in the code, then the resource "NAME2" will NOT 
//  be copied and linked to the binary file. (*)

constinit auto stream2 = 
    get_resource_stream<constinit_injected_resources::NAME3>();
// The NAME3 resource will be injected at compile time

// (*) If compilation flags "-fdata-sections -ffunction-sections -Wl,--gc-sections" are used

Other features:

  • Compile time stream
    auto ct_lambda = [] {
        using namespace injector;
        auto fib_ct = injector::get_resource_stream<constinit_injected_resources::FIBONACCI_CT>();

        int x = fib_value;
        for (int expected : {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377}) {
            fib_ct >> fib_value;
            if (fib_value != expected) return false;
        }
        return true;
    };
    
    static_assert(ct_lambda());
  • Injected enum parsing
    injector::injected_resources resource;
    auto s = injector::get_resource_stream<...>(); /* file content: "FIBONACCI lorem ipsum" */
    s >> resource;
    assert(resource == injector::injected_resources::FIBONACCI);

Installation:

  • via Cmake
    mkdir -p build && \
    cmake -B build && \
    cmake --build build --target test && \
    sudo cmake --build build --target install
  • via Conan:
    1. add remote repo:
      conan remote add zhekehz-conan https://zhekehz.jfrog.io/artifactory/api/conan/zhekehz-conan
    2. Configure CMake project:
      conan_cmake_run(REQUIRES resource-injector/0.1@zhekehz/stable)
      conan_basic_setup()
      find_package(resource-injector)
      see the exmaple subproject for more details

TODO:

  • add alignment
  • refactor compile-time parsers
  • ??? support binary files for COMPILE_TIME_RESOURCES