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.

157 lines
5.7 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/internal/initialiser.hpp
  3. *
  4. * Purpose: Automatic initialisation of Pantheios Core API
  5. *
  6. * Created: 21st June 2005
  7. * Updated: 2nd August 2010
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2005-2010, Matthew Wilson and Synesis Software
  12. * Copyright (c) 1999-2005, Synesis Software and Matthew Wilson
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are
  17. * met:
  18. *
  19. * - Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * - Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
  25. * names of any contributors may be used to endorse or promote products
  26. * derived from this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  31. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * ////////////////////////////////////////////////////////////////////// */
  41. /** \file pantheios/internal/initialiser.hpp
  42. *
  43. * [C, C++] INTERNAL FILE: Provides automatic initialisation of Pantheios
  44. * Core API, via the pantheios_initialiser Schwarz counter class.
  45. *
  46. * \note Inclusion of this file, via pantheios/pantheios.hpp, declares an
  47. * instance of the pantheios::pantheios_initialiser class in each
  48. * compilation unit in which it is included.
  49. */
  50. #ifndef PANTHEIOS_INCL_PANTHEIOS_HPP_INITIALISER
  51. #define PANTHEIOS_INCL_PANTHEIOS_HPP_INITIALISER
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  56. # include <pantheios/pantheios.h>
  57. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Classes
  60. */
  61. /** Schwarz Counter initialiser for the Pantheios library
  62. *
  63. * In "normal" use, this file is included by pantheios/pantheios.hpp, so
  64. * that every C++ compilation unit that may potentially be issuing
  65. * Pantheios calls contains, at file scope (well, actually in an
  66. * anonymous namespace that is itself at file scoope) an instance of the
  67. * initialiser. Since the Pantheios core is a reference-counted API
  68. * (see Chapter 11 of
  69. * <a href = "http://imperfectcplusplus.com" target="_blank">Imperfect C++</a>),
  70. * this means that the first C++ compilation linked in initialises the
  71. * core, and the remainder keep it initialised, until they are all
  72. * "completed". In other words, just including pantheios/pantheios.hpp
  73. * causes Pantheios to be initialised.
  74. *
  75. * In non-"normal" cases, the inclusion is not made, and the initialisation
  76. * is not performed. These cases are either when:
  77. * - The link-unit being built is a dynamic library (as discriminated by
  78. * the presence of the processor symbols <code>_USRDLL</code> or
  79. * <code>_AFXDLL</code>, and the preprocessor symbol
  80. * <code>PANTHEIOS_FORCE_AUTO_INIT</code> is <b>not</b> defined, or
  81. * - The preprocessor symbol <code>PANTHEIOS_NO_AUTO_INIT</code> <b>is</b>
  82. * defined.
  83. *
  84. * In either of these cases, you <b>must</b> explicitly initialise the
  85. * Pantheios core (which initialises the front-end and back-end(s))
  86. * explicitly, using
  87. * \link pantheios::pantheios_init pantheios_init()\endlink
  88. * and
  89. * \link pantheios::pantheios_uninit pantheios_uninit()\endlink.
  90. */
  91. class pantheios_initialiser
  92. {
  93. /// \name Member Types
  94. /// @{
  95. public:
  96. typedef pantheios_initialiser class_type;
  97. /// @}
  98. /// \name Construction
  99. /// @{
  100. public:
  101. /// Initialises the Pantheios API
  102. ///
  103. /// Calls pantheios::pantheios_init(), calling exit(1) on failure
  104. pantheios_initialiser()
  105. {
  106. #ifndef PANTHEIOS_NO_NAMESPACE
  107. using namespace pantheios;
  108. using namespace pantheios::core;
  109. #endif /* !PANTHEIOS_NO_NAMESPACE */
  110. if(pantheios_init() < 0)
  111. {
  112. pantheios_exitProcess(1);
  113. }
  114. }
  115. /// Uninitialises the Pantheios API
  116. ///
  117. /// Calls pantheios::pantheios_uninit()
  118. ~pantheios_initialiser()
  119. {
  120. #ifndef PANTHEIOS_NO_NAMESPACE
  121. using namespace pantheios;
  122. #endif /* !PANTHEIOS_NO_NAMESPACE */
  123. pantheios_uninit();
  124. }
  125. /// @}
  126. /// \name Not to be implemented
  127. /// @{
  128. private:
  129. pantheios_initialiser(class_type const&);
  130. class_type& operator =(class_type const&);
  131. /// @}
  132. };
  133. namespace
  134. {
  135. /// The per-compilation unit instance of pantheios_initialiser, which
  136. /// ensures that the Pantheios library is initialised prior to use.
  137. static pantheios_initialiser s_pantheios_initialiser;
  138. } /* anonymous namespace */
  139. /* ////////////////////////////////////////////////////////////////////// */
  140. #endif /* !PANTHEIOS_INCL_PANTHEIOS_HPP_INITIALISER */
  141. /* ///////////////////////////// end of file //////////////////////////// */