ApplicationConfiguration.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_APPLICATION__APPLICATION_CONFIGURATION
18 #define COM_BORA_SOFTWARE__BALAU_APPLICATION__APPLICATION_CONFIGURATION
19 
22 
23 namespace Balau {
24 
40  protected: template <typename BaseT, typename DeleterT = std::default_delete<BaseT>>
41  BindingBuilder<BaseT, DeleterT> & bind(std::string_view name = std::string_view()) const {
42  return Bind<BaseT, DeleterT>(*this).bind(name);
43  }
44 
45  protected: void addConfiguration(const ApplicationConfiguration & conf) const {
46  extraConfiguration.push_back(&conf);
47  }
48 
57  protected: void registerPostConstructionCall(const std::function<void (const Injector &)> & call) const {
58  postConstructionCalls.push_back(call);
59  }
60 
74  protected: void registerPreDestructionCall(const std::function<void ()> & call) const {
75  preDestructionCalls.push_back(call);
76  }
77 
92  protected: template <typename T> void registerStaticSingleton(std::shared_ptr<T> * ptrPtr, std::string_view name = std::string_view()) const {
93  staticSingletonPostConstructionCalls.emplace_back(new StaticSingletonRegistration(ptrPtr, name));
94  preDestructionCalls.push_back([ptrPtr] () { ptrPtr->reset(); });
95  }
96 
98 
99  private: std::vector<std::shared_ptr<Impl::BindingBuilderBase>> build() const override {
100  // Move the vector because it is not used anymore after this call from the injector.
101  return std::move(builders);
102  }
103 
104  public: std::vector<const InjectorConfiguration *> getExtraConfiguration() const override {
105  return extraConfiguration;
106  }
107 
108  public: std::list<std::function<void (const Injector& )>> getPostConstructionCalls() const override {
109  return postConstructionCalls;
110  }
111 
112  public: std::list<std::function<void ()>> getPreDestructionCalls() const override {
113  return preDestructionCalls;
114  }
115 
116  public: std::list<std::unique_ptr<StaticSingletonRegistrationBase>> getStaticSingletonPostConstructionCalls() const override {
117  return std::move(staticSingletonPostConstructionCalls);
118  }
119 
120  private: template <typename T, typename DeleterT> struct Bind {
121  static_assert(
122  std::negation<typename std::is_pointer<T>::type>::value
123  , "Raw pointers are not permitted in bindings. Use Unique or Shared bindings instead."
124  );
125 
126  const ApplicationConfiguration & parent;
127 
128  explicit Bind(const ApplicationConfiguration & parent_) : parent(parent_) {}
129 
130  BindingBuilder<T, DeleterT> & bind(std::string_view name) const {
131  parent.builders.emplace_back(
132  std::shared_ptr<Impl::BindingBuilderBase>(new BindingBuilder<T, DeleterT>(std::string(name)))
133  );
134 
135  return *reinterpret_cast<BindingBuilder<T, DeleterT> *>(parent.builders.back().get());
136  }
137  };
138 
139  private: template <typename T, typename DeleterT> struct Bind<const T, DeleterT> {
140  static_assert(
141  std::negation<typename std::is_pointer<T>::type>::value
142  , "Raw pointers are not permitted in bindings. Use Unique or Shared bindings instead."
143  );
144 
145  const ApplicationConfiguration & parent;
146 
147  explicit Bind(const ApplicationConfiguration & parent_) : parent(parent_) {}
148 
149  BindingBuilder<const T, DeleterT> & bind(std::string_view name) const {
150  parent.builders.emplace_back(
151  std::shared_ptr<Impl::BindingBuilderBase>(new BindingBuilder<const T, DeleterT>(std::string(name)))
152  );
153 
154  return *reinterpret_cast<BindingBuilder<const T, DeleterT> *>(parent.builders.back().get());
155  }
156  };
157 
158  private: mutable std::vector<std::shared_ptr<Impl::BindingBuilderBase>> builders;
159  private: mutable std::vector<const InjectorConfiguration *> extraConfiguration;
160  private: mutable std::list<std::function<void (const Injector& )>> postConstructionCalls;
161  private: mutable std::list<std::function<void ()>> preDestructionCalls;
162  private: mutable std::list<std::unique_ptr<StaticSingletonRegistrationBase>> staticSingletonPostConstructionCalls;
163 };
164 
165 } // namespace Balau
166 
167 #endif // COM_BORA_SOFTWARE__BALAU_APPLICATION__APPLICATION_CONFIGURATION
void registerPreDestructionCall(const std::function< void()> &call) const
Register with the injector a callback that will be called in the injector&#39;s destructor, before the bindings are deleted.
Definition: ApplicationConfiguration.hpp:74
The abstract base class for all Injector configuration classes.
void registerStaticSingleton(std::shared_ptr< T > *ptrPtr, std::string_view name=std::string_view()) const
Register a static singleton pointer that the injector will set up post-construction and invalidate pr...
Definition: ApplicationConfiguration.hpp:92
Application configurations specify application injector bindings.
Definition: ApplicationConfiguration.hpp:34
An injector binding candidate created via the injector configuration.
Definition: BindingBuilder.hpp:44
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
BindingBuilder< BaseT, DeleterT > & bind(std::string_view name=std::string_view()) const
Create a binding specification for the specified named or unnamed interface.
Definition: ApplicationConfiguration.hpp:41
The main dependency injector class.
Definition: Injector.hpp:41
Injector configurations specify injector bindings.
Definition: InjectorConfiguration.hpp:32
An injector binding candidate created via the injector configuration.
void registerPostConstructionCall(const std::function< void(const Injector &)> &call) const
Register with the injector a callback that will be called by the injector at the end of construction...
Definition: ApplicationConfiguration.hpp:57