ChimeraTK-DeviceAccess  03.18.00
LNMMathPlugin.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
5 #include "LNMAccessorPlugin.h"
6 
7 #include <boost/make_shared.hpp>
8 
9 #include <utility>
10 
11 namespace ChimeraTK::LNMBackend {
12 
13  // forward declaration needed for MathPlugin
14  class MathPluginFormulaHelper;
15 
17  class MathPlugin : public AccessorPlugin<MathPlugin> {
18  public:
19  MathPlugin(const LNMBackendRegisterInfo& info, size_t pluginIndex, std::map<std::string, std::string> parameters);
20 
21  void doRegisterInfoUpdate() override;
23 
24  template<typename UserType, typename TargetType>
25  boost::shared_ptr<NDRegisterAccessor<UserType>> decorateAccessor(
26  boost::shared_ptr<LogicalNameMappingBackend>& backend,
27  boost::shared_ptr<NDRegisterAccessor<TargetType>>& target, const UndecoratedParams& accessorParams);
28 
29  void openHook(const boost::shared_ptr<LogicalNameMappingBackend>& backend) override;
30  void postParsingHook(const boost::shared_ptr<const LogicalNameMappingBackend>& backend) override;
31  void closeHook() override;
32  void exceptionHook() override;
33 
36  boost::shared_ptr<MathPluginFormulaHelper> getFormulaHelper(boost::shared_ptr<LogicalNameMappingBackend> backend);
38 
39  bool _isWrite{false};
40 
41  std::map<std::string, std::string> _parameters;
42  std::string _formula; // extracted from _parameters
43  bool _enablePushParameters{false}; // extracted from _parameters
44  bool _hasPushParameter{false}; // only relevant if _isWrite
45 
46  // only used if _hasPushParameter == true
47  // The _writeMutex has two functions:
48  // - It protects resources which are shared by main accesor and parameter accessors
49  // - It is held while an accessor is doing the preWrite/writeTransfer/postWrite sequence.
50  // If the other thread would be able to do a transfer bwetween the preWrite and the actual transfer this
51  // would lead to wrong results (although formally the code is thread safe)
52  // Use a recursive mutex because it is allowed to call preWrite() multiple times before executing
53  // the writeTransfer, and the mutex is accquired in preWrite() and release in only in postWrite().
54  std::recursive_mutex _writeMutex;
57  std::vector<double> _lastMainValue;
59  static thread_local int64_t _writeLockCounter;
60 
61  bool _creatingFormulaHelper{false}; // a flag to prevent recursion
62 
63  private:
64  // store weak pointer because plugin lifetime should not extend MathPluginFormulaHelper lifetime
65  boost::weak_ptr<MathPluginFormulaHelper> _h;
66  };
67 
68 } // namespace ChimeraTK::LNMBackend
ChimeraTK::LNMBackend::MathPlugin::_formula
std::string _formula
Definition: LNMMathPlugin.h:42
ChimeraTK::DataType::float64
@ float64
Double precision float.
Definition: SupportedUserTypes.h:616
ChimeraTK::LNMBackend::MathPlugin::_parameters
std::map< std::string, std::string > _parameters
Definition: LNMMathPlugin.h:41
ChimeraTK::LNMBackend::AccessorPlugin
Base class for plugins that modify the behaviour of accessors in the logical name mapping backend.
Definition: LNMAccessorPlugin.h:104
ChimeraTK::LNMBackend::MathPlugin::doRegisterInfoUpdate
void doRegisterInfoUpdate() override
Implementation of the plugin specific register information update.
Definition: LNMMathPlugin.cc:37
ChimeraTK::LNMBackend
Definition: LNMMathPluginFormulaHelper.h:16
ChimeraTK::LNMBackend::MathPlugin::_allParametersWrittenAfterOpen
bool _allParametersWrittenAfterOpen
Definition: LNMMathPlugin.h:56
ChimeraTK::LNMBackend::MathPlugin
Math Plugin: Apply mathematical formula to register's data.
Definition: LNMMathPlugin.h:17
ChimeraTK::LNMBackend::MathPlugin::decorateAccessor
boost::shared_ptr< NDRegisterAccessor< UserType > > decorateAccessor(boost::shared_ptr< LogicalNameMappingBackend > &backend, boost::shared_ptr< NDRegisterAccessor< TargetType >> &target, const UndecoratedParams &accessorParams)
Definition: LNMMathPlugin.cc:428
ChimeraTK::LNMBackend::MathPlugin::_hasPushParameter
bool _hasPushParameter
Definition: LNMMathPlugin.h:44
ChimeraTK::LNMBackend::UndecoratedParams
Helper struct to hold extra parameters needed by some plugins, used in decorateAccessor()
Definition: LNMAccessorPlugin.h:13
ChimeraTK::LNMBackend::MathPlugin::closeHook
void closeHook() override
Hook called when the backend is closed, at the beginning of the close() function when the device is s...
Definition: LNMMathPlugin.cc:183
ChimeraTK::LNMBackend::MathPlugin::getFormulaHelper
boost::shared_ptr< MathPluginFormulaHelper > getFormulaHelper(boost::shared_ptr< LogicalNameMappingBackend > backend)
if not yet existing, creates the instance and returns it if already existing, backend ptr may be empt...
Definition: LNMMathPlugin.cc:113
ChimeraTK::LNMBackend::MathPlugin::_creatingFormulaHelper
bool _creatingFormulaHelper
Definition: LNMMathPlugin.h:61
ChimeraTK::DataValidity
DataValidity
The current state of the data.
Definition: TransferElement.h:41
ChimeraTK::LNMBackend::MathPlugin::_writeLockCounter
static thread_local int64_t _writeLockCounter
Definition: LNMMathPlugin.h:59
ChimeraTK::LNMBackendRegisterInfo
RegisterInfo structure for the LogicalNameMappingBackend.
Definition: LNMBackendRegisterInfo.h:22
ChimeraTK::LNMBackend::MathPlugin::info
LNMBackendRegisterInfo * info()
Definition: LNMMathPlugin.h:37
ChimeraTK::LNMBackend::AccessorPluginBase::_info
LNMBackendRegisterInfo _info
RegisterInfo describing the the target register for which this plugin instance should work.
Definition: LNMAccessorPlugin.h:93
ChimeraTK::LNMBackend::MathPlugin::_lastMainValue
std::vector< double > _lastMainValue
Definition: LNMMathPlugin.h:57
ChimeraTK::LNMBackend::MathPlugin::_writeMutex
std::recursive_mutex _writeMutex
Definition: LNMMathPlugin.h:54
ChimeraTK::DataType
A class to describe which of the supported data types is used.
Definition: SupportedUserTypes.h:599
ChimeraTK::LNMBackend::MathPlugin::_isWrite
bool _isWrite
Definition: LNMMathPlugin.h:39
ChimeraTK::LNMBackend::MathPlugin::MathPlugin
MathPlugin(const LNMBackendRegisterInfo &info, size_t pluginIndex, std::map< std::string, std::string > parameters)
Definition: LNMMathPlugin.cc:20
ChimeraTK::LNMBackend::MathPlugin::_lastMainValidity
ChimeraTK::DataValidity _lastMainValidity
Definition: LNMMathPlugin.h:58
ChimeraTK::LNMBackend::MathPlugin::openHook
void openHook(const boost::shared_ptr< LogicalNameMappingBackend > &backend) override
Hook called when the backend is opened, at the end of the open() function after all backend work has ...
Definition: LNMMathPlugin.cc:59
ChimeraTK::LNMBackend::MathPlugin::exceptionHook
void exceptionHook() override
Hook called when an exception is reported to the the backend via setException(), after the backend ha...
Definition: LNMMathPlugin.cc:187
ChimeraTK::LNMBackend::MathPlugin::_enablePushParameters
bool _enablePushParameters
Definition: LNMMathPlugin.h:43
ChimeraTK::LNMBackend::MathPlugin::_mainValueWrittenAfterOpen
bool _mainValueWrittenAfterOpen
Definition: LNMMathPlugin.h:55
ChimeraTK::LNMBackend::MathPlugin::postParsingHook
void postParsingHook(const boost::shared_ptr< const LogicalNameMappingBackend > &backend) override
Definition: LNMMathPlugin.cc:85
ChimeraTK::LNMBackend::MathPlugin::getTargetDataType
DataType getTargetDataType(DataType) const override
Return the data type for which the target accessor shall be obtained.
Definition: LNMMathPlugin.h:22
ChimeraTK::NDRegisterAccessor
N-dimensional register accessor.
Definition: ForwardDeclarations.h:17
LNMAccessorPlugin.h