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())
// 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

@ -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

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

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

@ -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);
};
/*

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

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

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

@ -44,6 +44,7 @@ DEALINGS IN THE SOFTWARE.
#include <pthread.h>
#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

@ -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:
@ -46,6 +47,8 @@ private:
#else
sem_t semaphore;
#endif
DISABLE_CLASS_COPY(Semaphore);
};
#endif

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