@@ -74,7 +74,11 @@ class VDeleter {
7474 cleanup ();
7575 }
7676
77- T* operator &() {
77+ const T* operator &() const {
78+ return &object;
79+ }
80+
81+ T* replace () {
7882 cleanup ();
7983 return &object;
8084 }
@@ -83,6 +87,16 @@ class VDeleter {
8387 return object;
8488 }
8589
90+ void operator =(T rhs) {
91+ cleanup ();
92+ object = rhs;
93+ }
94+
95+ template <typename V>
96+ bool operator ==(V rhs) {
97+ return object == T (rhs);
98+ }
99+
86100private:
87101 T object{VK_NULL_HANDLE};
88102 std::function<void (T)> deleter;
@@ -326,7 +340,7 @@ class HelloTriangleApplication {
326340 createInfo.enabledLayerCount = 0 ;
327341 }
328342
329- if (vkCreateInstance (&createInfo, nullptr , & instance) != VK_SUCCESS) {
343+ if (vkCreateInstance (&createInfo, nullptr , instance. replace () ) != VK_SUCCESS) {
330344 throw std::runtime_error (" failed to create instance!" );
331345 }
332346 }
@@ -339,13 +353,13 @@ class HelloTriangleApplication {
339353 createInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
340354 createInfo.pfnCallback = debugCallback;
341355
342- if (CreateDebugReportCallbackEXT (instance, &createInfo, nullptr , & callback) != VK_SUCCESS) {
356+ if (CreateDebugReportCallbackEXT (instance, &createInfo, nullptr , callback. replace () ) != VK_SUCCESS) {
343357 throw std::runtime_error (" failed to set up debug callback!" );
344358 }
345359 }
346360
347361 void createSurface () {
348- if (glfwCreateWindowSurface (instance, window, nullptr , & surface) != VK_SUCCESS) {
362+ if (glfwCreateWindowSurface (instance, window, nullptr , surface. replace () ) != VK_SUCCESS) {
349363 throw std::runtime_error (" failed to create window surface!" );
350364 }
351365 }
@@ -409,7 +423,7 @@ class HelloTriangleApplication {
409423 createInfo.enabledLayerCount = 0 ;
410424 }
411425
412- if (vkCreateDevice (physicalDevice, &createInfo, nullptr , & device) != VK_SUCCESS) {
426+ if (vkCreateDevice (physicalDevice, &createInfo, nullptr , device. replace () ) != VK_SUCCESS) {
413427 throw std::runtime_error (" failed to create logical device!" );
414428 }
415429
@@ -464,7 +478,7 @@ class HelloTriangleApplication {
464478 throw std::runtime_error (" failed to create swap chain!" );
465479 }
466480
467- *& swapChain = newSwapChain;
481+ swapChain = newSwapChain;
468482
469483 vkGetSwapchainImagesKHR (device, swapChain, &imageCount, nullptr );
470484 swapChainImages.resize (imageCount);
@@ -535,7 +549,7 @@ class HelloTriangleApplication {
535549 renderPassInfo.dependencyCount = 1 ;
536550 renderPassInfo.pDependencies = &dependency;
537551
538- if (vkCreateRenderPass (device, &renderPassInfo, nullptr , & renderPass) != VK_SUCCESS) {
552+ if (vkCreateRenderPass (device, &renderPassInfo, nullptr , renderPass. replace () ) != VK_SUCCESS) {
539553 throw std::runtime_error (" failed to create render pass!" );
540554 }
541555 }
@@ -561,7 +575,7 @@ class HelloTriangleApplication {
561575 layoutInfo.bindingCount = bindings.size ();
562576 layoutInfo.pBindings = bindings.data ();
563577
564- if (vkCreateDescriptorSetLayout (device, &layoutInfo, nullptr , & descriptorSetLayout) != VK_SUCCESS) {
578+ if (vkCreateDescriptorSetLayout (device, &layoutInfo, nullptr , descriptorSetLayout. replace () ) != VK_SUCCESS) {
565579 throw std::runtime_error (" failed to create descriptor set layout!" );
566580 }
567581 }
@@ -668,7 +682,7 @@ class HelloTriangleApplication {
668682 pipelineLayoutInfo.setLayoutCount = 1 ;
669683 pipelineLayoutInfo.pSetLayouts = setLayouts;
670684
671- if (vkCreatePipelineLayout (device, &pipelineLayoutInfo, nullptr , & pipelineLayout) != VK_SUCCESS) {
685+ if (vkCreatePipelineLayout (device, &pipelineLayoutInfo, nullptr , pipelineLayout. replace () ) != VK_SUCCESS) {
672686 throw std::runtime_error (" failed to create pipeline layout!" );
673687 }
674688
@@ -688,7 +702,7 @@ class HelloTriangleApplication {
688702 pipelineInfo.subpass = 0 ;
689703 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
690704
691- if (vkCreateGraphicsPipelines (device, VK_NULL_HANDLE, 1 , &pipelineInfo, nullptr , & graphicsPipeline) != VK_SUCCESS) {
705+ if (vkCreateGraphicsPipelines (device, VK_NULL_HANDLE, 1 , &pipelineInfo, nullptr , graphicsPipeline. replace () ) != VK_SUCCESS) {
692706 throw std::runtime_error (" failed to create graphics pipeline!" );
693707 }
694708 }
@@ -711,7 +725,7 @@ class HelloTriangleApplication {
711725 framebufferInfo.height = swapChainExtent.height ;
712726 framebufferInfo.layers = 1 ;
713727
714- if (vkCreateFramebuffer (device, &framebufferInfo, nullptr , & swapChainFramebuffers[i]) != VK_SUCCESS) {
728+ if (vkCreateFramebuffer (device, &framebufferInfo, nullptr , swapChainFramebuffers[i]. replace () ) != VK_SUCCESS) {
715729 throw std::runtime_error (" failed to create framebuffer!" );
716730 }
717731 }
@@ -724,7 +738,7 @@ class HelloTriangleApplication {
724738 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
725739 poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily ;
726740
727- if (vkCreateCommandPool (device, &poolInfo, nullptr , & commandPool) != VK_SUCCESS) {
741+ if (vkCreateCommandPool (device, &poolInfo, nullptr , commandPool. replace () ) != VK_SUCCESS) {
728742 throw std::runtime_error (" failed to create graphics command pool!" );
729743 }
730744 }
@@ -810,7 +824,7 @@ class HelloTriangleApplication {
810824 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
811825 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
812826
813- if (vkCreateSampler (device, &samplerInfo, nullptr , & textureSampler) != VK_SUCCESS) {
827+ if (vkCreateSampler (device, &samplerInfo, nullptr , textureSampler. replace () ) != VK_SUCCESS) {
814828 throw std::runtime_error (" failed to create texture sampler!" );
815829 }
816830 }
@@ -827,7 +841,7 @@ class HelloTriangleApplication {
827841 viewInfo.subresourceRange .baseArrayLayer = 0 ;
828842 viewInfo.subresourceRange .layerCount = 1 ;
829843
830- if (vkCreateImageView (device, &viewInfo, nullptr , & imageView) != VK_SUCCESS) {
844+ if (vkCreateImageView (device, &viewInfo, nullptr , imageView. replace () ) != VK_SUCCESS) {
831845 throw std::runtime_error (" failed to create texture image view!" );
832846 }
833847 }
@@ -848,7 +862,7 @@ class HelloTriangleApplication {
848862 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
849863 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
850864
851- if (vkCreateImage (device, &imageInfo, nullptr , & image) != VK_SUCCESS) {
865+ if (vkCreateImage (device, &imageInfo, nullptr , image. replace () ) != VK_SUCCESS) {
852866 throw std::runtime_error (" failed to create image!" );
853867 }
854868
@@ -860,7 +874,7 @@ class HelloTriangleApplication {
860874 allocInfo.allocationSize = memRequirements.size ;
861875 allocInfo.memoryTypeIndex = findMemoryType (memRequirements.memoryTypeBits , properties);
862876
863- if (vkAllocateMemory (device, &allocInfo, nullptr , & imageMemory) != VK_SUCCESS) {
877+ if (vkAllocateMemory (device, &allocInfo, nullptr , imageMemory. replace () ) != VK_SUCCESS) {
864878 throw std::runtime_error (" failed to allocate image memory!" );
865879 }
866880
@@ -999,7 +1013,7 @@ class HelloTriangleApplication {
9991013 poolInfo.pPoolSizes = poolSizes.data ();
10001014 poolInfo.maxSets = 1 ;
10011015
1002- if (vkCreateDescriptorPool (device, &poolInfo, nullptr , & descriptorPool) != VK_SUCCESS) {
1016+ if (vkCreateDescriptorPool (device, &poolInfo, nullptr , descriptorPool. replace () ) != VK_SUCCESS) {
10031017 throw std::runtime_error (" failed to create descriptor pool!" );
10041018 }
10051019 }
@@ -1054,7 +1068,7 @@ class HelloTriangleApplication {
10541068 bufferInfo.usage = usage;
10551069 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
10561070
1057- if (vkCreateBuffer (device, &bufferInfo, nullptr , & buffer) != VK_SUCCESS) {
1071+ if (vkCreateBuffer (device, &bufferInfo, nullptr , buffer. replace () ) != VK_SUCCESS) {
10581072 throw std::runtime_error (" failed to create buffer!" );
10591073 }
10601074
@@ -1066,7 +1080,7 @@ class HelloTriangleApplication {
10661080 allocInfo.allocationSize = memRequirements.size ;
10671081 allocInfo.memoryTypeIndex = findMemoryType (memRequirements.memoryTypeBits , properties);
10681082
1069- if (vkAllocateMemory (device, &allocInfo, nullptr , & bufferMemory) != VK_SUCCESS) {
1083+ if (vkAllocateMemory (device, &allocInfo, nullptr , bufferMemory. replace () ) != VK_SUCCESS) {
10701084 throw std::runtime_error (" failed to allocate buffer memory!" );
10711085 }
10721086
@@ -1193,8 +1207,8 @@ class HelloTriangleApplication {
11931207 VkSemaphoreCreateInfo semaphoreInfo = {};
11941208 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
11951209
1196- if (vkCreateSemaphore (device, &semaphoreInfo, nullptr , & imageAvailableSemaphore) != VK_SUCCESS ||
1197- vkCreateSemaphore (device, &semaphoreInfo, nullptr , & renderFinishedSemaphore) != VK_SUCCESS) {
1210+ if (vkCreateSemaphore (device, &semaphoreInfo, nullptr , imageAvailableSemaphore. replace () ) != VK_SUCCESS ||
1211+ vkCreateSemaphore (device, &semaphoreInfo, nullptr , renderFinishedSemaphore. replace () ) != VK_SUCCESS) {
11981212
11991213 throw std::runtime_error (" failed to create semaphores!" );
12001214 }
@@ -1278,7 +1292,7 @@ class HelloTriangleApplication {
12781292 createInfo.codeSize = code.size ();
12791293 createInfo.pCode = (uint32_t *) code.data ();
12801294
1281- if (vkCreateShaderModule (device, &createInfo, nullptr , & shaderModule) != VK_SUCCESS) {
1295+ if (vkCreateShaderModule (device, &createInfo, nullptr , shaderModule. replace () ) != VK_SUCCESS) {
12821296 throw std::runtime_error (" failed to create shader module!" );
12831297 }
12841298 }
0 commit comments