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.
This commit is contained in:
kwolekr 2015-10-27 02:51:43 -04:00
parent ca8e56c15a
commit c56d7fe0eb
11 changed files with 30 additions and 0 deletions

@ -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()) #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 #endif

@ -682,6 +682,8 @@ private:
// TODO: Add callback to update these when g_settings changes // TODO: Add callback to update these when g_settings changes
bool m_cache_smooth_lighting; bool m_cache_smooth_lighting;
bool m_cache_enable_shaders; bool m_cache_enable_shaders;
DISABLE_CLASS_COPY(Client);
}; };
#endif // !CLIENT_HEADER #endif // !CLIENT_HEADER

@ -164,6 +164,8 @@ private:
bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata); bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
friend class EmergeThread; friend class EmergeThread;
DISABLE_CLASS_COPY(EmergeManager);
}; };
#endif #endif

@ -137,6 +137,7 @@ protected:
private: private:
Mutex m_time_lock; Mutex m_time_lock;
DISABLE_CLASS_COPY(Environment);
}; };
/* /*

@ -365,6 +365,8 @@ private:
u32 m_unprocessed_count; u32 m_unprocessed_count;
u32 m_inc_trending_up_start_time; // milliseconds u32 m_inc_trending_up_start_time; // milliseconds
bool m_queue_size_timer_started; bool m_queue_size_timer_started;
DISABLE_CLASS_COPY(Map);
}; };
/* /*

@ -182,6 +182,9 @@ public:
virtual void makeChunk(BlockMakeData *data) {} virtual void makeChunk(BlockMakeData *data) {}
virtual int getGroundLevelAtPoint(v2s16 p) { return 0; } virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
private:
DISABLE_CLASS_COPY(Mapgen);
}; };
struct MapgenFactory { struct MapgenFactory {

@ -90,6 +90,9 @@ protected:
INodeDefManager *m_ndef; INodeDefManager *m_ndef;
std::vector<ObjDef *> m_objects; std::vector<ObjDef *> m_objects;
ObjDefType m_objtype; ObjDefType m_objtype;
private:
DISABLE_CLASS_COPY(ObjDefManager);
}; };
#endif #endif

@ -649,6 +649,8 @@ private:
Particles Particles
*/ */
std::vector<u32> m_particlespawner_ids; std::vector<u32> m_particlespawner_ids;
DISABLE_CLASS_COPY(Server);
}; };
/* /*

@ -44,6 +44,7 @@ DEALINGS IN THE SOFTWARE.
#include <pthread.h> #include <pthread.h>
#endif #endif
#include "basicmacros.h"
class Mutex class Mutex
{ {
@ -59,6 +60,8 @@ private:
#else // pthread #else // pthread
pthread_mutex_t mutex; pthread_mutex_t mutex;
#endif #endif
DISABLE_CLASS_COPY(Mutex);
}; };
#endif // C++11 #endif // C++11

@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <semaphore.h> #include <semaphore.h>
#endif #endif
#include "basicmacros.h"
class Semaphore { class Semaphore {
public: public:
@ -46,6 +47,8 @@ private:
#else #else
sem_t semaphore; sem_t semaphore;
#endif #endif
DISABLE_CLASS_COPY(Semaphore);
}; };
#endif #endif

@ -161,6 +161,7 @@ private:
std::thread *m_thread_obj; std::thread *m_thread_obj;
#endif #endif
DISABLE_CLASS_COPY(Thread);
}; };
#endif #endif