Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
48
BEGIN_VISP_NAMESPACE
54
template
<
typename
T>
55
class
VISP_EXPORT
vpDynamicFactory
56
{
57
public
:
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
103
protected
:
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
114
END_VISP_NAMESPACE
115
116
#endif
vpDynamicFactory::setJsonKeyFinder
void setJsonKeyFinder(const std::function< std::string(const nlohmann::json &)> &finderFn)
Definition
vpDynamicFactory.h:95
vpDynamicFactory::m_keyFinder
std::function< std::string(const nlohmann::json &)> m_keyFinder
Definition
vpDynamicFactory.h:110
vpDynamicFactory::buildFromJson
std::shared_ptr< T > buildFromJson(const nlohmann::json &j)
Definition
vpDynamicFactory.h:78
vpDynamicFactory::registerType
void registerType(const std::string &key, const std::function< std::shared_ptr< T >(const nlohmann::json &)> &function)
Definition
vpDynamicFactory.h:59
vpDynamicFactory::m_jsonRawBuilders
std::map< std::string, std::function< std::shared_ptr< T >(const std::string &)> > m_jsonRawBuilders
Definition
vpDynamicFactory.h:108
vpDynamicFactory::vpDynamicFactory
vpDynamicFactory()=default
vpDynamicFactory::m_jsonBuilders
std::map< std::string, std::function< std::shared_ptr< T >(const nlohmann::json &)> > m_jsonBuilders
Definition
vpDynamicFactory.h:107
vpDynamicFactory::~vpDynamicFactory
virtual ~vpDynamicFactory()
Definition
vpDynamicFactory.h:101
vpDynamicFactory::registerTypeRaw
void registerTypeRaw(const std::string &key, const std::function< std::shared_ptr< T >(const std::string &)> function)
Definition
vpDynamicFactory.h:67
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::badValue
@ badValue
Used to indicate that a value is not in the allowed range.
Definition
vpException.h:73
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
tracker
rbt
include
visp3
rbt
vpDynamicFactory.h
Generated by
1.17.0