Browse Source

[trac558] Return reference to dictionary string

Returns a reference to a string in the message dictionary instead of
a copy of it.
Stephen Morris 14 years ago
parent
commit
0c25e92e3d
2 changed files with 8 additions and 5 deletions
  1. 6 3
      src/lib/log/message_dictionary.cc
  2. 2 2
      src/lib/log/message_dictionary.h

+ 6 - 3
src/lib/log/message_dictionary.cc

@@ -84,13 +84,16 @@ MessageDictionary::load(const char* messages[]) {
     return (duplicates);
 }
 
-// Return message text or blank string
+// Return message text or blank string.  A reference is returned to a string
+// in the dictionary - this is fine, as the string is immediately used for
+// output.
 
-string
+const string&
 MessageDictionary::getText(const string& ident) const {
+    static const string empty("");
     Dictionary::const_iterator i = dictionary_.find(ident);
     if (i == dictionary_.end()) {
-        return (string(""));
+        return (empty);
     }
     else {
         return (i->second);

+ 2 - 2
src/lib/log/message_dictionary.h

@@ -136,7 +136,7 @@ public:
     /// \return Text associated with message or empty string if the ID is not
     /// recognised.  (Note: this precludes an ID being associated with an empty
     /// string.)
-    virtual std::string getText(const MessageID& ident) const {
+    virtual const std::string& getText(const MessageID& ident) const {
         return(getText(boost::lexical_cast<std::string>(ident)));
     }
 
@@ -150,7 +150,7 @@ public:
     /// \return Text associated with message or empty string if the ID is not
     /// recognised.  (Note: this precludes an ID being associated with an empty
     /// string.)
-    virtual std::string getText(const std::string& ident) const;
+    virtual const std::string& getText(const std::string& ident) const;
 
 
     /// \brief Number of Items in Dictionary