From: Mikko Rasa Date: Wed, 25 Apr 2018 10:16:55 +0000 (+0300) Subject: Make sure all classes have sensible copy semantics X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c3e242c2629cbc9645258b30aaf07b7285d4372b;p=libs%2Fcore.git Make sure all classes have sensible copy semantics They must either support copying or be derived from NonCopyable. --- diff --git a/source/core/application.h b/source/core/application.h index d0985be..59d5bf1 100644 --- a/source/core/application.h +++ b/source/core/application.h @@ -3,13 +3,14 @@ #include #include +#include "noncopyable.h" namespace Msp { /** Base class for applications. See also RegisteredApplication. */ -class Application +class Application: private NonCopyable { protected: class Starter diff --git a/source/core/getopt.h b/source/core/getopt.h index 407d345..8d78a72 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -6,6 +6,7 @@ #include #include #include +#include "noncopyable.h" namespace Msp { @@ -58,7 +59,7 @@ A built-in --help option is provided and will output a list of options, arguments and their associated help texts. An application may override this by providing its own option with the same name. */ -class GetOpt +class GetOpt: private NonCopyable { public: enum ArgType diff --git a/source/core/module.h b/source/core/module.h index 1d008ba..4a2f04c 100644 --- a/source/core/module.h +++ b/source/core/module.h @@ -2,10 +2,11 @@ #define MSP_CORE_MODULE_H_ #include +#include "noncopyable.h" namespace Msp { -class Module +class Module: private NonCopyable { private: struct Private; diff --git a/source/core/mutex.h b/source/core/mutex.h index 71fbdf0..3d3dc84 100644 --- a/source/core/mutex.h +++ b/source/core/mutex.h @@ -1,6 +1,7 @@ #ifndef MSP_CORE_MUTEX_H_ #define MSP_CORE_MUTEX_H_ +#include "noncopyable.h" #include "refptr.h" namespace Msp { @@ -9,7 +10,7 @@ namespace Msp { A class for controlling mutually exclusive access to a resource. Only one thread can hold a lock on the mutex at a time. */ -class Mutex +class Mutex: private NonCopyable { friend class Semaphore; @@ -18,8 +19,6 @@ private: Private *priv; - Mutex(const Mutex &); - Mutex &operator=(const Mutex &); public: Mutex(); ~Mutex(); diff --git a/source/core/process.h b/source/core/process.h index 5413587..55b62cd 100644 --- a/source/core/process.h +++ b/source/core/process.h @@ -5,6 +5,7 @@ #include #include #include +#include "noncopyable.h" namespace Msp { @@ -21,7 +22,7 @@ the process or capture its output, use an IO::Pipe. Redirections performed on the self object take effect immediately. It is recommended to perform such redirections directly on the Console objects. */ -class Process +class Process: private NonCopyable { public: typedef std::vector Arguments; diff --git a/source/core/semaphore.h b/source/core/semaphore.h index 61118c7..9ab25f2 100644 --- a/source/core/semaphore.h +++ b/source/core/semaphore.h @@ -2,11 +2,11 @@ #define MSP_CORE_SEMAPHORE_H_ #include -#include "mutex.h" +#include "noncopyable.h" namespace Msp { -class Semaphore +class Semaphore: private NonCopyable { private: struct Private; diff --git a/source/core/thread.h b/source/core/thread.h index 7cf17b8..6a17912 100644 --- a/source/core/thread.h +++ b/source/core/thread.h @@ -2,6 +2,7 @@ #define MSP_CORE_THREAD_H_ #include +#include "noncopyable.h" namespace Msp { @@ -12,7 +13,7 @@ automatically started upon creation - you must manually call launch() instead. This is to allow initializing variables of the derived class before the thread is started. */ -class Thread +class Thread: private NonCopyable { private: struct Private; @@ -32,9 +33,6 @@ private: protected: Thread(const std::string & = std::string()); -private: - Thread(const Thread &); - Thread &operator=(const Thread &); public: virtual ~Thread(); diff --git a/source/debug/errorreporter.h b/source/debug/errorreporter.h index b0385ff..98806dd 100644 --- a/source/debug/errorreporter.h +++ b/source/debug/errorreporter.h @@ -2,11 +2,12 @@ #define MSP_DEBUG_ERRORREPORTER_H_ #include +#include namespace Msp { namespace Debug { -class ErrorReporter +class ErrorReporter: private NonCopyable { private: ErrorReporter *prev; diff --git a/source/debug/profiler.h b/source/debug/profiler.h index 7a82c56..521ce95 100644 --- a/source/debug/profiler.h +++ b/source/debug/profiler.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -24,7 +25,7 @@ profiled. Note: This is not thread-safe. To profile multiple threads, create a separate Profiler for each thread. */ -class Profiler +class Profiler: private NonCopyable { public: struct CallInfo diff --git a/source/debug/profilingscope.h b/source/debug/profilingscope.h index 1076888..4e4294e 100644 --- a/source/debug/profilingscope.h +++ b/source/debug/profilingscope.h @@ -1,6 +1,7 @@ #ifndef MSP_DEBUG_PROFILINGSCOPE_H_ #define MSP_DEBUG_PROFILINGSCOPE_H_ +#include #include #include "profiler.h" @@ -12,7 +13,7 @@ RAII timing class to accompany Profiler. Timing starts when an object is created and ends when it goes out of scope. If there was another object in an outer scope, it is notified of the time used in inner scopes. */ -class ProfilingScope +class ProfilingScope: private NonCopyable { private: Profiler &profiler; diff --git a/source/io/base.h b/source/io/base.h index a6a8827..b6d1ff6 100644 --- a/source/io/base.h +++ b/source/io/base.h @@ -3,6 +3,7 @@ #include #include +#include #include "handle.h" #include "mode.h" #include "poll.h" @@ -13,7 +14,7 @@ namespace IO { /** Common interface for all I/O objects. */ -class Base +class Base: private NonCopyable { public: /** RAII synchronization primitive. Prevents concurrent access to the @@ -45,9 +46,6 @@ protected: Mutex *mutex; Base(); -private: - Base(const Base &); - Base &operator=(const Base &); public: virtual ~Base(); diff --git a/source/io/eventreader.h b/source/io/eventreader.h index 01c3721..3c01f09 100644 --- a/source/io/eventreader.h +++ b/source/io/eventreader.h @@ -1,6 +1,7 @@ #ifndef MSP_IO_EVENTREADER_H_ #define MSP_IO_EVENTREADER_H_ +#include #include "handle.h" namespace Msp { @@ -18,7 +19,7 @@ empty, a new overlapped read is started. Unix-based systems can poll the fd directly, so this class reduces to a simple passthrough to sys_read. */ -class EventReader +class EventReader: private NonCopyable { private: struct Private; diff --git a/source/io/poll.h b/source/io/poll.h index 952ea89..b65166d 100644 --- a/source/io/poll.h +++ b/source/io/poll.h @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace Msp { @@ -31,7 +32,7 @@ inline PollEvent operator~(PollEvent e) { return PollEvent(~static_cast(e)); } -class Poller +class Poller: private NonCopyable { public: struct Slot diff --git a/source/strings/format.cpp b/source/strings/format.cpp index 1b8b465..773eefa 100644 --- a/source/strings/format.cpp +++ b/source/strings/format.cpp @@ -11,6 +11,20 @@ Formatter::Formatter(const string &f): advance(); } +Formatter::Formatter(const Formatter &other): + fmt(other.fmt), + pos(fmt.begin()+(other.pos-other.fmt.begin())), + result(other.result) +{ } + +Formatter &Formatter::operator=(const Formatter &other) +{ + fmt = other.fmt; + pos = fmt.begin()+(other.pos-other.fmt.begin()); + result = other.result; + return *this; +} + const string &Formatter::str() const { if(pos!=fmt.end()) diff --git a/source/strings/format.h b/source/strings/format.h index 7bd8264..ea5d5b0 100644 --- a/source/strings/format.h +++ b/source/strings/format.h @@ -18,6 +18,8 @@ private: public: Formatter(const std::string &); + Formatter(const Formatter &); + Formatter &operator=(const Formatter &); /** Extracts the next conversion from the format string and formats the given value with it. Will throw if no more conversions are found. */ diff --git a/source/time/timer.h b/source/time/timer.h index 21aeb2b..575599f 100644 --- a/source/time/timer.h +++ b/source/time/timer.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "timedelta.h" #include "timestamp.h" @@ -18,7 +19,7 @@ of the returned slot. This class is thread-safe, to allow running timers in a separate thread. */ -class Timer +class Timer: private NonCopyable { public: class Slot