Skip to content

Commit c56d7fe

Browse files
committedOct 28, 2015
Add DISABLE_CLASS_COPY macro (and use it)
Use this macro to disallow copying of an object using the assignment operator or copy constructor. This catches otherwise silent-but-deadly mistakes such as "ServerMap map = env->getMap();" at compile time. If so desired, it is still possible to copy a class, but it now requires an explicit call to memcpy or std::copy.
1 parent ca8e56c commit c56d7fe

File tree

11 files changed

+30
-0
lines changed

11 files changed

+30
-0
lines changed
 

Diff for: ‎src/basicmacros.h

+8
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030

3131
#define CONTAINS(c, v) (std::find((c).begin(), (c).end(), (v)) != (c).end())
3232

33+
// To disable copy constructors and assignment operations for some class
34+
// 'Foobar', add the macro DISABLE_CLASS_COPY(Foobar) as a private member.
35+
// Note this also disables copying for any classes derived from 'Foobar' as well
36+
// as classes having a 'Foobar' member.
37+
#define DISABLE_CLASS_COPY(C) \
38+
C(const C &); \
39+
C &operator=(const C &)
40+
3341
#endif

Diff for: ‎src/client.h

+2
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
682682
// TODO: Add callback to update these when g_settings changes
683683
bool m_cache_smooth_lighting;
684684
bool m_cache_enable_shaders;
685+
686+
DISABLE_CLASS_COPY(Client);
685687
};
686688

687689
#endif // !CLIENT_HEADER

Diff for: ‎src/emerge.h

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class EmergeManager {
164164
bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
165165

166166
friend class EmergeThread;
167+
168+
DISABLE_CLASS_COPY(EmergeManager);
167169
};
168170

169171
#endif

Diff for: ‎src/environment.h

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class Environment
137137
private:
138138
Mutex m_time_lock;
139139

140+
DISABLE_CLASS_COPY(Environment);
140141
};
141142

142143
/*

Diff for: ‎src/map.h

+2
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ class Map /*: public NodeContainer*/
365365
u32 m_unprocessed_count;
366366
u32 m_inc_trending_up_start_time; // milliseconds
367367
bool m_queue_size_timer_started;
368+
369+
DISABLE_CLASS_COPY(Map);
368370
};
369371

370372
/*

Diff for: ‎src/mapgen.h

+3
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ class Mapgen {
182182

183183
virtual void makeChunk(BlockMakeData *data) {}
184184
virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
185+
186+
private:
187+
DISABLE_CLASS_COPY(Mapgen);
185188
};
186189

187190
struct MapgenFactory {

Diff for: ‎src/objdef.h

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class ObjDefManager {
9090
INodeDefManager *m_ndef;
9191
std::vector<ObjDef *> m_objects;
9292
ObjDefType m_objtype;
93+
94+
private:
95+
DISABLE_CLASS_COPY(ObjDefManager);
9396
};
9497

9598
#endif

Diff for: ‎src/server.h

+2
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
649649
Particles
650650
*/
651651
std::vector<u32> m_particlespawner_ids;
652+
653+
DISABLE_CLASS_COPY(Server);
652654
};
653655

654656
/*

Diff for: ‎src/threading/mutex.h

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ DEALINGS IN THE SOFTWARE.
4444
#include <pthread.h>
4545
#endif
4646

47+
#include "basicmacros.h"
4748

4849
class Mutex
4950
{
@@ -59,6 +60,8 @@ class Mutex
5960
#else // pthread
6061
pthread_mutex_t mutex;
6162
#endif
63+
64+
DISABLE_CLASS_COPY(Mutex);
6265
};
6366

6467
#endif // C++11

Diff for: ‎src/threading/semaphore.h

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2828
#include <semaphore.h>
2929
#endif
3030

31+
#include "basicmacros.h"
3132

3233
class Semaphore {
3334
public:
@@ -46,6 +47,8 @@ class Semaphore {
4647
#else
4748
sem_t semaphore;
4849
#endif
50+
51+
DISABLE_CLASS_COPY(Semaphore);
4952
};
5053

5154
#endif

Diff for: ‎src/threading/thread.h

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class Thread {
161161
std::thread *m_thread_obj;
162162
#endif
163163

164+
DISABLE_CLASS_COPY(Thread);
164165
};
165166

166167
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.