Skip to content

Commit

Permalink
Add DISABLE_CLASS_COPY macro (and use it)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kwolekr committed Oct 28, 2015
1 parent ca8e56c commit c56d7fe
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/basicmacros.h
Expand Up @@ -30,4 +30,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,

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

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

#endif
2 changes: 2 additions & 0 deletions src/client.h
Expand Up @@ -682,6 +682,8 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
// TODO: Add callback to update these when g_settings changes
bool m_cache_smooth_lighting;
bool m_cache_enable_shaders;

DISABLE_CLASS_COPY(Client);
};

#endif // !CLIENT_HEADER
2 changes: 2 additions & 0 deletions src/emerge.h
Expand Up @@ -164,6 +164,8 @@ class EmergeManager {
bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);

friend class EmergeThread;

DISABLE_CLASS_COPY(EmergeManager);
};

#endif
1 change: 1 addition & 0 deletions src/environment.h
Expand Up @@ -137,6 +137,7 @@ class Environment
private:
Mutex m_time_lock;

DISABLE_CLASS_COPY(Environment);
};

/*
Expand Down
2 changes: 2 additions & 0 deletions src/map.h
Expand Up @@ -365,6 +365,8 @@ class Map /*: public NodeContainer*/
u32 m_unprocessed_count;
u32 m_inc_trending_up_start_time; // milliseconds
bool m_queue_size_timer_started;

DISABLE_CLASS_COPY(Map);
};

/*
Expand Down
3 changes: 3 additions & 0 deletions src/mapgen.h
Expand Up @@ -182,6 +182,9 @@ class Mapgen {

virtual void makeChunk(BlockMakeData *data) {}
virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }

private:
DISABLE_CLASS_COPY(Mapgen);
};

struct MapgenFactory {
Expand Down
3 changes: 3 additions & 0 deletions src/objdef.h
Expand Up @@ -90,6 +90,9 @@ class ObjDefManager {
INodeDefManager *m_ndef;
std::vector<ObjDef *> m_objects;
ObjDefType m_objtype;

private:
DISABLE_CLASS_COPY(ObjDefManager);
};

#endif
2 changes: 2 additions & 0 deletions src/server.h
Expand Up @@ -649,6 +649,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
Particles
*/
std::vector<u32> m_particlespawner_ids;

DISABLE_CLASS_COPY(Server);
};

/*
Expand Down
3 changes: 3 additions & 0 deletions src/threading/mutex.h
Expand Up @@ -44,6 +44,7 @@ DEALINGS IN THE SOFTWARE.
#include <pthread.h>
#endif

#include "basicmacros.h"

class Mutex
{
Expand All @@ -59,6 +60,8 @@ class Mutex
#else // pthread
pthread_mutex_t mutex;
#endif

DISABLE_CLASS_COPY(Mutex);
};

#endif // C++11
Expand Down
3 changes: 3 additions & 0 deletions src/threading/semaphore.h
Expand Up @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <semaphore.h>
#endif

#include "basicmacros.h"

class Semaphore {
public:
Expand All @@ -46,6 +47,8 @@ class Semaphore {
#else
sem_t semaphore;
#endif

DISABLE_CLASS_COPY(Semaphore);
};

#endif
Expand Down
1 change: 1 addition & 0 deletions src/threading/thread.h
Expand Up @@ -161,6 +161,7 @@ class Thread {
std::thread *m_thread_obj;
#endif

DISABLE_CLASS_COPY(Thread);
};

#endif
Expand Down

0 comments on commit c56d7fe

Please sign in to comment.