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
master
kwolekr 2015-05-05 16:52:06 -04:00
parent b785577f03
commit 633af58a05
3 changed files with 46 additions and 48 deletions

View File

@ -241,7 +241,7 @@ void ContentFeatures::reset()
sound_dug = SimpleSoundSpec(); 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){ if(protocol_version < 24){
serializeOld(os, protocol_version); serializeOld(os, protocol_version);
@ -398,9 +398,9 @@ public:
virtual content_t allocateDummy(const std::string &name); virtual content_t allocateDummy(const std::string &name);
virtual void updateAliases(IItemDefManager *idef); virtual void updateAliases(IItemDefManager *idef);
virtual void updateTextures(IGameDef *gamedef, virtual void updateTextures(IGameDef *gamedef,
/*argument: */void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress), void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
/*argument: */void *progress_callback_args); void *progress_cbk_args);
void serialize(std::ostream &os, u16 protocol_version); void serialize(std::ostream &os, u16 protocol_version) const;
void deSerialize(std::istream &is); void deSerialize(std::istream &is);
inline virtual bool getNodeRegistrationStatus() const; inline virtual bool getNodeRegistrationStatus() const;
@ -409,6 +409,7 @@ public:
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how); virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how);
virtual bool cancelNodeResolveCallback(NodeResolver *nr); virtual bool cancelNodeResolveCallback(NodeResolver *nr);
virtual void runNodeResolveCallbacks(); virtual void runNodeResolveCallbacks();
virtual void resetNodeResolveState();
private: private:
void addNameIdMapping(content_t i, std::string name); void addNameIdMapping(content_t i, std::string name);
@ -474,8 +475,7 @@ void CNodeDefManager::clear()
m_group_to_items.clear(); m_group_to_items.clear();
m_next_id = 0; m_next_id = 0;
m_node_registration_complete = false; resetNodeResolveState();
m_pending_resolve_callbacks.clear();
u32 initial_length = 0; u32 initial_length = 0;
initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1); initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
@ -972,7 +972,7 @@ void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
#endif #endif
void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
{ {
writeU8(os, 1); // version writeU8(os, 1); // version
u16 count = 0; u16 count = 0;
@ -981,7 +981,7 @@ void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
if (i == CONTENT_IGNORE || i == CONTENT_AIR if (i == CONTENT_IGNORE || i == CONTENT_AIR
|| i == CONTENT_UNKNOWN) || i == CONTENT_UNKNOWN)
continue; continue;
ContentFeatures *f = &m_content_features[i]; const ContentFeatures *f = &m_content_features[i];
if (f->name == "") if (f->name == "")
continue; continue;
writeU16(os2, i); writeU16(os2, i);
@ -1062,7 +1062,7 @@ IWritableNodeDefManager *createNodeDefManager()
//// Serialization of old ContentFeatures formats //// 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) 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 //// NodeResolver
//// ////

View File

@ -265,9 +265,9 @@ struct ContentFeatures
ContentFeatures(); ContentFeatures();
~ContentFeatures(); ~ContentFeatures();
void reset(); 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 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); void deSerializeOld(std::istream &is, int version);
/* /*
@ -288,8 +288,7 @@ enum NodeResolveMethod {
NODE_RESOLVE_DEFERRED, NODE_RESOLVE_DEFERRED,
}; };
class INodeDefManager class INodeDefManager {
{
public: public:
INodeDefManager(){} INodeDefManager(){}
virtual ~INodeDefManager(){} virtual ~INodeDefManager(){}
@ -303,18 +302,15 @@ public:
const=0; 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 bool getNodeRegistrationStatus() const=0;
virtual void setNodeRegistrationStatus(bool completed)=0;
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0; virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0;
virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0; virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
virtual void runNodeResolveCallbacks()=0;
}; };
class IWritableNodeDefManager : public INodeDefManager class IWritableNodeDefManager : public INodeDefManager {
{
public: public:
IWritableNodeDefManager(){} IWritableNodeDefManager(){}
virtual ~IWritableNodeDefManager(){} virtual ~IWritableNodeDefManager(){}
@ -348,10 +344,10 @@ public:
Update tile textures to latest return values of TextueSource. Update tile textures to latest return values of TextueSource.
*/ */
virtual void updateTextures(IGameDef *gamedef, virtual void updateTextures(IGameDef *gamedef,
/*argument: */void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress), void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
/*argument: */void *progress_callback_args)=0; 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 void deSerialize(std::istream &is)=0;
virtual bool getNodeRegistrationStatus() const=0; virtual bool getNodeRegistrationStatus() const=0;
@ -360,6 +356,7 @@ public:
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0; virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0;
virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0; virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
virtual void runNodeResolveCallbacks()=0; virtual void runNodeResolveCallbacks()=0;
virtual void resetNodeResolveState()=0;
}; };
IWritableNodeDefManager *createNodeDefManager(); IWritableNodeDefManager *createNodeDefManager();

