Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafFieldInitHelper.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 2020- Kontur AS
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 // ##################################################################################################
20 #pragma once
21 
22 #include "cafDataFieldAccessor.h"
23 
24 #include "cafField.h"
25 #include "cafFieldProxyAccessor.h"
26 #include "cafFieldScriptingCapability.h"
27 #include "cafFieldValidator.h"
28 
29 #include <memory>
30 
31 namespace caffa
32 {
33 template <typename T>
34 concept DerivesFromFieldHandle = std::is_base_of<FieldHandle, T>::value;
35 
40 template <DerivesFromFieldHandle FieldType>
42 {
43 public:
44  using GetMethod = std::function<typename FieldType::FieldDataType()>;
45  using SetMethod = std::function<void( const typename FieldType::FieldDataType& )>;
46 
47  FieldInitHelper( FieldType& field, const std::string& keyword )
48  : m_field( field )
49  , m_keyword( keyword )
50  {
51  }
52 
53  FieldInitHelper& withDefault( const typename FieldType::FieldDataType& defaultValue )
54  {
55  m_field.setDefaultValue( defaultValue );
56  m_field = defaultValue;
57  return *this;
58  }
59 
60  FieldInitHelper& withScripting( bool readable = true, bool writable = true )
61  {
62  m_field.addCapability( std::make_unique<FieldScriptingCapability>( readable, writable ) );
63  return *this;
64  }
65 
66  FieldInitHelper& withAccessor( std::unique_ptr<DataFieldAccessor<typename FieldType::FieldDataType>> accessor )
67  {
68  m_field.setAccessor( std::move( accessor ) );
69  return *this;
70  }
71 
72  FieldInitHelper& withProxyGetAccessor( GetMethod getMethod )
73  {
74  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
75  accessor->registerGetMethod( getMethod );
76  return withAccessor( std::move( accessor ) );
77  }
78 
79  FieldInitHelper& withProxySetAccessor( SetMethod setMethod )
80  {
81  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
82  accessor->registerSetMethod( setMethod );
83  return withAccessor( std::move( accessor ) );
84  }
85 
86  FieldInitHelper& withProxyGetSetAccessor( GetMethod getMethod, SetMethod setMethod )
87  {
88  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
89  accessor->registerGetMethod( getMethod );
90  accessor->registerSetMethod( setMethod );
91  return withAccessor( std::move( accessor ) );
92  }
93 
94  FieldInitHelper& withValidator( std::unique_ptr<FieldValidator<typename FieldType::FieldDataType>> validator )
95  {
96  m_field.addValidator( std::move( validator ) );
97  return *this;
98  }
99 
100  FieldInitHelper& withDoc( const std::string& documentation )
101  {
102  m_field.setDocumentation( documentation );
103  return *this;
104  }
105 
106  FieldInitHelper& markDeprecated()
107  {
108  m_field.markDeprecated();
109  return *this;
110  }
111 
112 private:
113  FieldInitHelper() = delete;
114  FieldInitHelper( const FieldInitHelper& ) = delete;
115  FieldInitHelper( FieldInitHelper&& ) = delete;
116 
117  FieldInitHelper& operator=( const FieldInitHelper& ) = delete;
118  FieldInitHelper& operator=( FieldInitHelper&& ) = delete;
119 
120  FieldType& m_field;
121  const std::string& m_keyword;
122 };
123 } // namespace caffa
Used to validate the value of data fields Implementations need the the validate method as well as rea...
Definition: cafFieldValidator.h:94
Definition: cafFieldInitHelper.h:41
Abstract but typed data field accessor. Inherit to create different storage mechanisms.
Definition: cafDataFieldAccessor.h:42
Main Caffa namespace.
Definition: cafApplication.h:30