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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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