Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
slot_abstraction.h
Go to the documentation of this file.
1// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4// or works. Modified works should carry a notice stating that you changed the software and should note the date and
5// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12// are solely responsible for determining the appropriateness of using and distributing the software and you assume
13// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16// damage to property. The software developed by NIST employees is not subject to copyright protection within the
17// United States.
18
19
20
21#ifndef HEDGEHOG_SLOT_ABSTRACTION_H
22#define HEDGEHOG_SLOT_ABSTRACTION_H
23#pragma once
24
25#include <set>
26#include <memory>
27#include <utility>
28#include <iostream>
29#include <condition_variable>
30
31#include "../../../implementors/implementor/implementor_slot.h"
32
34namespace hh {
36namespace core {
37
38#ifndef DOXYGEN_SHOULD_SKIP_THIS
40namespace implementor {
42class ImplementorSlot;
43}
44#endif //DOXYGEN_SHOULD_SKIP_THIS
45
47namespace abstraction {
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
51class NotifierAbstraction;
52#endif //DOXYGEN_SHOULD_SKIP_THIS
53
56 private:
57 std::shared_ptr<std::mutex>
58 mutex_ = nullptr;
59
60 std::shared_ptr<std::condition_variable>
62
63 std::shared_ptr<implementor::ImplementorSlot>
64 concreteSlot_ = nullptr;
65
66 public:
69 explicit SlotAbstraction(std::shared_ptr<implementor::ImplementorSlot> concreteSlot) :
70 mutex_(std::make_shared<std::mutex>()),
71 slotConditionVariable_(std::make_shared<std::condition_variable>()),
72 concreteSlot_(std::move(concreteSlot)) {
73 concreteSlot_->initialize(this);
74 }
75
77 virtual ~SlotAbstraction() = default;
78
83 [[nodiscard]] std::set<SlotAbstraction *> const &slots() const { return concreteSlot_->slots(); }
84
89 [[nodiscard]] std::set<SlotAbstraction *> &slots() { return concreteSlot_->slots(); }
90
93 [[nodiscard]] std::set<NotifierAbstraction *> const &connectedNotifiers() const {
94 mutex_->lock();
95 std::set<NotifierAbstraction *> const & cn = concreteSlot_->connectedNotifiers();
96 mutex_->unlock();
97 return cn;
98 }
99
102 void addNotifier(NotifierAbstraction *const notifier) {
103 mutex_->lock();
104 concreteSlot_->addNotifier(notifier);
105 mutex_->unlock();
106 }
107
110 void removeNotifier(NotifierAbstraction *const notifier) {
111 mutex_->lock();
112 concreteSlot_->removeNotifier(notifier);
113 mutex_->unlock();
114 }
115
117 virtual void wakeUp() = 0;
118
121 [[nodiscard]] bool hasNotifierConnected() const {
122 return concreteSlot_->hasNotifierConnected();
123 };
124
126 void lockSlotMutex() { mutex_->lock(); }
127
129 void unlockSlotMutex() { mutex_->unlock(); }
130
131 protected:
134 [[nodiscard]] std::shared_ptr<std::mutex> const &mutex() const { return mutex_; }
135
138 [[nodiscard]] std::shared_ptr<std::condition_variable> const &slotConditionVariable() const {
140 }
141
145 this->mutex_ = copyableCore->mutex_;
147 }
148};
149}
150}
151}
152
153#endif //HEDGEHOG_SLOT_ABSTRACTION_H
Hedgehog main namespace.
Core abstraction to notify slots.
Core's abstraction to receive a signal.
std::set< SlotAbstraction * > & slots()
Accessor to slots.
std::shared_ptr< std::mutex > mutex_
Slot mutex.
std::set< NotifierAbstraction * > const & connectedNotifiers() const
Accessor to the NotifierAbstraction attached to this slot, protected with mutex.
std::shared_ptr< std::condition_variable > slotConditionVariable_
Slot condition variable.
std::set< SlotAbstraction * > const & slots() const
Const accessor to slots.
std::shared_ptr< std::mutex > const & mutex() const
Protected accessor to mutex.
void removeNotifier(NotifierAbstraction *const notifier)
Remove a NotifierAbstraction to this slot.
std::shared_ptr< implementor::ImplementorSlot > concreteSlot_
Concrete implementation of the slot.
bool hasNotifierConnected() const
Test if there is at least one notifier connected.
void addNotifier(NotifierAbstraction *const notifier)
Add a NotifierAbstraction to this slot.
virtual void wakeUp()=0
Wake up mechanism, called to notify the std::condition_variable.
SlotAbstraction(std::shared_ptr< implementor::ImplementorSlot > concreteSlot)
Constructor using a concrete slot implementation.
void copyInnerStructure(SlotAbstraction *copyableCore)
Copy the inner structure of copyableCore into this.
virtual ~SlotAbstraction()=default
Default destructor.
std::shared_ptr< std::condition_variable > const & slotConditionVariable() const
Protected accessor to condition variable.