From a6f222987754f76b73149a65bffd558bdf710f21 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 27 Nov 2015 14:05:30 +0100 Subject: [PATCH 1/2] Make classes final if they're not meant to be derived from --- assert/AssertFailed.h | 3 ++- crypto/RandomPadding.h | 2 +- crypto/kdf/DerivedKey.h | 2 +- crypto/kdf/DerivedKeyConfig.h | 2 +- data/DataFixture.h | 2 +- io/Console.h | 5 ++++- io/pipestream.h | 2 +- lock/ConditionBarrier.h | 4 +++- lock/MutexPoolLock.h | 2 +- logging/Logger.h | 2 +- process/subprocess.h | 5 ++++- random/OSRandomGenerator.h | 3 +++ random/Random.h | 4 +++- random/RandomDataBuffer.h | 2 +- random/RandomGenerator.h | 7 +++++++ random/RandomGeneratorThread.h | 2 +- random/ThreadsafeRandomDataBuffer.h | 2 +- 17 files changed, 36 insertions(+), 15 deletions(-) diff --git a/assert/AssertFailed.h b/assert/AssertFailed.h index 5e9d398c..3d9b8978 100644 --- a/assert/AssertFailed.h +++ b/assert/AssertFailed.h @@ -4,10 +4,11 @@ #include #include +#include "../macros.h" namespace cpputils { - class AssertFailed : public std::exception { + class AssertFailed final: public std::exception { public: AssertFailed(const std::string &message) : _message(message) { } diff --git a/crypto/RandomPadding.h b/crypto/RandomPadding.h index 147f2b1b..2dd2a427 100644 --- a/crypto/RandomPadding.h +++ b/crypto/RandomPadding.h @@ -7,7 +7,7 @@ namespace cpputils { //TODO Test - class RandomPadding { + class RandomPadding final { public: static Data add(const Data &data, size_t targetSize); static boost::optional remove(const Data &data); diff --git a/crypto/kdf/DerivedKey.h b/crypto/kdf/DerivedKey.h index a985c2df..2f6eb582 100644 --- a/crypto/kdf/DerivedKey.h +++ b/crypto/kdf/DerivedKey.h @@ -8,7 +8,7 @@ namespace cpputils { template - class DerivedKey { + class DerivedKey final { public: DerivedKey(DerivedKeyConfig config, const FixedSizeData &key): _config(std::move(config)), _key(key) {} DerivedKey(DerivedKey &&rhs) = default; diff --git a/crypto/kdf/DerivedKeyConfig.h b/crypto/kdf/DerivedKeyConfig.h index 43f676ee..045ed0eb 100644 --- a/crypto/kdf/DerivedKeyConfig.h +++ b/crypto/kdf/DerivedKeyConfig.h @@ -13,7 +13,7 @@ namespace cpputils { //TODO Test operator==/!= //TODO Use SCryptSettings as a member here instead of storing _N, _r, _p. - class DerivedKeyConfig { + class DerivedKeyConfig final { public: DerivedKeyConfig(Data salt, uint64_t N, uint32_t r, uint32_t p) : _salt(std::move(salt)), diff --git a/data/DataFixture.h b/data/DataFixture.h index 464e5ad4..a2ac705f 100644 --- a/data/DataFixture.h +++ b/data/DataFixture.h @@ -7,7 +7,7 @@ namespace cpputils { -class DataFixture { +class DataFixture final { public: static Data generate(size_t size, long long int seed = 1); diff --git a/io/Console.h b/io/Console.h index 5c88b891..882b0e82 100644 --- a/io/Console.h +++ b/io/Console.h @@ -6,6 +6,7 @@ #include #include #include +#include "../macros.h" namespace cpputils { @@ -17,7 +18,7 @@ public: virtual void print(const std::string &output) = 0; }; -class IOStreamConsole: public Console { +class IOStreamConsole final: public Console { public: IOStreamConsole(); IOStreamConsole(std::ostream &output, std::istream &input); @@ -30,6 +31,8 @@ private: std::ostream &_output; std::istream &_input; + + DISALLOW_COPY_AND_ASSIGN(IOStreamConsole); }; } diff --git a/io/pipestream.h b/io/pipestream.h index 101bb601..fa71c373 100644 --- a/io/pipestream.h +++ b/io/pipestream.h @@ -53,7 +53,7 @@ namespace cpputils { - class pipestream : public std::streambuf { + class pipestream final : public std::streambuf { private: typedef std::streambuf::traits_type traits_type; typedef std::string::size_type string_size_t; diff --git a/lock/ConditionBarrier.h b/lock/ConditionBarrier.h index 720c0722..67b086d0 100644 --- a/lock/ConditionBarrier.h +++ b/lock/ConditionBarrier.h @@ -12,7 +12,7 @@ namespace cpputils { // Like a condition variable, but without spurious wakeups. // The waiting threads are only woken, when notify() is called. // After a call to release(), future calls to wait() will not block anymore. - class ConditionBarrier { + class ConditionBarrier final { public: ConditionBarrier() :_mutex(), _cv(), _triggered(false) { } @@ -33,6 +33,8 @@ namespace cpputils { std::mutex _mutex; std::condition_variable _cv; bool _triggered; + + DISALLOW_COPY_AND_ASSIGN(ConditionBarrier); }; } diff --git a/lock/MutexPoolLock.h b/lock/MutexPoolLock.h index d70ba3af..43cbc228 100644 --- a/lock/MutexPoolLock.h +++ b/lock/MutexPoolLock.h @@ -6,7 +6,7 @@ namespace cpputils { template - class MutexPoolLock { + class MutexPoolLock final { public: MutexPoolLock(LockPool *pool, const LockName &lockName): _pool(pool), _lockName(lockName) { _pool->lock(_lockName); diff --git a/logging/Logger.h b/logging/Logger.h index c9a17f81..cc4f021f 100644 --- a/logging/Logger.h +++ b/logging/Logger.h @@ -7,7 +7,7 @@ namespace cpputils { namespace logging { - class Logger { + class Logger final { public: void setLogger(std::shared_ptr logger) { _logger = logger; diff --git a/process/subprocess.h b/process/subprocess.h index 18918f32..907aab76 100644 --- a/process/subprocess.h +++ b/process/subprocess.h @@ -3,15 +3,18 @@ #define MESSMER_CPPUTILS_PROCESS_SUBPROCESS_H #include +#include "../macros.h" namespace cpputils { //TODO Test - class Subprocess { + class Subprocess final { public: static std::string call(const std::string &command); static int callAndGetReturnCode(const std::string &command); private: static FILE* _call(const std::string &command); + + DISALLOW_COPY_AND_ASSIGN(Subprocess); }; } diff --git a/random/OSRandomGenerator.h b/random/OSRandomGenerator.h index 13e8db49..3642e3c8 100644 --- a/random/OSRandomGenerator.h +++ b/random/OSRandomGenerator.h @@ -12,6 +12,9 @@ namespace cpputils { protected: void _get(void *target, size_t bytes) override; + + private: + DISALLOW_COPY_AND_ASSIGN(OSRandomGenerator); }; inline OSRandomGenerator::OSRandomGenerator() {} diff --git a/random/Random.h b/random/Random.h index 15cf0fc2..49610460 100644 --- a/random/Random.h +++ b/random/Random.h @@ -9,7 +9,7 @@ #include namespace cpputils { - class Random { + class Random final { public: static PseudoRandomPool &PseudoRandom() { std::unique_lock lock(_mutex); @@ -25,6 +25,8 @@ namespace cpputils { private: static std::mutex _mutex; + + DISALLOW_COPY_AND_ASSIGN(Random); }; } diff --git a/random/RandomDataBuffer.h b/random/RandomDataBuffer.h index 09fa08b9..89c5fbe9 100644 --- a/random/RandomDataBuffer.h +++ b/random/RandomDataBuffer.h @@ -7,7 +7,7 @@ namespace cpputils { //TODO Test - class RandomDataBuffer { + class RandomDataBuffer final { public: RandomDataBuffer(); diff --git a/random/RandomGenerator.h b/random/RandomGenerator.h index 51641aeb..b0d0ba97 100644 --- a/random/RandomGenerator.h +++ b/random/RandomGenerator.h @@ -7,6 +7,8 @@ namespace cpputils { class RandomGenerator { public: + RandomGenerator(); + template FixedSizeData getFixedSize(); Data get(size_t size); @@ -14,8 +16,13 @@ namespace cpputils { virtual void _get(void *target, size_t bytes) = 0; private: static std::mutex _mutex; + + DISALLOW_COPY_AND_ASSIGN(RandomGenerator); }; + inline RandomGenerator::RandomGenerator() { + } + template inline FixedSizeData RandomGenerator::getFixedSize() { FixedSizeData result = FixedSizeData::Null(); _get(result.data(), SIZE); diff --git a/random/RandomGeneratorThread.h b/random/RandomGeneratorThread.h index 895b3fc5..fd678816 100644 --- a/random/RandomGeneratorThread.h +++ b/random/RandomGeneratorThread.h @@ -8,7 +8,7 @@ namespace cpputils { //TODO Test - class RandomGeneratorThread { + class RandomGeneratorThread final { public: RandomGeneratorThread(ThreadsafeRandomDataBuffer *buffer, size_t minSize, size_t maxSize); diff --git a/random/ThreadsafeRandomDataBuffer.h b/random/ThreadsafeRandomDataBuffer.h index daa36d36..a1047aa7 100644 --- a/random/ThreadsafeRandomDataBuffer.h +++ b/random/ThreadsafeRandomDataBuffer.h @@ -9,7 +9,7 @@ namespace cpputils { //TODO Test - class ThreadsafeRandomDataBuffer { + class ThreadsafeRandomDataBuffer final { public: ThreadsafeRandomDataBuffer(); From ec0bc13c27a2b4e8d75fa19f5ab039e05d7a8252 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 27 Nov 2015 17:53:52 +0100 Subject: [PATCH 2/2] Fix test cases --- lock/ConditionBarrier.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lock/ConditionBarrier.h b/lock/ConditionBarrier.h index 67b086d0..459718a8 100644 --- a/lock/ConditionBarrier.h +++ b/lock/ConditionBarrier.h @@ -4,6 +4,7 @@ #include #include +#include "../macros.h" //TODO Test //TODO Merge lock folder with thread folder