diff options
author | Brad King <brad.king@kitware.com> | 2019-12-04 19:21:48 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2019-12-04 19:22:01 (GMT) |
commit | e9e0e401c657c60ae7916d93321363ba22d3673a (patch) | |
tree | 2a5f818af7d3fd2c385dbaefa3f8db6a6d6ae7f1 /Tests | |
parent | 816aa0740aa2e6f8b15e84c5b9bb95f7b2629847 (diff) | |
parent | fc3b4caa2e6a1970c75830445ef4aa7d03c5a533 (diff) | |
download | CMake-e9e0e401c657c60ae7916d93321363ba22d3673a.zip CMake-e9e0e401c657c60ae7916d93321363ba22d3673a.tar.gz CMake-e9e0e401c657c60ae7916d93321363ba22d3673a.tar.bz2 |
Merge topic 'cmext-memory'
fc3b4caa2e Memory management: cast functions for managed pointers
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !4064
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/CMakeLib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Tests/CMakeLib/testCMExtMemory.cxx | 65 |
2 files changed, 66 insertions, 0 deletions
diff --git a/Tests/CMakeLib/CMakeLists.txt b/Tests/CMakeLib/CMakeLists.txt index 840afc1..976c924 100644 --- a/Tests/CMakeLib/CMakeLists.txt +++ b/Tests/CMakeLib/CMakeLists.txt @@ -25,6 +25,7 @@ set(CMakeLib_TESTS testUVProcessChain.cxx testUVRAII.cxx testUVStreambuf.cxx + testCMExtMemory.cxx ) add_executable(testUVProcessChainHelper testUVProcessChainHelper.cxx) diff --git a/Tests/CMakeLib/testCMExtMemory.cxx b/Tests/CMakeLib/testCMExtMemory.cxx new file mode 100644 index 0000000..6663c17 --- /dev/null +++ b/Tests/CMakeLib/testCMExtMemory.cxx @@ -0,0 +1,65 @@ +#include <iostream> +#include <memory> + +#include <cmext/memory> + +namespace { +class Base +{ +public: + virtual ~Base() = default; +}; + +class Derived : public Base +{ +public: + ~Derived() = default; + + void method() {} +}; + +template <typename T> +class Wrapper +{ +public: + Wrapper(T* v) + : value(v) + { + } + ~Wrapper() { delete value; } + + T* get() const { return value; } + +private: + T* value; +}; + +bool testReferenceCast() +{ + std::cout << "testReferenceCast()" << std::endl; + + std::unique_ptr<Base> u(new Derived); + cm::static_reference_cast<Derived>(u).method(); + cm::dynamic_reference_cast<Derived>(u).method(); + + std::shared_ptr<Base> s(new Derived); + cm::static_reference_cast<Derived>(s).method(); + cm::dynamic_reference_cast<Derived>(s).method(); + + // can also be used with custom wrappers + Wrapper<Base> w(new Derived); + cm::static_reference_cast<Derived>(w).method(); + cm::dynamic_reference_cast<Derived>(w).method(); + + return true; +} +} + +int testCMExtMemory(int /*unused*/, char* /*unused*/ []) +{ + if (!testReferenceCast()) { + return 1; + } + + return 0; +} |