log4cplus  2.0.0
property.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Module:  Log4CPLUS
00003 // File:    property.h
00004 // Created: 2/2002
00005 // Author:  Tad E. Smith
00006 //
00007 //
00008 // Copyright 2002-2015 Tad E. Smith
00009 //
00010 // Licensed under the Apache License, Version 2.0 (the "License");
00011 // you may not use this file except in compliance with the License.
00012 // You may obtain a copy of the License at
00013 //
00014 //     http://www.apache.org/licenses/LICENSE-2.0
00015 //
00016 // Unless required by applicable law or agreed to in writing, software
00017 // distributed under the License is distributed on an "AS IS" BASIS,
00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019 // See the License for the specific language governing permissions and
00020 // limitations under the License.
00021 
00024 #ifndef LOG4CPLUS_HELPERS_PROPERTY_HEADER_
00025 #define LOG4CPLUS_HELPERS_PROPERTY_HEADER_
00026 
00027 #include <log4cplus/config.hxx>
00028 
00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
00030 #pragma once
00031 #endif
00032 
00033 #include <log4cplus/streams.h>
00034 #include <log4cplus/tstring.h>
00035 #include <map>
00036 #include <vector>
00037 
00038 
00039 namespace log4cplus {
00040     namespace helpers {
00041 
00043         class LOG4CPLUS_EXPORT Properties {
00044         public:
00045             enum PFlags
00046             {
00047                 // These encoding related options occupy 2 bits of the flags
00048                 // and are mutually exclusive. These flags are synchronized
00049                 // with PCFlags in PropertyConfigurator.
00050 
00051                 fEncodingShift      = 3
00052                 , fEncodingMask     = 0x3
00053                 , fUnspecEncoding   = (0 << fEncodingShift)
00054 #if defined (LOG4CPLUS_HAVE_CODECVT_UTF8_FACET) && defined (UNICODE)
00055                 , fUTF8             = (1 << fEncodingShift)
00056 #endif
00057 #if (defined (LOG4CPLUS_HAVE_CODECVT_UTF16_FACET) || defined (_WIN32)) \
00058     && defined (UNICODE)
00059                 , fUTF16            = (2 << fEncodingShift)
00060 #endif
00061 #if defined (LOG4CPLUS_HAVE_CODECVT_UTF32_FACET) && defined (UNICODE)
00062                 , fUTF32            = (3 << fEncodingShift)
00063 #endif
00064             };
00065 
00066             Properties();
00067             explicit Properties(log4cplus::tistream& input);
00068             explicit Properties(const log4cplus::tstring& inputFile, unsigned flags = 0);
00069             virtual ~Properties();
00070 
00071           // constants
00072             static const tchar PROPERTIES_COMMENT_CHAR;
00073 
00074           // methods
00078             bool exists(const log4cplus::tstring& key) const;
00079             bool exists(tchar const * key) const;
00080 
00084             std::size_t size() const
00085             {
00086                 return data.size();
00087             }
00088 
00095             log4cplus::tstring const & getProperty(const log4cplus::tstring& key) const;
00096             log4cplus::tstring const & getProperty(tchar const * key) const;
00097 
00105             log4cplus::tstring getProperty(const log4cplus::tstring& key,
00106                                            const log4cplus::tstring& defaultVal) const;
00107 
00111             std::vector<log4cplus::tstring> propertyNames() const;
00112 
00116             void setProperty(const log4cplus::tstring& key, const log4cplus::tstring& value);
00117 
00121             bool removeProperty(const log4cplus::tstring& key);
00122 
00128             Properties getPropertySubset(const log4cplus::tstring& prefix) const;
00129 
00130             bool getInt (int & val, log4cplus::tstring const & key) const;
00131             bool getUInt (unsigned & val, log4cplus::tstring const & key) const;
00132             bool getLong (long & val, log4cplus::tstring const & key) const;
00133             bool getULong (unsigned long & val, log4cplus::tstring const & key) const;
00134             bool getBool (bool & val, log4cplus::tstring const & key) const;
00135             bool getString (log4cplus::tstring & val, log4cplus::tstring const & key) const;
00136 
00137         protected:
00138           // Types
00139             typedef std::map<log4cplus::tstring, log4cplus::tstring> StringMap;
00140 
00141           // Methods
00142             void init(log4cplus::tistream& input);
00143 
00144           // Data
00145             StringMap data;
00146             unsigned flags;
00147 
00148         private:
00149             template <typename StringType>
00150             log4cplus::tstring const & get_property_worker (
00151                 StringType const & key) const;
00152 
00153             template <typename ValType>
00154             bool get_type_val_worker (ValType & val,
00155                 log4cplus::tstring const & key) const;
00156         };
00157     } // end namespace helpers
00158 
00159 }
00160 
00161 
00162 #endif // LOG4CPLUS_HELPERS_PROPERTY_HEADER_