Skip to content

Commit

Permalink
move ApplicationContext to open source repo (#28)
Browse files Browse the repository at this point in the history
* move ApplicationContext to open source repo
  • Loading branch information
oathdruid authored May 28, 2024
1 parent 3b3d517 commit b0a8226
Show file tree
Hide file tree
Showing 14 changed files with 1,757 additions and 41 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
restore-keys: |
bazel-disk-gcc12-arenastring-
bazel-disk-
- run: sed -i '/single_version_override.*protobuf/s/25.3/25.3.arenastring/' MODULE.bazel
- run: sed -i "/single_version_override.*protobuf/s/version = '[^']*'/version = '25.3.arenastring'/" MODULE.bazel
- run: bazel test --config=ci --action_env=CC=gcc-12 --config=arenastring --features=asan test/...

gcc12-mutable-donated-string-asan:
Expand All @@ -82,7 +82,7 @@ jobs:
restore-keys: |
bazel-disk-gcc12-mutable-donated-string-
bazel-disk-
- run: sed -i '/single_version_override.*protobuf/s/25.3/25.3.arenastring/' MODULE.bazel
- run: sed -i "/single_version_override.*protobuf/s/version = '[^']*'/version = '25.3.arenastring'/" MODULE.bazel
- run: bazel test --config=ci --action_env=CC=gcc-12 --config=mutable-donated-string --features=asan test/...

clang14-basic-asan:
Expand Down Expand Up @@ -140,7 +140,7 @@ jobs:
restore-keys: |
bazel-disk-clang14-arenastring-
bazel-disk-
- run: sed -i '/single_version_override.*protobuf/s/25.3/25.3.arenastring/' MODULE.bazel
- run: sed -i "/single_version_override.*protobuf/s/version = '[^']*'/version = '25.3.arenastring'/" MODULE.bazel
- run: bazel test --config=ci --action_env=CC=clang-14 --cxxopt=-stdlib=libc++ --linkopt=-stdlib=libc++ --config=arenastring --features=asan test/...

clang14-mutable-donated-string-asan:
Expand All @@ -160,7 +160,7 @@ jobs:
restore-keys: |
bazel-disk-clang14-mutable-donated-string-
bazel-disk-
- run: sed -i '/single_version_override.*protobuf/s/25.3/25.3.arenastring/' MODULE.bazel
- run: sed -i "/single_version_override.*protobuf/s/version = '[^']*'/version = '25.3.arenastring'/" MODULE.bazel
- run: bazel test --config=ci --action_env=CC=clang-14 --cxxopt=-stdlib=libc++ --linkopt=-stdlib=libc++ --config=mutable-donated-string --features=asan test/...

gcc12-aarch64-asan:
Expand Down
5 changes: 5 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ alias(
actual = '//src/babylon/anyflow',
)

alias(
name = 'application_context',
actual = '//src/babylon:application_context',
)

alias(
name = 'concurrent',
actual = '//src/babylon/concurrent',
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14)

project(babylon VERSION 1.1.6)
project(babylon VERSION 1.2.0)

include(CTest) # for BUILD_TESTING option
include(CMakePackageConfigHelpers) # for write_basic_package_version_file
Expand Down
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module(
name = 'babylon',
version = '1.1.6',
version = '1.2.0',
compatibility_level = 1,
)

Expand All @@ -24,7 +24,7 @@ bazel_dep(name = 'boost.spirit', version = '1.83.0.bzl.1')
# test only dependency

# --registry=https://bcr.bazel.build
single_version_override(module_name = 'abseil-cpp', version = '20230802.1')
single_version_override(module_name = 'abseil-cpp', version = '20240116.2')
bazel_dep(name = 'googletest', version = '1.14.0', repo_name = 'com_google_googletest', dev_dependency = True)

# --registry=file://%workspace%/registry
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ common --registry=https://raw.githubusercontent.com/bazelboost/registry/main
- 增加依赖项
```
# in MODULE.bazel
bazel_dep(name = 'babylon', version = '1.1.5')
bazel_dep(name = 'babylon', version = '1.1.6')
```
- 添加依赖的子模块到编译目标,全部可用子模块可以参照[模块功能文档](#模块功能文档),或者[BUILD](BUILD)文件
```
Expand All @@ -74,9 +74,9 @@ cc_library(
# in WORKSPACE
http_archive(
name = 'com_baidu_babylon',
urls = ['https://github.com/baidu/babylon/archive/refs/tags/v1.1.5.tar.gz'],
strip_prefix = 'babylon-1.1.5',
sha256 = 'a8d37251972a522b4c6f4d28ac6bf536444ff0e0c0e47eebff37aa75ca2a65a6',
urls = ['https://github.com/baidu/babylon/archive/refs/tags/v1.1.6.tar.gz'],
strip_prefix = 'babylon-1.1.6',
sha256 = 'a5bbc29f55819c90e00b40f9b5d2716d5f0232a158d69c530d8c7bac5bd794b6',
)
```
- 增加传递依赖项,内容拷贝自babylon代码库的WORKSPACE,并和项目自身依赖项合并
Expand Down Expand Up @@ -113,8 +113,8 @@ set(BUILD_DEPS ON)
include(FetchContent)
FetchContent_Declare(
babylon
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.1.5.tar.gz"
URL_HASH SHA256=a8d37251972a522b4c6f4d28ac6bf536444ff0e0c0e47eebff37aa75ca2a65a6
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.1.6.tar.gz"
URL_HASH SHA256=a5bbc29f55819c90e00b40f9b5d2716d5f0232a158d69c530d8c7bac5bd794b6
)
FetchContent_MakeAvailable(babylon)
```
Expand Down
17 changes: 16 additions & 1 deletion src/babylon/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load('//:copts.bzl', 'BABYLON_COPTS')
cc_library(
name = 'babylon',
deps = [
':any', ':executor', ':future', ':mlock',
':any', ':application_context', ':executor', ':future', ':mlock',
':move_only_function', ':serialization',
':string_view', ':time', ':type_traits',
'//src/babylon/anyflow', '//src/babylon/concurrent',
Expand Down Expand Up @@ -49,6 +49,21 @@ cc_library(
],
)

cc_library(
name = 'application_context',
srcs = ['application_context.cpp'],
hdrs = ['application_context.h'],
includes = ['.'],
copts = BABYLON_COPTS,
strip_include_prefix = '//src',
deps = [
':any',
'//src/babylon/logging',
'//:boost.preprocessor',
'@com_google_absl//absl/container:flat_hash_map',
],
)

cc_library(
name = 'environment',
hdrs = ['environment.h', 'protect.h', 'unprotect.h'],
Expand Down
47 changes: 28 additions & 19 deletions src/babylon/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ BABYLON_NAMESPACE_BEGIN
// 在std::any的基础上,进一步支持不可拷贝,不可移动构造的对象
// 同时支持引用构造,可以支持同构处理引用和实体对象
class Any {
private:
struct Descriptor;

public:
enum class Type : uint8_t {
EMPTY = 0,
Expand Down Expand Up @@ -43,6 +40,14 @@ class Any {
constexpr static uint8_t MUTABLE_REFERENCE = NON_INPLACE | REFERENCE;
};

struct Descriptor {
const Id& type_id;
void (*const destructor)(void*);
void (*const deleter)(void*);
void (*const copy_constructor)(void*, const void*);
void* (*const copy_creater)(const void*);
};

// 默认构造,后续可用 = 赋值
inline Any() noexcept;
// 拷贝构造,如果other中已经持有一个对象实体
Expand Down Expand Up @@ -158,6 +163,8 @@ class Any {
template <typename T>
inline const T* get() const noexcept;

inline void* get(const Descriptor* descriptor) noexcept;

// 辅助判断是否是常量引用
inline bool is_const_reference() const noexcept;

Expand Down Expand Up @@ -193,17 +200,22 @@ class Any {
template <typename T>
inline static const Descriptor* descriptor() noexcept;

// release managed instance inside
// a successful release return std::unique_ptr of instance
// and reset this Any to an initialize empty state
//
// when type not match or is reference, release will fail
// and state of this Any is not changed
template <typename T>
inline ::std::unique_ptr<T> release() noexcept;
inline ::std::unique_ptr<void, void (*)(void*)> release(
const Descriptor* descriptor) noexcept;
inline ::std::unique_ptr<void, void (*)(void*)> release(
StringView type_name) noexcept;

private:
union Meta;

struct Descriptor {
const Id& type_id;
void (*const destructor)(void*);
void (*const deleter)(void*);
void (*const copy_constructor)(void*, const void*);
void* (*const copy_creater)(const void*);
};

template <typename T, typename E = void>
struct TypeDescriptor {
inline static void destructor(void* object) noexcept;
Expand All @@ -218,40 +230,37 @@ class Any {
struct TypeDescriptor<T, typename ::std::enable_if<
::std::is_copy_constructible<T>::value>::type>
: public TypeDescriptor<T, int> {
inline constexpr TypeDescriptor() noexcept : type_id(TypeId<T>().ID) {}
static void copy_constructor(void* ptr, const void* object);
static void* copy_creater(const void* object);

static constexpr Descriptor descriptor {
.type_id = TypeId<T>().ID,
.type_id = TypeId<T>::ID,
.destructor = TypeDescriptor<T, int>::destructor,
.deleter = TypeDescriptor<T, int>::deleter,
.copy_constructor = copy_constructor,
.copy_creater = copy_creater,
};

const Id& type_id;
};

template <typename T>
struct TypeDescriptor<T, typename ::std::enable_if<
!::std::is_copy_constructible<T>::value>::type>
: public TypeDescriptor<T, int> {
inline constexpr TypeDescriptor() noexcept : type_id(TypeId<T>().ID) {}
static void copy_constructor(void* ptr, const void* object);
static void* copy_creater(const void* object);

static constexpr Descriptor descriptor {
.type_id = TypeId<T>().ID,
.type_id = TypeId<T>::ID,
.destructor = TypeDescriptor<T, int>::destructor,
.deleter = TypeDescriptor<T, int>::deleter,
.copy_constructor = copy_constructor,
.copy_creater = copy_creater,
};

const Id& type_id;
};

inline static uint64_t meta_for_instance(
const Descriptor* descriptor) noexcept;

inline void destroy() noexcept;
inline void* raw_pointer() noexcept;
inline const void* const_raw_pointer() const noexcept;
Expand Down
83 changes: 77 additions & 6 deletions src/babylon/any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include "babylon/any.h"

#include <cassert>
// clang-format off
#include BABYLON_EXTERNAL(absl/base/optimization.h) // ABSL_PREDICT_FALSE
// clang-format on

BABYLON_NAMESPACE_BEGIN

Expand All @@ -11,10 +13,8 @@ BABYLON_NAMESPACE_BEGIN
template <typename T, typename E>
__attribute__((init_priority(101)))
Any::Meta Any::TypeDescriptor<T, E>::meta_for_instance {
.v = reinterpret_cast<uint64_t>(
&TypeDescriptor<typename ::std::decay<T>::type>().descriptor) |
static_cast<uint64_t>(HolderType::INSTANCE) << 56 |
static_cast<uint64_t>(Type::INSTANCE) << 48};
.v = Any::meta_for_instance(
&TypeDescriptor<typename ::std::decay<T>::type>().descriptor)};

template <typename T, typename E>
__attribute__((init_priority(101)))
Expand Down Expand Up @@ -86,6 +86,26 @@ void* Any::TypeDescriptor<
// Any::TypeDescriptor end
///////////////////////////////////////////////////////////////////////////////

template <>
struct Any::TypeDescriptor<void> : public TypeDescriptor<void, int> {
static void destructor(void*) noexcept {}
static void deleter(void*) noexcept {}
static void copy_constructor(void*, const void*) {}
static void* copy_creater(const void*) {
return nullptr;
}

static constexpr Descriptor descriptor {
.type_id = TypeId<void>::ID,
.destructor = destructor,
.deleter = deleter,
.copy_constructor = copy_constructor,
.copy_creater = copy_creater,
};

const Id& type_id;
};

///////////////////////////////////////////////////////////////////////////////
// Any begin
inline Any::Any() noexcept
Expand Down Expand Up @@ -292,6 +312,13 @@ inline const T* Any::get() const noexcept {
return cget<T>();
}

inline void* Any::get(const Descriptor* descriptor) noexcept {
if (_meta.descriptor() == descriptor) {
return raw_pointer();
}
return nullptr;
}

inline bool Any::is_const_reference() const noexcept {
return _meta.v & (static_cast<uint64_t>(HolderType::CONST) << 56);
}
Expand Down Expand Up @@ -353,7 +380,13 @@ inline const Id& Any::instance_type() const noexcept {

template <typename T>
inline const Any::Descriptor* Any::descriptor() noexcept {
return &TypeDescriptor<typename ::std::decay<T>::type>().descriptor;
return &TypeDescriptor<typename ::std::decay<T>::type>::descriptor;
}

inline uint64_t Any::meta_for_instance(const Descriptor* descriptor) noexcept {
return reinterpret_cast<uint64_t>(descriptor) |
static_cast<uint64_t>(HolderType::INSTANCE) << 56 |
static_cast<uint64_t>(Type::INSTANCE) << 48;
}

inline void Any::destroy() noexcept {
Expand Down Expand Up @@ -409,6 +442,44 @@ inline void Any::construct_inplace(T&& value) noexcept {
}
new (&_holder) DT(::std::forward<T>(value));
}

template <typename T>
inline ::std::unique_ptr<T> Any::release() noexcept {
if (ABSL_PREDICT_FALSE(_meta.v != TypeDescriptor<T>::meta_for_instance.v)) {
return nullptr;
}

auto pointer_value = _holder.pointer_value;
new (this) Any {};
return ::std::unique_ptr<T> {static_cast<T*>(pointer_value)};
}

inline ::std::unique_ptr<void, void (*)(void*)> Any::release(
const Descriptor* descriptor) noexcept {
if (ABSL_PREDICT_FALSE(_meta.v != meta_for_instance(descriptor))) {
return {nullptr, nullptr};
}

auto pointer_value = _holder.pointer_value;
new (this) Any {};
return {pointer_value, descriptor->deleter};
}

inline ::std::unique_ptr<void, void (*)(void*)> Any::release(
StringView type_name) noexcept {
if (ABSL_PREDICT_FALSE(_meta.m.holder_type != HolderType::INSTANCE)) {
return {nullptr, nullptr};
}

if (type_name == instance_type().name) {
auto pointer_value = _holder.pointer_value;
auto deleter = _meta.descriptor()->deleter;
new (this) Any {};
return {pointer_value, deleter};
}

return {nullptr, nullptr};
}
// Any end
///////////////////////////////////////////////////////////////////////////////

Expand Down
4 changes: 2 additions & 2 deletions src/babylon/anyflow/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ GraphData::declare_type<::babylon::Any>() noexcept {
template <typename T>
inline OutputData<T> GraphData::declare_type() noexcept {
if (_declared_type == &::babylon::TypeId<Any>::ID) {
_declared_type = &::babylon::TypeId<T>().ID;
_declared_type = &::babylon::TypeId<T>::ID;
set_default_on_reset<T>();
return OutputData<T>(*this);
} else if (_declared_type == &::babylon::TypeId<T>().ID) {
} else if (_declared_type == &::babylon::TypeId<T>::ID) {
return OutputData<T>(*this);
} else {
BABYLON_LOG(WARNING) << *this << " declare type["
Expand Down
Loading

0 comments on commit b0a8226

Please sign in to comment.