File: DeclarationManagerInterface.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (111 lines) | stat: -rw-r--r-- 4,490 bytes parent folder | download | duplicates (3)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "DeclarationManagerInterface.h"

namespace script
{

class DeclarationVisitorWrapper :
    public DeclarationVisitor
{
public:
    void visit(const decl::IDeclaration::Ptr& declaration) override
    {
        // Wrap this method to python
        PYBIND11_OVERLOAD_PURE(
            void,               // Return type
            DeclarationVisitor, // Parent class
            visit,              // Name of function in C++ (must match Python name)
            ScriptDeclaration(declaration)  // Argument(s)
        );
    }
};

ScriptDeclaration DeclarationManagerInterface::findDeclaration(decl::Type type, const std::string& name)
{
    return ScriptDeclaration(GlobalDeclarationManager().findDeclaration(type, name));
}

ScriptDeclaration DeclarationManagerInterface::findOrCreateDeclaration(decl::Type type, const std::string& name)
{
    return ScriptDeclaration(GlobalDeclarationManager().findOrCreateDeclaration(type, name));
}

void DeclarationManagerInterface::foreachDeclaration(decl::Type type, DeclarationVisitor& visitor)
{
    GlobalDeclarationManager().foreachDeclaration(type, [&](const decl::IDeclaration::Ptr& decl)
    {
        visitor.visit(decl);
    });
}

bool DeclarationManagerInterface::renameDeclaration(decl::Type type, const std::string& oldName, const std::string& newName)
{
    return GlobalDeclarationManager().renameDeclaration(type, oldName, newName);
}

void DeclarationManagerInterface::removeDeclaration(decl::Type type, const std::string& name)
{
    GlobalDeclarationManager().removeDeclaration(type, name);
}

void DeclarationManagerInterface::reloadDeclarations()
{
    GlobalDeclarationManager().reloadDeclarations();
}

void DeclarationManagerInterface::saveDeclaration(const ScriptDeclaration& decl)
{
    if (!decl.get()) return;

    GlobalDeclarationManager().saveDeclaration(decl.get());
}

void DeclarationManagerInterface::registerInterface(py::module& scope, py::dict& globals)
{
    py::class_<ScriptDeclaration> declaration(scope, "Declaration");

    py::enum_<decl::Type>(declaration, "Type")
        .value("NullType", decl::Type::None) // None is a reserved word in Python
        .value("Material", decl::Type::Material)
        .value("Table", decl::Type::Table)
        .value("EntityDef", decl::Type::EntityDef)
        .value("SoundShader", decl::Type::SoundShader)
        .value("ModelDef", decl::Type::ModelDef)
        .value("Particle", decl::Type::Particle)
        .value("Skin", decl::Type::Skin)
        .value("Fx", decl::Type::Fx)
        .export_values();

    py::class_<decl::DeclarationBlockSyntax>(scope, "DeclarationBlockSyntax")
        .def_readwrite("typeName", &decl::DeclarationBlockSyntax::typeName)
        .def_readwrite("name", &decl::DeclarationBlockSyntax::name)
        .def_readwrite("contents", &decl::DeclarationBlockSyntax::contents)
        .def_readwrite("modName", &decl::DeclarationBlockSyntax::modName);

    declaration.def(py::init<const decl::IDeclaration::Ptr&>());
    declaration.def("isNull", &ScriptDeclaration::isNull);
    declaration.def("getDeclName", &ScriptDeclaration::getDeclName);
    declaration.def("getDeclType", &ScriptDeclaration::getDeclType);
    declaration.def("getBlockSyntax", &ScriptDeclaration::getBlockSyntax);
    declaration.def("setBlockSyntax", &ScriptDeclaration::setBlockSyntax);
    declaration.def("getDeclFilePath", &ScriptDeclaration::getDeclFilePath);
    declaration.def("setDeclFilePath", &ScriptDeclaration::setDeclFilePath);

    py::class_<DeclarationVisitor, DeclarationVisitorWrapper>(scope, "DeclarationVisitor")
        .def(py::init<>())
        .def("visit", &DeclarationVisitor::visit);

    // IDeclarationManager interface
    py::class_<DeclarationManagerInterface>(scope, "DeclarationManager")
        .def("findDeclaration", &DeclarationManagerInterface::findDeclaration)
        .def("findOrCreateDeclaration", &DeclarationManagerInterface::findOrCreateDeclaration)
        .def("foreachDeclaration", &DeclarationManagerInterface::foreachDeclaration)
        .def("renameDeclaration", &DeclarationManagerInterface::renameDeclaration)
        .def("removeDeclaration", &DeclarationManagerInterface::removeDeclaration)
        .def("reloadDeclarations", &DeclarationManagerInterface::reloadDeclarations)
        .def("saveDeclaration", &DeclarationManagerInterface::saveDeclaration);

    // Now point the Python variable "GlobalDeclarationManager" to this instance
    globals["GlobalDeclarationManager"] = this;
}

}