farm-ng-core
output.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 
23 #include <boost/signals2.hpp>
24 #include <boost/signals2/connection.hpp>
25 
26 #include <functional>
27 
28 namespace farm_ng {
29 
30 template <class TArg>
31 class Input;
32 
33 /// A class that represents an ``Output`` type to be used in a ``Component``.
34 template <class TArg>
35 class Output {
36  public:
37  /// Default constructor, takes its `component`, and its `name`.
38  ///
39  /// Convention: If the variable name of the output is `out_foo_`, then
40  /// `name` shall be `foo`.
41  Output(Component const* component, std::string const& name)
42  : context_strand_(FARM_UNWRAP(component).getContextStrand()),
43  uri_(FARM_UNWRAP(component).uri()) {
44  uri_.query = FARM_FORMAT("out={}", name);
45  }
46 
47  /// Default destructor
48  ~Output() {}
49 
50  /// Call the actual function and send the result through the signal.
51  void send(TArg const& out) { signal_(out); }
52 
53  /// Return the signal to the output function signature.
54  boost::signals2::signal<void(TArg)>& signal() { return signal_; }
55 
56  /// Create a connection between the current signal and the receiving input.
57  void connect(Input<TArg>& input) { input.connect(signal()); }
58 
59  /// Returns the unique uri of the output.
60  [[nodiscard]] Uri const& uri() const { return uri_; }
61 
62  private:
63  /// The stranded context of the function.
64  ContextStrand context_strand_;
65  /// The configuration structure for the class instance.
66  Uri uri_;
67  /// The signal to the function.
68  boost::signals2::signal<void(TArg)> signal_;
69 };
70 
71 } // 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
uri.h
component.h
farm_ng::Output::uri
Uri const & uri() const
Returns the unique uri of the output.
Definition: output.h:60
farm_ng::Output::signal
boost::signals2::signal< void(TArg)> & signal()
Return the signal to the output function signature.
Definition: output.h:54
farm_ng::Input::connect
Input< TArg > & connect(boost::signals2::signal< void(TArg)> &signal)
Add a connection between the input signal with the internal signal_slot function.
Definition: input.h:58
farm_ng::Output::~Output
~Output()
Default destructor.
Definition: output.h:48
farm_ng::Output::send
void send(TArg const &out)
Call the actual function and send the result through the signal.
Definition: output.h:51
farm_ng::Uri
https://en.wikipedia.org/w/index.php?title=Uniform_Resource_Identifier&oldid=1072892451#Syntax
Definition: uri.h:22
farm_ng::Output::Output
Output(Component const *component, std::string const &name)
Default constructor, takes its component, and its name.
Definition: output.h:41
farm_ng::Output::connect
void connect(Input< TArg > &input)
Create a connection between the current signal and the receiving input.
Definition: output.h:57
context.h
farm_ng::Uri::query
std::string query
If URI is used in component, then query is used to denote inputs or outputs. For instance: '?...
Definition: uri.h:52
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::Component
Parent definition of a farm_ng::Component.
Definition: component.h:36
FARM_UNWRAP
#define FARM_UNWRAP(wrapper,...)
Returns *wrapper, but panics if wrapper is nullopt or null.
Definition: logger.h:576
farm_ng::Input
A class that represents in Input type to be used in a Component.
Definition: context.h:35