forked from sofa-framework/SofaPython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonFactory.h
More file actions
87 lines (72 loc) · 4.39 KB
/
PythonFactory.h
File metadata and controls
87 lines (72 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/******************************************************************************
* SofaPython3 plugin *
* (c) 2021 CNRS, University of Lille, INRIA *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/helper/Factory.h>
#include <sofa/helper/Factory.inl>
#include <sofa/core/objectmodel/Base.h>
#include <sofa/core/objectmodel/Event.h>
#include <pybind11/pybind11.h>
#include <SofaPython3/config.h>
/////////////////////////////// DECLARATION //////////////////////////////
namespace sofapython3
{
typedef std::function<pybind11::object(sofa::core::objectmodel::Base*)> componentDowncastingFunction;
typedef std::function<pybind11::object(sofa::core::objectmodel::BaseData*)> dataDowncastingFunction;
typedef std::function<pybind11::dict(sofa::core::objectmodel::Event*)> eventDowncastingFunction;
typedef std::function<sofa::core::objectmodel::BaseData*()> dataCreatorFunction;
typedef sofa::helper::Factory< std::string, sofa::core::objectmodel::BaseData> DataFactory;
class SOFAPYTHON3_API PythonFactory
{
public:
static pybind11::object toPython(sofa::core::objectmodel::Base* object);
static pybind11::object toPython(const sofa::core::objectmodel::BaseData* data);
static pybind11::object toPython(sofa::core::objectmodel::BaseData* data);
static pybind11::object valueToPython_ro(sofa::core::objectmodel::BaseData* data);
static void fromPython(sofa::core::objectmodel::BaseData* data, const pybind11::object& value);
static pybind11::object toPython(sofa::core::objectmodel::Event* event);
static sofa::core::objectmodel::BaseData* createInstance(const std::string& typeName);
static void registerType(const std::string& typeName, componentDowncastingFunction fct);
static void registerType(const std::string& typeName, dataDowncastingFunction fct);
static void registerType(const std::string& typeName, eventDowncastingFunction fct);
static void registerType(const std::string& typeName, dataCreatorFunction fct);
template<class T>
static void registerType(componentDowncastingFunction fct)
{
registerType(T::GetClass()->typeName, fct);
}
template<class T>
static void registerType(eventDowncastingFunction fct)
{
registerType(T::GetClassName(), fct);
}
template <class T>
static void registerType(const std::string& s) {
sofa::core::objectmodel::Data<T> a; //Solve compilation for Windows, apparently force instanciation for Data<T> ???
registerType(s, [](){ return new sofa::core::objectmodel::Data<T>(); });
}
static std::map<std::string, componentDowncastingFunction>::iterator searchLowestCastAvailable(const sofa::core::objectmodel::BaseClass* metaclass);
static void uniqueKeys(std::back_insert_iterator<sofa::type::vector<std::string> > it);
private:
static bool registerDefaultEvents();
static bool registerDefaultTypes();
static bool defaultEventsRegistered;
static bool defaultTypesRegistered;
};
} /// sofapython3