Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafRpcDataFieldAccessor.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 "cafDataFieldAccessor.h"
22 #include "cafFieldScriptingCapability.h"
23 #include "cafRpcClient.h"
24 
25 namespace caffa::rpc
26 {
27 template <class DataType>
29 {
30 public:
31  DataFieldAccessor( Client* client, caffa::FieldHandle* fieldHandle )
33  , m_client( client )
34  , m_fieldHandle( fieldHandle )
35  {
36  }
37 
38  std::unique_ptr<caffa::DataFieldAccessor<DataType>> clone() const override
39  {
40  return std::make_unique<DataFieldAccessor<DataType>>( m_client, m_fieldHandle );
41  }
42 
43  DataType value() override
44  {
45  return m_client->get<DataType>( m_fieldHandle->ownerObject(), m_fieldHandle->keyword() );
46  }
47 
48  void setValue( const DataType& value ) override
49  {
50  return m_client->set<DataType>( m_fieldHandle->ownerObject(), m_fieldHandle->keyword(), value );
51  }
52 
53  bool hasGetter() const override
54  {
55  auto scriptability = m_fieldHandle->capability<caffa::FieldScriptingCapability>();
56  return scriptability && scriptability->isReadable();
57  }
58  bool hasSetter() const override
59  {
60  auto scriptability = m_fieldHandle->capability<caffa::FieldScriptingCapability>();
61  return scriptability && scriptability->isWritable();
62  }
63 
64 private:
65  Client* m_client;
66 
67  caffa::FieldHandle* m_fieldHandle;
68 };
69 
70 } // namespace caffa::rpc
bool hasSetter() const override
Definition: cafRpcDataFieldAccessor.h:58
DataType get(const caffa::ObjectHandle *objectHandle, const std::string &fieldName) const
Get a value through RPC.
Definition: cafRpcClient.h:113
Abstract but typed data field accessor. Inherit to create different storage mechanisms.
Definition: cafDataFieldAccessor.h:42
Definition: cafRpcClient.h:37
Definition: cafFieldScriptingCapability.h:49
Base class for all fields, making it possible to handle them generically.
Definition: cafFieldHandle.h:19
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: cafRpcDataFieldAccessor.h:48
void set(const caffa::ObjectHandle *objectHandle, const std::string &fieldName, const DataType &value)
Set a value through RPC.
Definition: cafRpcClient.h:123
DataType value() override
Get the field value.
Definition: cafRpcDataFieldAccessor.h:43
std::unique_ptr< caffa::DataFieldAccessor< DataType > > clone() const override
Clone the accessor using polymorphism.
Definition: cafRpcDataFieldAccessor.h:38
Definition: cafRpcDataFieldAccessor.h:28
bool hasGetter() const override
Definition: cafRpcDataFieldAccessor.h:53
Definition: cafRestAppService.h:28