2011-09-09 22:39:47 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
#ifndef V8_SMART_POINTERS_H_
|
|
|
|
#define V8_SMART_POINTERS_H_
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
template<typename Deallocator, typename T>
|
|
|
|
class SmartPointerBase {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2011-09-08 13:51:06 +00:00
|
|
|
// Default constructor. Constructs an empty scoped pointer.
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartPointerBase() : p_(NULL) {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-09-08 13:51:06 +00:00
|
|
|
// Constructs a scoped pointer from a plain one.
|
2013-12-09 07:41:20 +00:00
|
|
|
explicit SmartPointerBase(T* ptr) : p_(ptr) {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Copy constructor removes the pointer from the original to avoid double
|
|
|
|
// freeing.
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartPointerBase(const SmartPointerBase<Deallocator, T>& rhs)
|
2012-07-19 18:58:23 +00:00
|
|
|
: p_(rhs.p_) {
|
|
|
|
const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 07:41:20 +00:00
|
|
|
T* operator->() const { return p_; }
|
2008-09-11 11:24:45 +00:00
|
|
|
|
2013-12-09 07:41:20 +00:00
|
|
|
T& operator*() const { return *p_; }
|
2008-09-11 11:24:45 +00:00
|
|
|
|
2013-12-09 07:41:20 +00:00
|
|
|
T* get() const { return p_; }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-12-18 16:25:45 +00:00
|
|
|
// You can use [n] to index as if it was a plain pointer.
|
2013-12-09 07:41:20 +00:00
|
|
|
T& operator[](size_t i) {
|
2011-09-08 13:51:06 +00:00
|
|
|
return p_[i];
|
2008-09-11 11:24:45 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-12-18 16:25:45 +00:00
|
|
|
// You can use [n] to index as if it was a plain pointer.
|
2013-12-09 07:41:20 +00:00
|
|
|
const T& operator[](size_t i) const {
|
2012-12-18 16:25:45 +00:00
|
|
|
return p_[i];
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// We don't have implicit conversion to a T* since that hinders migration:
|
|
|
|
// You would not be able to change a method from returning a T* to
|
2011-09-09 22:39:47 +00:00
|
|
|
// returning an SmartArrayPointer<T> and then get errors wherever it is used.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// If you want to take out the plain pointer and don't want it automatically
|
|
|
|
// deleted then call Detach(). Afterwards, the smart pointer is empty
|
|
|
|
// (NULL).
|
2013-12-09 07:41:20 +00:00
|
|
|
T* Detach() {
|
2011-09-08 13:51:06 +00:00
|
|
|
T* temp = p_;
|
|
|
|
p_ = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2013-12-09 07:41:20 +00:00
|
|
|
void Reset(T* new_value) {
|
|
|
|
ASSERT(p_ == NULL || p_ != new_value);
|
2012-12-18 16:25:45 +00:00
|
|
|
if (p_) Deallocator::Delete(p_);
|
|
|
|
p_ = new_value;
|
|
|
|
}
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
// Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like
|
2008-07-03 15:10:15 +00:00
|
|
|
// the copy constructor it removes the pointer in the original to avoid
|
|
|
|
// double freeing.
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartPointerBase<Deallocator, T>& operator=(
|
2012-07-19 18:58:23 +00:00
|
|
|
const SmartPointerBase<Deallocator, T>& rhs) {
|
2008-10-21 09:12:27 +00:00
|
|
|
ASSERT(is_empty());
|
2011-09-08 13:51:06 +00:00
|
|
|
T* tmp = rhs.p_; // swap to handle self-assignment
|
2012-07-19 18:58:23 +00:00
|
|
|
const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
|
2011-09-08 13:51:06 +00:00
|
|
|
p_ = tmp;
|
2008-07-03 15:10:15 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-09 07:41:20 +00:00
|
|
|
bool is_empty() const { return p_ == NULL; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// When the destructor of the scoped pointer is executed the plain pointer
|
|
|
|
// is deleted using DeleteArray. This implies that you must allocate with
|
|
|
|
// NewArray.
|
|
|
|
~SmartPointerBase() { if (p_) Deallocator::Delete(p_); }
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2011-09-08 13:51:06 +00:00
|
|
|
T* p_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
// A 'scoped array pointer' that calls DeleteArray on its pointer when the
|
|
|
|
// destructor is called.
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ArrayDeallocator {
|
|
|
|
static void Delete(T* array) {
|
|
|
|
DeleteArray(array);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class SmartArrayPointer: public SmartPointerBase<ArrayDeallocator<T>, T> {
|
|
|
|
public:
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartArrayPointer() { }
|
|
|
|
explicit SmartArrayPointer(T* ptr)
|
2012-07-19 18:58:23 +00:00
|
|
|
: SmartPointerBase<ArrayDeallocator<T>, T>(ptr) { }
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartArrayPointer(const SmartArrayPointer<T>& rhs)
|
2012-07-19 18:58:23 +00:00
|
|
|
: SmartPointerBase<ArrayDeallocator<T>, T>(rhs) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ObjectDeallocator {
|
2013-03-05 16:22:08 +00:00
|
|
|
static void Delete(T* object) {
|
|
|
|
delete object;
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-05 16:22:08 +00:00
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
template<typename T>
|
|
|
|
class SmartPointer: public SmartPointerBase<ObjectDeallocator<T>, T> {
|
|
|
|
public:
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartPointer() { }
|
|
|
|
explicit SmartPointer(T* ptr)
|
2012-07-19 18:58:23 +00:00
|
|
|
: SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { }
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartPointer(const SmartPointer<T>& rhs)
|
2012-07-19 18:58:23 +00:00
|
|
|
: SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { }
|
|
|
|
};
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
#endif // V8_SMART_POINTERS_H_
|