From 18a926a63b0151b916d901e44d7777e99de06415 Mon Sep 17 00:00:00 2001 From: Andrew Cox Date: Fri, 12 Dec 2014 17:32:19 +0000 Subject: [PATCH] Added a ScopedPointer to public API A simple template class to call delete on an owned pointer when it goes out of scope, whether that be by ordinary return or stack unwind for exception throw. Change-Id: Id4a9f09d1f0b69f85131f771d48d87bcc137350b Signed-off-by: Andrew Cox --- dali/public-api/common/scoped-pointer.h | 101 ++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 dali/public-api/common/scoped-pointer.h diff --git a/dali/public-api/common/scoped-pointer.h b/dali/public-api/common/scoped-pointer.h new file mode 100644 index 0000000..ebcdcd4 --- /dev/null +++ b/dali/public-api/common/scoped-pointer.h @@ -0,0 +1,101 @@ +#ifndef __DALI_SCOPED_POINTER_H__ +#define __DALI_SCOPED_POINTER_H__ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +namespace Dali +{ + +/** + * @brief Deletes the object pointed-to when it goes out of scope. + * + * A simple template class to call delete on an owned pointer when it goes + * out of scope, whether that be by ordinary return or stack unwind for + * exception throw. + */ +template +class ScopedPointer +{ +public: + + /** + * @brief Construct a ScopedPointer guarding a new Owned*. + */ + ScopedPointer( Owned * const owned ) : + mOwned( owned ) + {} + + /** + * @brief Destroy the ScopedPointer and clean up its Owned*. + */ + ~ScopedPointer() + { + if( mOwned != 0 ) + { + delete mOwned; + mOwned = 0; + } + } + + /** + * @brief Getter for the underlying pointer. + * @return The Owned* guarded by this object. + */ + Owned* Get() const + { + return mOwned; + } + + /** + * @brief Give up ownership of the object guarded by this pointer. + * @return The Owned* previously guarded by this object. + */ + Owned* Release() + { + Owned* const owned = mOwned; + mOwned = 0; + return owned; + } + + /** + * @brief Dereference this pointer. + */ + Owned& operator*() const + { + return *mOwned; + } + + /** + * @brief Allow member access through arrow notation. + */ + Owned * operator->() const + { + return mOwned; + } + +private: + + // Non-copyable: + ScopedPointer( const ScopedPointer& rhs ); + ScopedPointer& operator = ( const ScopedPointer& rhs ); + + Owned* mOwned; +}; + +} /* namespace Dali */ + +#endif /* __DALI_SCOPED_POINTER_H__ */ -- 2.7.4