00001 /* 00002 * Copyright 2005-2006 Intel Corporation 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __POINTERS_H__ 00018 #define __POINTERS_H__ 00019 00020 #include "../debug/DebugUtils.h" 00021 00022 namespace oasys { 00023 00032 template <typename _Class> 00033 class ScopePtr { 00034 public: 00035 ScopePtr() : ptr_(0) {} 00036 ScopePtr(_Class* obj) : ptr_(obj) {} 00037 ~ScopePtr() { if(ptr_) { delete ptr_; ptr_ = 0; } } 00038 00039 _Class& operator*() const { return *ptr_; } 00040 _Class* operator->() const { return ptr_; } 00041 00046 ScopePtr& operator=(_Class* ptr) { 00047 ASSERT(ptr_ == NULL); 00048 ptr_ = ptr; 00049 return *this; 00050 } 00051 00057 _Class*& ptr() { return ptr_; } 00058 00063 ScopePtr& operator=(const ScopePtr&); 00064 00065 private: 00066 _Class* ptr_; 00067 }; 00068 00072 class ScopeMalloc { 00073 public: 00074 ScopeMalloc(void* ptr = 0) : ptr_(ptr) {} 00075 00076 ~ScopeMalloc() { 00077 if (ptr_) { 00078 free(ptr_); 00079 ptr_ = 0; 00080 } 00081 } 00082 00087 ScopeMalloc& operator=(void* ptr) { 00088 ASSERT(ptr_ == NULL); 00089 ptr_ = ptr; 00090 return *this; 00091 } 00092 00098 void*& ptr() { return ptr_; } 00099 00104 ScopeMalloc& operator=(const ScopeMalloc&); 00105 00106 private: 00107 void* ptr_; 00108 }; 00109 00110 00111 } // namespace oasys 00112 00113 #endif //__POINTERS_H__