LCOV - code coverage report
Current view: top level - log - message_dictionary.h (source / functions) Hit Total Coverage
Test: report.info Lines: 9 9 100.0 %
Date: 2012-05-15 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 3 6 50.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
       2                 :            : //
       3                 :            : // Permission to use, copy, modify, and/or distribute this software for any
       4                 :            : // purpose with or without fee is hereby granted, provided that the above
       5                 :            : // copyright notice and this permission notice appear in all copies.
       6                 :            : //
       7                 :            : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
       8                 :            : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
       9                 :            : // AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
      10                 :            : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
      11                 :            : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
      12                 :            : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
      13                 :            : // PERFORMANCE OF THIS SOFTWARE.
      14                 :            : 
      15                 :            : #ifndef __MESSAGE_DICTIONARY_H
      16                 :            : #define __MESSAGE_DICTIONARY_H
      17                 :            : 
      18                 :            : #include <cstddef>
      19                 :            : #include <string>
      20                 :            : #include <map>
      21                 :            : #include <vector>
      22                 :            : 
      23                 :            : #include <boost/lexical_cast.hpp>
      24                 :            : 
      25                 :            : #include <log/message_types.h>
      26                 :            : 
      27                 :            : namespace isc {
      28                 :            : namespace log {
      29                 :            : 
      30                 :            : /// \brief Message Dictionary
      31                 :            : ///
      32                 :            : /// The message dictionary is a wrapper around a std::map object, and allows
      33                 :            : /// message text to be retrieved given the string identification.
      34                 :            : ///
      35                 :            : /// Adding text occurs in two modes:
      36                 :            : ///
      37                 :            : /// Through the "Add" method, ID/text mappings are added to the dictionary
      38                 :            : /// unless the ID already exists.  This is designed for use during program
      39                 :            : /// initialization, where a local message may supplant a compiled-in message.
      40                 :            : ///
      41                 :            : /// Through the "Replace" method, ID/text mappings are added to the dictionary
      42                 :            : /// only if the ID already exists.  This is for use when a message file is
      43                 :            : /// supplied to replace messages provided with the program.
      44                 :            : ///
      45                 :            : /// Although the class can be used stand-alone, it does supply a static method
      46                 :            : /// to return a particular instance - the "global" dictionary.
      47                 :            : 
      48                 :        156 : class MessageDictionary {
      49                 :            : public:
      50                 :            : 
      51                 :            :     typedef std::map<std::string, std::string> Dictionary;
      52                 :            :     typedef Dictionary::const_iterator  const_iterator;
      53                 :            : 
      54                 :            :     // Default constructor and assignment operator are OK for this class
      55                 :            : 
      56                 :            :     /// \brief Virtual Destructor
      57                 :            :     virtual ~MessageDictionary();
      58                 :            : 
      59                 :            :     /// \brief Add Message
      60                 :            :     ///
      61                 :            :     /// Adds a message to the dictionary.  If the ID already exists, the ID is
      62                 :            :     /// added to the overflow vector.
      63                 :            :     ///
      64                 :            :     /// \param ident Identification of the message to add
      65                 :            :     /// \param text Message text
      66                 :            :     ///
      67                 :            :     /// \return true if the message was added to the dictionary, false if the
      68                 :            :     /// message existed and it was not added.
      69                 :      18966 :     virtual bool add(const MessageID& ident, const std::string& text) {
      70         [ +  - ]:      18966 :         return (add(boost::lexical_cast<std::string>(ident), text));
      71                 :            :     }
      72                 :            : 
      73                 :            :     /// \brief Add Message
      74                 :            :     ///
      75                 :            :     /// Alternate signature.
      76                 :            :     ///
      77                 :            :     /// \param ident Identification of the message to add
      78                 :            :     /// \param text Message text
      79                 :            :     ///
      80                 :            :     /// \return true if the message was added to the dictionary, false if the
      81                 :            :     /// message existed and it was not added.
      82                 :            :     virtual bool add (const std::string& ident, const std::string& text);
      83                 :            : 
      84                 :            : 
      85                 :            :     /// \brief Replace Message
      86                 :            :     ///
      87                 :            :     /// Replaces a message in the dictionary.  If the ID does not exist, it is
      88                 :            :     /// added to the overflow vector.
      89                 :            :     ///
      90                 :            :     /// \param ident Identification of the message to replace
      91                 :            :     /// \param text Message text
      92                 :            :     ///
      93                 :            :     /// \return true if the message was added to the dictionary, false if the
      94                 :            :     /// message did not exist and it was not added.
      95                 :          3 :     virtual bool replace(const MessageID& ident, const std::string& text) {
      96         [ +  - ]:          3 :         return (replace(boost::lexical_cast<std::string>(ident), text));
      97                 :            :     }
      98                 :            : 
      99                 :            : 
     100                 :            :     /// \brief Replace Message
     101                 :            :     ///
     102                 :            :     /// Alternate signature.
     103                 :            :     ///
     104                 :            :     /// \param ident Identification of the message to replace
     105                 :            :     /// \param text Message text
     106                 :            :     ///
     107                 :            :     /// \return true if the message was added to the dictionary, false if the
     108                 :            :     /// message did not exist and it was not added.
     109                 :            :     virtual bool replace(const std::string& ident, const std::string& text);
     110                 :            : 
     111                 :            : 
     112                 :            :     /// \brief Load Dictionary
     113                 :            :     ///
     114                 :            :     /// Designed to be used during the initialization of programs, this
     115                 :            :     /// accepts a set of (ID, text) pairs as a one-dimensional array of
     116                 :            :     /// const char* and adds them to the dictionary.  The messages are added
     117                 :            :     /// using "Add".
     118                 :            :     ///
     119                 :            :     /// \param elements null-terminated array of const char* alternating ID and
     120                 :            :     /// message text.  This should be an odd number of elements long, the last
     121                 :            :     /// elemnent being NULL.  If it is an even number of elements long, the
     122                 :            :     /// last ID is ignored.
     123                 :            :     ///
     124                 :            :     /// \return Vector of message IDs that were not loaded because an ID of the
     125                 :            :     /// same name already existing in the dictionary.  This vector may be
     126                 :            :     /// empty.
     127                 :            :     virtual std::vector<std::string> load(const char* elements[]);
     128                 :            : 
     129                 :            : 
     130                 :            :     /// \brief Get Message Text
     131                 :            :     ///
     132                 :            :     /// Given an ID, retrieve associated message text.
     133                 :            :     ///
     134                 :            :     /// \param ident Message identification
     135                 :            :     ///
     136                 :            :     /// \return Text associated with message or empty string if the ID is not
     137                 :            :     /// recognised.  (Note: this precludes an ID being associated with an empty
     138                 :            :     /// string.)
     139                 :      40827 :     virtual const std::string& getText(const MessageID& ident) const {
     140         [ +  - ]:      40827 :         return(getText(boost::lexical_cast<std::string>(ident)));
     141                 :            :     }
     142                 :            : 
     143                 :            : 
     144                 :            :     /// \brief Get Message Text
     145                 :            :     ///
     146                 :            :     /// Alternate signature.
     147                 :            :     ///
     148                 :            :     /// \param ident Message identification
     149                 :            :     ///
     150                 :            :     /// \return Text associated with message or empty string if the ID is not
     151                 :            :     /// recognised.  (Note: this precludes an ID being associated with an empty
     152                 :            :     /// string.)
     153                 :            :     virtual const std::string& getText(const std::string& ident) const;
     154                 :            : 
     155                 :            : 
     156                 :            :     /// \brief Number of Items in Dictionary
     157                 :            :     ///
     158                 :            :     /// \return Number of items in the dictionary
     159                 :          6 :     virtual size_t size() const {
     160                 :          6 :         return (dictionary_.size());
     161                 :            :     }
     162                 :            : 
     163                 :            : 
     164                 :            :     /// \brief Return begin() iterator of internal map
     165                 :            :     const_iterator begin() const {
     166                 :            :         return (dictionary_.begin());
     167                 :            :     }
     168                 :            : 
     169                 :            : 
     170                 :            :     /// \brief Return end() iterator of internal map
     171                 :            :     const_iterator end() const {
     172                 :            :         return (dictionary_.end());
     173                 :            :     }
     174                 :            : 
     175                 :            : 
     176                 :            :     /// \brief Return Global Dictionary
     177                 :            :     ///
     178                 :            :     /// Returns a pointer to the singleton global dictionary.
     179                 :            :     ///
     180                 :            :     /// \return Pointer to global dictionary.
     181                 :            :     static MessageDictionary& globalDictionary();
     182                 :            : 
     183                 :            : private:
     184                 :            :     Dictionary       dictionary_;   ///< Holds the ID to text lookups
     185                 :            : };
     186                 :            : 
     187                 :            : } // namespace log
     188                 :            : } // namespace isc
     189                 :            : 
     190                 :            : #endif // __MESSAGE_DICTIONARY_H

Generated by: LCOV version 1.9