File: cppcookie.py

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (202 lines) | stat: -rw-r--r-- 6,469 bytes parent folder | download | duplicates (4)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from utils import _n, _ext, _n_item, get_namespace

_templates = {}

_templates['void_cookie_function'] = \
'''\
%s\
void
%s_checked(Connection && c%s)
{%s\
  xpp::generic::check<Connection, xpp::%s::error::dispatcher>(
      std::forward<Connection>(c),
      %s_checked(std::forward<Connection>(c)%s));
}

%s\
void
%s(Connection && c%s)
{%s\
  %s(std::forward<Connection>(c)%s);
}
'''

def _void_cookie_function(ns, name, c_name, template, return_value, protos, calls, initializer):
    if len(template) == 0: template = "template<typename Connection>\n"
    return _templates['void_cookie_function'] % \
            ( template
            , name
            , protos
            , initializer
            , ns
            , c_name
            , calls
            , template
            , name
            , protos
            , initializer
            , c_name
            , calls
            )

_templates['cookie_static_getter'] = \
'''\
%s\
    static
    %s
    cookie(xcb_connection_t * const c%s)
    {%s\
      return base::cookie(c%s);
    }
'''

def _cookie_static_getter(template, return_value, protos, calls, initializer):
    return _templates['cookie_static_getter'] % \
            ( template
            , return_value
            , protos
            , initializer
            , calls
            )

class CppCookie(object):
    def __init__(self, namespace, is_void, name, reply, parameter_list):
        self.namespace = namespace
        self.is_void = is_void
        self.name = name
        self.reply = reply
        self.parameter_list = parameter_list
        self.request_name = _ext(_n_item(self.name[-1]))
        self.c_name = "xcb" \
            + (("_" + get_namespace(namespace)) if namespace.is_ext else "") \
            + "_" + self.request_name

    def comma(self):
        return self.parameter_list.comma()

    def calls(self, sort):
        return self.parameter_list.calls(sort)

    def protos(self, sort, defaults):
        return self.parameter_list.protos(sort, defaults)

    def iterator_template(self, indent="    ", tail="\n"):
        prefix = "template<typename " + ("Connection, typename " if self.is_void else "")
        return indent + prefix \
                + ", typename ".join(self.parameter_list.iterator_templates \
                                   + self.parameter_list.templates) \
                + ">" + tail \
                if len(self.parameter_list.iterator_templates) > 0 \
                else ""

    def iterator_calls(self, sort):
        return self.parameter_list.iterator_calls(sort)

    def iterator_protos(self, sort, defaults):
        return self.parameter_list.iterator_protos(sort, defaults)

    def iterator_initializers(self):
        return self.parameter_list.iterator_initializers()

    def void_functions(self, protos, calls, template="", initializer=[]):
        inits = "" if len(initializer) > 0 else "\n"
        for i in initializer:
            inits += "\n"
            for line in i.split('\n'):
                inits += "      " + line + "\n"

        return_value = "xcb_void_cookie_t"

        return _void_cookie_function(get_namespace(self.namespace),
                                     self.request_name,
                                     self.c_name,
                                     template,
                                     return_value,
                                     self.comma() + protos,
                                     self.comma() + calls,
                                     inits)


    def static_reply_methods(self, protos, calls, template="", initializer=[]):
        inits = "" if len(initializer) > 0 else "\n"
        for i in initializer:
            inits += "\n"
            for line in i.split('\n'):
                inits += "      " + line + "\n"

        if self.is_void: return_value = "xcb_void_cookie_t"
        else: return_value = self.c_name + "_cookie_t"

        return _cookie_static_getter(template,
                                     return_value,
                                     self.comma() + protos,
                                     self.comma() + calls,
                                     inits)


    def make_static_getter(self):
        default = self.static_reply_methods(self.protos(False, False), self.calls(False))

        if self.parameter_list.has_defaults:
            default = self.static_reply_methods(self.protos(True, True), self.calls(False))

        wrapped = ""
        if self.parameter_list.want_wrap:
            wrapped = \
                self.static_reply_methods(self.iterator_protos(True, True),
                        self.iterator_calls(False), self.iterator_template(),
                        self.iterator_initializers())

        default_args = ""
        if self.parameter_list.is_reordered():
            default_args = \
                self.static_reply_methods(self.protos(True, True), self.calls(False))

        result = ""

        if (self.parameter_list.has_defaults
            or self.parameter_list.is_reordered()
            or self.parameter_list.want_wrap):
            result += default

        if self.parameter_list.is_reordered():
            result += "\n" + default_args

        if self.parameter_list.want_wrap:
            result += "\n" + wrapped

        return result

    def make_void_functions(self):
        default = self.void_functions(self.protos(False, False), self.calls(False))

        if self.parameter_list.has_defaults:
            default = self.void_functions(self.protos(True, True), self.calls(False))

        wrapped = ""
        if self.parameter_list.want_wrap:
            wrapped = \
                self.void_functions(self.iterator_protos(True, True),
                        self.iterator_calls(False),
                        self.iterator_template(indent=""),
                        self.iterator_initializers())

        default_args = ""
        if self.parameter_list.is_reordered():
            default_args = \
                self.void_functions(self.protos(True, True), self.calls(False))

        result = ""

        if (self.parameter_list.has_defaults
            or self.parameter_list.is_reordered()
            or self.parameter_list.want_wrap):
            result += default

        if self.parameter_list.is_reordered():
            result += "\n" + default_args

        if self.parameter_list.want_wrap:
            result += "\n" + wrapped

        return result