diff --git a/externals/coda-oss/UnitTest/TestCase.h b/externals/coda-oss/UnitTest/TestCase.h index 87a8d8c96..5da55e9a1 100644 --- a/externals/coda-oss/UnitTest/TestCase.h +++ b/externals/coda-oss/UnitTest/TestCase.h @@ -10,15 +10,17 @@ #undef TEST_CHECK #undef TEST_ASSERT #undef TEST_ASSERT_NULL +#undef TEST_ASSERT_NOT_NULL #undef TEST_ASSERT_TRUE #undef TEST_ASSERT_FALSE #undef TEST_MAIN #undef TEST_CASE #define TEST_CHECK(X) #define TEST_MAIN(X) -#define TEST_ASSERT_NULL(X) testName, Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsNull((X)) -#define TEST_ASSERT_TRUE(X) testName, Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsTrue((X)) -#define TEST_ASSERT_FALSE(X) testName, Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsFalse((X)) +#define TEST_ASSERT_NULL(X) testName, Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsNull(X) +#define TEST_ASSERT_NOT_NULL(X) testName, Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsNotNull(X) +#define TEST_ASSERT_TRUE(X) testName, Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsTrue(X) +#define TEST_ASSERT_FALSE(X) testName, Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsFalse(X) #define TEST_ASSERT(X) TEST_ASSERT_TRUE(X) #define CODA_OSS_testMethod_(X) testMethod ## _ ## X #define TEST_CASE(X) TEST_METHOD(X) { CODA_OSS_testMethod_(X)(#X); } void CODA_OSS_testMethod_(X)(std::string testName) diff --git a/externals/coda-oss/UnitTest/pch.h b/externals/coda-oss/UnitTest/pch.h index 1f3867fb2..d47d48d8e 100644 --- a/externals/coda-oss/UnitTest/pch.h +++ b/externals/coda-oss/UnitTest/pch.h @@ -57,6 +57,7 @@ #include "import/except.h" #include "import/mem.h" #include +#include #include "import/cli.h" #include "polygon/DrawPolygon.h" #include "polygon/PolygonMask.h" diff --git a/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj b/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj index f8b3725dc..b622944fa 100644 --- a/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj +++ b/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj @@ -114,6 +114,7 @@ + diff --git a/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj.filters b/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj.filters index f4f2ef1df..34ff4e3d0 100644 --- a/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj.filters +++ b/externals/coda-oss/modules/c++/coda-oss-lite.vcxproj.filters @@ -717,6 +717,9 @@ sys + + mem + diff --git a/externals/coda-oss/modules/c++/mem/include/mem/AutoPtr.h b/externals/coda-oss/modules/c++/mem/include/mem/AutoPtr.h new file mode 100644 index 000000000..a7d010c86 --- /dev/null +++ b/externals/coda-oss/modules/c++/mem/include/mem/AutoPtr.h @@ -0,0 +1,113 @@ +/* ========================================================================= + * This file is part of mem-c++ + * ========================================================================= + * + * (C) Copyright 2004 - 2018, MDA Information Systems LLC + * (C) Copyright 2022, Maxar Technologies, Inc. + * + * mem-c++ is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; If not, + * see . + * + */ + +#ifndef CODA_OSS_mem_AutoPtr_h_INCLUDED_ +#define CODA_OSS_mem_AutoPtr_h_INCLUDED_ +#pragma once + +#include +#include + +#include "coda_oss/memory.h" + +namespace mem +{ +// std::auto_ptr (deprecated in C++17) is copyable while +// std::unique_ptr (new in C++11) isn't. While unique_ptr +// is right, making the change doesn't always work well with +// legacy code. Using shared_ptr instead of unique_ptr is +// copyable, but that changes semantics: the "old" copied-from +// object is still valid. +// +// Thus, this class to help make the transition easier. +// Using (very) sparingly! + +template +class AutoPtr final +{ + std::unique_ptr ptr_; + +public: + // https://en.cppreference.com/w/cpp/memory/auto_ptr/auto_ptr + explicit AutoPtr(T* p = nullptr) noexcept : ptr_(p) + { + } + + AutoPtr& operator=(AutoPtr& r) noexcept + { + reset(r.release()); + return *this; + } + AutoPtr(AutoPtr& r) noexcept + { + *this = r; + } + AutoPtr& operator=(const AutoPtr&) = delete; // can't change a "const" object + AutoPtr(const AutoPtr&) = delete; + + ~AutoPtr() = default; + AutoPtr(AutoPtr&&) = default; + AutoPtr& operator=(AutoPtr&&) = default; + + template + AutoPtr& operator=(std::unique_ptr&& p) noexcept + { + ptr_ = std::move(p); + return *this; + } + template + AutoPtr(std::unique_ptr&& p) noexcept + { + *this = std::move(p); + } + + + T* get() const noexcept + { + return ptr_.get(); + } + + T* release() noexcept + { + return ptr_.release(); + } + + template + void reset(U* p = nullptr) noexcept + { + ptr_.reset(p); + } + + T& operator*() const noexcept + { + return *get(); + } + T* operator->() const noexcept + { + return get(); + } +}; + +} // namespace mem + +#endif // CODA_OSS_mem_AutoPtr_h_INCLUDED_ diff --git a/externals/coda-oss/modules/c++/mem/unittests/test_unique_ptr.cpp b/externals/coda-oss/modules/c++/mem/unittests/test_unique_ptr.cpp index a4e53aeb8..9b462b4b6 100644 --- a/externals/coda-oss/modules/c++/mem/unittests/test_unique_ptr.cpp +++ b/externals/coda-oss/modules/c++/mem/unittests/test_unique_ptr.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "TestCase.h" @@ -87,7 +88,47 @@ TEST_CASE(test_make_unique) } } +static void f(const std::string& testName, mem::AutoPtr p) +{ + TEST_ASSERT_NOT_NULL(p.get()); + TEST_ASSERT_EQ(123, p->mVal); +} +TEST_CASE(memAutoPtr) +{ + { + mem::AutoPtr fooCtor; + TEST_ASSERT_NULL(fooCtor.get()); + + fooCtor.reset(new Foo(123)); + TEST_ASSERT_NOT_NULL(fooCtor.get()); + TEST_ASSERT_EQ(123, fooCtor->mVal); + } + { + mem::AutoPtr fooCtor(new Foo(123)); + TEST_ASSERT_NOT_NULL(fooCtor.get()); + TEST_ASSERT_EQ(123, fooCtor->mVal); + } + { + mem::AutoPtr fooCtor(new Foo(123)); + mem::AutoPtr other(fooCtor); + TEST_ASSERT_NOT_NULL(other.get()); + TEST_ASSERT_EQ(123, other->mVal); + } + { + mem::AutoPtr fooCtor(new Foo(123)); + f(testName, fooCtor); + TEST_ASSERT_NULL(fooCtor.get()); + } + { + std::unique_ptr fooCtor(new Foo(123)); + f(testName, std::move(fooCtor)); + TEST_ASSERT_NULL(fooCtor.get()); + } +} + + TEST_MAIN( TEST_CHECK(testStdUniquePtr); TEST_CHECK(test_make_unique); + TEST_CHECK(memAutoPtr); ) diff --git a/externals/coda-oss/modules/c++/pch.h b/externals/coda-oss/modules/c++/pch.h index 3ae1f6303..4cfebea98 100644 --- a/externals/coda-oss/modules/c++/pch.h +++ b/externals/coda-oss/modules/c++/pch.h @@ -18,6 +18,7 @@ #pragma warning(disable: 4355) // '...': used in base member initializer list #pragma warning(disable: 5220) // '...': a non-static data member with a volatile qualified type no longer implies that compiler generated copy/move constructors and copy/move assignment operators are not trivial #pragma warning(disable: 5204) // '...': class has virtual functions, but its trivial destructor is not virtual; instances of objects derived from this class may not be destructed correctly +#pragma warning(disable : 5264) // '...': '...' variable is not used // add headers that you want to pre-compile here #include "framework.h" @@ -51,6 +52,7 @@ #include #include #include +#include #include #include #include