Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WGPU_MAKE_INIT_STRUCT and WGPUStringView #352

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
#include "webgpu.h"
{{end}}

{{- if eq .Name "webgpu"}}
#define WGPU_COMMA ,
#if defined(__cplusplus)
# if __cplusplus >= 201103L
# define WGPU_MAKE_INIT_STRUCT(type, value) (type value)
# else
# define WGPU_MAKE_INIT_STRUCT(type, value) value
# endif
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define WGPU_MAKE_INIT_STRUCT(type, value) ((type) value)
#else
# define WGPU_MAKE_INIT_STRUCT(type, value) value
#endif
{{end}}

/**
* \defgroup Constants
* \brief Constants.
Expand Down Expand Up @@ -221,6 +236,37 @@ typedef struct WGPUChainedStructOut {
* @{
*/

/**
* Nullable value defining a pointer+length view into a UTF-8 encoded string.
*
* Values passed into the API may use the special length value @ref WGPU_STRLEN
* to indicate a null-terminated string.
* Non-null values passed out of the API (for example as callback arguments)
* always provide an explicit length and **may or may not be null-terminated**.
*
* Some inputs to the API accept null values. Those which do not accept null
* values "default" to the empty string when null values are passed.
*
* Values are encoded as follows:
* - `{NULL, WGPU_STRLEN}`: the null value.
* - `{non_null_pointer, WGPU_STRLEN}`: a null-terminated string view.
* - `{any, 0}`: the empty string.
* - `{NULL, non_zero_length}`: not allowed (null dereference).
* - `{non_null_pointer, non_zero_length}`: an explictly-sized string view.
*
* To format explicitly-sized strings with `printf`, use `%.*s`
* (`%s` with a "precision" argument `.*` specifying a max length).
*/
typedef struct WGPUStringView {
char const * WGPU_NULLABLE data;
size_t length;
} WGPUStringView;

#define WGPU_STRING_VIEW_INIT WGPU_MAKE_INIT_STRUCT(WGPUStringView, { \
/*.data=*/NULL WGPU_COMMA \
/*.length=*/WGPU_STRLEN WGPU_COMMA \
})

/**
* \defgroup WGPUCallbackInfo
* \brief Callback info structures that are used in asynchronous functions.
Expand Down
41 changes: 41 additions & 0 deletions webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@
#include <stdint.h>
#include <stddef.h>

#define WGPU_COMMA ,
#if defined(__cplusplus)
# if __cplusplus >= 201103L
# define WGPU_MAKE_INIT_STRUCT(type, value) (type value)
# else
# define WGPU_MAKE_INIT_STRUCT(type, value) value
# endif
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define WGPU_MAKE_INIT_STRUCT(type, value) ((type) value)
#else
# define WGPU_MAKE_INIT_STRUCT(type, value) value
#endif


/**
* \defgroup Constants
Expand Down Expand Up @@ -998,6 +1011,34 @@ typedef struct WGPUChainedStructOut {
* @{
*/

/**
* Nullable value defining a pointer+length view into a string.
*
* Values passed into the API may use the special length value @ref WGPU_STRLEN
* to indicate a null-terminated string.
* Non-null values passed out of the API (for example as callback arguments)
* always provide an explicit length and **may or may not be null-terminated**.
*
* Some inputs to the API accept null values. Those which do not accept null
* values "default" to the empty string when null values are passed.
*
* Values are encoded as follows:
* - `{NULL, WGPU_STRLEN}`: the null value.
* - `{non_null_pointer, WGPU_STRLEN}`: a null-terminated string view.
* - `{any, 0}`: the empty string.
* - `{NULL, non_zero_length}`: not allowed (null dereference).
* - `{non_null_pointer, non_zero_length}`: an explictly-sized string view.
*/
typedef struct WGPUStringView {
char const * WGPU_NULLABLE data;
size_t length;
} WGPUStringView;

#define WGPU_STRING_VIEW_INIT WGPU_MAKE_INIT_STRUCT(WGPUStringView, { \
/*.data=*/NULL WGPU_COMMA \
/*.length=*/WGPU_STRLEN WGPU_COMMA \
})

/**
* \defgroup WGPUCallbackInfo
* \brief Callback info structures that are used in asynchronous functions.
Expand Down
Loading