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 
66 template <typename FieldType>
68 {
69 public:
70  using GetMethod = std::function<typename FieldType::FieldDataType()>;
71  using SetMethod = std::function<void( const typename FieldType::FieldDataType& )>;
72 
73  FieldInitHelper( FieldType& field, const std::string& keyword )
74  : m_field( field )
75  , m_keyword( keyword )
76  {
77  }
78 
79  FieldInitHelper& withDefault( const typename FieldType::FieldDataType& defaultValue )
80  {
81  m_field.setDefaultValue( defaultValue );
82  m_field = defaultValue;
83  return *this;
84  }
85 
86  FieldInitHelper& withScripting( const std::string& scriptFieldKeyword, bool readable = true, bool writable = true )
87  {
88  m_field.addCapability(
89  std::make_unique<FieldScriptingCapability>( scriptFieldKeyword.empty() ? m_keyword : scriptFieldKeyword,
90  readable,
91  writable ) );
92  return *this;
93  }
94 
95  FieldInitHelper& withScripting( bool readable = true, bool writable = true )
96  {
97  m_field.addCapability( std::make_unique<FieldScriptingCapability>( m_keyword, readable, writable ) );
98  return *this;
99  }
100 
101  FieldInitHelper& withAccessor( std::unique_ptr<DataFieldAccessor<typename FieldType::FieldDataType>> accessor )
102  {
103  m_field.setAccessor( std::move( accessor ) );
104  return *this;
105  }
106 
107  FieldInitHelper& withProxyGetAccessor( GetMethod getMethod )
108  {
109  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
110  accessor->registerGetMethod( getMethod );
111  return withAccessor( std::move( accessor ) );
112  }
113 
114  FieldInitHelper& withProxySetAccessor( SetMethod setMethod )
115  {
116  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
117  accessor->registerSetMethod( setMethod );
118  return withAccessor( std::move( accessor ) );
119  }
120 
121  FieldInitHelper& withProxyGetSetAccessor( GetMethod getMethod, SetMethod setMethod )
122  {
123  auto accessor = std::make_unique<caffa::FieldProxyAccessor<typename FieldType::FieldDataType>>();
124  accessor->registerGetMethod( getMethod );
125  accessor->registerSetMethod( setMethod );
126  return withAccessor( std::move( accessor ) );
127  }
128 
129  FieldInitHelper& withValidator( std::unique_ptr<FieldValidator<typename FieldType::FieldDataType>> validator )
130  {
131  m_field.addValidator( std::move( validator ) );
132  return *this;
133  }
134 
135  FieldInitHelper& withDoc( const std::string& documentation )
136  {
137  auto doc = std::make_unique<caffa::FieldDocumentationCapability>( documentation );
138  m_field.addCapability( std::move( doc ) );
139  return *this;
140  }
141 
142 private:
143  FieldInitHelper() = delete;
144  FieldInitHelper( const FieldInitHelper& ) = delete;
145  FieldInitHelper( FieldInitHelper&& ) = delete;
146 
147  FieldInitHelper& operator=( const FieldInitHelper& ) = delete;
148  FieldInitHelper& operator=( FieldInitHelper&& ) = delete;
149 
150  FieldType& m_field;
151  const std::string& m_keyword;
152 };
153 
154 class Object : public ObjectHandle
155 {
156 public:
157  CAFFA_HEADER_INIT( Object, ObjectHandle )
158 
159  Object( bool generateUuid = true );
160  ~Object() noexcept override;
161 
167  template <typename FieldType>
168  FieldInitHelper<FieldType> initField( FieldType& field, const std::string& keyword )
169  {
170  AddIoCapabilityToField( &field );
171  addField( &field, keyword );
172  return FieldInitHelper( field, keyword );
173  }
174 
183  template <typename MethodType, typename CallbackT>
184  void initMethod( MethodType& method,
185  const std::string& keyword,
186  const std::vector<std::string>& argumentNames,
187  CallbackT&& callback,
188  MethodHandle::Type type = MethodHandle::Type::READ_WRITE )
189  {
190  addMethod( &method, keyword, type );
191  method.setCallback( callback );
192  method.setArgumentNames( argumentNames );
193  }
194 
204  template <typename MethodType, typename CallbackT>
205  void initMethodWithDoc( MethodType& method,
206  const std::string& keyword,
207  const std::vector<std::string>& argumentNames,
208  const std::string& documentation,
209  CallbackT&& callback,
210  MethodHandle::Type type = MethodHandle::Type::READ_WRITE )
211  {
212  addMethod( &method, keyword, type );
213  method.setCallback( callback );
214  method.setArgumentNames( argumentNames );
215  method.setDocumentation( documentation );
216  }
217 
218  std::string uuid() const override;
219  void setUuid( const std::string& uuid ) override;
220 
227  ObjectHandle::Ptr deepClone( caffa::ObjectFactory* optionalObjectFactory = nullptr ) const override;
228 
236  template <typename DerivedClass>
237  std::shared_ptr<DerivedClass> typedDeepClone( caffa::ObjectFactory* optionalObjectFactory = nullptr ) const
238  {
239  return std::dynamic_pointer_cast<DerivedClass>( deepClone( optionalObjectFactory ) );
240  }
241 
247  bool readFromJsonFile( const std::string& filePath );
248 
254  bool writeToJsonFile( const std::string& filePath ) const;
255 
256 private:
258 };
259 
260 } // End of namespace caffa
Abstract but typed data field accessor. Inherit to create different storage mechanisms.
Definition: cafDataFieldAccessor.h:43
Definition: cafObject.h:68
Used to validate the value of data fields Implementations need the the validate method as well as rea...
Definition: cafFieldValidator.h:104
Definition: cafObjectFactory.h:39
Definition: cafObjectHandle.h:55
void addMethod(MethodHandle *method, const std::string &keyword, MethodHandle::Type type)
Definition: cafObjectHandle.cpp:106
void addField(FieldHandle *field, const std::string &keyword)
Definition: cafObjectHandle.cpp:91
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:184
ObjectHandle::Ptr deepClone(caffa::ObjectFactory *optionalObjectFactory=nullptr) const override
Deep clone the object using an optional object factory.
Definition: cafObject.cpp:53
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:205
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:237
FieldInitHelper< FieldType > initField(FieldType &field, const std::string &keyword)
Definition: cafObject.h:168
Definition: object.py:7
Main Caffa namespace.
Definition: __init__.py:1