Scorum
dbservice_dbs_factory.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <typeinfo>
6 
7 #include <boost/config.hpp>
8 #include <boost/type_index.hpp>
9 #include <boost/container/flat_map.hpp>
10 
11 namespace scorum {
12 namespace chain {
13 
14 class database;
15 class dbs_base;
16 
18 {
19  using BaseServicePtr = std::unique_ptr<dbs_base>;
20 
21 protected:
23 
25 
26  virtual ~dbservice_dbs_factory();
27 
28 public:
29  template <typename ConcreteService> ConcreteService& obtain_service() const
30  {
31  return obtain_service_explicit<ConcreteService>();
32  }
33 
34  template <typename ConcreteService, typename... TDependencies>
35  ConcreteService& obtain_service_explicit(TDependencies&... dependencies) const
36  {
37  auto it = _dbs.find(boost::typeindex::type_id<ConcreteService>());
38  if (it == _dbs.end())
39  {
40  it = _dbs.insert(std::make_pair(boost::typeindex::type_id<ConcreteService>(),
41  BaseServicePtr(new ConcreteService(_db_core, dependencies...))))
42  .first;
43  }
44 
45  BaseServicePtr& ret = it->second;
46  return static_cast<ConcreteService&>(*ret);
47  }
48 
49 private:
50  mutable boost::container::flat_map<boost::typeindex::type_index, BaseServicePtr> _dbs;
51  database& _db_core;
52 };
53 } // namespace chain
54 } // namespace scorum
tracks the blockchain state in an extensible manner
Definition: database.hpp:52
ConcreteService & obtain_service_explicit(TDependencies &... dependencies) const
Definition: asset.cpp:15