From e0d4ea8fe95d59f0d6e21376e8d16b2c05b0495c Mon Sep 17 00:00:00 2001 From: Zofia Abramowska Date: Mon, 17 Nov 2014 17:48:30 +0100 Subject: [PATCH] Optimize message() method of exceptions message() returned std::string through value, causing unnecessary copy. Changed it to return const std::string reference. Change-Id: I8d9631fb2468924d35ba4376b1821d0d01c7f70c --- src/common/exceptions/BucketDeserializationException.h | 11 +++++------ src/common/exceptions/BucketNotExistsException.h | 9 ++++++--- src/common/exceptions/BucketRecordCorruptedException.h | 14 +++++++------- src/common/exceptions/BucketSerializationException.h | 11 +++++------ src/common/exceptions/CannotCreateFileException.h | 11 +++++------ src/common/exceptions/ContextErrorException.h | 9 ++++++--- src/common/exceptions/DefaultBucketDeletionException.h | 10 +++++++--- src/common/exceptions/DefaultBucketSetNoneException.h | 10 +++++++--- src/common/exceptions/DescriptorNotExistsException.h | 10 ++++------ src/common/exceptions/Exception.h | 2 +- src/common/exceptions/FileNotFoundException.h | 13 ++++++------- src/common/exceptions/InitException.h | 9 ++++++--- src/common/exceptions/InvalidBucketIdException.h | 11 +++++------ src/common/exceptions/InvalidProtocolException.h | 12 ++++++------ src/common/exceptions/NoMemoryException.h | 8 ++++---- src/common/exceptions/NotImplementedException.h | 9 ++++++--- src/common/exceptions/NullPointerException.h | 10 ++++------ src/common/exceptions/OutOfDataException.h | 12 +++++------- src/common/exceptions/PluginErrorException.h | 2 +- src/common/exceptions/PluginNotFoundException.h | 14 ++++++-------- src/common/exceptions/UnexpectedErrorException.h | 17 ++++++----------- 21 files changed, 108 insertions(+), 106 deletions(-) diff --git a/src/common/exceptions/BucketDeserializationException.h b/src/common/exceptions/BucketDeserializationException.h index c5670c8..36ef8b0 100644 --- a/src/common/exceptions/BucketDeserializationException.h +++ b/src/common/exceptions/BucketDeserializationException.h @@ -29,13 +29,12 @@ namespace Cynara { class BucketDeserializationException : public DatabaseException { public: - BucketDeserializationException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) {} + BucketDeserializationException(const PolicyBucketId &bucket) : m_bucketId(bucket) { + m_message = "Could not deserialize bucket " + bucketId(); + } virtual ~BucketDeserializationException() {}; - const std::string message(void) const { - if (m_message.empty()) { - m_message = "Could not deserialize bucket " + m_bucketId; - } + const std::string &message(void) const { return m_message; } @@ -44,7 +43,7 @@ public: } private: - mutable std::string m_message; + std::string m_message; PolicyBucketId m_bucketId; }; diff --git a/src/common/exceptions/BucketNotExistsException.h b/src/common/exceptions/BucketNotExistsException.h index 9ba9505..68d041b 100644 --- a/src/common/exceptions/BucketNotExistsException.h +++ b/src/common/exceptions/BucketNotExistsException.h @@ -33,11 +33,13 @@ namespace Cynara { class BucketNotExistsException : public Exception { public: BucketNotExistsException() = delete; - BucketNotExistsException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) {} + BucketNotExistsException(const PolicyBucketId &bucketId) + : m_bucketId(bucketId), m_message("BucketNotExistsException") { + } virtual ~BucketNotExistsException() {}; - virtual const std::string message(void) const { - return "BucketNotExistsException"; + virtual const std::string &message(void) const { + return m_message; } const PolicyBucketId &bucketId(void) const { @@ -46,6 +48,7 @@ public: private: PolicyBucketId m_bucketId; + std::string m_message; }; } /* namespace Cynara */ diff --git a/src/common/exceptions/BucketRecordCorruptedException.h b/src/common/exceptions/BucketRecordCorruptedException.h index 92b29f7..a5ffb7d 100644 --- a/src/common/exceptions/BucketRecordCorruptedException.h +++ b/src/common/exceptions/BucketRecordCorruptedException.h @@ -39,25 +39,25 @@ public: BucketRecordCorruptedException withLineNumber(const size_t &lineNumber) const { BucketRecordCorruptedException copy(*this); copy.m_lineNumber = lineNumber; - copy.m_whatMsg.clear(); + copy.m_message.clear(); return copy; } BucketRecordCorruptedException withFilename(const std::string &filename) const { BucketRecordCorruptedException copy(*this); copy.m_filename = filename; - copy.m_whatMsg.clear(); + copy.m_message.clear(); return copy; } - virtual const std::string message(void) const { - if (m_whatMsg.empty()) { - m_whatMsg = "Bucket record corrupted at" + virtual const std::string &message(void) const { + if (m_message.empty()) { + m_message = "Bucket record corrupted at" + formatedFilename() + formatedLineNumber() + ": <" + slicedLine() + ">"; } - return m_whatMsg; + return m_message; } const std::string &filename(void) const { @@ -91,7 +91,7 @@ private: size_t m_lineNumber; std::string m_line; std::string m_filename; - mutable std::string m_whatMsg; + mutable std::string m_message; }; } /* namespace Cynara */ diff --git a/src/common/exceptions/BucketSerializationException.h b/src/common/exceptions/BucketSerializationException.h index 1e02823..23ff4cc 100644 --- a/src/common/exceptions/BucketSerializationException.h +++ b/src/common/exceptions/BucketSerializationException.h @@ -29,13 +29,12 @@ namespace Cynara { class BucketSerializationException : public DatabaseException { public: - BucketSerializationException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) {} + BucketSerializationException(const PolicyBucketId &bucket) : m_bucketId(bucket) { + m_message = "Could not serialize bucket " + bucketId(); + } virtual ~BucketSerializationException() {}; - const std::string message(void) const { - if (m_message.empty()) { - m_message = "Could not serialize bucket " + m_bucketId; - } + const std::string &message(void) const { return m_message; } @@ -44,7 +43,7 @@ public: } private: - mutable std::string m_message; + std::string m_message; PolicyBucketId m_bucketId; }; diff --git a/src/common/exceptions/CannotCreateFileException.h b/src/common/exceptions/CannotCreateFileException.h index 67c1d67..f7b8d92 100644 --- a/src/common/exceptions/CannotCreateFileException.h +++ b/src/common/exceptions/CannotCreateFileException.h @@ -31,13 +31,12 @@ namespace Cynara { class CannotCreateFileException : public DatabaseException { public: - CannotCreateFileException(const std::string &filename) : m_filename(filename) {}; + CannotCreateFileException(const std::string &file) : m_filename(file) { + m_message = "File " + filename() + " cannot be created"; + }; virtual ~CannotCreateFileException() {}; - const std::string message(void) const { - if (m_message.empty()) { - m_message = "File " + filename() + " cannot be created"; - } + const std::string &message(void) const { return m_message; } @@ -46,7 +45,7 @@ public: } private: - mutable std::string m_message; + std::string m_message; std::string m_filename; }; diff --git a/src/common/exceptions/ContextErrorException.h b/src/common/exceptions/ContextErrorException.h index 771065a..692b26e 100644 --- a/src/common/exceptions/ContextErrorException.h +++ b/src/common/exceptions/ContextErrorException.h @@ -29,12 +29,15 @@ namespace Cynara { class ContextErrorException : public Exception { public: - ContextErrorException() = default; + ContextErrorException() : m_message("ContextErrorException") {} virtual ~ContextErrorException() {}; - virtual const std::string message(void) const { - return "ContextErrorException"; + virtual const std::string &message(void) const { + return m_message; } + +private: + std::string m_message; }; } /* namespace Cynara */ diff --git a/src/common/exceptions/DefaultBucketDeletionException.h b/src/common/exceptions/DefaultBucketDeletionException.h index a4742be..7982f90 100644 --- a/src/common/exceptions/DefaultBucketDeletionException.h +++ b/src/common/exceptions/DefaultBucketDeletionException.h @@ -31,12 +31,16 @@ namespace Cynara { class DefaultBucketDeletionException : public Exception { public: - DefaultBucketDeletionException() = default; + DefaultBucketDeletionException() : m_message("DefaultBucketDeletionException") { + } virtual ~DefaultBucketDeletionException() {}; - virtual const std::string message(void) const { - return "DefaultBucketDeletionException"; + virtual const std::string &message(void) const { + return m_message; } + +private: + std::string m_message; }; } /* namespace Cynara */ diff --git a/src/common/exceptions/DefaultBucketSetNoneException.h b/src/common/exceptions/DefaultBucketSetNoneException.h index 243c4d8..a7b2d4b 100644 --- a/src/common/exceptions/DefaultBucketSetNoneException.h +++ b/src/common/exceptions/DefaultBucketSetNoneException.h @@ -31,12 +31,16 @@ namespace Cynara { class DefaultBucketSetNoneException : public Exception { public: - DefaultBucketSetNoneException() = default; + DefaultBucketSetNoneException() : m_message("DefaultBucketSetNoneException") { + } virtual ~DefaultBucketSetNoneException() {}; - virtual const std::string message(void) const { - return "DefaultBucketSetNoneException"; + virtual const std::string &message(void) const { + return m_message; } + +private: + std::string m_message; }; } /* namespace Cynara */ diff --git a/src/common/exceptions/DescriptorNotExistsException.h b/src/common/exceptions/DescriptorNotExistsException.h index 3535b91..ffb6bba 100644 --- a/src/common/exceptions/DescriptorNotExistsException.h +++ b/src/common/exceptions/DescriptorNotExistsException.h @@ -35,19 +35,17 @@ class DescriptorNotExistsException : public Exception { public: DescriptorNotExistsException() = delete; DescriptorNotExistsException(int desc) { - std::ostringstream stream; - stream << "trying access not existing descriptor [" << desc << "]"; - m_whatMsg = stream.str(); + m_message = "trying access not existing descriptor [" + std::to_string(desc) + "]"; } virtual ~DescriptorNotExistsException() {}; - virtual const std::string message(void) const { - return m_whatMsg; + virtual const std::string &message(void) const { + return m_message; } private: - std::string m_whatMsg; + std::string m_message; }; } // namespace Cynara diff --git a/src/common/exceptions/Exception.h b/src/common/exceptions/Exception.h index c5eb709..5dbaa6b 100644 --- a/src/common/exceptions/Exception.h +++ b/src/common/exceptions/Exception.h @@ -46,7 +46,7 @@ public: return m_whatMessage.c_str(); } - virtual const std::string message(void) const = 0; + virtual const std::string &message(void) const = 0; private: std::string m_backtrace; diff --git a/src/common/exceptions/FileNotFoundException.h b/src/common/exceptions/FileNotFoundException.h index 4c446e2..2c43ca5 100644 --- a/src/common/exceptions/FileNotFoundException.h +++ b/src/common/exceptions/FileNotFoundException.h @@ -31,13 +31,12 @@ namespace Cynara { class FileNotFoundException : public DatabaseException { public: - FileNotFoundException(const std::string &filename) : m_filename(filename) {}; - virtual ~FileNotFoundException() {}; + FileNotFoundException(const std::string &file) : m_filename(file) { + m_message = "File " + filename() + " not found or corrupted badly"; + } + virtual ~FileNotFoundException() {} - const std::string message(void) const { - if (m_message.empty()) { - m_message = "File " + filename() + " not found or corrupted badly"; - } + const std::string &message(void) const { return m_message; } @@ -46,7 +45,7 @@ public: } private: - mutable std::string m_message; + std::string m_message; std::string m_filename; }; diff --git a/src/common/exceptions/InitException.h b/src/common/exceptions/InitException.h index cdb09a2..22f8615 100644 --- a/src/common/exceptions/InitException.h +++ b/src/common/exceptions/InitException.h @@ -31,12 +31,15 @@ namespace Cynara { class InitException : public Exception { public: - InitException() = default; + InitException() : m_message("InitException") {} virtual ~InitException() {}; - virtual const std::string message(void) const { - return "InitException"; + virtual const std::string &message(void) const { + return m_message; } + +private: + std::string m_message; }; } /* namespace Cynara */ diff --git a/src/common/exceptions/InvalidBucketIdException.h b/src/common/exceptions/InvalidBucketIdException.h index f8772e4..6fe40b0 100644 --- a/src/common/exceptions/InvalidBucketIdException.h +++ b/src/common/exceptions/InvalidBucketIdException.h @@ -31,13 +31,12 @@ namespace Cynara { class InvalidBucketIdException : public Exception { public: - InvalidBucketIdException(const std::string &bucketId) : m_bucketId(bucketId) {}; + InvalidBucketIdException(const std::string &bucket) : m_bucketId(bucket) { + m_message = "Bucket ID " + bucketId() + " contains forbidden characters"; + }; virtual ~InvalidBucketIdException() {}; - const std::string message(void) const { - if (m_message.empty()) { - m_message = "Bucket ID " + bucketId() + " contains forbidden characters"; - } + const std::string &message(void) const { return m_message; } @@ -46,7 +45,7 @@ public: } private: - mutable std::string m_message; + std::string m_message; std::string m_bucketId; }; diff --git a/src/common/exceptions/InvalidProtocolException.h b/src/common/exceptions/InvalidProtocolException.h index 9b6a740..65c9f77 100644 --- a/src/common/exceptions/InvalidProtocolException.h +++ b/src/common/exceptions/InvalidProtocolException.h @@ -42,21 +42,21 @@ public: m_exceptionType(exceptionType) { switch(m_exceptionType) { case InvalidSignature: - m_whatMessage = "No valid signature found"; + m_message = "No valid signature found"; break; case WrongOpCode: - m_whatMessage = "Wrong request code"; + m_message = "Wrong request code"; break; case Other: - m_whatMessage = "Unknown problem"; + m_message = "Unknown problem"; break; } } virtual ~InvalidProtocolException() {}; - virtual const std::string message(void) const { - return m_whatMessage; + virtual const std::string &message(void) const { + return m_message; } ExceptionType exceptionType(void) const { @@ -64,7 +64,7 @@ public: } private: - std::string m_whatMessage; + std::string m_message; ExceptionType m_exceptionType; }; diff --git a/src/common/exceptions/NoMemoryException.h b/src/common/exceptions/NoMemoryException.h index 2eca090..8c811f5 100644 --- a/src/common/exceptions/NoMemoryException.h +++ b/src/common/exceptions/NoMemoryException.h @@ -34,17 +34,17 @@ class NoMemoryException : public Exception { public: NoMemoryException() = delete; NoMemoryException(const std::string &errorMsg) { - m_whatMessage = "NoMemoryException with message <" + errorMsg + ">"; + m_message = "NoMemoryException with message <" + errorMsg + ">"; } virtual ~NoMemoryException() {}; - virtual const std::string message(void) const { - return m_whatMessage; + virtual const std::string &message(void) const { + return m_message; } private: - std::string m_whatMessage; + std::string m_message; }; } // namespace Cynara diff --git a/src/common/exceptions/NotImplementedException.h b/src/common/exceptions/NotImplementedException.h index 8ca3762..c29e616 100644 --- a/src/common/exceptions/NotImplementedException.h +++ b/src/common/exceptions/NotImplementedException.h @@ -31,12 +31,15 @@ namespace Cynara { class NotImplementedException : public Exception { public: - NotImplementedException() = default; + NotImplementedException() :m_message("NotImplementedException") {} virtual ~NotImplementedException() {}; - virtual const std::string message(void) const { - return "NotImplementedException"; + virtual const std::string &message(void) const { + return m_message; } + +private: + std::string m_message; }; } /* namespace Cynara */ diff --git a/src/common/exceptions/NullPointerException.h b/src/common/exceptions/NullPointerException.h index e735555..9336332 100644 --- a/src/common/exceptions/NullPointerException.h +++ b/src/common/exceptions/NullPointerException.h @@ -34,19 +34,17 @@ class NullPointerException : public Exception { public: NullPointerException() = delete; NullPointerException(const char *varName) { - m_whatMsg = std::string("unexpected null value in variable <") - + std::string(varName) - + std::string(">"); + m_message = "unexpected null value in variable <" + std::string(varName) + ">"; } virtual ~NullPointerException() {}; - virtual const std::string message(void) const { - return m_whatMsg; + virtual const std::string &message(void) const { + return m_message; } private: - std::string m_whatMsg; + std::string m_message; }; } // namespace Cynara diff --git a/src/common/exceptions/OutOfDataException.h b/src/common/exceptions/OutOfDataException.h index 6420819..776d2f4 100644 --- a/src/common/exceptions/OutOfDataException.h +++ b/src/common/exceptions/OutOfDataException.h @@ -35,20 +35,18 @@ class OutOfDataException : public Exception { public: OutOfDataException() = delete; OutOfDataException(size_t dataRange, size_t accessTry) { - std::ostringstream stream; - stream << "trying access [" << accessTry << "]"; - stream << " which exceeds data range [" << dataRange << "]"; - m_whatMsg = stream.str(); + m_message = "trying access [" + std::to_string(accessTry) + "]" + + " which exceeds data range [" + std::to_string(dataRange) + "]"; } virtual ~OutOfDataException() {}; - virtual const std::string message(void) const { - return m_whatMsg; + virtual const std::string &message(void) const { + return m_message; } private: - std::string m_whatMsg; + std::string m_message; }; } // namespace Cynara diff --git a/src/common/exceptions/PluginErrorException.h b/src/common/exceptions/PluginErrorException.h index a29eab5..1a6c7eb 100644 --- a/src/common/exceptions/PluginErrorException.h +++ b/src/common/exceptions/PluginErrorException.h @@ -35,7 +35,7 @@ public: "privilege <" + key.privilege().toString() + ">"; } - const std::string message(void) const { + const std::string &message(void) const { return m_message; } diff --git a/src/common/exceptions/PluginNotFoundException.h b/src/common/exceptions/PluginNotFoundException.h index 4492a53..010f2f5 100644 --- a/src/common/exceptions/PluginNotFoundException.h +++ b/src/common/exceptions/PluginNotFoundException.h @@ -35,21 +35,19 @@ class PluginNotFoundException : public Exception { public: PluginNotFoundException() = delete; PluginNotFoundException(const PolicyResult &result) { - std::ostringstream stream; - stream << "No proper plugin found to interprete PolicyResult {type = [" - << result.policyType() << "], metadata = <" - << result.metadata().substr(0, 20) << ">}"; - m_whatMessage = stream.str(); + m_message = "No proper plugin found to interprete PolicyResult {type = [" + + std::to_string(result.policyType()) + "], metadata = <" + + result.metadata().substr(0, 20) + ">}"; } virtual ~PluginNotFoundException() {}; - virtual const std::string message(void) const { - return m_whatMessage; + virtual const std::string &message(void) const { + return m_message; } private: - std::string m_whatMessage; + std::string m_message; }; } // namespace Cynara diff --git a/src/common/exceptions/UnexpectedErrorException.h b/src/common/exceptions/UnexpectedErrorException.h index 3080c0f..4e0261d 100644 --- a/src/common/exceptions/UnexpectedErrorException.h +++ b/src/common/exceptions/UnexpectedErrorException.h @@ -34,26 +34,21 @@ class UnexpectedErrorException : public Exception { public: UnexpectedErrorException() = delete; UnexpectedErrorException(int errorCode, const char *errorMsg) { - std::ostringstream stream; - stream << "UnexpectedErrorException with errorCode =[" << errorCode << "] and message <"; - stream << errorMsg << ">"; - m_whatMessage = stream.str(); + m_message = "UnexpectedErrorException with errorCode =[" + std::to_string(errorCode) + + "] and message <" + errorMsg + ">"; } UnexpectedErrorException(const char *errorMsg) { - std::ostringstream stream; - stream << "UnexpectedErrorException with message <"; - stream << errorMsg << ">"; - m_whatMessage = stream.str(); + m_message = "UnexpectedErrorException with message <" + std::string(errorMsg) + ">"; } virtual ~UnexpectedErrorException() {}; - virtual const std::string message(void) const { - return m_whatMessage; + virtual const std::string &message(void) const { + return m_message; } private: - std::string m_whatMessage; + std::string m_message; }; } // namespace Cynara -- 2.7.4