From c56d7fe0eba7905b0a63c4a1cfe909988653c23d Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 27 Oct 2015 02:51:43 -0400 Subject: 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. --- src/basicmacros.h | 8 ++++++++ src/client.h | 2 ++ src/emerge.h | 2 ++ src/environment.h | 1 + src/map.h | 2 ++ src/mapgen.h | 3 +++ src/objdef.h | 3 +++ src/server.h | 2 ++ src/threading/mutex.h | 3 +++ src/threading/semaphore.h | 3 +++ src/threading/thread.h | 1 + 11 files changed, 30 insertions(+) (limited to 'src') diff --git a/src/basicmacros.h b/src/basicmacros.h index 05987e32f..cebf06043 100644 --- a/src/basicmacros.h +++ b/src/basicmacros.h @@ -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 diff --git a/src/client.h b/src/client.h index 5b57aa52a..07fb79dca 100644 --- a/src/client.h +++ b/src/client.h @@ -682,6 +682,8 @@ private: // 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 diff --git a/src/emerge.h b/src/emerge.h index 47ff218b8..a143b660f 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -164,6 +164,8 @@ private: bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata); friend class EmergeThread; + + DISABLE_CLASS_COPY(EmergeManager); }; #endif diff --git a/src/environment.h b/src/environment.h index 1984cf40d..cb8be71b2 100644 --- a/src/environment.h +++ b/src/environment.h @@ -137,6 +137,7 @@ protected: private: Mutex m_time_lock; + DISABLE_CLASS_COPY(Environment); }; /* diff --git a/src/map.h b/src/map.h index 78af8c8d3..78614d228 100644 --- a/src/map.h +++ b/src/map.h @@ -365,6 +365,8 @@ private: u32 m_unprocessed_count; u32 m_inc_trending_up_start_time; // milliseconds bool m_queue_size_timer_started; + + DISABLE_CLASS_COPY(Map); }; /* diff --git a/src/mapgen.h b/src/mapgen.h index 0561ddf98..57e995847 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -182,6 +182,9 @@ public: virtual void makeChunk(BlockMakeData *data) {} virtual int getGroundLevelAtPoint(v2s16 p) { return 0; } + +private: + DISABLE_CLASS_COPY(Mapgen); }; struct MapgenFactory { diff --git a/src/objdef.h b/src/objdef.h index 65e5c0176..e7e956e51 100644 --- a/src/objdef.h +++ b/src/objdef.h @@ -90,6 +90,9 @@ protected: INodeDefManager *m_ndef; std::vector m_objects; ObjDefType m_objtype; + +private: + DISABLE_CLASS_COPY(ObjDefManager); }; #endif diff --git a/src/server.h b/src/server.h index fa732010a..a4be7d3fb 100644 --- a/src/server.h +++ b/src/server.h @@ -649,6 +649,8 @@ private: Particles */ std::vector m_particlespawner_ids; + + DISABLE_CLASS_COPY(Server); }; /* diff --git a/src/threading/mutex.h b/src/threading/mutex.h index 4c9af71bf..f1a4882b7 100644 --- a/src/threading/mutex.h +++ b/src/threading/mutex.h @@ -44,6 +44,7 @@ DEALINGS IN THE SOFTWARE. #include #endif +#include "basicmacros.h" class Mutex { @@ -59,6 +60,8 @@ private: #else // pthread pthread_mutex_t mutex; #endif + + DISABLE_CLASS_COPY(Mutex); }; #endif // C++11 diff --git a/src/threading/semaphore.h b/src/threading/semaphore.h index 58d758f2e..736f2bc78 100644 --- a/src/threading/semaphore.h +++ b/src/threading/semaphore.h @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #endif +#include "basicmacros.h" class Semaphore { public: @@ -46,6 +47,8 @@ private: #else sem_t semaphore; #endif + + DISABLE_CLASS_COPY(Semaphore); }; #endif diff --git a/src/threading/thread.h b/src/threading/thread.h index 3d85e0eb9..83ca785c7 100644 --- a/src/threading/thread.h +++ b/src/threading/thread.h @@ -161,6 +161,7 @@ private: std::thread *m_thread_obj; #endif + DISABLE_CLASS_COPY(Thread); }; #endif -- cgit v1.2.3