From 33a6382f4d1a241b541e5be136fef769b34cd533 Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 17 Jul 2020 11:46:21 +0300 Subject: [PATCH] NO_COPY_CLASS c++11 --- lib/main/concurrent/piconditionlock.h | 2 +- lib/main/concurrent/piconditionvar.h | 2 +- lib/main/core/pibase.h | 16 ++++------------ lib/main/io_devices/piiodevice.h | 3 +-- lib/main/io_utils/pidiagnostics.h | 5 ++--- lib/main/thread/pimutex.h | 16 ++++++++++------ lib/main/thread/pithread.h | 5 ++--- lib/main/thread/pitimer.h | 9 +++------ 8 files changed, 24 insertions(+), 34 deletions(-) diff --git a/lib/main/concurrent/piconditionlock.h b/lib/main/concurrent/piconditionlock.h index 4919e693..eba7a8a3 100644 --- a/lib/main/concurrent/piconditionlock.h +++ b/lib/main/concurrent/piconditionlock.h @@ -28,6 +28,7 @@ */ class PIP_EXPORT PIConditionLock { public: + NO_COPY_CLASS(PIConditionLock) explicit PIConditionLock(); ~PIConditionLock(); @@ -43,7 +44,6 @@ public: void * handle(); private: - NO_COPY_CLASS(PIConditionLock) PRIVATE_DECLARATION }; diff --git a/lib/main/concurrent/piconditionvar.h b/lib/main/concurrent/piconditionvar.h index dc32d43d..4d011187 100644 --- a/lib/main/concurrent/piconditionvar.h +++ b/lib/main/concurrent/piconditionvar.h @@ -32,6 +32,7 @@ */ class PIP_EXPORT PIConditionVariable { public: + NO_COPY_CLASS(PIConditionVariable) explicit PIConditionVariable(); virtual ~PIConditionVariable(); @@ -111,7 +112,6 @@ public: virtual bool waitFor(PIConditionLock& lk, int timeoutMs, const std::function& condition); private: - NO_COPY_CLASS(PIConditionVariable) PRIVATE_DECLARATION }; diff --git a/lib/main/core/pibase.h b/lib/main/core/pibase.h index cad880fd..0fb52771 100644 --- a/lib/main/core/pibase.h +++ b/lib/main/core/pibase.h @@ -96,9 +96,7 @@ #endif -#ifdef PIP_CXX11_SUPPORT -# include -#endif +#include #include #ifdef WINDOWS @@ -220,8 +218,8 @@ #define PRIVATEWB __privateinitializer__.p #define NO_COPY_CLASS(name) \ - explicit name(const name & ); \ - void operator =(const name & ); + name(const name&) = delete; \ + name& operator=(const name&) = delete; #ifdef FREERTOS # define PIP_MIN_MSLEEP 10. @@ -255,15 +253,9 @@ typedef unsigned long long ullong; typedef long long llong; typedef long double ldouble; -#ifdef PIP_CXX11_SUPPORT -#define piMove(v) std::move(v) -#else -#define piMove(v) v -#endif - /*! \brief Templated function for swap two values * \details Example:\n \snippet piincludes.cpp swap */ -template inline void piSwap(T & f, T & s) {T t(piMove(f)); f = piMove(s); s = piMove(t);} +template inline void piSwap(T & f, T & s) {T t(std::move(f)); f = std::move(s); s = std::move(t);} /*! \brief Templated function for swap two values without "=" * \details Example:\n \snippet piincludes.cpp swapBinary */ diff --git a/lib/main/io_devices/piiodevice.h b/lib/main/io_devices/piiodevice.h index 74a6e8b7..a9d1d227 100644 --- a/lib/main/io_devices/piiodevice.h +++ b/lib/main/io_devices/piiodevice.h @@ -54,6 +54,7 @@ class PIP_EXPORT PIIODevice: public PIThread PIOBJECT_SUBCLASS(PIIODevice, PIThread) friend void __DevicePool_threadReadDP(void * ddp); public: + NO_COPY_CLASS(PIIODevice) //! Constructs a empty PIIODevice explicit PIIODevice(); @@ -388,8 +389,6 @@ protected: void * ret_data_; private: - NO_COPY_CLASS(PIIODevice) - EVENT_HANDLER2(void, check_start, void * , data, int, delim); EVENT_HANDLER(void, write_func); diff --git a/lib/main/io_utils/pidiagnostics.h b/lib/main/io_utils/pidiagnostics.h index 74a69348..9815bb19 100644 --- a/lib/main/io_utils/pidiagnostics.h +++ b/lib/main/io_utils/pidiagnostics.h @@ -32,12 +32,13 @@ class PIP_EXPORT PIDiagnostics: public PITimer PIOBJECT_SUBCLASS(PIDiagnostics, PITimer) friend class PIConnection; public: + NO_COPY_CLASS(PIDiagnostics) //! Constructs an empty diagnostics and if "start_" start it PIDiagnostics(bool start_ = true); virtual ~PIDiagnostics(); - + //! Connection quality enum Quality { Unknown /** Unknown, no one packet received yet */ = 1, @@ -122,8 +123,6 @@ public: //! \} private: - NO_COPY_CLASS(PIDiagnostics) - struct Entry { Entry() {bytes_ok = bytes_fail = 0; cnt_ok = cnt_fail = 0; empty = true;} ullong bytes_ok; diff --git a/lib/main/thread/pimutex.h b/lib/main/thread/pimutex.h index 0c72f50b..02892916 100644 --- a/lib/main/thread/pimutex.h +++ b/lib/main/thread/pimutex.h @@ -1,9 +1,9 @@ /*! \file pimutex.h - * \brief Mutex + * \brief PIMutexLocker */ /* PIP - Platform Independent Primitives - Mutex + PIMutexLocker Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru This program is free software: you can redistribute it and/or modify @@ -25,15 +25,19 @@ #include "piinit.h" - +//! \brief PIMutexLocker +//! \details Same as std::lock_guard. +//! When a PIMutexLocker object is created, it attempts to lock the mutex it is given, if "condition" true. +//! When control leaves the scope in which the PIMutexLocker object was created, +//! the PIMutexLocker is destructed and the mutex is released, if "condition" true. +//! If "condition" false this class do nothing. +//! The PIMutexLocker class is non-copyable. class PIP_EXPORT PIMutexLocker { public: + NO_COPY_CLASS(PIMutexLocker) PIMutexLocker(PIMutex & m, bool condition = true): mutex(m), cond(condition) {if (cond) mutex.lock();} ~PIMutexLocker() {if (cond) mutex.unlock();} - - PIMutexLocker(const PIMutexLocker&) = delete; - PIMutexLocker& operator=(const PIMutexLocker&) = delete; private: PIMutex & mutex; std::atomic_bool cond; diff --git a/lib/main/thread/pithread.h b/lib/main/thread/pithread.h index ababe98e..2724f674 100644 --- a/lib/main/thread/pithread.h +++ b/lib/main/thread/pithread.h @@ -71,7 +71,8 @@ class PIP_EXPORT PIThread: public PIObject PIOBJECT_SUBCLASS(PIThread, PIObject) friend class PIIntrospectionThreads; public: - + NO_COPY_CLASS(PIThread) + //! Contructs thread with custom data "data", external function "func" and main loop delay "loop_delay". PIThread(void * data, ThreadFunc func, bool startNow = false, int loop_delay = -1); @@ -257,8 +258,6 @@ protected: PRIVATE_DECLARATION private: - NO_COPY_CLASS(PIThread) - bool _startThread(void * func); void _beginThread(); void _runThread(); diff --git a/lib/main/thread/pitimer.h b/lib/main/thread/pitimer.h index e5109cab..7ffb9338 100644 --- a/lib/main/thread/pitimer.h +++ b/lib/main/thread/pitimer.h @@ -78,7 +78,8 @@ protected: class PIP_EXPORT PITimer: public PIObject { PIOBJECT_SUBCLASS(PITimer, PIObject) public: - + NO_COPY_CLASS(PITimer) + //! \brief Constructs timer with PITimer::Thread implementation explicit PITimer(); @@ -254,17 +255,13 @@ protected: virtual void tick(void * data_, int delimiter) {} void * data_t; - volatile bool lockRun, callEvents; + std::atomic_bool lockRun, callEvents; PIMutex mutex_; TimerEvent ret_func; TimerImplementation imp_mode; PIVector delims; mutable _PITimerBase * imp; - -private: - NO_COPY_CLASS(PITimer) - };