farm-ng-core
component.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Copyright (c) farm-ng, inc. All rights reserved.
16 
17 #pragma once
18 
19 #include "farm_ng/core/misc/uri.h"
22 
24 #include <farm_ng/core/misc/void.h>
25 
26 #include <typeinfo>
27 
28 namespace farm_ng {
29 
30 /// Transforms identifier to C++ source identifier
31 ///
32 /// Used for uri on Component templated class
33 std::string demangleTypeid(std::type_info const &type);
34 
35 /// Parent definition of a farm_ng::Component
36 class Component {
37  public:
38  /// Default constructor of a context, it gets its own `io_context`.
39  template <class TDerived>
40  explicit Component(
41  Context const &ctx,
42  std::string scheme,
43  TDerived * /*unused*/,
44  std::string const &path)
45  : context_strand_(ctx),
46  uri_(
47  scheme,
48  FARM_FORMAT("[{}]", demangleTypeid(typeid(TDerived))),
49  path) {
50  static_assert(
51  std::is_base_of_v<Component, TDerived>,
52  "must be derived from Component");
53  }
54 
55  /// Virtual destructor for component derived classes
56  virtual ~Component() = default;
57 
58  /// Returns the owned ``Context`` by the component.
59  [[nodiscard]] Context getContext() const;
60 
61  /// Returns the owned ``ContextStrand`` by the component.
62  [[nodiscard]] ContextStrand getContextStrand() const;
63 
64  /// Returns the unique uri of the component instance.
65  [[nodiscard]] Uri const &uri() const;
66 
67  protected:
68  /// Placeholder for a component reset callback
69  virtual void onReset(Void const & /*unused*/) {}
70 
71  private:
72  ContextStrand context_strand_;
73  Uri uri_;
74 };
75 
76 /// Templated ``Input`` constructor
77 template <class TArg>
79  Component const *component,
80  std::string const &name,
81  std::function<void(TArg)> const &f,
82  InputConfig const &config)
83  : context_strand_(FARM_UNWRAP(component).getContextStrand()),
84  uri_(FARM_UNWRAP(component).uri()),
85  config_(config),
86  function_(f),
87  count_(0) {
88  uri_.query = FARM_FORMAT("in={}", name);
89 }
90 
91 } // namespace farm_ng
FARM_FORMAT
#define FARM_FORMAT(...)
Formats the cstr using the libfmt library.
Definition: format.h:125
farm_ng
Definition: backtrace.cpp:102
farm_ng::demangleTypeid
std::string demangleTypeid(std::type_info const &type)
Transforms identifier to C++ source identifier.
Definition: component.cpp:22
uri.h
void.h
logger.h
farm_ng::Void
Regular void type.
Definition: void.h:20
farm_ng::Component::getContext
Context getContext() const
Returns the owned Context by the component.
Definition: component.cpp:34
farm_ng::Component::Component
Component(Context const &ctx, std::string scheme, TDerived *, std::string const &path)
Default constructor of a context, it gets its own io_context.
Definition: component.h:40
farm_ng::Uri
https://en.wikipedia.org/w/index.php?title=Uniform_Resource_Identifier&oldid=1072892451#Syntax
Definition: uri.h:22
farm_ng::Input::Input
Input(Component const *component, std::string const &name, std::function< void(TArg)> const &f, InputConfig const &config=InputConfig())
Default constructor, takes its component, its name, a callback function and its configuration.
Definition: component.h:78
farm_ng::Component::uri
const Uri & uri() const
Returns the unique uri of the component instance.
Definition: component.cpp:38
farm_ng::Component::onReset
virtual void onReset(Void const &)
Placeholder for a component reset callback.
Definition: component.h:69
core.event_service_tool.config
def config
Definition: event_service_tool.py:76
context.h
farm_ng::Component::~Component
virtual ~Component()=default
Virtual destructor for component derived classes.
farm_ng::ContextStrand
Contains a stranded context to post and dispatch handlers with the guarantee that none of the handler...
Definition: context.h:86
farm_ng::InputConfig
The configuration of an Input class.
Definition: input.h:31
farm_ng::Component
Parent definition of a farm_ng::Component.
Definition: component.h:36
farm_ng::Component::getContextStrand
ContextStrand getContextStrand() const
Returns the owned ContextStrand by the component.
Definition: component.cpp:36
farm_ng::Context
Contains the execution state context.
Definition: context.h:41
input.h
FARM_UNWRAP
#define FARM_UNWRAP(wrapper,...)
Returns *wrapper, but panics if wrapper is nullopt or null.
Definition: logger.h:576
core.event_service.type
type
Definition: event_service.py:547