Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpDynamicFactory.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
35#ifndef VP_DYNAMIC_FACTORY_H
36#define VP_DYNAMIC_FACTORY_H
37
38#include <visp3/core/vpConfig.h>
39#include <visp3/core/vpException.h>
40
41#if defined(VISP_HAVE_NLOHMANN_JSON)
42#include VISP_NLOHMANN_JSON(json.hpp)
43#endif
44
45#include <map>
46#include <functional>
47
54template<typename T>
55class VISP_EXPORT vpDynamicFactory
56{
57public:
58#if defined(VISP_HAVE_NLOHMANN_JSON)
59 void registerType(const std::string &key, const std::function<std::shared_ptr<T>(const nlohmann::json &)> &function)
60 {
61 if (m_jsonBuilders.find(key) != m_jsonBuilders.end() || m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
62 throw vpException(vpException::badValue, "Type %s was already registered in the factory", key.c_str());
63 }
64 m_jsonBuilders[key] = function;
65 }
66
67 void registerTypeRaw(const std::string &key, const std::function<std::shared_ptr<T>(const std::string &)> function)
68 {
69 if (m_jsonBuilders.find(key) != m_jsonBuilders.end() || m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
70 throw vpException(vpException::badValue, "Type %s was already registered in the factory", key.c_str());
71 }
72
73 m_jsonRawBuilders.insert({ key, function });
74 // m_jsonRawBuilders[key] = [](const std::string &s) -> std::shared_ptr<T> {
75
76 }
77
78 std::shared_ptr<T> buildFromJson(const nlohmann::json &j)
79 {
80 const std::string key = m_keyFinder(j);
81
82 if (m_jsonBuilders.find(key) != m_jsonBuilders.end()) {
83 return m_jsonBuilders[key](j);
84 }
85
86 else if (m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
87 return m_jsonRawBuilders[key](j.dump());
88 }
89 else {
90 return nullptr;
91 }
92 }
93
94
95 void setJsonKeyFinder(const std::function<std::string(const nlohmann::json &)> &finderFn)
96 {
97 m_keyFinder = finderFn;
98 }
99#endif
100
101 virtual ~vpDynamicFactory() { }
102
103protected:
104
105 vpDynamicFactory() = default;
106#if defined(VISP_HAVE_NLOHMANN_JSON)
107 std::map<std::string, std::function<std::shared_ptr<T>(const nlohmann::json &)>> m_jsonBuilders;
108 std::map<std::string, std::function<std::shared_ptr<T>(const std::string &)>> m_jsonRawBuilders;
109
110 std::function<std::string(const nlohmann::json &)> m_keyFinder;
111#endif
112};
113
114END_VISP_NAMESPACE
115
116#endif
void setJsonKeyFinder(const std::function< std::string(const nlohmann::json &)> &finderFn)
std::function< std::string(const nlohmann::json &)> m_keyFinder
std::shared_ptr< T > buildFromJson(const nlohmann::json &j)
void registerType(const std::string &key, const std::function< std::shared_ptr< T >(const nlohmann::json &)> &function)
std::map< std::string, std::function< std::shared_ptr< T >(const std::string &)> > m_jsonRawBuilders
vpDynamicFactory()=default
std::map< std::string, std::function< std::shared_ptr< T >(const nlohmann::json &)> > m_jsonBuilders
virtual ~vpDynamicFactory()
void registerTypeRaw(const std::string &key, const std::function< std::shared_ptr< T >(const std::string &)> function)
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73