Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafObject.h
1 // ##################################################################################################
2 //
3 // Custom Visualization Core library
4 // Copyright (C) 2011-2013 Ceetron AS
5 // Copyright (C) 2013-2020 Ceetron Solutions AS
6 // Copyright (C) 2020- Kontur AS
7 //
8 // This library may be used under the terms of either the GNU General Public License or
9 // the GNU Lesser General Public License as follows:
10 //
11 // GNU General Public License Usage
12 // This library is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.
20 //
21 // See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
22 // for more details.
23 //
24 // GNU Lesser General Public License Usage
25 // This library is free software; you can redistribute it and/or modify
26 // it under the terms of the GNU Lesser General Public License as published by
27 // the Free Software Foundation; either version 2.1 of the License, or
28 // (at your option) any later version.
29 //
30 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
31 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
32 // FITNESS FOR A PARTICULAR PURPOSE.
33 //
34 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
35 // for more details.
36 //
37 // ##################################################################################################
38 
39 #pragma once
40 
41 #include "cafDataFieldAccessor.h"
42 #include "cafField.h"
43 #include "cafFieldDocumentationCapability.h"
44 #include "cafFieldJsonCapability.h"
45 #include "cafFieldJsonCapabilitySpecializations.h"
46 #include "cafFieldProxyAccessor.h"
47 #include "cafFieldScriptingCapability.h"
48 #include "cafFieldValidator.h"
49 #include "cafObjectCapability.h"
50 #include "cafObjectHandle.h"
51 #include "cafObjectMacros.h"
52 
53 #include <set>
54 
55 namespace caffa
56 {
57 class UiEditorAttribute;
58 class UiTreeOrdering;
59 class ObjectCapability;
60 class ObjectFactory;
61 
62 template <typename T>
63 concept DerivesFromFieldHandle = std::is_base_of<FieldHandle, T>::value;
64 
69 template <DerivesFromFieldHandle FieldType>
71 {
72 public:
73  using GetMethod = std::function<typename FieldType::FieldDataType()>;
74  using SetMethod = std::function<void( const typename FieldType::FieldDataType& )>;
75 
76  FieldInitHelper( FieldType& field, const std::string& keyword )
77  : m_field( field )
78  , m_keyword( keyword )
79  {
80  }
81 
82  FieldInitHelper& withDefault( const typename FieldType::FieldDataType& defaultValue )
83  {
84  m_field.setDefaultValue( defaultValue );
85  m_field = defaultValue;
86  return *this;
87  }
88 
89  FieldInitHelper& withScripting( const std::string& scriptFieldKeyword, bool readable = true, bool writable = true )
90  {
91  m_field.addCapability(
92  std::make_unique<FieldScriptingCapability>( scriptFieldKeyword.empty() ? m_keyword : scriptFieldKeyword,
93  readable,
94  writable ) );
95  return *this;
96  }
97 
98  FieldInitHelper& withScripting( bool readable = true, bool writable = true )
99  {
100  m_field.addCapability( std::make_unique<FieldScriptingCapability>( m_keyword, readable, writable ) );
101  return *this;
102  }
103 
104  FieldInitHelper& withAccessor( std::unique_ptr<DataFieldAccessor<typename FieldType::FieldDataType>> accessor )
105  {
106  m_field.setAccessor( std::move( accessor ) );
107  return *this;
108  }
109 
110  FieldInitHelper& withProxyGetAccessor( GetMethod getMethod )
111  {
112  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
113  accessor->registerGetMethod( getMethod );
114  return withAccessor( std::move( accessor ) );
115  }
116 
117  FieldInitHelper& withProxySetAccessor( SetMethod setMethod )
118  {
119  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
120  accessor->registerSetMethod( setMethod );
121  return withAccessor( std::move( accessor ) );
122  }
123 
124  FieldInitHelper& withProxyGetSetAccessor( GetMethod getMethod, SetMethod setMethod )
125  {
126  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
127  accessor->registerGetMethod( getMethod );
128  accessor->registerSetMethod( setMethod );
129  return withAccessor( std::move( accessor ) );
130  }
131 
132  FieldInitHelper& withValidator( std::unique_ptr<FieldValidator<typename FieldType::FieldDataType>> validator )
133  {
134  m_field.addValidator( std::move( validator ) );
135  return *this;
136  }
137 
138  FieldInitHelper& withDoc( const std::string& documentation )
139  {
140  auto doc = std::make_unique<caffa::FieldDocumentationCapability>( documentation );
141  m_field.addCapability( std::move( doc ) );
142  return *this;
143  }
144 
145  FieldInitHelper& markDeprecated()
146  {
147  m_field.markDeprecated();
148  return *this;
149  }
150 
151 private:
152  FieldInitHelper() = delete;
153  FieldInitHelper( const FieldInitHelper& ) = delete;
154  FieldInitHelper( FieldInitHelper&& ) = delete;
155 
156  FieldInitHelper& operator=( const FieldInitHelper& ) = delete;
157  FieldInitHelper& operator=( FieldInitHelper&& ) = delete;
158 
159  FieldType& m_field;
160  const std::string& m_keyword;
161 };
162 
163 class Object : public ObjectHandle
164 {
165 public:
166  CAFFA_HEADER_INIT( Object, ObjectHandle )
167 
168  Object( bool generateUuid = true );
169  ~Object() noexcept override;
170 
176  template <typename FieldType>
177  FieldInitHelper<FieldType> initField( FieldType& field, const std::string& keyword )
178  {
179  AddIoCapabilityToField( &field );
180  addField( &field, keyword );
181  return FieldInitHelper( field, keyword );
182  }
183 
192  template <typename MethodType, typename CallbackT>
193  void initMethod( MethodType& method,
194  const std::string& keyword,
195  const std::vector<std::string>& argumentNames,
196  CallbackT&& callback,
197  MethodHandle::Type type = MethodHandle::Type::READ_WRITE )
198  {
199  addMethod( &method, keyword, type );
200  method.setCallback( callback );
201  method.setArgumentNames( argumentNames );
202  }
203 
212  template <typename MethodType, typename CallbackT>
213  void initMethodWithSession( MethodType& method,
214  const std::string& keyword,
215  const std::vector<std::string>& argumentNames,
216  CallbackT&& callback,
217  MethodHandle::Type type = MethodHandle::Type::READ_WRITE )
218  {
219  addMethod( &method, keyword, type );
220  method.setCallbackWithSession( callback );
221  method.setArgumentNames( argumentNames );
222  }
223 
233  template <typename MethodType, typename CallbackT>
234  void initMethodWithDoc( MethodType& method,
235  const std::string& keyword,
236  const std::vector<std::string>& argumentNames,
237  const std::string& documentation,
238  CallbackT&& callback,
239  MethodHandle::Type type = MethodHandle::Type::READ_WRITE )
240  {
241  addMethod( &method, keyword, type );
242  method.setCallback( callback );
243  method.setArgumentNames( argumentNames );
244  method.setDocumentation( documentation );
245  }
255  template <typename MethodType, typename CallbackT>
256  void initMethodWithSessionAndDoc( MethodType& method,
257  const std::string& keyword,
258  const std::vector<std::string>& argumentNames,
259  const std::string& documentation,
260  CallbackT&& callback,
261  MethodHandle::Type type = MethodHandle::Type::READ_WRITE )
262  {
263  addMethod( &method, keyword, type );
264  method.setCallbackWithSession( callback );
265  method.setArgumentNames( argumentNames );
266  method.setDocumentation( documentation );
267  }
268 
269  std::string uuid() const override;
270  void setUuid( const std::string& uuid ) override;
271 
278  ObjectHandle::Ptr deepClone( caffa::ObjectFactory* optionalObjectFactory = nullptr ) const override;
279 
287  template <typename DerivedClass>
288  std::shared_ptr<DerivedClass> typedDeepClone( caffa::ObjectFactory* optionalObjectFactory = nullptr ) const
289  {
290  return std::dynamic_pointer_cast<DerivedClass>( deepClone( optionalObjectFactory ) );
291  }
292 
298  bool readFromJsonFile( const std::string& filePath );
299 
305  bool writeToJsonFile( const std::string& filePath ) const;
306 
307 private:
309 };
310 
311 } // End of namespace caffa
void initMethodWithSession(MethodType &method, const std::string &keyword, const std::vector< std::string > &argumentNames, CallbackT &&callback, MethodHandle::Type type=MethodHandle::Type::READ_WRITE)
Definition: cafObject.h:213
Used to validate the value of data fields Implementations need the the validate method as well as rea...
Definition: cafFieldValidator.h:103
Definition: cafObject.h:70
void initMethodWithDoc(MethodType &method, const std::string &keyword, const std::vector< std::string > &argumentNames, const std::string &documentation, CallbackT &&callback, MethodHandle::Type type=MethodHandle::Type::READ_WRITE)
Definition: cafObject.h:234
Abstract but typed data field accessor. Inherit to create different storage mechanisms.
Definition: cafDataFieldAccessor.h:42
Definition: cafObjectFactory.h:38
Definition: cafObject.h:163
std::shared_ptr< DerivedClass > typedDeepClone(caffa::ObjectFactory *optionalObjectFactory=nullptr) const
Deep clone and cast to the typed class using an optional object factory.
Definition: cafObject.h:288
void initMethod(MethodType &method, const std::string &keyword, const std::vector< std::string > &argumentNames, CallbackT &&callback, MethodHandle::Type type=MethodHandle::Type::READ_WRITE)
Definition: cafObject.h:193
Definition: cafObjectHandle.h:53
void initMethodWithSessionAndDoc(MethodType &method, const std::string &keyword, const std::vector< std::string > &argumentNames, const std::string &documentation, CallbackT &&callback, MethodHandle::Type type=MethodHandle::Type::READ_WRITE)
Definition: cafObject.h:256
FieldInitHelper< FieldType > initField(FieldType &field, const std::string &keyword)
Definition: cafObject.h:177
Main Caffa namespace.
Definition: cafAppEnum.h:38