property.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: property.h
  4. // Created: 2/2002
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2002-2017 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. , fThrow = (1 << 5)
  56. };
  57. Properties();
  58. explicit Properties(log4cplus::tistream& input);
  59. explicit Properties(const log4cplus::tstring& inputFile, unsigned flags = 0);
  60. virtual ~Properties();
  61. // constants
  62. static const tchar PROPERTIES_COMMENT_CHAR;
  63. // methods
  64. /**
  65. * Tests to see if <code>key</code> can be found in this map.
  66. */
  67. bool exists(const log4cplus::tstring& key) const;
  68. bool exists(tchar const * key) const;
  69. /**
  70. * Returns the number of entries in this map.
  71. */
  72. std::size_t size() const
  73. {
  74. return data.size();
  75. }
  76. /**
  77. * Searches for the property with the specified key in this property
  78. * list. If the key is not found in this property list, the default
  79. * property list, and its defaults, recursively, are then checked.
  80. * The method returns <code>null</code> if the property is not found.
  81. */
  82. log4cplus::tstring const & getProperty(const log4cplus::tstring& key) const;
  83. log4cplus::tstring const & getProperty(tchar const * key) const;
  84. /**
  85. * Searches for the property with the specified key in this property
  86. * list. If the key is not found in this property list, the default
  87. * property list, and its defaults, recursively, are then checked.
  88. * The method returns the default value argument if the property is
  89. * not found.
  90. */
  91. log4cplus::tstring getProperty(const log4cplus::tstring& key,
  92. const log4cplus::tstring& defaultVal) const;
  93. /**
  94. * Returns all the keys in this property list.
  95. */
  96. std::vector<log4cplus::tstring> propertyNames() const;
  97. /**
  98. * Inserts <code>value</code> into this map indexed by <code>key</code>.
  99. */
  100. void setProperty(const log4cplus::tstring& key, const log4cplus::tstring& value);
  101. /**
  102. * Removed the property index by <code>key</code> from this map.
  103. */
  104. bool removeProperty(const log4cplus::tstring& key);
  105. /**
  106. * Returns a subset of the "properties" whose keys start with
  107. * "prefix". The returned "properties" have "prefix" trimmed from
  108. * their keys.
  109. */
  110. Properties getPropertySubset(const log4cplus::tstring& prefix) const;
  111. bool getInt (int & val, log4cplus::tstring const & key) const;
  112. bool getUInt (unsigned & val, log4cplus::tstring const & key) const;
  113. bool getLong (long & val, log4cplus::tstring const & key) const;
  114. bool getULong (unsigned long & val, log4cplus::tstring const & key) const;
  115. bool getBool (bool & val, log4cplus::tstring const & key) const;
  116. bool getString (log4cplus::tstring & val, log4cplus::tstring const & key) const;
  117. protected:
  118. // Types
  119. typedef std::map<log4cplus::tstring, log4cplus::tstring> StringMap;
  120. // Methods
  121. void init(log4cplus::tistream& input);
  122. // Data
  123. StringMap data;
  124. unsigned flags;
  125. private:
  126. template <typename StringType>
  127. log4cplus::tstring const & get_property_worker (
  128. StringType const & key) const;
  129. template <typename ValType>
  130. bool get_type_val_worker (ValType & val,
  131. log4cplus::tstring const & key) const;
  132. };
  133. class LogLog;
  134. bool
  135. substVars (tstring & dest, const tstring & val,
  136. Properties const & props, LogLog& loglog,
  137. unsigned flags);
  138. } // end namespace helpers
  139. }
  140. #endif // LOG4CPLUS_HELPERS_PROPERTY_HEADER_