Hide implementation details of net ordered list

This commit is contained in:
Yousef Khadadeh 2025-12-10 10:24:57 +03:00
parent c6f2ff18ef
commit 67507a9a39
2 changed files with 80 additions and 47 deletions

View file

@ -16,23 +16,16 @@ namespace NetworkingObjects {
ERASE_SINGLE ERASE_SINGLE
}; };
template <typename T> class NetObjOrderedListObjectInfo { template <typename T> struct NetObjOrderedListObjectInfo {
public: NetObjOrderedListObjectInfo(const NetObjPtr<T>& iObj, uint32_t iPos, bool iSyncIgnore):
NetObjOrderedListObjectInfo(const NetObjPtr<T>& initObj, uint32_t initPos, bool initSyncIgnore): obj(iObj),
obj(initObj), pos(iPos),
pos(initPos), syncIgnore(iSyncIgnore)
syncIgnore(initSyncIgnore)
{} {}
uint32_t get_pos() const { return pos; } NetObjOrderedListObjectInfo() = delete;
const NetObjPtr<T>& get_obj() const { return obj; }
private:
NetObjPtr<T> obj; NetObjPtr<T> obj;
uint32_t pos; uint32_t pos;
bool syncIgnore; bool syncIgnore;
template <typename S> friend class NetObjOrderedList;
template <typename S> friend class NetObjOrderedListServer;
template <typename S> friend class NetObjOrderedListClient;
template <typename S> friend void set_positions_for_object_info_vector(std::vector<std::shared_ptr<NetObjOrderedListObjectInfo<S>>>& v, uint32_t i);
}; };
template <typename T> using NetObjOrderedListObjectInfoPtr = std::shared_ptr<NetObjOrderedListObjectInfo<T>>; template <typename T> using NetObjOrderedListObjectInfoPtr = std::shared_ptr<NetObjOrderedListObjectInfo<T>>;
@ -51,29 +44,26 @@ namespace NetworkingObjects {
l->insert(l, l->size(), newObj); l->insert(l, l->size(), newObj);
return newObj; return newObj;
} }
static NetObjPtr<T> push_back_and_send_create(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjPtr<T>& newObj) { static void push_back_and_send_create(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjPtr<T>& newObj) {
l->insert(l, l->size(), newObj); l->insert(l, l->size(), newObj);
return newObj;
} }
static NetObjPtr<T> insert_and_send_create(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t posToInsertAt, const NetObjPtr<T>& newObj) { static void insert_and_send_create(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t posToInsertAt, const NetObjPtr<T>& newObj) {
l->insert(l, posToInsertAt, newObj); l->insert(l, posToInsertAt, newObj);
return newObj;
} }
static void erase(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) { static NetObjPtr<T> erase(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) {
l->erase_by_index(l, indexToErase); return l->erase_by_index(l, indexToErase);
} }
static void erase(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjOrderedListObjectInfoPtr<T>& p) { static uint32_t erase(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjPtr<T>& p) {
l->erase_by_index(l, p->pos); return l->erase_by_ptr(l, p);
} }
virtual uint32_t size() const = 0; virtual uint32_t size() const = 0;
virtual const std::vector<NetObjOrderedListObjectInfoPtr<T>>& get_data() const = 0;
virtual const NetObjOrderedListObjectInfoPtr<T>& info_at(uint32_t index) const = 0;
virtual const NetObjPtr<T>& at(uint32_t index) const = 0; virtual const NetObjPtr<T>& at(uint32_t index) const = 0;
virtual bool empty() const = 0; virtual bool empty() const = 0;
virtual ~NetObjOrderedList() {} virtual ~NetObjOrderedList() {}
protected: protected:
virtual void insert(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t posToInsertAt, const NetObjPtr<T>& newObj) = 0; virtual void insert(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t posToInsertAt, const NetObjPtr<T>& newObj) = 0;
virtual void erase_by_index(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) = 0; virtual NetObjPtr<T> erase_by_index(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) = 0;
virtual uint32_t erase_by_ptr(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjPtr<T>& p) = 0;
virtual void write_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryOutputArchive& a) = 0; virtual void write_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryOutputArchive& a) = 0;
virtual void read_update(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>&) = 0; virtual void read_update(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>&) = 0;
virtual void read_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>&) = 0; virtual void read_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>&) = 0;
@ -96,12 +86,6 @@ namespace NetworkingObjects {
virtual uint32_t size() const override { virtual uint32_t size() const override {
return data.size(); return data.size();
} }
virtual const std::vector<NetObjOrderedListObjectInfoPtr<T>>& get_data() const override {
return data;
}
virtual const NetObjOrderedListObjectInfoPtr<T>& info_at(uint32_t index) const override {
return data[index];
}
virtual const NetObjPtr<T>& at(uint32_t index) const override { virtual const NetObjPtr<T>& at(uint32_t index) const override {
return data[index]->obj; return data[index]->obj;
} }
@ -137,17 +121,34 @@ namespace NetworkingObjects {
}); });
set_positions_for_object_info_vector<T>(data, actualPosToInsertAt); set_positions_for_object_info_vector<T>(data, actualPosToInsertAt);
} }
virtual void erase_by_index(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) override { virtual NetObjPtr<T> erase_by_index(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) override {
if(data.empty()) if(data.empty())
return; return {};
indexToErase = std::min<uint32_t>(indexToErase, data.size() - 1); indexToErase = std::min<uint32_t>(indexToErase, data.size() - 1);
l.send_server_update_to_all_clients(RELIABLE_COMMAND_CHANNEL, [indexToErase](const NetObjPtr<NetObjOrderedList<T>>&, cereal::PortableBinaryOutputArchive& a) { l.send_server_update_to_all_clients(RELIABLE_COMMAND_CHANNEL, [indexToErase](const NetObjPtr<NetObjOrderedList<T>>&, cereal::PortableBinaryOutputArchive& a) {
a(ObjPtrOrderedListCommand_StoC::ERASE_SINGLE, indexToErase); a(ObjPtrOrderedListCommand_StoC::ERASE_SINGLE, indexToErase);
}); });
NetObjID idToErase = data[indexToErase]->obj.get_net_id(); NetObjID idToErase = data[indexToErase]->obj.get_net_id();
NetObjPtr<T> toRet = data[indexToErase]->obj;
data.erase(data.begin() + indexToErase); data.erase(data.begin() + indexToErase);
idToDataMap.erase(idToErase); idToDataMap.erase(idToErase);
set_positions_for_object_info_vector<T>(data, indexToErase); set_positions_for_object_info_vector<T>(data, indexToErase);
return toRet;
}
virtual uint32_t erase_by_ptr(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjPtr<T>& p) override {
if(data.empty())
return std::numeric_limits<uint32_t>::max();
auto it = idToDataMap.find(p.get_net_id());
if(it == idToDataMap.end())
return std::numeric_limits<uint32_t>::max();
uint32_t indexToErase = it->second->pos;
data.erase(data.begin() + indexToErase);
idToDataMap.erase(it);
l.send_server_update_to_all_clients(RELIABLE_COMMAND_CHANNEL, [indexToErase](const NetObjPtr<NetObjOrderedList<T>>&, cereal::PortableBinaryOutputArchive& a) {
a(ObjPtrOrderedListCommand_StoC::ERASE_SINGLE, indexToErase);
});
set_positions_for_object_info_vector<T>(data, indexToErase);
return indexToErase;
} }
virtual void write_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryOutputArchive& a) override { virtual void write_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryOutputArchive& a) override {
a(static_cast<uint32_t>(data.size())); a(static_cast<uint32_t>(data.size()));
@ -192,12 +193,6 @@ namespace NetworkingObjects {
virtual uint32_t size() const override { virtual uint32_t size() const override {
return clientData.size(); return clientData.size();
} }
virtual const std::vector<NetObjOrderedListObjectInfoPtr<T>>& get_data() const override {
return clientData;
}
virtual const NetObjOrderedListObjectInfoPtr<T>& info_at(uint32_t index) const override {
return clientData[index];
}
virtual const NetObjPtr<T>& at(uint32_t index) const override { virtual const NetObjPtr<T>& at(uint32_t index) const override {
return clientData[index]->obj; return clientData[index]->obj;
} }
@ -236,11 +231,11 @@ namespace NetworkingObjects {
}); });
set_positions_for_object_info_vector<T>(clientData, actualPosToInsertAt); set_positions_for_object_info_vector<T>(clientData, actualPosToInsertAt);
} }
virtual void erase_by_index(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) override { virtual NetObjPtr<T> erase_by_index(const NetObjPtr<NetObjOrderedList<T>>& l, uint32_t indexToErase) override {
if(clientData.empty()) if(clientData.empty())
return; return {};
indexToErase = std::min<uint32_t>(indexToErase, clientData.size() - 1); indexToErase = std::min<uint32_t>(indexToErase, clientData.size() - 1);
auto& p = clientData[indexToErase]; auto p = clientData[indexToErase];
l.send_client_update(RELIABLE_COMMAND_CHANNEL, [&p](const NetObjPtr<NetObjOrderedList<T>>&, cereal::PortableBinaryOutputArchive& a) { l.send_client_update(RELIABLE_COMMAND_CHANNEL, [&p](const NetObjPtr<NetObjOrderedList<T>>&, cereal::PortableBinaryOutputArchive& a) {
a(ObjPtrOrderedListCommand_CtoS::ERASE_SINGLE, p->obj.get_net_id()); a(ObjPtrOrderedListCommand_CtoS::ERASE_SINGLE, p->obj.get_net_id());
}); });
@ -249,6 +244,24 @@ namespace NetworkingObjects {
clientIdToDataMap.erase(p->obj.get_net_id()); clientIdToDataMap.erase(p->obj.get_net_id());
clientData.erase(clientData.begin() + iToErase); clientData.erase(clientData.begin() + iToErase);
set_positions_for_object_info_vector<T>(clientData, iToErase); set_positions_for_object_info_vector<T>(clientData, iToErase);
return p->obj;
}
virtual uint32_t erase_by_ptr(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjPtr<T>& p) override {
if(clientData.empty())
return std::numeric_limits<uint32_t>::max();
auto it = clientIdToDataMap.find(p.get_net_id());
if(it == clientIdToDataMap.end())
return std::numeric_limits<uint32_t>::max();
l.send_client_update(RELIABLE_COMMAND_CHANNEL, [&p](const NetObjPtr<NetObjOrderedList<T>>&, cereal::PortableBinaryOutputArchive& a) {
a(ObjPtrOrderedListCommand_CtoS::ERASE_SINGLE, p.get_net_id());
});
it->second->syncIgnore = true;
uint32_t iToErase = it->second->pos;
auto toRet = clientData[iToErase];
clientIdToDataMap.erase(it);
clientData.erase(clientData.begin() + iToErase);
set_positions_for_object_info_vector<T>(clientData, iToErase);
return iToErase;
} }
virtual void write_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryOutputArchive& a) override { virtual void write_constructor(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryOutputArchive& a) override {
a(static_cast<uint32_t>(clientData.size())); a(static_cast<uint32_t>(clientData.size()));

View file

@ -13,16 +13,36 @@ void Bookmark::scale_up(const WorldScalar& scaleUpAmount) {
BookmarkManager::BookmarkManager(World& w): BookmarkManager::BookmarkManager(World& w):
world(w) world(w)
{ {
// NOTE: Should find a way for bookmarks added from the network to take into account the client's canvasScale
// Have the canvasScale variable come with the ClientData object, and the grid scale commands can be part of
// the client data class
if(w.netObjMan.is_server()) if(w.netObjMan.is_server())
bookmarks = w.netObjMan.make_obj<NetworkingObjects::NetObjOrderedList<Bookmark>>(); bookmarks = w.netObjMan.make_obj<NetworkingObjects::NetObjOrderedList<Bookmark>>();
} }
void BookmarkManager::add_bookmark(const std::string& name) { void BookmarkManager::add_bookmark(const std::string& name) {
NetworkingObjects::NetObjPtr<Bookmark> a = bookmarks->emplace_back_direct(bookmarks, Bookmark{name, world.drawData.cam.c, world.main.window.size.cast<int32_t>()}); NetworkingObjects::NetObjPtr<Bookmark> bookmarkPtr = bookmarks->emplace_back_direct(bookmarks, Bookmark{name, world.drawData.cam.c, world.main.window.size.cast<int32_t>()});
uint32_t pos = bookmarks->size() - 1;
world.undo.push({[&bookmarks = bookmarks, bookmarkPtr](){
bookmarks->erase(bookmarks, bookmarkPtr);
return true;
},
[&bookmarks = bookmarks, bookmarkPtr, pos]() {
bookmarks->insert_and_send_create(bookmarks, pos, bookmarkPtr);
return true;
}});
} }
void BookmarkManager::remove_bookmark(uint32_t bookmarkIndex) { void BookmarkManager::remove_bookmark(uint32_t bookmarkIndex) {
bookmarks->erase(bookmarks, bookmarkIndex); NetworkingObjects::NetObjPtr<Bookmark> bookmarkPtr = bookmarks->erase(bookmarks, bookmarkIndex);
world.undo.push({[&bookmarks = bookmarks, bookmarkPtr, bookmarkIndex]() {
bookmarks->insert_and_send_create(bookmarks, bookmarkIndex, bookmarkPtr);
return true;
},
[&bookmarks = bookmarks, bookmarkPtr](){
bookmarks->erase(bookmarks, bookmarkPtr);
return true;
}});
} }
void BookmarkManager::jump_to_bookmark(uint32_t bookmarkIndex) { void BookmarkManager::jump_to_bookmark(uint32_t bookmarkIndex) {