You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.6 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: property.h
  4. // Created: 2/2002
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2002-2013 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_HELPERS_PROPERTY_HEADER_
  23. #define LOG4CPLUS_HELPERS_PROPERTY_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/streams.h>
  29. #include <log4cplus/tstring.h>
  30. #include <map>
  31. #include <vector>
  32. namespace log4cplus {
  33. namespace helpers {
  34. //! \sa log4cplus::PropertyConfigurator
  35. class LOG4CPLUS_EXPORT Properties {
  36. public:
  37. enum PFlags
  38. {
  39. // These encoding related options occupy 2 bits of the flags
  40. // and are mutually exclusive. These flags are synchronized
  41. // with PCFlags in PropertyConfigurator.
  42. fEncodingShift = 3
  43. , fEncodingMask = 0x3
  44. , fUnspecEncoding = (0 << fEncodingShift)
  45. #if defined (LOG4CPLUS_HAVE_CODECVT_UTF8_FACET) && defined (UNICODE)
  46. , fUTF8 = (1 << fEncodingShift)
  47. #endif
  48. #if (defined (LOG4CPLUS_HAVE_CODECVT_UTF16_FACET) || defined (_WIN32)) \
  49. && defined (UNICODE)
  50. , fUTF16 = (2 << fEncodingShift)
  51. #endif
  52. #if defined (LOG4CPLUS_HAVE_CODECVT_UTF32_FACET) && defined (UNICODE)
  53. , fUTF32 = (3 << fEncodingShift)
  54. #endif
  55. };
  56. Properties();
  57. explicit Properties(log4cplus::tistream& input);
  58. explicit Properties(const log4cplus::tstring& inputFile, unsigned flags = 0);
  59. virtual ~Properties();
  60. // constants
  61. static const tchar PROPERTIES_COMMENT_CHAR;
  62. // methods
  63. /**
  64. * Tests to see if <code>key</code> can be found in this map.
  65. */
  66. bool exists(const log4cplus::tstring& key) const;
  67. bool exists(tchar const * key) const;
  68. /**
  69. * Returns the number of entries in this map.
  70. */
  71. std::size_t size() const
  72. {
  73. return data.size();
  74. }
  75. /**
  76. * Searches for the property with the specified key in this property
  77. * list. If the key is not found in this property list, the default
  78. * property list, and its defaults, recursively, are then checked.
  79. * The method returns <code>null</code> if the property is not found.
  80. */
  81. log4cplus::tstring const & getProperty(const log4cplus::tstring& key) const;
  82. log4cplus::tstring const & getProperty(tchar const * key) const;
  83. /**
  84. * Searches for the property with the specified key in this property
  85. * list. If the key is not found in this property list, the default
  86. * property list, and its defaults, recursively, are then checked.
  87. * The method returns the default value argument if the property is
  88. * not found.
  89. */
  90. log4cplus::tstring getProperty(const log4cplus::tstring& key,
  91. const log4cplus::tstring& defaultVal) const;
  92. /**
  93. * Returns all the keys in this property list.
  94. */
  95. std::vector<log4cplus::tstring> propertyNames() const;
  96. /**
  97. * Inserts <code>value</code> into this map indexed by <code>key</code>.
  98. */
  99. void setProperty(const log4cplus::tstring& key, const log4cplus::tstring& value);
  100. /**
  101. * Removed the property index by <code>key</code> from this map.
  102. */
  103. bool removeProperty(const log4cplus::tstring& key);
  104. /**
  105. * Returns a subset of the "properties" whose keys start with
  106. * "prefix". The returned "properties" have "prefix" trimmed from
  107. * their keys.
  108. */
  109. Properties getPropertySubset(const log4cplus::tstring& prefix) const;
  110. bool getInt (int & val, log4cplus::tstring const & key) const;
  111. bool getUInt (unsigned & val, log4cplus::tstring const & key) const;
  112. bool getLong (long & val, log4cplus::tstring const & key) const;
  113. bool getULong (unsigned long & val, log4cplus::tstring const & key) const;
  114. bool getBool (bool & val, log4cplus::tstring const & key) const;
  115. protected:
  116. // Types
  117. typedef std::map<log4cplus::tstring, log4cplus::tstring> StringMap;
  118. // Methods
  119. void init(log4cplus::tistream& input);
  120. // Data
  121. StringMap data;
  122. unsigned flags;
  123. private:
  124. template <typename StringType>
  125. log4cplus::tstring const & get_property_worker (
  126. StringType const & key) const;
  127. template <typename ValType>
  128. bool get_type_val_worker (ValType & val,
  129. log4cplus::tstring const & key) const;
  130. };
  131. } // end namespace helpers
  132. }
  133. #endif // LOG4CPLUS_HELPERS_PROPERTY_HEADER_