From: kwolekr Date: Tue, 5 May 2015 20:52:06 +0000 (-0400) Subject: NodeDefManager: Improve const-correctness of interfaces X-Git-Tag: 0.4.13~304 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=633af58a05fb9b3ad7a0a178011f4243e5f8be2e;p=oweals%2Fminetest.git NodeDefManager: Improve const-correctness of interfaces - Add ability to explicitly reset NodeResolve state (useful for unittesting) - Remove non-essential NodeResolve methods modifying state from INodeDefManager - Add const qualifier to NodeDefManager and ContentFeatures serialize --- diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 48207da1f..2442799c7 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -241,7 +241,7 @@ void ContentFeatures::reset() sound_dug = SimpleSoundSpec(); } -void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) +void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const { if(protocol_version < 24){ serializeOld(os, protocol_version); @@ -398,9 +398,9 @@ public: virtual content_t allocateDummy(const std::string &name); virtual void updateAliases(IItemDefManager *idef); virtual void updateTextures(IGameDef *gamedef, - /*argument: */void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress), - /*argument: */void *progress_callback_args); - void serialize(std::ostream &os, u16 protocol_version); + void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress), + void *progress_cbk_args); + void serialize(std::ostream &os, u16 protocol_version) const; void deSerialize(std::istream &is); inline virtual bool getNodeRegistrationStatus() const; @@ -409,6 +409,7 @@ public: virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how); virtual bool cancelNodeResolveCallback(NodeResolver *nr); virtual void runNodeResolveCallbacks(); + virtual void resetNodeResolveState(); private: void addNameIdMapping(content_t i, std::string name); @@ -474,8 +475,7 @@ void CNodeDefManager::clear() m_group_to_items.clear(); m_next_id = 0; - m_node_registration_complete = false; - m_pending_resolve_callbacks.clear(); + resetNodeResolveState(); u32 initial_length = 0; initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1); @@ -972,7 +972,7 @@ void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, #endif -void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) +void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const { writeU8(os, 1); // version u16 count = 0; @@ -981,7 +981,7 @@ void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) continue; - ContentFeatures *f = &m_content_features[i]; + const ContentFeatures *f = &m_content_features[i]; if (f->name == "") continue; writeU16(os2, i); @@ -1062,7 +1062,7 @@ IWritableNodeDefManager *createNodeDefManager() //// Serialization of old ContentFeatures formats -void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) +void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const { if (protocol_version == 13) { @@ -1342,6 +1342,13 @@ void CNodeDefManager::runNodeResolveCallbacks() } +void CNodeDefManager::resetNodeResolveState() +{ + m_node_registration_complete = false; + m_pending_resolve_callbacks.clear(); +} + + //// //// NodeResolver //// diff --git a/src/nodedef.h b/src/nodedef.h index d9829c495..42b345059 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -265,9 +265,9 @@ struct ContentFeatures ContentFeatures(); ~ContentFeatures(); void reset(); - void serialize(std::ostream &os, u16 protocol_version); + void serialize(std::ostream &os, u16 protocol_version) const; void deSerialize(std::istream &is); - void serializeOld(std::ostream &os, u16 protocol_version); + void serializeOld(std::ostream &os, u16 protocol_version) const; void deSerializeOld(std::istream &is, int version); /* @@ -288,48 +288,44 @@ enum NodeResolveMethod { NODE_RESOLVE_DEFERRED, }; -class INodeDefManager -{ +class INodeDefManager { public: INodeDefManager(){} virtual ~INodeDefManager(){} // Get node definition - virtual const ContentFeatures& get(content_t c) const=0; - virtual const ContentFeatures& get(const MapNode &n) const=0; + virtual const ContentFeatures &get(content_t c) const=0; + virtual const ContentFeatures &get(const MapNode &n) const=0; virtual bool getId(const std::string &name, content_t &result) const=0; virtual content_t getId(const std::string &name) const=0; // Allows "group:name" in addition to regular node names virtual void getIds(const std::string &name, std::set &result) const=0; - virtual const ContentFeatures& get(const std::string &name) const=0; + virtual const ContentFeatures &get(const std::string &name) const=0; - virtual void serialize(std::ostream &os, u16 protocol_version)=0; + virtual void serialize(std::ostream &os, u16 protocol_version) const=0; virtual bool getNodeRegistrationStatus() const=0; - virtual void setNodeRegistrationStatus(bool completed)=0; virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0; virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0; - virtual void runNodeResolveCallbacks()=0; }; -class IWritableNodeDefManager : public INodeDefManager -{ +class IWritableNodeDefManager : public INodeDefManager { public: IWritableNodeDefManager(){} virtual ~IWritableNodeDefManager(){} virtual IWritableNodeDefManager* clone()=0; // Get node definition - virtual const ContentFeatures& get(content_t c) const=0; - virtual const ContentFeatures& get(const MapNode &n) const=0; + virtual const ContentFeatures &get(content_t c) const=0; + virtual const ContentFeatures &get(const MapNode &n) const=0; virtual bool getId(const std::string &name, content_t &result) const=0; // If not found, returns CONTENT_IGNORE virtual content_t getId(const std::string &name) const=0; // Allows "group:name" in addition to regular node names virtual void getIds(const std::string &name, std::set &result) - const=0; + const=0; // If not found, returns the features of CONTENT_UNKNOWN - virtual const ContentFeatures& get(const std::string &name) const=0; + virtual const ContentFeatures &get(const std::string &name) const=0; // Register node definition by name (allocate an id) // If returns CONTENT_IGNORE, could not allocate id @@ -348,10 +344,10 @@ public: Update tile textures to latest return values of TextueSource. */ virtual void updateTextures(IGameDef *gamedef, - /*argument: */void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress), - /*argument: */void *progress_callback_args)=0; + void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress), + void *progress_cbk_args)=0; - virtual void serialize(std::ostream &os, u16 protocol_version)=0; + virtual void serialize(std::ostream &os, u16 protocol_version) const=0; virtual void deSerialize(std::istream &is)=0; virtual bool getNodeRegistrationStatus() const=0; @@ -360,6 +356,7 @@ public: virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0; virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0; virtual void runNodeResolveCallbacks()=0; + virtual void resetNodeResolveState()=0; }; IWritableNodeDefManager *createNodeDefManager(); diff --git a/src/unittest/test_noderesolver.cpp b/src/unittest/test_noderesolver.cpp index c440b2399..a4ea647bc 100644 --- a/src/unittest/test_noderesolver.cpp +++ b/src/unittest/test_noderesolver.cpp @@ -31,36 +31,30 @@ public: void runTests(IGameDef *gamedef); - void testNodeResolving(INodeDefManager *ndef); - void testPendingResolveCancellation(INodeDefManager *ndef); - void testDirectResolveMethod(INodeDefManager *ndef); - void testNoneResolveMethod(INodeDefManager *ndef); + void testNodeResolving(IWritableNodeDefManager *ndef); + void testPendingResolveCancellation(IWritableNodeDefManager *ndef); + void testDirectResolveMethod(IWritableNodeDefManager *ndef); + void testNoneResolveMethod(IWritableNodeDefManager *ndef); }; static TestNodeResolver g_test_instance; void TestNodeResolver::runTests(IGameDef *gamedef) { - IWritableNodeDefManager *parent_ndef; - INodeDefManager *ndef; + IWritableNodeDefManager *ndef = + (IWritableNodeDefManager *)gamedef->getNodeDefManager(); - parent_ndef = (IWritableNodeDefManager *)gamedef->getNodeDefManager(); - - ndef = parent_ndef->clone(); + ndef->resetNodeResolveState(); TEST(testNodeResolving, ndef); - delete ndef; - ndef = parent_ndef->clone(); + ndef->resetNodeResolveState(); TEST(testPendingResolveCancellation, ndef); - delete ndef; - ndef = parent_ndef->clone(); + ndef->resetNodeResolveState(); TEST(testDirectResolveMethod, ndef); - delete ndef; - ndef = parent_ndef->clone(); + ndef->resetNodeResolveState(); TEST(testNoneResolveMethod, ndef); - delete ndef; } class Foobar : public NodeResolver { @@ -113,7 +107,7 @@ void Foobaz::resolveNodeNames() } -void TestNodeResolver::testNodeResolving(INodeDefManager *ndef) +void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef) { Foobar foobar; size_t i; @@ -191,7 +185,7 @@ void TestNodeResolver::testNodeResolving(INodeDefManager *ndef) } -void TestNodeResolver::testPendingResolveCancellation(INodeDefManager *ndef) +void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *ndef) { Foobaz foobaz1; foobaz1.test_content1 = 1234; @@ -219,7 +213,7 @@ void TestNodeResolver::testPendingResolveCancellation(INodeDefManager *ndef) } -void TestNodeResolver::testDirectResolveMethod(INodeDefManager *ndef) +void TestNodeResolver::testDirectResolveMethod(IWritableNodeDefManager *ndef) { Foobaz foobaz; @@ -240,7 +234,7 @@ void TestNodeResolver::testDirectResolveMethod(INodeDefManager *ndef) } -void TestNodeResolver::testNoneResolveMethod(INodeDefManager *ndef) +void TestNodeResolver::testNoneResolveMethod(IWritableNodeDefManager *ndef) { Foobaz foobaz;