Skip to content

KendallHarter/cpp-dyn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpp-dyn

A C++26 library for Rust-like dyn traits

This library depends on reflection, which is currently only implemented here, so it is not ready for "real" use yet.

Installation

This is a single header library. The source can be copied from include/khct/cpp_dyn.hpp.

It is also designed to be used from CMake, either as a subdirectory or an external project:

include(FetchContent)

FetchContent_Declare(
   cpp_dyn
   GIT_REPOSITORY https://github.com/KendallHarter/cpp-dyn
   GIT_TAG main
)

FetchContent_MakeAvailable(cpp_dyn)

target_link_library(my_target PRIVATE cpp_dyn)

Basic Usage

#include "khct/cpp_dyn.hpp"
#include <cassert>

// Defining an interface
struct [[=khct::trait]] my_interface {
   int get_data() const noexcept;
   void set_data(int) noexcept;
};

// Defining a class that implements that interface
struct [[=khct::impl_for<my_interface>]] my_struct {
   int get_data() const noexcept { return data_; }
   void set_data(int new_data) noexcept { data_ = new_data; }

private:
   int data_ = 1;
};

// Using that interface
int take_interface(khct::non_owning_dyn_trait<my_interface> obj) noexcept
{
   obj.call(obj.set_data, 20);
   return obj.call(obj.get_data);
}

int main()
{
   my_struct s;
   assert(take_interface(khct::dyn<my_interface>(&s)) == 20);
}

See here for more detailed usage.

Limitations

Explicit this parameters are not supported.

Compilation errors are a nightmare.

About

A C++26 library for Rust-like dyn traits

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published