Newer
Older
Martin Christoph Hierholzer
committed
* ArrayAccessor.h
*
* Created on: Jun 07, 2016
* Author: Martin Hierholzer
*/
#ifndef CHIMERATK_ARRAY_ACCESSOR_H
#define CHIMERATK_ARRAY_ACCESSOR_H
#include <string>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/thread.hpp>
Martin Christoph Hierholzer
committed
#include <mtca4u/OneDRegisterAccessor.h>
#include "VariableNetworkNode.h"
Martin Christoph Hierholzer
committed
/** Accessor for array variables (i.e. vectors). Note for users: Use the convenience classes
Martin Christoph Hierholzer
committed
* ArrayPollInput, ArrayPushInput, ArrayOutput instead of this class directly. */
Martin Christoph Hierholzer
committed
class ArrayAccessor : public mtca4u::OneDRegisterAccessor<UserType> {
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
/** Change meta data (name, unit, description and optionally tags). This function may only be used on
* Application-type nodes. If the optional argument tags is omitted, the tags will not be changed. To clear the
* tags, an empty set can be passed. */
Martin Christoph Hierholzer
committed
void setMetaData(const std::string &name, const std::string &unit, const std::string &description) {
node.setMetaData(name, unit, description);
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
void setMetaData(const std::string &name, const std::string &unit, const std::string &description,
const std::unordered_set<std::string> &tags) {
node.setMetaData(name, unit, description, tags);
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
/** Convert into VariableNetworkNode */
operator VariableNetworkNode() {
return node;
Martin Christoph Hierholzer
committed
/** Connect with other node */
VariableNetworkNode operator>>(const VariableNetworkNode &otherNode) {
return node >> otherNode;
void replace(ArrayAccessor<UserType> &&other) {
operator=(std::move(other));
}
/** Move constructor */
ArrayAccessor(ArrayAccessor<UserType> &&other) {
operator=(std::move(other));
Martin Christoph Hierholzer
committed
}
void replace(const mtca4u::NDRegisterAccessorBridge<UserType> &newAccessor) = delete;
Martin Christoph Hierholzer
committed
/** Move-assignment operator as an alternative for replace where applicable. This is needed to allow late
* initialisation of ApplicationModules using ArrayAccessors */
ArrayAccessor<UserType>& operator=(ArrayAccessor<UserType> &&other) {
assert(this->_impl == nullptr);
node = other.node; // just copies the pointer, but other will be destroyed right after this move constructor
other.node = VariableNetworkNode();
node.setAppAccessorPointer(this);
// Note: the accessor is registered by the VariableNetworkNode, so we don't have to re-register. Setting the
// owner of the other accessor to nullptr will prevent unregistering the accessor.
Martin Christoph Hierholzer
committed
return *this;
}
Martin Christoph Hierholzer
committed
using mtca4u::OneDRegisterAccessor<UserType>::operator=;
Martin Christoph Hierholzer
committed
~ArrayAccessor() {
if(getOwner() != nullptr) getOwner()->unregisterAccessor(node);
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
/** Add a tag. Valid names for tags only contain alpha-numeric characters (i.e. no spaces and no special
* characters). */
void addTag(const std::string &tag) {
node.addTag(tag);
}
void read() {
Profiler::stopMeasurement();
mtca4u::OneDRegisterAccessor<UserType>::read();
Profiler::startMeasurement();
}
EntityOwner* getOwner() const { return node.getOwningModule(); }
Martin Christoph Hierholzer
committed
protected:
Martin Christoph Hierholzer
committed
ArrayAccessor(Module *owner, const std::string &name, VariableDirection direction, std::string unit,
Martin Christoph Hierholzer
committed
size_t nElements, UpdateMode mode, const std::string &description,
const std::unordered_set<std::string> &tags={})
: node(owner, this, name, direction, unit, nElements, mode, description, &typeid(UserType), tags)
Martin Christoph Hierholzer
committed
{
owner->registerAccessor(*this);
}
/** Default constructor creates a dysfunctional accessor (to be assigned with a real accessor later) */
Martin Christoph Hierholzer
committed
ArrayAccessor() {}
Martin Christoph Hierholzer
committed
VariableNetworkNode node;
Martin Christoph Hierholzer
committed
/** Convenience class for input array accessors with UpdateMode::push */
template< typename UserType >
struct ArrayPushInput : public ArrayAccessor<UserType> {
Martin Christoph Hierholzer
committed
ArrayPushInput(Module *owner, const std::string &name, std::string unit, size_t nElements,
const std::string &description, const std::unordered_set<std::string> &tags={})
: ArrayAccessor<UserType>(owner, name, VariableDirection::consuming, unit, nElements, UpdateMode::push,
description, tags)
Martin Christoph Hierholzer
committed
{}
ArrayPushInput() : ArrayAccessor<UserType>() {}
using ArrayAccessor<UserType>::operator=;
};
/** Convenience class for input array accessors with UpdateMode::poll */
template< typename UserType >
struct ArrayPollInput : public ArrayAccessor<UserType> {
Martin Christoph Hierholzer
committed
ArrayPollInput(Module *owner, const std::string &name, std::string unit, size_t nElements,
const std::string &description, const std::unordered_set<std::string> &tags={})
: ArrayAccessor<UserType>(owner, name, VariableDirection::consuming, unit, nElements, UpdateMode::poll,
description, tags)
Martin Christoph Hierholzer
committed
{}
ArrayPollInput() : ArrayAccessor<UserType>() {}
Martin Christoph Hierholzer
committed
void doReadTransfer() override { this->doReadTransferLatest(); }
void read() { this->readLatest(); }
Martin Christoph Hierholzer
committed
using ArrayAccessor<UserType>::operator=;
};
/** Convenience class for output array accessors (always UpdateMode::push) */
template< typename UserType >
struct ArrayOutput : public ArrayAccessor<UserType> {
Martin Christoph Hierholzer
committed
ArrayOutput(Module *owner, const std::string &name, std::string unit, size_t nElements,
const std::string &description, const std::unordered_set<std::string> &tags={})
: ArrayAccessor<UserType>(owner, name, VariableDirection::feeding, unit, nElements, UpdateMode::push,
description, tags)
Martin Christoph Hierholzer
committed
{}
ArrayOutput() : ArrayAccessor<UserType>() {}
using ArrayAccessor<UserType>::operator=;
};
} /* namespace ChimeraTK */
#endif /* CHIMERATK_ARRAY_ACCESSOR_H */