Add a home brewed make_unique to ease creating unique_ptrs.
authorLei Zhang <antiagainst@gmail.com>
Fri, 19 Aug 2016 03:16:21 +0000 (23:16 -0400)
committerLei Zhang <antiagainst@google.com>
Wed, 24 Aug 2016 13:41:55 +0000 (09:41 -0400)
source/opt/libspirv.cpp
source/opt/make_unique.h [new file with mode: 0644]
test/opt/pass_fixture.h
test/opt/test_iterator.cpp
test/opt/test_pass_manager.cpp
test/opt/test_types.cpp

index c076762..a8eced0 100644 (file)
@@ -27,6 +27,7 @@
 #include "libspirv.hpp"
 
 #include "ir_loader.h"
+#include "make_unique.h"
 
 namespace spvtools {
 
@@ -36,8 +37,8 @@ namespace {
 spv_result_t SetSpvHeader(void* builder, spv_endianness_t, uint32_t magic,
                           uint32_t version, uint32_t generator,
                           uint32_t id_bound, uint32_t reserved) {
-  reinterpret_cast<ir::IrLoader*>(builder)->SetModuleHeader(
-      magic, version, generator, id_bound, reserved);
+  reinterpret_cast<ir::IrLoader*>(builder)
+      ->SetModuleHeader(magic, version, generator, id_bound, reserved);
   return SPV_SUCCESS;
 };
 
@@ -88,7 +89,7 @@ std::unique_ptr<ir::Module> SpvTools::BuildModule(
     const std::vector<uint32_t>& binary) {
   spv_diagnostic diagnostic = nullptr;
 
-  std::unique_ptr<ir::Module> module(new ir::Module);
+  auto module = MakeUnique<ir::Module>();
   ir::IrLoader loader(module.get());
 
   spv_result_t status =
diff --git a/source/opt/make_unique.h b/source/opt/make_unique.h
new file mode 100644 (file)
index 0000000..3701c5d
--- /dev/null
@@ -0,0 +1,42 @@
+// Copyright (c) 2016 Google Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and/or associated documentation files (the
+// "Materials"), to deal in the Materials without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Materials, and to
+// permit persons to whom the Materials are furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Materials.
+//
+// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
+// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
+// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
+//    https://www.khronos.org/registry/
+//
+// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
+
+#ifndef LIBSPIRV_OPT_MAKE_UNIQUE_H_
+#define LIBSPIRV_OPT_MAKE_UNIQUE_H_
+
+#include <memory>
+#include <utility>
+
+namespace spvtools {
+
+template <typename T, typename... Args>
+std::unique_ptr<T> MakeUnique(Args&&... args) {
+  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+}  // namespace spvtools
+
+#endif  // LIBSPIRV_OPT_MAKE_UNIQUE_H_
index 9d403d0..848de7d 100644 (file)
@@ -27,7 +27,6 @@
 #ifndef LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_
 #define LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_
 
-#include <memory>
 #include <string>
 #include <tuple>
 #include <vector>
@@ -37,6 +36,7 @@
 #include "opt/libspirv.hpp"
 #include "opt/pass_manager.h"
 #include "opt/passes.h"
+#include "opt/make_unique.h"
 
 namespace spvtools {
 
@@ -59,8 +59,8 @@ class PassTest : public TestT {
   std::tuple<std::string, bool> OptimizeAndDisassemble(
       opt::Pass* pass, const std::string& original, bool skip_nop = false) {
     std::unique_ptr<ir::Module> module = tools_.BuildModule(original);
-    EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
-                               << original << std::endl;
+    EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n" << original
+                               << std::endl;
     if (!module) {
       return std::make_tuple(std::string(), false);
     }
@@ -71,8 +71,7 @@ class PassTest : public TestT {
     module->ToBinary(&binary, skip_nop);
     std::string optimized;
     EXPECT_EQ(SPV_SUCCESS, tools_.Disassemble(binary, &optimized))
-        << "Disassembling failed for shader:\n"
-        << original << std::endl;
+        << "Disassembling failed for shader:\n" << original << std::endl;
     return std::make_tuple(optimized, modified);
   }
 
@@ -82,7 +81,7 @@ class PassTest : public TestT {
   template <typename PassT>
   std::tuple<std::string, bool> SinglePassRunAndDisassemble(
       const std::string& assembly, bool skip_nop = false) {
-    auto pass = std::unique_ptr<PassT>(new PassT);
+    auto pass = MakeUnique<PassT>();
     return OptimizeAndDisassemble(pass.get(), assembly, skip_nop);
   }
 
index c67886f..cf79348 100644 (file)
@@ -30,6 +30,7 @@
 #include "gmock/gmock.h"
 
 #include "opt/iterator.h"
+#include "opt/make_unique.h"
 
 namespace {
 
@@ -148,7 +149,7 @@ TEST(Iterator, InsertBeginEnd) {
   // Insert at the beginning
   expected.insert(expected.begin(), -100);
   ir::UptrVectorIterator<int> begin(&data, data.begin());
-  auto insert_point = begin.InsertBefore(std::unique_ptr<int>(new int(-100)));
+  auto insert_point = begin.InsertBefore(MakeUnique<int>(-100));
   for (int i = 0; i < count + 1; ++i) {
     actual.push_back(*(insert_point++));
   }
@@ -159,9 +160,9 @@ TEST(Iterator, InsertBeginEnd) {
   expected.push_back(-36);
   expected.push_back(-77);
   ir::UptrVectorIterator<int> end(&data, data.end());
-  end = end.InsertBefore(std::unique_ptr<int>(new int(-77)));
-  end = end.InsertBefore(std::unique_ptr<int>(new int(-36)));
-  end = end.InsertBefore(std::unique_ptr<int>(new int(-42)));
+  end = end.InsertBefore(MakeUnique<int>(-77));
+  end = end.InsertBefore(MakeUnique<int>(-36));
+  end = end.InsertBefore(MakeUnique<int>(-42));
 
   actual.clear();
   begin = ir::UptrVectorIterator<int>(&data, data.begin());
@@ -189,8 +190,8 @@ TEST(Iterator, InsertMiddle) {
 
   ir::UptrVectorIterator<int> it(&data, data.begin());
   for (int i = 0; i < insert_pos; ++i) ++it;
-  it = it.InsertBefore(std::unique_ptr<int>(new int(-100)));
-  it = it.InsertBefore(std::unique_ptr<int>(new int(-42)));
+  it = it.InsertBefore(MakeUnique<int>(-100));
+  it = it.InsertBefore(MakeUnique<int>(-42));
   auto begin = ir::UptrVectorIterator<int>(&data, data.begin());
   for (int i = 0; i < count + 2; ++i) {
     actual.push_back(*(begin++));
index 5c48f8f..0e8c45a 100644 (file)
@@ -25,6 +25,7 @@
 // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
 
 #include "pass_fixture.h"
+#include "opt/make_unique.h"
 
 namespace {
 
@@ -38,7 +39,7 @@ TEST(PassManager, Interface) {
   EXPECT_EQ(1u, manager.NumPasses());
   EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
 
-  manager.AddPass(std::unique_ptr<opt::NullPass>(new opt::NullPass));
+  manager.AddPass(MakeUnique<opt::NullPass>());
   EXPECT_EQ(2u, manager.NumPasses());
   EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
   EXPECT_STREQ("null", manager.GetPass(1)->name());
@@ -54,7 +55,7 @@ TEST(PassManager, Interface) {
 class AppendOpNopPass : public opt::Pass {
   const char* name() const override { return "AppendOpNop"; }
   bool Process(ir::Module* module) override {
-    std::unique_ptr<ir::Instruction> inst(new ir::Instruction());
+    auto inst = MakeUnique<ir::Instruction>();
     module->AddDebugInst(std::move(inst));
     return true;
   }
@@ -64,8 +65,7 @@ class AppendOpNopPass : public opt::Pass {
 class DuplicateInstPass : public opt::Pass {
   const char* name() const override { return "DuplicateInst"; }
   bool Process(ir::Module* module) override {
-    std::unique_ptr<ir::Instruction> inst(
-        new ir::Instruction(*(--module->debug_end())));
+    auto inst = MakeUnique<ir::Instruction>(*(--module->debug_end()));
     module->AddDebugInst(std::move(inst));
     return true;
   }
index b9b336e..a2e75e8 100644 (file)
 
 #include <gtest/gtest.h>
 
-#include "source/opt/types.h"
+#include "opt/make_unique.h"
+#include "opt/types.h"
 
 namespace {
 
 using namespace spvtools::opt::analysis;
+using spvtools::MakeUnique;
 
 // Fixture class providing some element types.
 class SameTypeTest : public ::testing::Test {
@@ -241,18 +243,18 @@ TEST(Types, FloatWidth) {
 }
 
 TEST(Types, VectorElementCount) {
-  auto s32 = std::unique_ptr<Integer>(new Integer(32, true));
+  auto s32 = MakeUnique<Integer>(32, true);
   for (uint32_t c : {2, 3, 4}) {
-    auto s32v = std::unique_ptr<Vector>(new Vector(s32.get(), c));
+    auto s32v = MakeUnique<Vector>(s32.get(), c);
     EXPECT_EQ(c, s32v->element_count());
   }
 }
 
 TEST(Types, MatrixElementCount) {
-  auto s32 = std::unique_ptr<Integer>(new Integer(32, true));
-  auto s32v4 = std::unique_ptr<Vector>(new Vector(s32.get(), 4));
+  auto s32 = MakeUnique<Integer>(32, true);
+  auto s32v4 = MakeUnique<Vector>(s32.get(), 4);
   for (uint32_t c : {1, 2, 3, 4, 10, 100}) {
-    auto s32m = std::unique_ptr<Matrix>(new Matrix(s32v4.get(), c));
+    auto s32m = MakeUnique<Matrix>(s32v4.get(), c);
     EXPECT_EQ(c, s32m->element_count());
   }
 }