OGLplus  (0.59.0) a C++ wrapper for rendering APIs

main_ctx_object.hpp
Go to the documentation of this file.
1 #ifndef EAGINE_MAIN_CTX_OBJECT_HPP
9 #define EAGINE_MAIN_CTX_OBJECT_HPP
10 
11 #include "assert.hpp"
12 #include "identifier_t.hpp"
13 #include "logging/logger.hpp"
14 #include "main_ctx_fwd.hpp"
15 
16 namespace eagine {
17 
18 template <typename T>
19 auto application_config_initial(
20  application_config& config,
21  string_view key,
22  T& initial,
23  string_view tag) -> T&;
24 
30 public:
31  main_ctx_log_backend_getter() noexcept;
32  main_ctx_log_backend_getter(master_ctx&) noexcept;
33 
34  auto operator()() noexcept -> auto* {
35  return _backend;
36  }
37 
38 private:
39  logger_backend* const _backend{nullptr};
40 };
41 
46 public:
49 
50  main_ctx_object_parent_info(master_ctx& ctx) noexcept
51  : _context{&ctx} {}
52 
55  : _object{&obj} {}
56 
57 private:
58  friend class main_ctx_object;
59 
60  master_ctx* _context{nullptr};
61  const main_ctx_object* _object{nullptr};
62 };
63 
64 class application_config;
65 
72  : public named_logging_object<main_ctx_log_backend_getter> {
74 
75 public:
77  main_ctx_object(identifier obj_id, main_ctx_parent parent) noexcept
78  : base{_make_base(obj_id, parent)} {}
79 
81  auto process_instance_id() const noexcept -> process_instance_id_t;
82 
84  auto main_context() const noexcept -> main_ctx&;
85 
87  auto app_config() const noexcept -> application_config&;
88 
91  return {*this};
92  }
93 
95  template <typename T>
96  auto cfg_init(string_view key, T initial, string_view tag = {}) -> T {
97  return application_config_initial(app_config(), key, initial, tag);
98  }
99 
101  template <typename Extractable, typename T>
102  auto cfg_extr(
103  string_view key,
104  T initial,
105  string_view tag = {},
106  type_identity<Extractable> = {}) -> T {
107  Extractable value(initial);
108  return extract_or(
109  application_config_initial(app_config(), key, value, tag), initial);
110  }
111 
112 private:
113  static auto _make_base(identifier obj_id, main_ctx_parent parent) noexcept
114  -> base {
115  if(parent._object) {
116  return base{obj_id, static_cast<const base&>(*parent._object)};
117  }
118  if(parent._context) {
119  return base{obj_id, main_ctx_log_backend_getter{*parent._context}};
120  }
121  return base{obj_id, main_ctx_log_backend_getter{}};
122  }
123 };
124 
125 } // namespace eagine
126 
127 #endif // EAGINE_MAIN_CTX_OBJECT_HPP
Helper class used to initialize main context objects.
Definition: main_ctx_object.hpp:45
const main_ctx_object_parent_info & main_ctx_parent
Alias for main_ctx_object_parent_info parameter type.
Definition: main_ctx_fwd.hpp:24
Class for a single-instance object providing useful information ans services.
Definition: main_ctx.hpp:45
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
Base class for main context objects.
Definition: main_ctx_object.hpp:71
Basic template for logging objects.
Definition: logger.hpp:235
main_ctx_object_parent_info(main_ctx &) noexcept
Construction from main_ctx instance.
Definition: main_ctx_object.hpp:48
Common code is placed in this namespace.
Definition: eagine.hpp:21
Helper class used in the implementation of logging in main context object.
Definition: main_ctx_object.hpp:29
main_ctx_object_parent_info(const main_ctx_object &obj) noexcept
Construction from main context object.
Definition: main_ctx_object.hpp:54
main_ctx_object(identifier obj_id, main_ctx_parent parent) noexcept
Initialization from object id and parent.
Definition: main_ctx_object.hpp:77
auto cfg_extr(string_view key, T initial, string_view tag={}, type_identity< Extractable >={}) -> T
Reads and returns the configuration value identified by key.
Definition: main_ctx_object.hpp:102
auto cfg_init(string_view key, T initial, string_view tag={}) -> T
Reads and returns the configuration value identified by key.
Definition: main_ctx_object.hpp:96
std::uint32_t process_instance_id_t
Unique process identifier type (does not necessarily match to OS PID).
Definition: identifier_t.hpp:22
auto as_parent() noexcept -> main_ctx_object_parent_info
Returns this as main_ctx_object_parent_info.
Definition: main_ctx_object.hpp:90
auto main_context() const noexcept -> main_ctx &
Returns a reference to the main context singleton.
auto process_instance_id() const noexcept -> process_instance_id_t
Returns the process id.
auto app_config() const noexcept -> application_config &
Returns a reference to the application config object.
Interface for logging backend implementations.
Definition: backend.hpp:60
Template type used mostly for function type-tag dispatching.
Definition: type_identity.hpp:19
basic_identifier< 10, 6, default_identifier_char_set, identifier_t > identifier
Default identifier type used throughout the project.
Definition: identifier.hpp:346

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).