Blue Brain BioExplorer
ActionInterface.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * Responsible Author: Daniel.Nachbaur@epfl.ch
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #pragma once
23 
25 
26 #include <functional>
27 #include <string>
28 
29 namespace core
30 {
49 {
50 public:
51  virtual ~ActionInterface() = default;
52 
59  virtual void registerNotification(const RpcDescription& desc, const std::function<void()>& action) = 0;
60 
69  virtual void registerNotification(const RpcParameterDescription& desc, const PropertyMap& input,
70  const std::function<void(PropertyMap)>& action) = 0;
71 
82  virtual void registerRequest(const RpcParameterDescription& desc, const PropertyMap& input,
83  const PropertyMap& output, const std::function<PropertyMap(PropertyMap)>& action) = 0;
84 
94  virtual void registerRequest(const RpcDescription& desc, const PropertyMap& output,
95  const std::function<PropertyMap()>& action) = 0;
96 
98  void registerNotification(const std::string& name, const std::function<void()>& action)
99  {
100  _registerNotification(name, [action] { action(); });
101  }
102 
104  template <typename Params>
105  void registerNotification(const std::string& name, const std::function<void(Params)>& action)
106  {
107  _registerNotification(name, [action](const std::string& param) {
108  Params params;
109  if (!from_json(params, param))
110  throw std::runtime_error("from_json failed");
111  action(params);
112  });
113  }
114 
116  template <typename Params, typename RetVal>
117  void registerRequest(const std::string& name, const std::function<RetVal(Params)>& action)
118  {
119  _registerRequest(name, [action](const std::string& param) {
120  Params params;
121  if (!from_json(params, param))
122  throw std::runtime_error("from_json failed");
123  return to_json(action(params));
124  });
125  }
126 
128  template <typename RetVal>
129  void registerRequest(const std::string& name, const std::function<RetVal()>& action)
130  {
131  _registerRequest(name, [action] { return to_json(action()); });
132  }
133 
134 protected:
135  using RetParamFunc = std::function<std::string(std::string)>;
136  using RetFunc = std::function<std::string()>;
137  using ParamFunc = std::function<void(std::string)>;
138  using VoidFunc = std::function<void()>;
139 
140 private:
141  virtual void _registerRequest(const std::string&, const RetParamFunc&) {}
142  virtual void _registerRequest(const std::string&, const RetFunc&) {}
143  virtual void _registerNotification(const std::string&, const ParamFunc&) {}
144  virtual void _registerNotification(const std::string&, const VoidFunc&) {}
145 };
146 } // namespace core
std::function< std::string(std::string)> RetParamFunc
void registerNotification(const std::string &name, const std::function< void()> &action)
virtual void registerNotification(const RpcParameterDescription &desc, const PropertyMap &input, const std::function< void(PropertyMap)> &action)=0
void registerRequest(const std::string &name, const std::function< RetVal()> &action)
virtual ~ActionInterface()=default
std::function< void(std::string)> ParamFunc
void registerNotification(const std::string &name, const std::function< void(Params)> &action)
virtual void registerRequest(const RpcParameterDescription &desc, const PropertyMap &input, const PropertyMap &output, const std::function< PropertyMap(PropertyMap)> &action)=0
void registerRequest(const std::string &name, const std::function< RetVal(Params)> &action)
virtual void registerNotification(const RpcDescription &desc, const std::function< void()> &action)=0
std::function< void()> VoidFunc
virtual void registerRequest(const RpcDescription &desc, const PropertyMap &output, const std::function< PropertyMap()> &action)=0
std::function< std::string()> RetFunc
std::string to_json(const core::PropertyMap &obj)
bool from_json(T &obj, const std::string &json, PRE preUpdateFunc=[] {}, POST postUpdateFunc=[] {})