Skip to content

Commit 633af58

Browse files
committedMay 5, 2015
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
1 parent b785577 commit 633af58

File tree

3 files changed

+46
-48
lines changed

3 files changed

+46
-48
lines changed
 

‎src/nodedef.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void ContentFeatures::reset()
241241
sound_dug = SimpleSoundSpec();
242242
}
243243

244-
void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
244+
void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
245245
{
246246
if(protocol_version < 24){
247247
serializeOld(os, protocol_version);
@@ -398,9 +398,9 @@ class CNodeDefManager: public IWritableNodeDefManager {
398398
virtual content_t allocateDummy(const std::string &name);
399399
virtual void updateAliases(IItemDefManager *idef);
400400
virtual void updateTextures(IGameDef *gamedef,
401-
/*argument: */void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
402-
/*argument: */void *progress_callback_args);
403-
void serialize(std::ostream &os, u16 protocol_version);
401+
void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
402+
void *progress_cbk_args);
403+
void serialize(std::ostream &os, u16 protocol_version) const;
404404
void deSerialize(std::istream &is);
405405

406406
inline virtual bool getNodeRegistrationStatus() const;
@@ -409,6 +409,7 @@ class CNodeDefManager: public IWritableNodeDefManager {
409409
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how);
410410
virtual bool cancelNodeResolveCallback(NodeResolver *nr);
411411
virtual void runNodeResolveCallbacks();
412+
virtual void resetNodeResolveState();
412413

413414
private:
414415
void addNameIdMapping(content_t i, std::string name);
@@ -474,8 +475,7 @@ void CNodeDefManager::clear()
474475
m_group_to_items.clear();
475476
m_next_id = 0;
476477

477-
m_node_registration_complete = false;
478-
m_pending_resolve_callbacks.clear();
478+
resetNodeResolveState();
479479

480480
u32 initial_length = 0;
481481
initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
@@ -972,7 +972,7 @@ void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
972972
#endif
973973

974974

975-
void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
975+
void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
976976
{
977977
writeU8(os, 1); // version
978978
u16 count = 0;
@@ -981,7 +981,7 @@ void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
981981
if (i == CONTENT_IGNORE || i == CONTENT_AIR
982982
|| i == CONTENT_UNKNOWN)
983983
continue;
984-
ContentFeatures *f = &m_content_features[i];
984+
const ContentFeatures *f = &m_content_features[i];
985985
if (f->name == "")
986986
continue;
987987
writeU16(os2, i);
@@ -1062,7 +1062,7 @@ IWritableNodeDefManager *createNodeDefManager()
10621062

10631063

10641064
//// Serialization of old ContentFeatures formats
1065-
void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
1065+
void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
10661066
{
10671067
if (protocol_version == 13)
10681068
{
@@ -1342,6 +1342,13 @@ void CNodeDefManager::runNodeResolveCallbacks()
13421342
}
13431343

13441344

1345+
void CNodeDefManager::resetNodeResolveState()
1346+
{
1347+
m_node_registration_complete = false;
1348+
m_pending_resolve_callbacks.clear();
1349+
}
1350+
1351+
13451352
////
13461353
//// NodeResolver
13471354
////

‎src/nodedef.h

+16-19
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ struct ContentFeatures
265265
ContentFeatures();
266266
~ContentFeatures();
267267
void reset();
268-
void serialize(std::ostream &os, u16 protocol_version);
268+
void serialize(std::ostream &os, u16 protocol_version) const;
269269
void deSerialize(std::istream &is);
270-
void serializeOld(std::ostream &os, u16 protocol_version);
270+
void serializeOld(std::ostream &os, u16 protocol_version) const;
271271
void deSerializeOld(std::istream &is, int version);
272272

273273
/*
@@ -288,48 +288,44 @@ enum NodeResolveMethod {
288288
NODE_RESOLVE_DEFERRED,
289289
};
290290

291-
class INodeDefManager
292-
{
291+
class INodeDefManager {
293292
public:
294293
INodeDefManager(){}
295294
virtual ~INodeDefManager(){}
296295
// Get node definition
297-
virtual const ContentFeatures& get(content_t c) const=0;
298-
virtual const ContentFeatures& get(const MapNode &n) const=0;
296+
virtual const ContentFeatures &get(content_t c) const=0;
297+
virtual const ContentFeatures &get(const MapNode &n) const=0;
299298
virtual bool getId(const std::string &name, content_t &result) const=0;
300299
virtual content_t getId(const std::string &name) const=0;
301300
// Allows "group:name" in addition to regular node names
302301
virtual void getIds(const std::string &name, std::set<content_t> &result)
303302
const=0;
304-
virtual const ContentFeatures& get(const std::string &name) const=0;
303+
virtual const ContentFeatures &get(const std::string &name) const=0;
305304

306-
virtual void serialize(std::ostream &os, u16 protocol_version)=0;
305+
virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
307306

308307
virtual bool getNodeRegistrationStatus() const=0;
309-
virtual void setNodeRegistrationStatus(bool completed)=0;
310308

311309
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0;
312310
virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
313-
virtual void runNodeResolveCallbacks()=0;
314311
};
315312

316-
class IWritableNodeDefManager : public INodeDefManager
317-
{
313+
class IWritableNodeDefManager : public INodeDefManager {
318314
public:
319315
IWritableNodeDefManager(){}
320316
virtual ~IWritableNodeDefManager(){}
321317
virtual IWritableNodeDefManager* clone()=0;
322318
// Get node definition
323-
virtual const ContentFeatures& get(content_t c) const=0;
324-
virtual const ContentFeatures& get(const MapNode &n) const=0;
319+
virtual const ContentFeatures &get(content_t c) const=0;
320+
virtual const ContentFeatures &get(const MapNode &n) const=0;
325321
virtual bool getId(const std::string &name, content_t &result) const=0;
326322
// If not found, returns CONTENT_IGNORE
327323
virtual content_t getId(const std::string &name) const=0;
328324
// Allows "group:name" in addition to regular node names
329325
virtual void getIds(const std::string &name, std::set<content_t> &result)
330-
const=0;
326+
const=0;
331327
// If not found, returns the features of CONTENT_UNKNOWN
332-
virtual const ContentFeatures& get(const std::string &name) const=0;
328+
virtual const ContentFeatures &get(const std::string &name) const=0;
333329

334330
// Register node definition by name (allocate an id)
335331
// If returns CONTENT_IGNORE, could not allocate id
@@ -348,10 +344,10 @@ class IWritableNodeDefManager : public INodeDefManager
348344
Update tile textures to latest return values of TextueSource.
349345
*/
350346
virtual void updateTextures(IGameDef *gamedef,
351-
/*argument: */void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
352-
/*argument: */void *progress_callback_args)=0;
347+
void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
348+
void *progress_cbk_args)=0;
353349

354-
virtual void serialize(std::ostream &os, u16 protocol_version)=0;
350+
virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
355351
virtual void deSerialize(std::istream &is)=0;
356352

357353
virtual bool getNodeRegistrationStatus() const=0;
@@ -360,6 +356,7 @@ class IWritableNodeDefManager : public INodeDefManager
360356
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0;
361357
virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
362358
virtual void runNodeResolveCallbacks()=0;
359+
virtual void resetNodeResolveState()=0;
363360
};
364361

365362
IWritableNodeDefManager *createNodeDefManager();

‎src/unittest/test_noderesolver.cpp

+14-20
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,30 @@ class TestNodeResolver : public TestBase {
3131

3232
void runTests(IGameDef *gamedef);
3333

34-
void testNodeResolving(INodeDefManager *ndef);
35-
void testPendingResolveCancellation(INodeDefManager *ndef);
36-
void testDirectResolveMethod(INodeDefManager *ndef);
37-
void testNoneResolveMethod(INodeDefManager *ndef);
34+
void testNodeResolving(IWritableNodeDefManager *ndef);
35+
void testPendingResolveCancellation(IWritableNodeDefManager *ndef);
36+
void testDirectResolveMethod(IWritableNodeDefManager *ndef);
37+
void testNoneResolveMethod(IWritableNodeDefManager *ndef);
3838
};
3939

4040
static TestNodeResolver g_test_instance;
4141

4242
void TestNodeResolver::runTests(IGameDef *gamedef)
4343
{
44-
IWritableNodeDefManager *parent_ndef;
45-
INodeDefManager *ndef;
44+
IWritableNodeDefManager *ndef =
45+
(IWritableNodeDefManager *)gamedef->getNodeDefManager();
4646

47-
parent_ndef = (IWritableNodeDefManager *)gamedef->getNodeDefManager();
48-
49-
ndef = parent_ndef->clone();
47+
ndef->resetNodeResolveState();
5048
TEST(testNodeResolving, ndef);
51-
delete ndef;
5249

53-
ndef = parent_ndef->clone();
50+
ndef->resetNodeResolveState();
5451
TEST(testPendingResolveCancellation, ndef);
55-
delete ndef;
5652

57-
ndef = parent_ndef->clone();
53+
ndef->resetNodeResolveState();
5854
TEST(testDirectResolveMethod, ndef);
59-
delete ndef;
6055

61-
ndef = parent_ndef->clone();
56+
ndef->resetNodeResolveState();
6257
TEST(testNoneResolveMethod, ndef);
63-
delete ndef;
6458
}
6559

6660
class Foobar : public NodeResolver {
@@ -113,7 +107,7 @@ void Foobaz::resolveNodeNames()
113107
}
114108

115109

116-
void TestNodeResolver::testNodeResolving(INodeDefManager *ndef)
110+
void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef)
117111
{
118112
Foobar foobar;
119113
size_t i;
@@ -191,7 +185,7 @@ void TestNodeResolver::testNodeResolving(INodeDefManager *ndef)
191185
}
192186

193187

194-
void TestNodeResolver::testPendingResolveCancellation(INodeDefManager *ndef)
188+
void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *ndef)
195189
{
196190
Foobaz foobaz1;
197191
foobaz1.test_content1 = 1234;
@@ -219,7 +213,7 @@ void TestNodeResolver::testPendingResolveCancellation(INodeDefManager *ndef)
219213
}
220214

221215

222-
void TestNodeResolver::testDirectResolveMethod(INodeDefManager *ndef)
216+
void TestNodeResolver::testDirectResolveMethod(IWritableNodeDefManager *ndef)
223217
{
224218
Foobaz foobaz;
225219

@@ -240,7 +234,7 @@ void TestNodeResolver::testDirectResolveMethod(INodeDefManager *ndef)
240234
}
241235

242236

243-
void TestNodeResolver::testNoneResolveMethod(INodeDefManager *ndef)
237+
void TestNodeResolver::testNoneResolveMethod(IWritableNodeDefManager *ndef)
244238
{
245239
Foobaz foobaz;
246240

0 commit comments

Comments
 (0)
Please sign in to comment.