Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafDataFieldAccessor.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) Gaute Lindkvist
5 //
6 // GNU Lesser General Public License Usage
7 // This library is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation; either version 2.1 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
17 // for more details.
18 //
19 #pragma once
20 
21 #include <memory>
22 #include <optional>
23 
24 namespace caffa
25 {
31 {
32 public:
33  virtual ~DataFieldAccessorInterface() = default;
34 };
35 
41 template <class DataType>
43 {
44 public:
50  virtual std::unique_ptr<DataFieldAccessor<DataType>> clone() const = 0;
51 
57  virtual DataType value() = 0;
58 
65  virtual void setValue( const DataType& value ) = 0;
66 
71  virtual bool hasSetter() const = 0;
72 
77  virtual bool hasGetter() const = 0;
78 };
79 
85 template <class DataType>
87 {
88 public:
94 
100  DataFieldDirectStorageAccessor( const DataType& value )
101  : m_value( value )
102 
103  {
104  }
105 
106  std::unique_ptr<DataFieldAccessor<DataType>> clone() const override
107  {
108  return std::make_unique<DataFieldDirectStorageAccessor<DataType>>( m_value );
109  }
110 
111  DataType value() override { return m_value; };
112 
113  void setValue( const DataType& value ) override { m_value = value; }
114 
115  bool hasGetter() const override { return true; }
116  bool hasSetter() const override { return true; }
117 
118 private:
119  DataType m_value;
120 };
121 
122 } // namespace caffa
std::unique_ptr< DataFieldAccessor< DataType > > clone() const override
Clone the accessor using polymorphism.
Definition: cafDataFieldAccessor.h:106
DataType value() override
Get the field value.
Definition: cafDataFieldAccessor.h:111
Abstract but typed data field accessor. Inherit to create different storage mechanisms.
Definition: cafDataFieldAccessor.h:42
bool hasSetter() const override
Definition: cafDataFieldAccessor.h:116
DataFieldDirectStorageAccessor(const DataType &value)
Construct a new Data Field Direct Storage Accessor object with a default value.
Definition: cafDataFieldAccessor.h:100
bool hasGetter() const override
Definition: cafDataFieldAccessor.h:115
Basic non-typed interface which exists only to allow non-typed pointers.
Definition: cafDataFieldAccessor.h:30
void setValue(const DataType &value) override
Set the value with the accessor. Will throw a std::runtime_exception if the accessor has limits and t...
Definition: cafDataFieldAccessor.h:113
Main Caffa namespace.
Definition: cafApplication.h:30
Direct storage accessor, which stores data values in local memory.
Definition: cafDataFieldAccessor.h:86