#pragma once #include #include #include #include #include #include "NetObjPtr.decl.hpp" #include "NetObjManager.hpp" #include "NetObjManagerTypeList.hpp" namespace NetworkingObjects { template NetObjPtr::NetObjPtr(): objMan(nullptr), sharedPtr(nullptr) {} template NetObjPtr::NetObjPtr(const NetObjPtr& other): objMan(other.objMan), id(other.id), sharedPtr(other.sharedPtr) {} template NetObjPtr& NetObjPtr::operator=(const NetObjPtr& other) { if(this != &other) { if(sharedPtr && sharedPtr.use_count() == 2) objMan->objectData.erase(id); objMan = other.objMan; id = other.id; sharedPtr = other.sharedPtr; } return *this; } template NetObjPtr::~NetObjPtr() { if(sharedPtr && sharedPtr.use_count() == 2) objMan->objectData.erase(id); } template template NetObjPtr NetObjPtr::cast() const { return NetObjPtr(objMan, id, std::static_pointer_cast(sharedPtr)); } template NetObjManager* NetObjPtr::get_obj_man() const { return objMan; } template NetObjID NetObjPtr::get_net_id() const { return id; } template T* NetObjPtr::get() const { return sharedPtr.get(); } template std::add_lvalue_reference_t NetObjPtr::operator*() const requires(!std::is_void_v) { return *sharedPtr; } template T* NetObjPtr::operator->() const { return sharedPtr.get(); } template NetObjPtr::operator bool() const { return sharedPtr != nullptr; } template std::strong_ordering NetObjPtr::operator<=>(const NetObjPtr& rhs) const { return sharedPtr <=> rhs.sharedPtr; } template void NetObjPtr::send_client_update(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { if(objMan->client && !objMan->client->is_disconnected()) { auto ss(std::make_shared()); { cereal::PortableBinaryOutputArchive m(*ss); m(objMan->updateCommandID, id); sendUpdateFunc(*this, m); } objMan->client->send_string_stream_to_server(channel, ss); } } template void NetObjPtr::send_server_update_to_all_clients(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { if(objMan->server && !objMan->server->is_disconnected()) { auto ss(std::make_shared()); { cereal::PortableBinaryOutputArchive m(*ss); m(objMan->updateCommandID, id); sendUpdateFunc(*this, m); } objMan->server->send_string_stream_to_all_clients(channel, ss); } } template void NetObjPtr::send_update_to_all(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { if(objMan->is_server()) send_server_update_to_all_clients(channel, sendUpdateFunc); else send_client_update(channel, sendUpdateFunc); } template void NetObjPtr::write_create_message(cereal::PortableBinaryOutputArchive& a) const { a(id); objMan->typeList->get_type_index_data(objMan->isServer).writeConstructorFunc(this->cast(), a); } template NetObjPtr::NetObjPtr(NetObjManager* initObjMan, NetObjID initID, const std::shared_ptr& initPtr): objMan(initObjMan), id(initID), sharedPtr(initPtr) {} }