Merge pull request #1010 from hannes-harnisch/master

Adding configuration option to disable struct/union setters and union constructors
This commit is contained in:
Andreas Süßenbach 2021-07-06 14:25:51 +02:00 committed by GitHub
commit 793f70dbad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -139,7 +139,7 @@ vk::Image image = device.createImage({{}, vk::ImageType::e2D, vk::Format::eR8G8B
### Designated Initializers ### Designated Initializers
Beginning with C++20, C++ supports designated initializers. As that feature requires to not have any user-declared or inherited constructors, you have to `#define VULKAN_HPP_NO_STRUCT_CONSTRUCTORS`, which removes all the structure constructors from vulkan.hpp. Instead you can then use aggregate initialization. The first few vk-lines in your source might then look like Beginning with C++20, C++ supports designated initializers. As that feature requires to not have any user-declared or inherited constructors, you have to `#define VULKAN_HPP_NO_CONSTRUCTORS`, which removes all the structure and union constructors from vulkan.hpp. Instead you can then use aggregate initialization. The first few vk-lines in your source might then look like
```c++ ```c++
// initialize the vk::ApplicationInfo structure // initialize the vk::ApplicationInfo structure
vk::ApplicationInfo applicationInfo{ .pApplicationName = AppName, vk::ApplicationInfo applicationInfo{ .pApplicationName = AppName,
@ -549,6 +549,10 @@ This is set to be the compiler-dependent attribute used to mark functions as inl
By default, the namespace used with vulkan.hpp is ```vk```. By defining ```VULKAN_HPP_NAMESPACE``` before including vulkan.hpp, you can adjust this. By default, the namespace used with vulkan.hpp is ```vk```. By defining ```VULKAN_HPP_NAMESPACE``` before including vulkan.hpp, you can adjust this.
#### VULKAN_HPP_NO_CONSTRUCTORS
With C++20, designated initializers are available. Their use requires the absence of any user-defined constructors. Define ```VULKAN_HPP_NO_CONSTRUCTORS``` to remove constructors from structs and unions.
#### VULKAN_HPP_NO_EXCEPTIONS #### VULKAN_HPP_NO_EXCEPTIONS
When a vulkan function returns an error code that is not specified to be a success code, an exception is thrown unless ```VULKAN_HPP_NO_EXCEPTIONS``` is defined before including vulkan.hpp. When a vulkan function returns an error code that is not specified to be a success code, an exception is thrown unless ```VULKAN_HPP_NO_EXCEPTIONS``` is defined before including vulkan.hpp.
@ -557,11 +561,15 @@ When a vulkan function returns an error code that is not specified to be a succe
With C++17, all ```vk```-functions returning something are declared with the attribute ```[[nodiscard]]```. This can be removed by defining ```VULKAN_HPP_NO_NODISCARD_WARNINGS``` before including vulkan.hpp. With C++17, all ```vk```-functions returning something are declared with the attribute ```[[nodiscard]]```. This can be removed by defining ```VULKAN_HPP_NO_NODISCARD_WARNINGS``` before including vulkan.hpp.
#### VULKAN_HPP_NO_SETTERS
By defining ```VULKAN_HPP_NO_SETTERS```before including vulkan.hpp, setter member functions will not be available within structs and unions. Modifying their data members will then only be possible via direct assignment.
#### VULKAN_HPP_NO_SMART_HANDLE #### VULKAN_HPP_NO_SMART_HANDLE
By defining ```VULKAN_HPP_NO_SMART_HANDLE``` before including vulkan.hpp, the helper class ```UniqueHandle``` and all the unique handle types are not available. By defining ```VULKAN_HPP_NO_SMART_HANDLE``` before including vulkan.hpp, the helper class ```UniqueHandle``` and all the unique handle types are not available.
### VULKAN_HPP_NO_SPACESHIP_OPERATOR #### VULKAN_HPP_NO_SPACESHIP_OPERATOR
With C++20, the so-called spaceship-operator ```<=>``` is introduced. If that operator is supported, all the structs and classes in vulkan.hpp use the default implementation of it. As currently some implementations of this operator are very slow, and others seem to be incomplete, by defining ```VULKAN_HPP_NO_SPACESHIP_OPERATOR``` before including vulkan.hpp you can remove that operator from those structs and classes. With C++20, the so-called spaceship-operator ```<=>``` is introduced. If that operator is supported, all the structs and classes in vulkan.hpp use the default implementation of it. As currently some implementations of this operator are very slow, and others seem to be incomplete, by defining ```VULKAN_HPP_NO_SPACESHIP_OPERATOR``` before including vulkan.hpp you can remove that operator from those structs and classes.

View File

@ -9049,10 +9049,12 @@ void VulkanHppGenerator::appendStructure( std::string &
if ( !structure.second.returnedOnly ) if ( !structure.second.returnedOnly )
{ {
// only structs that are not returnedOnly get setters! // only structs that are not returnedOnly get setters!
constructorAndSetters += "\n#if !defined( VULKAN_HPP_NO_STRUCT_SETTERS )";
for ( size_t i = 0; i < structure.second.members.size(); i++ ) for ( size_t i = 0; i < structure.second.members.size(); i++ )
{ {
appendStructSetter( constructorAndSetters, stripPrefix( structure.first, "Vk" ), structure.second.members, i ); appendStructSetter( constructorAndSetters, stripPrefix( structure.first, "Vk" ), structure.second.members, i );
} }
constructorAndSetters += "#endif /*VULKAN_HPP_NO_STRUCT_SETTERS*/\n";
} }
// operator==() and operator!=() // operator==() and operator!=()
@ -9286,8 +9288,8 @@ void VulkanHppGenerator::appendUnion( std::string & str, std::pair<std::string,
str += " union " + unionName + str += " union " + unionName +
"\n" "\n"
" {\n" " {\n"
" " + "#if !defined( VULKAN_HPP_NO_UNION_CONSTRUCTORS )\n"
unionName + "( VULKAN_HPP_NAMESPACE::" + unionName + " " + unionName + "( VULKAN_HPP_NAMESPACE::" + unionName +
" const & rhs ) VULKAN_HPP_NOEXCEPT\n" " const & rhs ) VULKAN_HPP_NOEXCEPT\n"
" {\n" " {\n"
" memcpy( static_cast<void*>( this ), &rhs, sizeof( VULKAN_HPP_NAMESPACE::" + " memcpy( static_cast<void*>( this ), &rhs, sizeof( VULKAN_HPP_NAMESPACE::" +
@ -9329,12 +9331,15 @@ void VulkanHppGenerator::appendUnion( std::string & str, std::pair<std::string,
{ "unionName", stripPrefix( structure.first, "Vk" ) } } ); { "unionName", stripPrefix( structure.first, "Vk" ) } } );
firstMember = false; firstMember = false;
} }
str += "#endif /*VULKAN_HPP_NO_UNION_CONSTRUCTORS*/\n";
str += "\n#if !defined( VULKAN_HPP_NO_UNION_SETTERS )";
// one setter per union element // one setter per union element
for ( size_t i = 0; i < structure.second.members.size(); i++ ) for ( size_t i = 0; i < structure.second.members.size(); i++ )
{ {
appendStructSetter( str, stripPrefix( structure.first, "Vk" ), structure.second.members, i ); appendStructSetter( str, stripPrefix( structure.first, "Vk" ), structure.second.members, i );
} }
str += "#endif /*VULKAN_HPP_NO_UNION_SETTERS*/\n";
// assignment operator // assignment operator
static const std::string operatorsTemplate = R"( static const std::string operatorsTemplate = R"(
@ -16283,6 +16288,24 @@ int main( int argc, char ** argv )
# include <vector> # include <vector>
#endif #endif
#if defined( VULKAN_HPP_NO_CONSTRUCTORS )
# if !defined( VULKAN_HPP_NO_STRUCT_CONSTRUCTORS )
# define VULKAN_HPP_NO_STRUCT_CONSTRUCTORS
# endif
# if !defined( VULKAN_HPP_NO_UNION_CONSTRUCTORS )
# define VULKAN_HPP_NO_UNION_CONSTRUCTORS
# endif
#endif
#if defined( VULKAN_HPP_NO_SETTERS )
# if !defined( VULKAN_HPP_NO_STRUCT_SETTERS )
# define VULKAN_HPP_NO_STRUCT_SETTERS
# endif
# if !defined( VULKAN_HPP_NO_UNION_SETTERS )
# define VULKAN_HPP_NO_UNION_SETTERS
# endif
#endif
#if !defined( VULKAN_HPP_ASSERT ) #if !defined( VULKAN_HPP_ASSERT )
# include <cassert> # include <cassert>
# define VULKAN_HPP_ASSERT assert # define VULKAN_HPP_ASSERT assert