objectregistry.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: objectregistry.h
  4. // Created: 3/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2015 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_SPI_OBJECT_REGISTRY_HEADER_
  23. #define LOG4CPLUS_SPI_OBJECT_REGISTRY_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/tstring.h>
  29. #include <log4cplus/thread/syncprims.h>
  30. #include <map>
  31. #include <memory>
  32. #include <vector>
  33. namespace log4cplus {
  34. namespace spi {
  35. /**
  36. * This is the base class used to implement the functionality required
  37. * by the ObjectRegistry template class.
  38. */
  39. class LOG4CPLUS_EXPORT ObjectRegistryBase {
  40. public:
  41. // public methods
  42. /**
  43. * Tests to see whether or not an object is bound in the
  44. * registry as <code>name</code>.
  45. */
  46. bool exists(const log4cplus::tstring& name) const;
  47. /**
  48. * Returns the names of all registered objects.
  49. */
  50. std::vector<log4cplus::tstring> getAllNames() const;
  51. //! This function is internal implementation detail.
  52. //! It is related to work-around needed for initialization when
  53. //! using C++11 threads and mutexes.
  54. void _enableLocking (bool);
  55. protected:
  56. // Ctor and Dtor
  57. ObjectRegistryBase();
  58. virtual ~ObjectRegistryBase();
  59. // protected methods
  60. /**
  61. * Used to enter an object into the registry. (The registry now
  62. * owns <code>object</code>.)
  63. */
  64. bool putVal(const log4cplus::tstring& name, void* object);
  65. /**
  66. * Used to retrieve an object from the registry. (The registry
  67. * owns the returned pointer.)
  68. */
  69. void* getVal(const log4cplus::tstring& name) const;
  70. /**
  71. * Deletes <code>object</code>.
  72. */
  73. virtual void deleteObject(void *object) const = 0;
  74. /**
  75. * Deletes all objects from this registry.
  76. */
  77. virtual void clear();
  78. // Types
  79. typedef std::map<log4cplus::tstring, void*> ObjectMap;
  80. // Data
  81. thread::Mutex mutex;
  82. ObjectMap data;
  83. private:
  84. ObjectRegistryBase (ObjectRegistryBase const &);
  85. ObjectRegistryBase & operator = (ObjectRegistryBase const &);
  86. bool volatile locking;
  87. };
  88. }
  89. }
  90. #endif // LOG4CPLUS_SPI_OBJECT_REGISTRY_HEADER_