1 /**
2  * @copyright
3  * ====================================================================
4  *    Licensed to the Apache Software Foundation (ASF) under one
5  *    or more contributor license agreements.  See the NOTICE file
6  *    distributed with this work for additional information
7  *    regarding copyright ownership.  The ASF licenses this file
8  *    to you under the Apache License, Version 2.0 (the
9  *    "License"); you may not use this file except in compliance
10  *    with the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *    Unless required by applicable law or agreed to in writing,
15  *    software distributed under the License is distributed on an
16  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  *    KIND, either express or implied.  See the License for the
18  *    specific language governing permissions and limitations
19  *    under the License.
20  * ====================================================================
21  * @endcopyright
22  */
23 
24 #ifndef SVNXX_PRIVATE_INIT_HPP
25 #define SVNXX_PRIVATE_INIT_HPP
26 
27 #include <memory>
28 #include <mutex>
29 #include <stdexcept>
30 
31 #include <apr_pools.h>
32 
33 #include "svnxx/init.hpp"
34 #include "svnxx/detail/noncopyable.hpp"
35 
36 namespace apache {
37 namespace subversion {
38 namespace svnxx {
39 namespace detail {
40 
41 class global_state : noncopyable
42 {
43 public:
44   using ptr = std::shared_ptr<global_state>;
45   using weak_ptr = std::weak_ptr<global_state>;
46 
47   ~global_state();
48 
49   static ptr create();
get()50   static ptr get()
51     {
52       auto state = self.lock();
53       if (!state)
54         {
55           throw std::logic_error(
56               "The SVN++ library is not initialized."
57               " Did you forget to create an instance of "
58               " the apache::subversion::svnxx::init class?");
59         }
60       return state;
61     }
62 
get_root_pool() const63   apr_pool_t* get_root_pool() const noexcept
64     {
65       return root_pool;
66     }
67 
68 private:
69   // Thou shalt not create global_states other than through the factory.
70   global_state();
71 
72   apr_pool_t* root_pool{nullptr};
73 
74   static std::mutex guard;
75   static weak_ptr self;
76 };
77 
78 } // namespace detail
79 } // namespace svnxx
80 } // namespace subversion
81 } // namespace apache
82 
83 #endif // SVNXX_PRIVATE_INIT_HPP
84