EnvironmentProperties.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 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__ENVIRONMENT_PROPERTIES
18 #define COM_BORA_SOFTWARE__BALAU_APPLICATION__ENVIRONMENT_PROPERTIES
19 
20 #include <Balau/Dev/Assert.hpp>
22 #include <Balau/Application/Impl/BindingMap.hpp>
24 #include <Balau/Resource/File.hpp>
26 #include <Balau/Util/Files.hpp>
27 #include <Balau/Util/Streams.hpp>
28 
29 namespace Balau {
30 
31 namespace Impl {
32 
33 class EnvironmentConfigurationBuilder;
34 
35 } // namespace Impl
36 
63  public: class Item {
69  public: std::string_view getName() const {
70  return key.name;
71  }
72 
78  public: Impl::BindingKey getKey() const {
79  return key.toKey();
80  }
81 
89  public: template <typename ValueT> bool isValue() const {
90  return key == Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Value, ValueT>), key.name);
91  }
92 
100  public: template <typename BaseT> bool isUnique() const {
101  return key == Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Unique, BaseT, std::default_delete<BaseT>>), key.name);
102  }
103 
110  public: bool isComposite() const {
111  return key == Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Shared, EnvironmentProperties>), key.name);
112  }
113 
126  public: template <typename ValueT> ValueT getValue() const {
127  return owner.getValue<ValueT>(
128  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Value, ValueT>), key.name)
129  );
130  }
131 
144  public: template <typename ValueT> ValueT getValue(const ValueT & defaultValue) const {
145  return owner.getValue<ValueT>(
146  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Value, ValueT>), key.name), defaultValue
147  );
148  }
149 
162  public: template <typename BaseT> std::unique_ptr<BaseT> getUnique() const {
163  return owner.getUnique<BaseT>(
164  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Unique, BaseT, std::default_delete<BaseT>>), key.name)
165  );
166  }
167 
180  public: template <typename BaseT> std::unique_ptr<BaseT> getUnique(std::unique_ptr<BaseT> && defaultValue) const {
181  return owner.getUnique<BaseT>(
182  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Unique, BaseT, std::default_delete<BaseT>>), key.name)
183  , std::move(defaultValue)
184  );
185  }
186 
196  public: std::shared_ptr<EnvironmentProperties> getComposite() const {
197  return owner.getComposite(
198  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Shared, EnvironmentProperties>), key.name)
199  );
200  }
201 
210  public: std::shared_ptr<EnvironmentProperties> getCompositeOrNull() const {
211  return owner.getCompositeOrNull(
212  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Shared, EnvironmentProperties>), key.name)
213  );
214  }
215 
216  friend class EnvironmentProperties;
217 
218  private: Item(const EnvironmentProperties & owner_, const Impl::BindingKeyView & key_)
219  : owner(owner_)
220  , key(key_) {}
221 
222  private: const EnvironmentProperties & owner;
223  private: const Impl::BindingKeyView key;
224  };
225 
229  public: class iterator {
233  public: iterator(const iterator & copy)
234  : owner(copy.owner)
235  , mapIterator(copy.mapIterator) {}
236 
240  public: iterator & operator = (const iterator & copy) {
241  mapIterator = copy.mapIterator;
242  return *this;
243  }
244 
248  public: iterator operator ++ (int) {
249  iterator ret = *this;
250  ++mapIterator;
251  return ret;
252  }
253 
257  public: iterator & operator ++ () {
258  ++mapIterator;
259  return *this;
260  }
261 
265  public: Item operator * () const {
266  return Item(owner, Impl::BindingKeyView(mapIterator->key));
267  }
268 
272  public: bool operator == (const iterator & rhs) const {
273  return mapIterator == rhs.mapIterator;
274  }
275 
279  public: bool operator != (const iterator & rhs) const {
280  return mapIterator != rhs.mapIterator;
281  }
282 
283  friend class EnvironmentProperties;
284 
285  private: explicit iterator(const EnvironmentProperties & owner_, Impl::BindingMap::ConstIterator mapIterator_)
286  : owner(owner_)
287  , mapIterator(mapIterator_) {}
288 
289  private: const EnvironmentProperties & owner;
290  private: Impl::BindingMap::ConstIterator mapIterator;
291  };
292 
296  public: typedef iterator const_iterator;
297 
299 
307  public: template <typename ValueT>
308  bool hasValue(std::string_view name) const {
309  const Impl::BindingKeyView keyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Value, ValueT>), name);
310  return bindings->hasBinding(keyView);
311  }
312 
320  public: template <typename BaseT>
321  bool hasUnique(std::string_view name) const {
322  const Impl::BindingKeyView keyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Unique, BaseT, std::default_delete<BaseT>>), name);
323  return bindings->hasBinding(keyView);
324  }
325 
332  public: bool hasComposite(std::string_view name) const {
333  const Impl::BindingKeyView keyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Shared, EnvironmentProperties>), name);
334  return bindings->hasBinding(keyView);
335  }
336 
349  public: template <typename ValueT> ValueT getValue(std::string_view name) const {
350  return getValue<ValueT>(
351  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Value, ValueT>), name)
352  );
353  }
354 
367  public: template <typename ValueT> ValueT getValue(std::string_view name, const ValueT & defaultValue) const {
368  return getValue<ValueT>(
369  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Value, ValueT>), name), defaultValue
370  );
371  }
372 
385  public: template <typename BaseT> std::unique_ptr<BaseT> getUnique(std::string_view name) const {
386  return getUnique<BaseT>(
387  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Unique, BaseT, std::default_delete<BaseT>>), name)
388  );
389  }
390 
403  public: template <typename BaseT> std::unique_ptr<BaseT> getUnique(std::string_view name, std::unique_ptr<BaseT> && defaultValue) const {
404  return getUnique<BaseT>(
405  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Unique, BaseT, std::default_delete<BaseT>>), name), defaultValue
406  );
407  }
408 
418  public: std::shared_ptr<EnvironmentProperties> getComposite(std::string_view name) const {
419  return getComposite(
420  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Shared, EnvironmentProperties>), name)
421  );
422  }
423 
432  public: std::shared_ptr<EnvironmentProperties> getCompositeOrNull(std::string_view name) const {
433  return getCompositeOrNull(
434  Impl::BindingKeyView(typeid(Impl::BindingKeyType<Impl::BindingMetaType::Shared, EnvironmentProperties>), name)
435  );
436  }
437 
441  public: iterator begin() {
442  return iterator(*this, bindings->begin());
443  }
444 
448  public: iterator end() {
449  return iterator(*this, bindings->end());
450  }
451 
455  public: const_iterator begin() const {
456  return const_iterator(*this, bindings->begin());
457  }
458 
462  public: const_iterator end() const {
463  return const_iterator(*this, bindings->end());
464  }
465 
469  public: bool empty() const {
470  return bindings->begin() == bindings->end();
471  }
472 
478  public: EnvironmentProperties(std::unique_ptr<const Impl::BindingMap> && bindings_)
479  : bindings(std::move(bindings_)) {}
480 
487  : bindings(new Impl::BindingMap()) {}
488 
490 
491  friend class EnvironmentConfiguration;
492  friend class Impl::EnvironmentConfigurationBuilder;
493 
494  private: template <typename ValueT> ValueT getValue(const Impl::BindingKeyView & keyView) const {
495  const auto * binding = bindings->find(keyView);
496 
497  if (binding != nullptr) {
498  return static_cast<const Impl::AbstractValueBinding<ValueT> *>(binding->get())->get(nullptr);
499  } else {
501  }
502  }
503 
504  private: template <typename ValueT> ValueT getValue(const Impl::BindingKeyView & keyView, const ValueT & defaultValue) const {
505  const auto * binding = bindings->find(keyView);
506 
507  if (binding != nullptr) {
508  return static_cast<const Impl::AbstractValueBinding<ValueT> *>(binding->get())->get(nullptr);
509  } else {
510  return defaultValue;
511  }
512  }
513 
514  private: template <typename BaseT, typename DeleterT = std::default_delete<BaseT>> std::unique_ptr<BaseT> getUnique(const Impl::BindingKeyView & keyView) const {
515  const auto * binding = bindings->find(keyView);
516 
517  if (binding != nullptr) {
518  return static_cast<const Impl::AbstractUniquePtrBinding<BaseT, DeleterT> *>(binding->get())->get(nullptr);
519  } else {
521  }
522  }
523 
524  private: template <typename BaseT, typename DeleterT = std::default_delete<BaseT>> std::unique_ptr<BaseT> getUnique(const Impl::BindingKeyView & keyView, std::unique_ptr<BaseT> && defaultValue) const {
525  const auto * binding = bindings->find(keyView);
526 
527  if (binding != nullptr) {
528  return static_cast<const Impl::AbstractUniquePtrBinding<BaseT, DeleterT> *>(binding->get())->get(nullptr);
529  } else {
530  return std::move(defaultValue);
531  }
532  }
533 
534  private: std::shared_ptr<EnvironmentProperties> getComposite(const Impl::BindingKeyView & keyView) const {
535  const auto * binding = bindings->find(keyView);
536 
537  if (binding != nullptr) {
538  // Environment properties shared bindings are always of type ProvidedSingletonBinding.
539  // These do not use the injector instance, thus nullptr can be passed without any issues.
540  return static_cast<const Impl::AbstractSharedPtrBinding<EnvironmentProperties> *>(binding->get())->get(nullptr);
541  } else {
543  }
544  }
545 
546  private: std::shared_ptr<EnvironmentProperties> getCompositeOrNull(const Impl::BindingKeyView & keyView) const {
547  const auto * binding = bindings->find(keyView);
548 
549  if (binding != nullptr) {
550  // Environment properties shared bindings are always of type ProvidedSingletonBinding.
551  // These do not use the injector instance, thus nullptr can be passed without any issues.
552  return static_cast<const Impl::AbstractSharedPtrBinding<EnvironmentProperties> *>(binding->get())->get(nullptr);
553  } else {
554  return std::shared_ptr<EnvironmentProperties>();
555  }
556  }
557 
558  private: const std::unique_ptr<const Impl::BindingMap> bindings;
559 };
560 
561 } // namespace Balau
562 
563 #endif // COM_BORA_SOFTWARE__BALAU_APPLICATION__ENVIRONMENT_PROPERTIES
bool hasUnique(std::string_view name) const
Returns true if the composite property has a unique pointer binding matching the specified type and n...
Definition: EnvironmentProperties.hpp:321
bool operator==(const BalauException &lhs, const BalauException &rhs)
Base class comparison function for Balau exceptions.
Definition: BalauException.hpp:112
const_iterator begin() const
Get a const iterator positioned at the beginning of the environment properties list.
Definition: EnvironmentProperties.hpp:455
bool empty() const
Returns true if the environment properties object is empty.
Definition: EnvironmentProperties.hpp:469
Utilities for files.
A file on the local file system.
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
ValueT getValue() const
Get the non-polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:126
bool isValue() const
Returns true if the item is a value binding matching the specified type and name. ...
Definition: EnvironmentProperties.hpp:89
Utilities for streams.
STL namespace.
std::unique_ptr< BaseT > getUnique() const
Get the polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:162
ValueT getValue(const ValueT &defaultValue) const
Get the non-polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:144
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
Impl::BindingKey getKey() const
Get the key of the item.
Definition: EnvironmentProperties.hpp:78
A hierarchical environment properties holder created from a composite property.
Definition: EnvironmentProperties.hpp:59
ValueT getValue(std::string_view name) const
Get a non-polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:349
Environment configurations specify typed and untyped environment injector bindings.
Definition: EnvironmentConfiguration.hpp:97
std::unique_ptr< BaseT > getUnique(std::string_view name) const
Get a polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:385
The EnvironmentProperties iterator.
Definition: EnvironmentProperties.hpp:229
iterator end()
Get an iterator positioned at the end of the environment properties list.
Definition: EnvironmentProperties.hpp:448
std::string_view getName() const
Get the name of the item.
Definition: EnvironmentProperties.hpp:69
EnvironmentProperties(std::unique_ptr< const Impl::BindingMap > &&bindings_)
Create an environment properties instance.
Definition: EnvironmentProperties.hpp:478
bool hasComposite(std::string_view name) const
Returns true if the composite property has a child composite binding with the specified name...
Definition: EnvironmentProperties.hpp:332
std::shared_ptr< EnvironmentProperties > getComposite() const
Get the environment properties instance with the specified name.
Definition: EnvironmentProperties.hpp:196
std::shared_ptr< EnvironmentProperties > getCompositeOrNull() const
Get the environment properties instance with the specified name.
Definition: EnvironmentProperties.hpp:210
std::shared_ptr< EnvironmentProperties > getCompositeOrNull(std::string_view name) const
Get a child environment properties instance with the specified name.
Definition: EnvironmentProperties.hpp:432
const_iterator end() const
Get a const iterator positioned at the end of the environment properties list.
Definition: EnvironmentProperties.hpp:462
std::shared_ptr< EnvironmentProperties > getComposite(std::string_view name) const
Get a child environment properties instance with the specified name.
Definition: EnvironmentProperties.hpp:418
bool hasValue(std::string_view name) const
Returns true if the composite property has a value binding matching the specified type and name...
Definition: EnvironmentProperties.hpp:308
EnvironmentProperties()
Create an empty environment properties instance.
Definition: EnvironmentProperties.hpp:486
bool isUnique() const
Returns true if the item is a unique pointer binding matching the specified type and name...
Definition: EnvironmentProperties.hpp:100
std::unique_ptr< BaseT > getUnique(std::string_view name, std::unique_ptr< BaseT > &&defaultValue) const
Get a polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:403
Instances of the Item class are returned from iterators.
Definition: EnvironmentProperties.hpp:63
Assertion utilities for development purposes.
ValueT getValue(std::string_view name, const ValueT &defaultValue) const
Get a non-polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:367
std::unique_ptr< BaseT > getUnique(std::unique_ptr< BaseT > &&defaultValue) const
Get the polymorphic value of the specified type and name.
Definition: EnvironmentProperties.hpp:180
iterator(const iterator &copy)
Create an iterator by copying the supplied iterator.
Definition: EnvironmentProperties.hpp:233
iterator const_iterator
The const iterator of EnvironmentProperties is the same as a non-const one.
Definition: EnvironmentProperties.hpp:296
bool isComposite() const
Returns true if the item is a composite binding with the specified name.
Definition: EnvironmentProperties.hpp:110
iterator begin()
Get an iterator positioned at the beginning of the environment properties list.
Definition: EnvironmentProperties.hpp:441
An injector binding candidate created via the injector configuration.
Balau exceptions for the injector framework.
Pre-defined universal from-string functions.
Thrown when no binding is found in the injector.
Definition: InjectorExceptions.hpp:75