Re-sync with internal repository
[hiphop-php.git] / third-party / thrift / src / thrift / compiler / generate / templates / py3 / types.pxd.mustache
blob00822b8f7d073e002aabca44383791c956ad7585
1 {{!
3   Copyright (c) Meta Platforms, Inc. and affiliates.
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
9       http://www.apache.org/licenses/LICENSE-2.0
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
17 }}{{!
19 Wraps structs and exceptions defined in the thrift interface file and implemented
20 in the cpp2 generated code. This file just exposes the C++ classes and attributes
21 to Cython. The Pythonic wrappers of the individual structs and fields are
22 implemented in the Struct.pyx file.
25 {{> common/auto_generated_py}}
27 from libc.stdint cimport (
28     int8_t as cint8_t,
29     int16_t as cint16_t,
30     int32_t as cint32_t,
31     int64_t as cint64_t,
32     uint32_t as cuint32_t,
34 from libcpp.string cimport string
35 from libcpp cimport bool as cbool, nullptr, nullptr_t
36 from cpython cimport bool as pbool
37 from libcpp.memory cimport shared_ptr, unique_ptr
38 from libcpp.utility cimport move as cmove
39 from libcpp.vector cimport vector
40 from libcpp.set cimport set as cset
41 from libcpp.map cimport map as cmap, pair as cpair
42 from thrift.py3.exceptions cimport cTException
43 cimport folly.iobuf as _fbthrift_iobuf
44 cimport thrift.py3.exceptions
45 cimport thrift.py3.types
46 from thrift.py3.types cimport (
47     bstring,
48     bytes_to_string,
49     field_ref as __field_ref,
50     optional_field_ref as __optional_field_ref,
51     required_field_ref as __required_field_ref,
52     terse_field_ref as __terse_field_ref,
53     union_field_ref as __union_field_ref,
54     get_union_field_value as __get_union_field_value,
56 from thrift.py3.common cimport (
57     RpcOptions as __RpcOptions,
58     Protocol as __Protocol,
59     cThriftMetadata as __fbthrift_cThriftMetadata,
60     MetadataBox as __MetadataBox,
62 from folly.optional cimport cOptional as __cOptional
63 {{#program:has_stream?}}
64 from folly cimport cFollyTry
65 from cpython.ref cimport PyObject
66 from thrift.py3.stream cimport (
67     ClientBufferedStream, cClientBufferedStream, cClientBufferedStreamWrapper,
68     ResponseAndClientBufferedStream, cResponseAndClientBufferedStream,
69     ServerStream, cServerStream, ResponseAndServerStream
71 {{/program:has_stream?}}
72 {{#program:includeNamespaces}}
73 cimport {{#includeNamespace}}{{value}}.{{/includeNamespace}}types as _{{#includeNamespace}}{{value}}_{{/includeNamespace}}types
74 {{/program:includeNamespaces}}
76 cimport {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.types_fields as _fbthrift_types_fields
78 cdef extern from "{{program:includePrefix}}gen-py3/{{program:name}}/types.h":
79   pass
81 {{#program:cppIncludes}}
82 cdef extern from "{{.}}":
83   pass
84 {{/program:cppIncludes}}{{!
85 }}{{#program:customTemplates}}
86 {{> types/custom_template}}
87 {{/program:customTemplates}}{{!
88 }}{{#program:customTypes}}
89 {{> types/custom_type}}
90 {{/program:customTypes}}
92 {{#program:enums}}
93 {{#first?}}
94 cdef extern from "{{program:includePrefix}}{{program:cpp_gen_path}}/{{program:name}}_metadata.h" namespace "apache::thrift::detail::md":
95     cdef cppclass EnumMetadata[T]:
96         @staticmethod
97         void gen(__fbthrift_cThriftMetadata &metadata)
98 cdef extern from "{{program:includePrefix}}{{program:cpp_gen_path}}/{{program:name}}_types.h"{{!
99 }} namespace "{{#program:cppNamespaces}}::{{value}}{{/program:cppNamespaces}}":
100 {{/first?}}
101     cdef cppclass c{{enum:name}} "::{{#program:cppNamespaces}}{{value}}::{{/program:cppNamespaces}}{{enum:cpp_name}}":
102         pass
104 {{/program:enums}}
107 {{> types/enum_defs }}
109 {{#program:structs}}
110 {{#first?}}
111 cdef extern from "{{program:includePrefix}}{{program:cpp_gen_path}}/{{program:name}}_metadata.h" namespace "apache::thrift::detail::md":
112     cdef cppclass ExceptionMetadata[T]:
113         @staticmethod
114         void gen(__fbthrift_cThriftMetadata &metadata)
115 cdef extern from "{{program:includePrefix}}{{program:cpp_gen_path}}/{{program:name}}_metadata.h" namespace "apache::thrift::detail::md":
116     cdef cppclass StructMetadata[T]:
117         @staticmethod
118         void gen(__fbthrift_cThriftMetadata &metadata)
119 cdef extern from "{{program:includePrefix}}{{program:cpp_gen_path}}/{{program:name}}_types_custom_protocol.h"{{!
120 }} namespace "{{#program:cppNamespaces}}::{{value}}{{/program:cppNamespaces}}":
121 {{/first?}}
122     {{#struct:union?}}
123     cdef enum c{{struct:name}}__type "::{{#program:cppNamespaces}}{{value}}::{{/program:cppNamespaces}}{{struct:name}}::Type":
124         c{{struct:name}}__type___EMPTY__ "::{{#program:cppNamespaces}}{{value}}::{{/program:cppNamespaces}}{{struct:name}}::Type::__EMPTY__",
125     {{#struct:py3_fields}}
126         c{{struct:name}}__type_{{field:py_name}} "::{{#program:cppNamespaces}}{{value}}::{{/program:cppNamespaces}}{{struct:name}}::Type::{{field:cppName}}",
127     {{/struct:py3_fields}}
128     {{/struct:union?}}
130     cdef cppclass c{{struct:name}} "::{{#program:cppNamespaces}}{{value}}::{{/program:cppNamespaces}}{{struct:name}}"{{#struct:exception?}}(cTException){{/struct:exception?}}:
131         c{{struct:name}}() except +
132         {{^struct:cpp_noncopyable?}}
133         c{{struct:name}}(const c{{struct:name}}&) except +
134         {{/struct:cpp_noncopyable?}}
135         {{^struct:cpp_noncomparable}}
136         bint operator==(c{{struct:name}}&)
137         bint operator!=(c{{struct:name}}&)
138         {{#struct:is_struct_orderable?}}
139         bint operator<(c{{struct:name}}&)
140         bint operator>(c{{struct:name}}&)
141         bint operator<=(c{{struct:name}}&)
142         bint operator>=(c{{struct:name}}&)
143         {{/struct:is_struct_orderable?}}
144         {{/struct:cpp_noncomparable}}
145     {{^struct:union?}}
146     {{#struct:fields_and_mixin_fields}}
147         {{#field:has_ref_accessor?}}
148         {{#field:optional?}}
149         __optional_field_ref[{{#field:type}}{{> types/cython_cpp_struct_field_type}}{{/field:type}}] {{!
150             }}{{field:py_name}}_ref "{{> types/field_cppname}}" ()
151         {{/field:optional?}}
152         {{#field:required?}}
153         __required_field_ref[{{#field:type}}{{> types/cython_cpp_struct_field_type}}{{/field:type}}] {{!
154             }}{{field:py_name}}_ref "{{> types/field_cppname}}" ()
155         {{/field:required?}}
156         {{#field:opt_in_req_out?}}
157         __field_ref[{{#field:type}}{{> types/cython_cpp_struct_field_type}}{{/field:type}}] {{!
158             }}{{field:py_name}}_ref "{{> types/field_cppname}}" ()
159         {{/field:opt_in_req_out?}}
160         {{#field:terse?}}
161         __terse_field_ref[{{#field:type}}{{> types/cython_cpp_struct_field_type}}{{/field:type}}] {{!
162             }}{{field:py_name}}_ref "{{> types/field_cppname}}" ()
163         {{/field:terse?}}
164         {{/field:has_ref_accessor?}}
165         {{^field:has_ref_accessor?}}
166         {{#field:reference?}}
167         {{#field:type}}{{> types/cython_cpp_struct_field_type}}{{/field:type}} {{field:py_name}}_ref "{{> types/field_cppname}}" ()
168         {{/field:reference?}}
169         {{/field:has_ref_accessor?}}
170     {{/struct:fields_and_mixin_fields}}
171     {{/struct:union?}}
172     {{#struct:union?}}
173         c{{struct:name}}__type getType() const
174     {{#struct:py3_fields}}
175         {{^field:has_ref_api?}}
176         __union_field_ref[{{#field:type}}{{> types/cython_cpp_struct_field_type}}{{/field:type}}] {{!
177             }}{{field:py_name}}_ref"{{> types/field_cppname}}_ref" ()
178         {{/field:has_ref_api?}}{{!
179         }}{{#field:has_ref_api?}}
180         {{#field:type}}const {{> types/cython_cpp_struct_field_type}}& get_{{field:py_name}} "get_{{field:cppName}}" () const{{/field:type}}
181         {{! Note that the set methods don't take in a shared/unique_ptr, they just take in an object ref,
182             but they return the appropriate ptr }}
183         {{#field:type}}{{> types/cython_cpp_struct_field_type}}& set_{{field:py_name}} "set_{{field:cppName}}" (const {{> types/cython_cpp_type}}&){{/field:type}}
184         {{/field:has_ref_api?}}
185     {{/struct:py3_fields}}{{/struct:union?}}
187 {{/program:structs}}
189 {{#program:structs}}
190 {{#struct:union?}}
191 cdef class __{{struct:name}}Type(thrift.py3.types.CompiledEnum):
192     pass
195 {{/struct:union?}}
198 cdef class {{struct:name}}({{> types/python_struct_class}}):
199     cdef shared_ptr[c{{struct:name}}] _cpp_obj
200     {{^struct:union?}}
201     cdef _fbthrift_types_fields.__{{struct:name}}_FieldsSetter _fields_setter
202     {{#struct:fields_and_mixin_fields}}
203     cdef inline object {{field:py_name}}_impl(self)
204     {{/struct:fields_and_mixin_fields}}
205     {{!Memoized Python wrappers of struct fields, created lazily}}
206     {{#struct:py3_fields}}
207     {{#field:type}}{{#type:struct}}
208     cdef {{> types/cython_python_type}} __fbthrift_cached_{{field:py_name}}
209     {{/type:struct}}{{#type:container?}}
210     cdef {{> types/cython_python_type}} __fbthrift_cached_{{field:py_name}}
211     {{/type:container?}}{{#type:enum?}}
212     cdef object __fbthrift_cached_{{field:py_name}}
213     {{/type:enum?}}{{#type:iobuf?}}
214     cdef _fbthrift_iobuf.IOBuf __fbthrift_cached_{{field:py_name}}
215     {{/type:iobuf?}}{{#type:iobufRef?}}
216     cdef _fbthrift_iobuf.IOBuf __fbthrift_cached_{{field:py_name}}
217     {{/type:iobufRef?}}{{/field:type}}
218     {{/struct:py3_fields}}{{/struct:union?}}
219     {{#struct:union?}}
220     cdef readonly __{{struct:name}}Type type
221     cdef readonly object value
222     cdef _load_cache({{struct:name}} self)
223     {{/struct:union?}}
224     {{#struct:union?}}
226     @staticmethod
227     cdef unique_ptr[c{{struct:name}}] _make_instance(
228         c{{struct:name}}* base_instance{{^struct:union?}},
229         bint* __isNOTSET{{/struct:union?}}{{#struct:py3_fields}},
230         {{#field:type}}{{!
231             }}{{#type:cythonTypeNoneable?}}{{!
232                 }}{{> types/cython_python_type}} {{!
233             }}{{/type:cythonTypeNoneable?}}{{!
234             }}{{^type:cythonTypeNoneable?}}{{!
235                 }}object {{!
236             }}{{/type:cythonTypeNoneable?}}{{!
237             }}{{field:py_name}}{{!
238         }}{{/field:type}}{{/struct:py3_fields}}
239     ) except *
240     {{/struct:union?}}
242     @staticmethod
243     cdef _fbthrift_create(shared_ptr[c{{struct:name}}])
245 {{/program:structs}}
247 {{#program:containerTypes}}
248 cdef class {{> types/cython_python_type}}(thrift.py3.types.{{!
249         }}{{#type:list?}}List{{/type:list?}}{{!
250         }}{{#type:set?}}Set{{/type:set?}}{{!
251         }}{{#type:map?}}Map{{/type:map?}}{{!
252         }}):
253     cdef shared_ptr[{{> types/cython_cpp_type}}] _cpp_obj
254     @staticmethod
255     cdef _fbthrift_create(shared_ptr[{{> types/cython_cpp_type}}])
256     @staticmethod
257     cdef shared_ptr[{{> types/cython_cpp_type}}] _make_instance(object items) except *
259 {{/program:containerTypes}}
261 {{#program:constants}}
262 {{#first?}}
263 cdef extern from "{{program:includePrefix}}{{program:cpp_gen_path}}/{{program:name}}_constants.h"{{!
264 }} namespace "{{#program:cppNamespaces}}::{{value}}{{/program:cppNamespaces}}":
265 {{/first?}}
266     {{#constant:type}}
267     cdef {{^type:string?}}{{> types/cython_cpp_type}}{{/type:string?}}{{#type:string?}}const char*{{/type:string?}} {{!
268     }}c{{constant:name}} "::{{#program:cppNamespaces}}{{value}}::{{/program:cppNamespaces}}{{program:name}}_constants::{{constant:name}}"{{!
269     }}{{#type:struct?}}(){{/type:struct?}}{{#type:string?}}(){{/type:string?}}{{#type:binary?}}(){{/type:binary?}}{{#type:container?}}(){{/type:container?}}{{#type:enum?}}(){{/type:enum?}}
270 {{/constant:type}}{{/program:constants}}
271 {{#program:has_stream?}}{{! check if it's a stream case and if not short-circuit it to save get stream_types calls }}
273 {{#program:stream_types}}
274 cdef class ClientBufferedStream__{{type:flat_name}}(ClientBufferedStream):
275     cdef unique_ptr[cClientBufferedStreamWrapper[{{ > types/cython_cpp_type}}]] _gen
277     @staticmethod
278     cdef _fbthrift_create(cClientBufferedStream[{{ > types/cython_cpp_type}}]& c_obj, __RpcOptions rpc_options)
280     @staticmethod
281     cdef void callback(
282         cFollyTry[__cOptional[{{ > types/cython_cpp_type}}]]&& res,
283         PyObject* userdata,
284     )
286 cdef class ServerStream__{{type:flat_name}}(ServerStream):
287     pass
289 {{/program:stream_types}}
290 {{#program:response_and_stream_types}}
292 cdef class ResponseAndClientBufferedStream__{{> stream/response_class_name_suffix}}(ResponseAndClientBufferedStream):
293     cdef ClientBufferedStream__{{#type:stream_elem_type}}{{type:flat_name}}{{/type:stream_elem_type}} _stream
294     cdef {{#type:stream_first_response_type}}{{ > types/cython_python_type}}{{/type:stream_first_response_type}} _response
296     @staticmethod
297     cdef _fbthrift_create(cResponseAndClientBufferedStream[{{!
298         }}{{#type:stream_first_response_type}}{{ > types/cython_cpp_type}}{{/type:stream_first_response_type}}, {{!
299         }}{{#type:stream_elem_type}}{{ > types/cython_cpp_type}}{{/type:stream_elem_type}}]& c_obj, __RpcOptions rpc_options)
302 cdef class ResponseAndServerStream__{{> stream/response_class_name_suffix}}(ResponseAndServerStream):
303     pass
305 {{/program:response_and_stream_types}}
306 {{/program:has_stream?}}