-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
#ifndef VARIABLEACCESS_HPP | ||
#define VARIABLEACCESS_HPP | ||
|
||
#include <functional> | ||
#include <optional> | ||
#include <utility> | ||
|
||
namespace fmu4cpp { | ||
|
||
template<typename T> | ||
struct VariableAccess { | ||
virtual T get() = 0; | ||
virtual void set(T value) = 0; | ||
|
||
virtual ~VariableAccess() = default; | ||
}; | ||
|
||
template<typename T> | ||
class PtrAccess final : public VariableAccess<T> { | ||
|
||
public: | ||
explicit PtrAccess(T *ptr) : ptr_(ptr) {} | ||
|
||
T get() override { | ||
return *ptr_; | ||
} | ||
|
||
void set(T value) override { | ||
*ptr_ = value; | ||
} | ||
|
||
private: | ||
T *ptr_; | ||
}; | ||
|
||
template<typename T> | ||
class LambdaAccess final : public VariableAccess<T> { | ||
|
||
public: | ||
LambdaAccess(std::function<T()> getter, std::optional<std::function<void(T)>> setter) | ||
: getter_(std::move(getter)), | ||
setter_(std::move(setter)) {} | ||
|
||
T get() override { | ||
return getter_(); | ||
} | ||
|
||
void set(T value) override { | ||
if (setter_) { | ||
setter_->operator()(value); | ||
} | ||
} | ||
|
||
private: | ||
std::function<T()> getter_; | ||
std::optional<std::function<void(T)>> setter_; | ||
}; | ||
|
||
}// namespace fmu4cpp | ||
|
||
#endif//VARIABLEACCESS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters