Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafMethodHandle.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 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 "cafObjectAttribute.h"
23 
24 #include <nlohmann/json.hpp>
25 
26 #include <string>
27 #include <vector>
28 
29 namespace caffa
30 {
31 class MethodHandle;
32 class ObjectHandle;
33 class ObjectFactory;
34 
36 {
37 public:
38  MethodAccessorInterface( const ObjectHandle* selfHandle, const MethodHandle* methodHandle, ObjectFactory* objectFactory )
39  : m_selfHandle( selfHandle )
40  , m_methodHandle( methodHandle )
41  , m_objectFactory( objectFactory )
42  {
43  }
44  virtual ~MethodAccessorInterface() = default;
45  virtual std::string execute( const std::string& jsonArgumentsString ) const = 0;
46 
47  ObjectFactory* objectFactory() const { return m_objectFactory; }
48 
49 protected:
50  const ObjectHandle* m_selfHandle;
51  const MethodHandle* m_methodHandle;
52  ObjectFactory* m_objectFactory;
53 };
54 
56 {
57 public:
58  enum class Type
59  {
60  READ_WRITE = 0,
61  READ_ONLY
62  };
63 
64  MethodHandle()
65  : m_documentation( "" )
66  {
67  }
68  ~MethodHandle() override = default;
69 
70  std::string keyword() const override { return m_name; }
71  void setArgumentNames( const std::vector<std::string>& argumentNames ) { m_argumentNames = argumentNames; }
72  const std::vector<std::string>& argumentNames() const { return m_argumentNames; }
73 
74  Type type() const { return m_type; }
75  const std::string& documentation() const { return m_documentation; }
76  void setDocumentation( const std::string& documentation ) { m_documentation = documentation; }
77 
78  virtual std::string execute( const std::string& jsonArgumentsString ) const = 0;
79  virtual std::string schema() const = 0;
80  virtual nlohmann::json jsonSchema() const = 0;
81 
82  MethodAccessorInterface* accessor() const { return m_accessor.get(); }
83  void setAccessor( std::unique_ptr<MethodAccessorInterface> accessor ) { m_accessor = std::move( accessor ); }
84 
85 private:
86  friend class ObjectHandle;
87  void setName( const std::string& name ) { m_name = name; }
88  void setType( Type type ) { m_type = type; }
89 
90  std::string m_name;
91  std::vector<std::string> m_argumentNames;
92  Type m_type;
93  std::string m_documentation;
94 
95  std::unique_ptr<MethodAccessorInterface> m_accessor;
96 };
97 } // namespace caffa
Definition: cafMethodHandle.h:36
Definition: cafMethodHandle.h:56
Definition: cafObjectAttribute.h:27
Definition: cafObjectFactory.h:39
Definition: cafObjectHandle.h:55
Main Caffa namespace.
Definition: __init__.py:1