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.

160 lines
5.5 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: property.h
  4. // Created: 2/2002
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2002-2010 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. class LOG4CPLUS_EXPORT Properties {
  35. public:
  36. enum PFlags
  37. {
  38. // These encoding related options occupy 2 bits of the flags
  39. // and are mutually exclusive. These flags are synchronized
  40. // with PCFlags in PropertyConfigurator.
  41. fEncodingShift = 3
  42. , fEncodingMask = 0x3
  43. , fUnspecEncoding = (0 << fEncodingShift)
  44. #if defined (LOG4CPLUS_HAVE_CODECVT_UTF8_FACET) && defined (UNICODE)
  45. , fUTF8 = (1 << fEncodingShift)
  46. #endif
  47. #if (defined (LOG4CPLUS_HAVE_CODECVT_UTF16_FACET) || defined (_WIN32)) \
  48. && defined (UNICODE)
  49. , fUTF16 = (2 << fEncodingShift)
  50. #endif
  51. #if defined (LOG4CPLUS_HAVE_CODECVT_UTF32_FACET) && defined (UNICODE)
  52. , fUTF32 = (3 << fEncodingShift)
  53. #endif
  54. };
  55. Properties();
  56. explicit Properties(log4cplus::tistream& input);
  57. explicit Properties(const log4cplus::tstring& inputFile, unsigned flags = 0);
  58. virtual ~Properties();
  59. // constants
  60. static const tchar PROPERTIES_COMMENT_CHAR;
  61. // methods
  62. /**
  63. * Tests to see if <code>key</code> can be found in this map.
  64. */
  65. bool exists(const log4cplus::tstring& key) const;
  66. bool exists(tchar const * key) const;
  67. /**
  68. * Returns the number of entries in this map.
  69. */
  70. std::size_t size() const
  71. {
  72. return data.size();
  73. }
  74. /**
  75. * Searches for the property with the specified key in this property
  76. * list. If the key is not found in this property list, the default
  77. * property list, and its defaults, recursively, are then checked.
  78. * The method returns <code>null</code> if the property is not found.
  79. */
  80. log4cplus::tstring const & getProperty(const log4cplus::tstring& key) const;
  81. log4cplus::tstring const & getProperty(tchar const * key) const;
  82. /**
  83. * Searches for the property with the specified key in this property
  84. * list. If the key is not found in this property list, the default
  85. * property list, and its defaults, recursively, are then checked.
  86. * The method returns the default value argument if the property is
  87. * not found.
  88. */
  89. log4cplus::tstring getProperty(const log4cplus::tstring& key,
  90. const log4cplus::tstring& defaultVal) const;
  91. /**
  92. * Returns all the keys in this property list.
  93. */
  94. std::vector<log4cplus::tstring> propertyNames() const;
  95. /**
  96. * Inserts <code>value</code> into this map indexed by <code>key</code>.
  97. */
  98. void setProperty(const log4cplus::tstring& key, const log4cplus::tstring& value);
  99. /**
  100. * Removed the property index by <code>key</code> from this map.
  101. */
  102. bool removeProperty(const log4cplus::tstring& key);
  103. /**
  104. * Returns a subset of the "properties" whose keys start with
  105. * "prefix". The returned "properties" have "prefix" trimmed from
  106. * their keys.
  107. */
  108. Properties getPropertySubset(const log4cplus::tstring& prefix) const;
  109. bool getInt (int & val, log4cplus::tstring const & key) const;
  110. bool getUInt (unsigned & val, log4cplus::tstring const & key) const;
  111. bool getLong (long & val, log4cplus::tstring const & key) const;
  112. bool getULong (unsigned long & val, log4cplus::tstring const & key) const;
  113. bool getBool (bool & val, log4cplus::tstring const & key) const;
  114. protected:
  115. // Types
  116. typedef std::map<log4cplus::tstring, log4cplus::tstring> StringMap;
  117. // Methods
  118. void init(log4cplus::tistream& input);
  119. // Data
  120. StringMap data;
  121. private:
  122. template <typename StringType>
  123. log4cplus::tstring const & get_property_worker (
  124. StringType const & key) const;
  125. template <typename ValType>
  126. bool get_type_val_worker (ValType & val,
  127. log4cplus::tstring const & key) const;
  128. };
  129. } // end namespace helpers
  130. }
  131. #endif // LOG4CPLUS_HELPERS_PROPERTY_HEADER_