13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -----------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Unique pointer.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deUniquePtr.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <exception>
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Object
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Object (bool& exists)
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_exists(exists)
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_exists = true;
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	~Object (void)
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_exists = false;
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool& m_exists;
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct CustomDeleter
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	CustomDeleter (bool* called) : m_called(called) {}
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void operator() (Object* ptr)
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!*m_called);
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete ptr;
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		*m_called = true;
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool* m_called;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
663c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMovePtr<Object> createObject (bool& exists)
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	UniquePtr<Object> objectPtr(new Object(exists));
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return objectPtr.move();
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // anonymous
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid UniquePtr_selfTest (void)
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic test.
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			UniquePtr<Object> ptr(new Object(exists));
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr.get() != DE_NULL);
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Exception test.
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		try
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			UniquePtr<Object> ptr(new Object(exists));
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr.get() != DE_NULL);
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			throw std::exception();
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		catch (const std::exception&)
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(!exists);
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Expression test.
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool test	= (UniquePtr<Object>(new Object(exists))).get() != DE_NULL && exists;
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(test);
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Custom deleter.
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool deleterCalled = false;
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			UniquePtr<Object, CustomDeleter> ptr(new Object(exists), CustomDeleter(&deleterCalled));
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(!deleterCalled);
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr.get() != DE_NULL);
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(deleterCalled);
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// MovePtr -> MovePtr moving
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		MovePtr<Object> ptr(new Object(exists));
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(exists);
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			MovePtr<Object> ptr2 = ptr;
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			// Ownership moved to ptr2, should be deleted when ptr2 goes out of scope.
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// UniquePtr -> MovePtr moving
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		UniquePtr<Object> ptr(new Object(exists));
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(exists);
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			MovePtr<Object> ptr2 = ptr.move();
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			// Ownership moved to ptr2, should be deleted when ptr2 goes out of scope.
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// MovePtr -> UniquePtr moving
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			UniquePtr<Object> ptr(createObject(exists));
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1613fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// MovePtr assignment
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists1 = false;
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists2 = false;
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		MovePtr<Object> ptr1(new Object(exists1));
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		MovePtr<Object> ptr2(new Object(exists2));
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ptr1 = ptr2;
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists1);
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(exists2);
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// MovePtr stealing
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		Object* raw = DE_NULL;
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			MovePtr<Object> ptr1(new Object(exists));
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			raw = ptr1.release();
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(raw != DE_NULL);
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr1.get() == DE_NULL);
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(exists);
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete raw;
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Null MovePtr and assigning to it.
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			MovePtr<Object> ptr1;
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr1.get() == DE_NULL);
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			MovePtr<Object> ptr2(new Object(exists));
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			ptr1 = ptr2;
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr1.get() != DE_NULL);
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr2.get() == DE_NULL);
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2033fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if 0
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// UniquePtr assignment or copy construction should not compile. This
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// piece of code is intentionally commented out. To manually test that
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// copying a UniquePtr is statically forbidden, uncomment and try to
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// compile.
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		UniquePtr<Object> ptr(new Object(exists));
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			UniquePtr<Object> ptr2(ptr);
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de
222