instance.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2014 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <memory>
  16. #include <mongocxx/config/prelude.hpp>
  17. namespace mongocxx {
  18. MONGOCXX_INLINE_NAMESPACE_BEGIN
  19. class logger;
  20. ///
  21. /// Class representing an instance of the MongoDB driver.
  22. ///
  23. /// The constructor and destructor initialize and shut down the driver, respectively. Therefore, an
  24. /// instance must be created before using the driver and must remain alive until all other mongocxx
  25. /// objects are destroyed. After the instance destructor runs, the driver may not be used.
  26. ///
  27. /// Exactly one instance must be created in a given program. Not constructing an instance or
  28. /// constructing more than one instance in a program are errors, even if the multiple instances have
  29. /// non-overlapping lifetimes.
  30. ///
  31. /// The following is a correct example of using an instance in a program, as the instance is kept
  32. /// alive for as long as the driver is in use:
  33. ///
  34. /// \code
  35. ///
  36. /// #include <mongocxx/client.hpp>
  37. /// #include <mongocxx/instance.hpp>
  38. /// #include <mongocxx/uri.hpp>
  39. ///
  40. /// int main() {
  41. /// mongocxx::instance inst{};
  42. /// mongocxx::client conn{mongocxx::uri{}};
  43. /// ...
  44. /// }
  45. ///
  46. /// \endcode
  47. ///
  48. /// An example of using instance incorrectly might look as follows:
  49. ///
  50. /// \code
  51. ///
  52. /// #include <mongocxx/client.hpp>
  53. /// #include <mongocxx/instance.hpp>
  54. /// #include <mongocxx/uri.hpp>
  55. ///
  56. /// client get_client() {
  57. /// mongocxx::instance inst{};
  58. /// mongocxx::client conn{mongocxx::uri{}};
  59. ///
  60. /// return client;
  61. /// } // ERROR! The instance is no longer alive after this function returns.
  62. ///
  63. /// int main() {
  64. /// mongocxx::client conn = get_client();
  65. /// ...
  66. /// }
  67. ///
  68. /// \endcode
  69. ///
  70. /// For examples of more advanced usage of instance, see
  71. /// `examples/mongocxx/instance_management.cpp`.
  72. ///
  73. class MONGOCXX_API instance {
  74. public:
  75. ///
  76. /// Creates an instance of the driver.
  77. ///
  78. instance();
  79. ///
  80. /// Creates an instance of the driver with a user provided log handler.
  81. /// @param logger The logger that the driver will direct log messages to.
  82. ///
  83. /// @throws mongocxx::logic_error if an instance already exists.
  84. ///
  85. instance(std::unique_ptr<logger> logger);
  86. ///
  87. /// Move constructs an instance of the driver.
  88. ///
  89. instance(instance&&) noexcept;
  90. ///
  91. /// Move assigns an instance of the driver.
  92. ///
  93. instance& operator=(instance&&) noexcept;
  94. ///
  95. /// Destroys an instance of the driver.
  96. ///
  97. ~instance();
  98. ///
  99. /// Returns the current unique instance of the driver. If an instance was explicitly created,
  100. /// that will be returned. If no instance has yet been created, a default instance will be
  101. /// constructed and returned. If a default instance is constructed, its destruction will be
  102. /// sequenced according to the rules for the destruction of static local variables at program
  103. /// exit (see http://en.cppreference.com/w/cpp/utility/program/exit).
  104. ///
  105. /// Note that, if you need to configure the instance in any way (e.g. with a logger), you cannot
  106. /// use this method to cause the instance to be constructed. You must explicitly create an
  107. /// properly configured instance object. You can, however, use this method to obtain that
  108. /// configured instance object.
  109. ///
  110. /// @note This method is intended primarily for test authors, where managing the lifetime of the
  111. /// instance w.r.t. the test framework can be problematic.
  112. ///
  113. static instance& current();
  114. private:
  115. class MONGOCXX_PRIVATE impl;
  116. std::unique_ptr<impl> _impl;
  117. };
  118. MONGOCXX_INLINE_NAMESPACE_END
  119. } // namespace mongocxx
  120. #include <mongocxx/config/postlude.hpp>