farm-ng-core
input.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"
21 
22 #include <boost/signals2/connection.hpp>
23 #include <boost/signals2/signal.hpp>
25 
26 #include <functional>
27 
28 namespace farm_ng {
29 
30 /// The configuration of an ``Input`` class.
31 class InputConfig {
32  public:
33  explicit InputConfig(size_t max_queue_length = 0);
34 
36 };
37 
38 /// A class that represents in ``Input`` type to be used in a ``Component``.
39 template <class TArg>
40 class Input {
41  public:
42  /// Default constructor, takes its `component`, its `name`, a callback
43  /// function and its configuration.
44  ///
45  /// Convention: If the variable name of the input is `in_foo_`, then
46  /// `name` shall be `foo`.
47  explicit Input(
48  Component const* component,
49  std::string const& name,
50  std::function<void(TArg)> const& f,
51  InputConfig const& config = InputConfig());
52 
53  /// Default destructor
54  ~Input() {}
55 
56  /// Add a connection between the input signal with the internal signal_slot
57  /// function
58  Input<TArg>& connect(boost::signals2::signal<void(TArg)>& signal) {
59  connections_.push_back(
60  std::make_shared<boost::signals2::scoped_connection>(signal.connect(
61  std::bind(&Input<TArg>::signalSlot, this, std::placeholders::_1))));
62  return *this;
63  }
64 
65  /// Call the actual function and send the result through the signal.
66  void send(TArg const& value) { signalSlot(value); }
67 
68  /// Returns the unique uri of the input.
69  [[nodiscard]] Uri const& uri() const { return uri_; }
70 
71  private:
72  // signalSlot is called by the signal, on the sending thread.
73  bool signalSlot(TArg value) {
74  int count = count_.load();
75  if (config_.max_queue_length != 0 && count > 0 &&
76  size_t(count) >= config_.max_queue_length) {
77  return false;
78  }
79  count_++;
80  // request the strand to invoke the function and send the message value
81  // NOTE: seems that `io_context::strand::post` it's deprecated and need
82  // to be use `boost::asio::post` instead. See:
83  // https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/reference/io_context__strand/post.html
84  context_strand_.getAsioStrand().post([this, value = std::move(value)] {
85  this->strandedSlot(std::move(value));
86  });
87  return true;
88  }
89 
90  // strandedSlot is called on the strand associated with ContextStrand
91  // context_strand_, and it can be a generic place to track things like
92  // function execution time, and manage the queue length.
93  void strandedSlot(TArg value) {
94  // begin timing
95  function_(std::move(value));
96  // end timing
97  // e.g. context_strand_.report_timing(input_name_, timing)
98  count_--;
99  }
100 
101  /// The stranded context of the function.
102  ContextStrand context_strand_;
103  /// The uri of the input.
104  Uri uri_;
105  /// The configuration structure for the class instance.
106  InputConfig config_;
107  /// The callback function definition.
108  std::function<void(TArg)> function_;
109  /// The list contained connections between other functions.
110  std::vector<std::shared_ptr<boost::signals2::scoped_connection>> connections_;
111  /// A counter tracking the number of calls made to the function.
112  std::atomic<int> count_;
113 };
114 
115 } // namespace farm_ng
farm_ng
Definition: backtrace.cpp:102
uri.h
farm_ng::InputConfig::InputConfig
InputConfig(size_t max_queue_length=0)
Definition: input.cpp:23
logger.h
farm_ng::Input::~Input
~Input()
Default destructor.
Definition: input.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::ContextStrand::getAsioStrand
Context::Strand & getAsioStrand()
Return the io_context::strand` owned by the object instance.
Definition: context.cpp:56
sophus::count
auto count(ImageViewBool mask, bool truth_value) -> int
Returns number of pixels equal truth_value in mask.
Definition: image_types.cpp:13
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::Input::send
void send(TArg const &value)
Call the actual function and send the result through the signal.
Definition: input.h:66
core.event_service_tool.config
def config
Definition: event_service_tool.py:76
context.h
farm_ng::Input::uri
Uri const & uri() const
Returns the unique uri of the input.
Definition: input.h:69
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::InputConfig::max_queue_length
size_t max_queue_length
Definition: input.h:35
farm_ng::Input
A class that represents in Input type to be used in a Component.
Definition: context.h:35