2010-12-07 20:09:37 +01:00
|
|
|
// Copyright (c) 2010, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
// $Id: scoped_ptr.h 153 2010-12-08 14:45:38Z verm $
|
2010-12-07 20:09:37 +01:00
|
|
|
|
|
|
|
/// @file scoped_ptr.h
|
|
|
|
/// @brief
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
|
|
|
|
/// @class scoped_ptr
|
|
|
|
/// @brief auto_ptr without the transfer of ownership semantics
|
|
|
|
template<class T>
|
|
|
|
class scoped_ptr {
|
|
|
|
T* ptr;
|
|
|
|
scoped_ptr(scoped_ptr const&);
|
|
|
|
scoped_ptr& operator=(scoped_ptr const&);
|
2011-11-12 02:23:09 +01:00
|
|
|
|
|
|
|
typedef T *scoped_ptr<T>::*unspecified_bool_type;
|
2010-12-07 20:09:37 +01:00
|
|
|
public:
|
|
|
|
typedef T element_type;
|
|
|
|
|
|
|
|
T& operator*() const {return *ptr; }
|
|
|
|
T* operator->() const { return ptr; }
|
|
|
|
T* get() const { return ptr; }
|
2011-11-12 02:23:09 +01:00
|
|
|
operator unspecified_bool_type() const { return ptr ? &scoped_ptr<T>::ptr : 0; }
|
2010-12-07 20:09:37 +01:00
|
|
|
|
2011-09-28 21:50:50 +02:00
|
|
|
void reset(T *p = 0) {
|
2010-12-07 20:09:37 +01:00
|
|
|
delete ptr;
|
|
|
|
ptr = p;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:50:50 +02:00
|
|
|
explicit scoped_ptr(T *ptr = 0) : ptr(ptr){ }
|
2010-12-07 20:09:37 +01:00
|
|
|
~scoped_ptr() { delete ptr; }
|
|
|
|
};
|
|
|
|
|
2012-02-20 19:22:04 +01:00
|
|
|
/// A generic scoped holder for non-pointer handles
|
|
|
|
template<class T, class Del = void(*)(T)>
|
|
|
|
class scoped_holder {
|
|
|
|
T value;
|
|
|
|
Del destructor;
|
|
|
|
|
|
|
|
scoped_holder(scoped_holder const&);
|
|
|
|
scoped_holder& operator=(scoped_holder const&);
|
|
|
|
public:
|
|
|
|
operator T() const { return value; }
|
|
|
|
T operator->() const { return value; }
|
|
|
|
|
|
|
|
scoped_holder& operator=(T new_value) {
|
|
|
|
if (value)
|
|
|
|
destructor(value);
|
|
|
|
value = new_value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_holder(T value, Del destructor)
|
|
|
|
: value(value)
|
|
|
|
, destructor(destructor)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~scoped_holder() { if (value) destructor(value); }
|
|
|
|
};
|
|
|
|
|
2010-12-07 20:09:37 +01:00
|
|
|
}
|