File: generator_utils.cpp

package info (click to toggle)
sdbus-cpp 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,296 kB
  • sloc: cpp: 9,859; xml: 170; ansic: 135; makefile: 27
file content (162 lines) | stat: -rw-r--r-- 3,947 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * Inspired by: http://dbus-cplusplus.sourceforge.net/
 */

#include <iostream>
#include <cstdlib>
#include <map>

#include "generator_utils.h"
#include "reserved_names.h"


std::string underscorize(const std::string& str)
{
    std::string res = str;
    for (unsigned int i = 0; i < res.length(); ++i)
    {
        if (!isalpha(res[i]) && !isdigit(res[i]))
        {
            res[i] = '_';
        }
    }
    return res;
}

std::string stub_name(const std::string& name)
{
    return "_" + underscorize(name) + "_stub";
}

const char *atomic_type_to_string(char t)
{
    static std::map<char, const char*> atos
    {
            { 'y', "uint8_t" },
            { 'b', "bool" },
            { 'n', "int16_t" },
            { 'q', "uint16_t" },
            { 'i', "int32_t" },
            { 'u', "uint32_t" },
            { 'x', "int64_t" },
            { 't', "uint64_t" },
            { 'd', "double" },
            { 's', "std::string" },
            { 'o', "sdbus::ObjectPath" },
            { 'g', "sdbus::Signature" },
            { 'v', "sdbus::Variant" },
            { 'h', "sdbus::UnixFd" },
            { '\0', "" }
    };

    if (atos.count(t))
    {
        return atos[t];
    }

    return nullptr;
}

static void _parse_signature(const std::string &signature, std::string &type, unsigned int &i, bool only_once = false)
{
    for (; i < signature.length(); ++i)
    {
        switch (signature[i])
        {
            case 'a':
            {
                switch (signature[++i])
                {
                    case '{':
                    {
                        type += "std::map<";
                        ++i;
                        _parse_signature(signature, type, i);
                        type += ">";

                        break;
                    }
                    case '(':
                    {
                        type += "std::vector<sdbus::Struct<";
                        ++i;
                        _parse_signature(signature, type, i);
                        type += ">>";

                        break;
                    }
                    case '\0':
                    {
                        std::cerr << 
                            "Invalid array definition. Type is missing after '" << signature
                            << "'."
                            << std::endl;
                        exit(-1);
                    }

                    default:
                    {
                        type += "std::vector<";
                        _parse_signature(signature, type, i, true);

                        type += ">";

                        break;
                    }
                }
                break;
            }
            case '(':
            {
                type += "sdbus::Struct<";
                ++i;

                _parse_signature(signature, type, i);

                type += ">";
                break;
            }
            case ')':
            case '}':
            {
                return;
            }
            default:
            {
                const char *atom = atomic_type_to_string(signature[i]);
                if (!atom)
                {
                    std::cerr << "Invalid signature: " << signature << std::endl;
                    exit(-1);
                }
                type += atom;

                break;
            }
        }

        if (only_once)
            return;

        if (i + 1 < signature.length() && signature[i + 1] != ')' && signature[i + 1] != '}')
        {
            type += ", ";
        }
    }
}

std::string signature_to_type(const std::string& signature)
{
    std::string type;
    unsigned int i = 0;
    _parse_signature(signature, type, i);
    return type;
}

std::string mangle_name(const std::string& name)
{
    if (reserved_names.find(name) != reserved_names.end())
        return name + "_";
    else
        return name;
}