|
|
@@ -1,6 +1,7 @@
|
|
|
#ifndef SOPHIAR2_SOPHIAR_MANAGER_H
|
|
|
#define SOPHIAR2_SOPHIAR_MANAGER_H
|
|
|
|
|
|
+#include "core/timestamp_helper.hpp"
|
|
|
#include "utility/tiny_signal.hpp"
|
|
|
#include "utility/signal_muxer.hpp"
|
|
|
#include "utility/slot_demuxer.hpp"
|
|
|
@@ -12,13 +13,18 @@
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
#include <cassert>
|
|
|
+#include <coroutine>
|
|
|
#include <exception>
|
|
|
#include <memory>
|
|
|
#include <string>
|
|
|
#include <type_traits>
|
|
|
+#include <typeindex>
|
|
|
+#include <typeinfo>
|
|
|
|
|
|
namespace sophiar {
|
|
|
|
|
|
+ using global_obj_index_type = uint16_t;
|
|
|
+
|
|
|
class sophiar_obj;
|
|
|
|
|
|
class sophiar_manager {
|
|
|
@@ -40,7 +46,46 @@ namespace sophiar {
|
|
|
register_object_type_impl(type_name, &Derived::new_instance);
|
|
|
}
|
|
|
|
|
|
+ template<typename SmallObjType>
|
|
|
+ global_obj_index_type register_global_obj(const std::string &obj_name) {
|
|
|
+ auto ret = register_global_obj_impl(obj_name, typeid(SmallObjType), nullptr);
|
|
|
+ if ((~ret) == 0) { // first time of register
|
|
|
+ using ptr_type = typename SmallObjType::pointer;
|
|
|
+ auto placeholder = new ptr_type;
|
|
|
+ ret = register_global_obj_impl(obj_name, typeid(SmallObjType), placeholder);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ template<typename SmallObjType>
|
|
|
+ void update_global_obj(global_obj_index_type obj_index,
|
|
|
+ const typename SmallObjType::pointer &value,
|
|
|
+ timestamp_type ts = current_timestamp()) {
|
|
|
+ using ptr_type = typename SmallObjType::pointer;
|
|
|
+ auto placeholder = get_global_obj_placeholder(obj_index, typeid(SmallObjType));
|
|
|
+ auto &inner_ptr = *static_cast<ptr_type *>(placeholder);
|
|
|
+ inner_ptr = value;
|
|
|
+ update_global_obj_timestamp(obj_index, ts);
|
|
|
+ }
|
|
|
+
|
|
|
+ template<typename SmallObjType>
|
|
|
+ typename SmallObjType::pointer get_global_obj(global_obj_index_type obj_index) {
|
|
|
+ using ptr_type = typename SmallObjType::pointer;
|
|
|
+ auto placeholder = get_global_obj_placeholder(obj_index, typeid(SmallObjType));
|
|
|
+ return *static_cast<ptr_type *>(placeholder);
|
|
|
+ }
|
|
|
+
|
|
|
+ timestamp_type get_global_obj_update_timestamp(global_obj_index_type obj_index);
|
|
|
+
|
|
|
+ template<typename SmallObjType>
|
|
|
+ boost::asio::awaitable<typename SmallObjType::pointer>
|
|
|
+ await_next_global_obj(global_obj_index_type obj_index) {
|
|
|
+ co_await await_global_obj_update(obj_index);
|
|
|
+ co_return get_global_obj<SmallObjType>(obj_index);
|
|
|
+ }
|
|
|
+
|
|
|
template<typename ...Args>
|
|
|
+ [[deprecated("Use obj pool system instead.")]]
|
|
|
void register_signal(sophiar_obj *obj,
|
|
|
const std::string &signal_name,
|
|
|
tiny_signal<Args...> &signal) {
|
|
|
@@ -50,6 +95,7 @@ namespace sophiar {
|
|
|
|
|
|
// 将套用 slot_demuxer
|
|
|
template<typename ...Args>
|
|
|
+ [[deprecated("Use obj pool system instead.")]]
|
|
|
void register_slot(sophiar_obj *obj,
|
|
|
const std::string &slot_name,
|
|
|
typename tiny_signal<Args...>::slot_type &slot) {
|
|
|
@@ -103,6 +149,18 @@ namespace sophiar {
|
|
|
tiny_slot_base *slot_base,
|
|
|
slot_demuxer_base *demuxer_base);
|
|
|
|
|
|
+ global_obj_index_type register_global_obj_impl(const std::string &obj_name,
|
|
|
+ std::type_index obj_type,
|
|
|
+ void *placeholder); // small_obj<Derive>::pointer
|
|
|
+
|
|
|
+ void *get_global_obj_placeholder(global_obj_index_type obj_index,
|
|
|
+ std::type_index obj_type);
|
|
|
+
|
|
|
+ void update_global_obj_timestamp(global_obj_index_type obj_index,
|
|
|
+ timestamp_type ts);
|
|
|
+
|
|
|
+ boost::asio::awaitable<void> await_global_obj_update(global_obj_index_type obj_index);
|
|
|
+
|
|
|
};
|
|
|
|
|
|
extern sophiar_manager global_sophiar_manager;
|