View File

@ -31,36 +31,30 @@ public:
void runTests(IGameDef *gamedef); void runTests(IGameDef *gamedef);
void testNodeResolving(INodeDefManager *ndef); void testNodeResolving(IWritableNodeDefManager *ndef);
void testPendingResolveCancellation(INodeDefManager *ndef); void testPendingResolveCancellation(IWritableNodeDefManager *ndef);
void testDirectResolveMethod(INodeDefManager *ndef); void testDirectResolveMethod(IWritableNodeDefManager *ndef);
void testNoneResolveMethod(INodeDefManager *ndef); void testNoneResolveMethod(IWritableNodeDefManager *ndef);
}; };
static TestNodeResolver g_test_instance; static TestNodeResolver g_test_instance;
void TestNodeResolver::runTests(IGameDef *gamedef) void TestNodeResolver::runTests(IGameDef *gamedef)
{ {
IWritableNodeDefManager *parent_ndef; IWritableNodeDefManager *ndef =
INodeDefManager *ndef; (IWritableNodeDefManager *)gamedef->getNodeDefManager();
parent_ndef = (IWritableNodeDefManager *)gamedef->getNodeDefManager(); ndef->resetNodeResolveState();
ndef = parent_ndef->clone();
TEST(testNodeResolving, ndef); TEST(testNodeResolving, ndef);
delete ndef;
ndef = parent_ndef->clone(); ndef->resetNodeResolveState();
TEST(testPendingResolveCancellation, ndef); TEST(testPendingResolveCancellation, ndef);
delete ndef;
ndef = parent_ndef->clone(); ndef->resetNodeResolveState();
TEST(testDirectResolveMethod, ndef); TEST(testDirectResolveMethod, ndef);
delete ndef;
ndef = parent_ndef->clone(); ndef->resetNodeResolveState();
TEST(testNoneResolveMethod, ndef); TEST(testNoneResolveMethod, ndef);
delete ndef;
} }
class Foobar : public NodeResolver { class Foobar : public NodeResolver {
@ -113,7 +107,7 @@ void Foobaz::resolveNodeNames()
} }
void TestNodeResolver::testNodeResolving(INodeDefManager *ndef) void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef)
{ {
Foobar foobar; Foobar foobar;
size_t i; size_t i;
@ -191,7 +185,7 @@ void TestNodeResolver::testNodeResolving(INodeDefManager *ndef)
} }
void TestNodeResolver::testPendingResolveCancellation(INodeDefManager *ndef) void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *ndef)
{ {
Foobaz foobaz1; Foobaz foobaz1;
foobaz1.test_content1 = 1234; 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; Foobaz foobaz;
@ -240,7 +234,7 @@ void TestNodeResolver::testDirectResolveMethod(INodeDefManager *ndef)
} }
void TestNodeResolver::testNoneResolveMethod(INodeDefManager *ndef) void TestNodeResolver::testNoneResolveMethod(IWritableNodeDefManager *ndef)
{ {
Foobaz foobaz; Foobaz foobaz;