Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafRpcChildFieldAccessor.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 3D-Radar 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 #pragma once
20 
21 #include "cafChildFieldAccessor.h"
22 #include "cafFieldScriptingCapability.h"
23 #include "cafJsonSerializer.h"
24 #include "cafRpcClient.h"
25 
26 namespace caffa::rpc
27 {
29 {
30 public:
31  ChildFieldAccessor( Client* client, caffa::FieldHandle* fieldHandle )
32  : caffa::ChildFieldAccessor( fieldHandle )
33  , m_client( client )
34  {
35  }
36 
37  std::shared_ptr<ObjectHandle> object() override
38  {
39  m_remoteObject = getShallowCopyOfRemoteObject();
40  return m_remoteObject;
41  }
42 
43  std::shared_ptr<const ObjectHandle> object() const override
44  {
45  m_remoteObject = getShallowCopyOfRemoteObject();
46  return m_remoteObject;
47  }
48 
49  void setObject( std::shared_ptr<ObjectHandle> object ) override
50  {
51  m_remoteObject = object;
52  m_client->setChildObject( m_field->ownerObject(), m_field->keyword(), m_remoteObject.get() );
53  }
54 
55  void clear() override
56  {
57  m_remoteObject.reset();
58  m_client->clearChildObjects( m_field->ownerObject(), m_field->keyword() );
59  }
60 
61  bool hasGetter() const override
62  {
63  auto scriptability = m_field->capability<caffa::FieldScriptingCapability>();
64  return scriptability && scriptability->isReadable();
65  }
66 
67  bool hasSetter() const override
68  {
69  auto scriptability = m_field->capability<caffa::FieldScriptingCapability>();
70  return scriptability && scriptability->isWritable();
71  }
72 
73 private:
74  std::shared_ptr<ObjectHandle> getShallowCopyOfRemoteObject() const
75  {
76  return m_client->getChildObject( m_field->ownerObject(), m_field->keyword() );
77  }
78 
79 private:
80  Client* m_client;
81 
82  mutable std::shared_ptr<ObjectHandle> m_remoteObject;
83 };
84 
85 } // namespace caffa::rpc
Definition: cafRpcChildFieldAccessor.h:28
Definition: cafChildFieldAccessor.h:30
Definition: cafRpcClient.h:37
bool hasGetter() const override
Definition: cafRpcChildFieldAccessor.h:61
Definition: cafFieldScriptingCapability.h:49
Base class for all fields, making it possible to handle them generically.
Definition: cafFieldHandle.h:19
bool hasSetter() const override
Definition: cafRpcChildFieldAccessor.h:67
Definition: cafRestAppService.h:28