experiments with shared libraries

This commit is contained in:
BlackMATov
2024-01-28 02:39:14 +07:00
parent 28e42523b4
commit 9effcfbf56
5 changed files with 157 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ include(SetupTargets)
add_subdirectory(manuals)
add_subdirectory(singles)
add_subdirectory(unbench)
add_subdirectory(unshared)
add_subdirectory(untests)
add_subdirectory(vendors)

View File

@@ -0,0 +1,38 @@
project(meta.hpp.shared)
# lib
file(GLOB_RECURSE META_SHARED_LIB_SOURCES CONFIGURE_DEPENDS
"meta_shared_lib/*.cpp"
"meta_shared_lib/*.hpp")
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${META_SHARED_LIB_SOURCES})
add_library(${PROJECT_NAME}.lib SHARED ${META_SHARED_LIB_SOURCES})
add_library(${PROJECT_NAME}::lib ALIAS ${PROJECT_NAME}.lib)
target_link_libraries(${PROJECT_NAME}.lib PUBLIC
meta.hpp::meta.hpp
meta.hpp::setup_targets)
target_include_directories(${PROJECT_NAME}.lib PUBLIC
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/meta_shared_lib)
include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME}.lib)
# exe
file(GLOB_RECURSE META_SHARED_EXE_SOURCES CONFIGURE_DEPENDS
"meta_shared_exe/*.cpp"
"meta_shared_exe/*.hpp")
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${META_SHARED_EXE_SOURCES})
add_executable(${PROJECT_NAME}.exe ${META_SHARED_EXE_SOURCES})
target_link_libraries(${PROJECT_NAME}.exe PRIVATE
${PROJECT_NAME}::lib
meta.hpp.vendors::doctest
meta.hpp.vendors::fmt)
add_test(${PROJECT_NAME}.exe ${PROJECT_NAME}.exe)

View File

@@ -0,0 +1,41 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/meta.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2021-2024, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <meta_shared_lib.hpp>
#include <doctest/doctest.h>
TEST_CASE("meta/meta_shared/exe") {
namespace meta = meta_hpp;
namespace shared = meta_shared_lib;
const meta::scope library_scope = shared::get_library_scope();
REQUIRE(library_scope);
//
const meta::number_type int_type = library_scope.get_typedef("int").as_number();
REQUIRE(int_type);
const meta::class_type ivec2_type = library_scope.get_typedef("ivec2").as_class();
REQUIRE(ivec2_type);
const meta::constructor ivec2_ctor2 = ivec2_type.get_constructor_with({int_type, int_type});
REQUIRE(ivec2_ctor2);
const meta::method ivec2_length2 = ivec2_type.get_method("length2");
REQUIRE(ivec2_length2);
//
const meta::uvalue ivec2_inst = ivec2_ctor2.create(shared::wrap_int(3), shared::wrap_int(4));
REQUIRE(ivec2_inst);
const meta::uvalue ivec2_inst_length2 = ivec2_length2.invoke(ivec2_inst);
REQUIRE(ivec2_inst_length2);
CHECK(shared::unwrap_int(ivec2_inst_length2) == 25);
}

View File

@@ -0,0 +1,56 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/meta.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2021-2024, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include "meta_shared_lib.hpp"
namespace
{
using namespace meta_shared_lib;
struct ivec2 {
int x;
int y;
explicit ivec2(int nv) : x{nv}, y{nv} {}
ivec2(int nx, int ny) : x{nx}, y{ny} {}
int length2() const noexcept {
return x * x + y * y;
}
};
void bind_ivec2() {
meta::class_<ivec2>()
.constructor_<int>()
.constructor_<int, int>()
.member_("x", &ivec2::x)
.member_("y", &ivec2::y)
.method_("length2", &ivec2::length2);
}
const auto library_scope = []() -> meta::scope {
bind_ivec2();
return meta::local_scope_("library_scope")
.typedef_<int>("int")
.typedef_<ivec2>("ivec2");
}();
}
namespace meta_shared_lib
{
meta::scope get_library_scope() {
return library_scope;
}
meta::uvalue wrap_int(int value) {
return value;
}
int unwrap_int(const meta::uvalue& value) {
return value.as<int>();
}
}

View File

@@ -0,0 +1,21 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/meta.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2021-2024, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include <meta.hpp.shared.lib_export.h>
#include <meta.hpp/meta_all.hpp>
namespace meta_shared_lib
{
namespace meta = meta_hpp;
META_HPP_SHARED_LIB_EXPORT meta::scope get_library_scope();
META_HPP_SHARED_LIB_EXPORT meta::uvalue wrap_int(int value);
META_HPP_SHARED_LIB_EXPORT int unwrap_int(const meta::uvalue& value);
}