farm-ng-core
context.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"
20 
21 #include <boost/asio/io_service.hpp>
22 #include <boost/asio/strand.hpp>
23 // #include <boost/asio/executor_work_guard.hpp>
24 // #include <boost/asio/io_context.hpp>
25 // #include <boost/asio/io_context_strand.hpp>
26 
27 #include <iostream>
28 #include <set>
29 
30 namespace farm_ng {
31 
32 class Component;
33 
34 template <class TArg>
35 class Input;
36 
37 template <class TArg>
38 class Output;
39 
40 /// Contains the execution state context.
41 class Context {
42  public:
43  // using WorkGuard =
44  // boost::asio::executor_work_guard<boost::asio::io_context::executor_type>;
45  using WorkGuard = boost::asio::io_service::work;
46  // boost::asio::io_contex
47  using IoContext = boost::asio::io_service;
48  using Strand = Context::IoContext::strand;
49 
50  /// Default constructor of a context.
51  Context();
52 
53  /// Returns a reference to the owned `io_context`.
55 
56  /// Returns a shared pointer to the owned `io_context`.
57  std::shared_ptr<IoContext> getAsioIoContextPtr();
58 
59  /// Run the `io_context`.
60  void run();
61 
62  // Restart the context and run, to be used when context has stopped
63  // i.e. when all posted jobs are complete and no workguard.
64  void restartAndRun();
65 
66  /// clear the work for advanced use cases where you want to execute a finite
67  /// number of tasks, e.g. in tests, or to run the pipeline to completion for a
68  /// given input.
69  void clearWorkGuard();
70 
71  /// Create a new pointer for the worker guard.
72  void setWorkGuard();
73 
74  private:
75  // A shared pointer to the owned `io_context`.
76  std::shared_ptr<IoContext> ctx_;
77  // A shared pointer to the owned worker guard.
78  std::shared_ptr<std::unique_ptr<WorkGuard>> work_;
79 };
80 
81 /// Contains a stranded context to post and dispatch handlers with
82 /// the guarantee that none of the handlers will execute concurrently.
83 ///
84 /// More info:
85 /// https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/reference/io_context__strand.html
87  public:
88  /// Default constructor of a stranded context based on an allocated
89  /// ``Context``.
90  explicit ContextStrand(Context const& ctx);
91 
92  /// Return the `io_context` owned by the ``Context`` member.
94 
95  /// Return the `io_context::strand`` owned by the object instance.
97 
98  /// Returns the owned ``Context`` by the component.
99  [[nodiscard]] Context getContext() const;
100 
101  private:
102  // The ``Context`` object owned by the class.
103  Context ctx_;
104  // A shared pointer to the ``io_context::strand`` owned by the class.
105  std::shared_ptr<Context::Strand> strand_;
106 };
107 
108 } // namespace farm_ng
farm_ng
Definition: backtrace.cpp:102
uri.h
farm_ng::Context::getAsioIoContext
IoContext & getAsioIoContext()
Returns a reference to the owned io_context.
Definition: context.cpp:31
farm_ng::ContextStrand::getAsioIoContext
Context::IoContext & getAsioIoContext()
Return the io_context owned by the Context member.
Definition: context.cpp:52
farm_ng::ContextStrand::ContextStrand
ContextStrand(Context const &ctx)
Default constructor of a stranded context based on an allocated Context.
Definition: context.cpp:48
farm_ng::Context::Strand
Context::IoContext::strand Strand
Definition: context.h:48
farm_ng::Context::run
void run()
Run the io_context.
Definition: context.cpp:37
farm_ng::Context::getAsioIoContextPtr
std::shared_ptr< IoContext > getAsioIoContextPtr()
Returns a shared pointer to the owned io_context.
Definition: context.cpp:33
farm_ng::Context::WorkGuard
boost::asio::io_service::work WorkGuard
Definition: context.h:45
farm_ng::Context::clearWorkGuard
void clearWorkGuard()
clear the work for advanced use cases where you want to execute a finite number of tasks,...
Definition: context.cpp:44
farm_ng::Context::setWorkGuard
void setWorkGuard()
Create a new pointer for the worker guard.
Definition: context.cpp:46
farm_ng::ContextStrand::getAsioStrand
Context::Strand & getAsioStrand()
Return the io_context::strand` owned by the object instance.
Definition: context.cpp:56
farm_ng::Output
A class that represents an Output type to be used in a Component.
Definition: context.h:38
farm_ng::Context::IoContext
boost::asio::io_service IoContext
Definition: context.h:47
farm_ng::Context::restartAndRun
void restartAndRun()
Definition: context.cpp:39
farm_ng::ContextStrand::getContext
Context getContext() const
Returns the owned Context by the component.
Definition: context.cpp:59
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::Context::Context
Context()
Default constructor of a context.
Definition: context.cpp:24
farm_ng::Context
Contains the execution state context.
Definition: context.h:41
farm_ng::Input
A class that represents in Input type to be used in a Component.
Definition: context.h:35