Optimize message() method of exceptions 59/30459/5
authorZofia Abramowska <z.abramowska@samsung.com>
Mon, 17 Nov 2014 16:48:30 +0000 (17:48 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 24 Nov 2014 20:00:01 +0000 (12:00 -0800)
message() returned std::string through value, causing unnecessary
copy. Changed it to return const std::string reference.

Change-Id: I8d9631fb2468924d35ba4376b1821d0d01c7f70c

21 files changed:
src/common/exceptions/BucketDeserializationException.h
src/common/exceptions/BucketNotExistsException.h
src/common/exceptions/BucketRecordCorruptedException.h
src/common/exceptions/BucketSerializationException.h
src/common/exceptions/CannotCreateFileException.h
src/common/exceptions/ContextErrorException.h
src/common/exceptions/DefaultBucketDeletionException.h
src/common/exceptions/DefaultBucketSetNoneException.h
src/common/exceptions/DescriptorNotExistsException.h
src/common/exceptions/Exception.h
src/common/exceptions/FileNotFoundException.h
src/common/exceptions/InitException.h
src/common/exceptions/InvalidBucketIdException.h
src/common/exceptions/InvalidProtocolException.h
src/common/exceptions/NoMemoryException.h
src/common/exceptions/NotImplementedException.h
src/common/exceptions/NullPointerException.h
src/common/exceptions/OutOfDataException.h
src/common/exceptions/PluginErrorException.h
src/common/exceptions/PluginNotFoundException.h
src/common/exceptions/UnexpectedErrorException.h

index c5670c8..36ef8b0 100644 (file)
@@ -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;
 };
 
index 9ba9505..68d041b 100644 (file)
@@ -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 */
index 92b29f7..a5ffb7d 100644 (file)
@@ -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 */
index 1e02823..23ff4cc 100644 (file)
@@ -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;
 };
 
index 67c1d67..f7b8d92 100644 (file)
@@ -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;
 };
 
index 771065a..692b26e 100644 (file)
@@ -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 */
index a4742be..7982f90 100644 (file)
@@ -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 */
index 243c4d8..a7b2d4b 100644 (file)
@@ -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 */
index 3535b91..ffb6bba 100644 (file)
@@ -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
index c5eb709..5dbaa6b 100644 (file)
@@ -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;
index 4c446e2..2c43ca5 100644 (file)
@@ -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;
 };
 
index cdb09a2..22f8615 100644 (file)
@@ -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 */
index f8772e4..6fe40b0 100644 (file)
@@ -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;
 };
 
index 9b6a740..65c9f77 100644 (file)
@@ -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;
 };
 
index 2eca090..8c811f5 100644 (file)
@@ -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
index 8ca3762..c29e616 100644 (file)
@@ -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 */
index e735555..9336332 100644 (file)
@@ -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
index 6420819..776d2f4 100644 (file)
@@ -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
index a29eab5..1a6c7eb 100644 (file)
@@ -35,7 +35,7 @@ public:
                     "privilege <" + key.privilege().toString() + ">";
     }
 
-    const std::string message(void) const {
+    const std::string &message(void) const {
         return m_message;
     }
 
index 4492a53..010f2f5 100644 (file)
@@ -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
index 3080c0f..4e0261d 100644 (file)
@@ -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