在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Manu343726/siplasplas开源软件地址:https://github.com/Manu343726/siplasplas开源编程语言:C++ 79.9%开源软件介绍:siplasplasA library for C++ reflection and introspection FeaturesReflection metadataAll reflection metadata is processed by DRLParser, a python script that takes input about a proejct (Compilation options, include dirs, etc) and scans the project headers, generating C++ header files with the reflection information of the corresponding input header. All generated code is C++11 compatible. CMake integrationUsers should not worry about DRLParser and its input, a set of cmake
scripts is given to simplify reflection in user projects. Just include
add_library(MyLibrary myLib.cpp)
target_include_directories(MyLibrary PUBLIC include/)
target_compile_options(MyLibrary PRIVATE -std=c++11 -Wall)
configure_siplasplas_reflection(MyLibrary) This will add a custom pre-build target that automatically runs DRLParser and generates reflection metadata headers before building your library. Static reflectionSIplasplas provides a template-based API to access to static reflection information of user defined types: // particle.hpp
class Particle
{
public:
struct Position
{
float x, y, z;
};
struct Color
{
float a, r, g, b;
};
enum class State
{
Alive,
Dead
};
Position position;
Color color;
State state;
}; siplasplas uses a libclang based script to generate C++ code with all the metadata. After running this script, include both the user header and the generated header: #include <particle.hpp>
#include <reflection/particle.hpp> // Reflection data (generated code)
std::vector<Particle> particles;
std::ostream& operator<<(std::ostream& os, const Particle::Position& position)
{
using PositionClass = cpp::static_reflection::Class<Particle::Position>;
os << "{";
// For each coordinate in the Position class...
cpp::foreach_type<PositionClass::Fields>([&](auto type)
{
using Field = cpp::meta::type_t<decltype(type)>;
os << Field::spelling() << ": " // Field name ("x", "y", "z")
<< cpp::invoke(Field::get(), position) // Field value (Like C++17 invoke with member object ptr)
<< " ";
});
return os << "}";
}
std::ostream& operator<<(std::ostream& os, const Particle::Color& color)
{
using ColorClass = cpp::static_reflection::Class<Particle::Color>;
os << "{";
// For each channel in the Color class...
cpp::foreach_type<ColorClass::Fields>([&](auto type)
{
using Field = cpp::meta::type_t<decltype(type)>;
os << Field::spelling() << ": " // channel name (r, g, b, ...)
<< cpp::invoke(Field::get(), color)*255 // channel value
<< " ";
});
return os << "}";
}
std::ostream& operator<<(std::ostream& os, const Particle::State& state)
{
// Use static reflection to get the name of the enum value:
return os << cpp::static_reflection::Enum<Particle::State>::toString(state);
}
int main()
{
for(const auto& particle : particles)
{
std::cout << "position: " << particle.position << std::endl;
std::cout << "color: " << particle.color << std::endl;
std::cout << "state: " << particle.state << std::endl;
}
} The static reflection API currently supports:
Dynamic reflectionSiplasplas also supports dynamic reflection in the form of a simple entity based component system: cpp::dynamic_reflection::Runtime runtime = loadDynamicReflection();
// Get dynamic reflection info of the class ::Particle::Position:
cpp::dynamic_reflection::Class& positionClass = runtime.class_("Particle").class_("Position");
// Manipulate a particle object using dynamic reflection:
Particle particle;
positionCLass.field_("x").get(particle.position) = 42.0f; // particle.position.x = 42
// You can also create objects dynamically:
auto particle2 = runtime.class_("Particle").create();
// Returned objects are dynamically manipulable too:
particle2["color"]["r"] = 0.5f; The dynamic reflection API can be used to load APIs from external libraries at runtime in a straightforward way: int main()
{
cpp::DynamicLibrary lib{"libmylibrary.so"};
cpp::dynamic_reflection::Runtimeloader loader{lib};
cpp::dynamic_reflection::Runtime& runtime = loader.runtime();
auto myObject = runtime.class_("MyClass").create();
// Invoke MyClass::function with params 1 and "hello":
myObject("function")(1, std::string("hello!"));
} Other features:Siplasplas offers other features, building blocks for the APIs explained above, including:
Supported compilerssiplasplas has been tested in GCC 5.1/5.2/6.1, Clang 3.6/3.7/3.8, and Visual Studio 2015. DocumentationDocumentation is available here The documentation is available in Doxygen and Standardese format, each one with multiple versions corresponding to the latest documentation of each siplasplas release and active branch. Installation
TL;DRYou can build siplasplas from sources: $ git clone https://github.com/Manu343726/siplasplas --recursive
$ cd siplasplas
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . Or download the set(SIPLASPLAS_PACKAGE_URL <url to siplasplas release package>)
set(SIPLASPLAS_INSTALL_DRLPARSER_DEPENDENCIES ON) # Download DRLparser deps with pip
set(SIPLASPLAS_LIBCLANG_VERSION 3.8.0) # libclang version
set(SIPLASPLAS_DOWNLOAD_LIBCLANG ON) # Download and configure libclang automatically
include(bootstrap.cmake) This will download and configure a siplasplas installation in your buildtree. After including
find_package(Siplasplas)
target_link_libraries(MyLibrary PUBLIC siplasplas-reflection-dynamic) The module defines one imported library for each siplasplas module. All inter-module dependencies are already solved. Prerequisites
DependenciesAll siplasplas dependencies are managed automatically through CMake, users don't have to worry about installing deps. Anyway, here is the list of the thrid party dependencies of siplasplas:
siplasplas also depends on some python modules:
Download and configure the projectClone the siplasplas repository $ git clone https://github.com/Manu343726/siplasplas --recursive Create a $ cd siplasplas
$ mkdir build Run cmake in the build directory $ cd build
$ cmake ..
To build the library, invoke the default build target: $ cmake --build . # Or just "make" if using Makefiles generator ConfigurationThe default cmake invocation will build siplasplas as dynamic libraries (one per module) using the default generator. Also, siplasplas configuration can be modified using some options and variables:
AcknowledgementsMany thanks to:
Licensesiplasplas project is released under the MIT open source license. This license applies to all C++ sources, CMake scripts, and any other file except explicitly stated. |
2022-08-15
2022-08-17
2022-09-23
2022-08-18
2023-10-27
请发表评论