/* * Inkscape::Util::shared_ptr - like T const *, but stronger * * Authors: * MenTaLguY * * Copyright (C) 2006 MenTaLguY * * Released under GNU GPL, read the file 'COPYING' for more information */ #ifndef SEEN_INKSCAPE_UTIL_SHARE_H #define SEEN_INKSCAPE_UTIL_SHARE_H #include "gc-core.h" #include namespace Inkscape { namespace Util { template class shared_ptr { public: shared_ptr() : _obj(NULL) {} template shared_ptr(shared_ptr const &other) : _obj(other._obj) {} T const *pointer() const { return _obj; } template operator T1 const *() const { return _obj; } operator bool() const { return _obj; } T const &operator*() const { return *_obj; } T const *operator->() const { return _obj; } T const &operator[](int i) const { return _obj[i]; } shared_ptr operator+(int i) const { return share_unsafe(_obj+i); } shared_ptr operator-(int i) const { return share_unsafe(_obj-i); } shared_ptr &operator+=(int i) const { _obj += i; return *this; } shared_ptr &operator-=(int i) const { _obj -= i; return *this; } template std::ptrdiff_t operator-(shared_ptr const &other) { return _obj - other._obj; } template shared_ptr &operator=(shared_ptr const &other) { _obj = other._obj; return *this; } template bool operator==(shared_ptr const &other) const { return _obj == other._obj; } template bool operator!=(shared_ptr const &other) const { return _obj != other._obj; } template bool operator>(shared_ptr const &other) const { return _obj > other._obj; } template bool operator<(shared_ptr const &other) const { return _obj < other._obj; } static shared_ptr share_unsafe(T const *obj) { return shared_ptr(obj); } protected: explicit shared_ptr(T const *obj) : _obj(obj) {} private: T const *_obj; }; template inline shared_ptr share(T const *obj) { return share_unsafe(obj ? new T(*obj) : NULL); } shared_ptr share_string(char const *string); shared_ptr share_string(char const *string, std::size_t length); template inline shared_ptr reshare(T const *obj) { return shared_ptr::share_unsafe(obj); } template inline shared_ptr share_unsafe(T const *obj) { return shared_ptr::share_unsafe(obj); } inline shared_ptr share_static_string(char const *string) { return share_unsafe(string); } template inline shared_ptr static_cast_shared(shared_ptr const &ref) { return reshare(static_cast(ref.pointer())); } template inline shared_ptr dynamic_cast_shared(shared_ptr const &ref) { return reshare(dynamic_cast(ref.pointer())); } template inline shared_ptr reinterpret_cast_shared(shared_ptr const &ref) { return reshare(reinterpret_cast(ref.pointer())); } } } #endif /* Local Variables: mode:c++ c-file-style:"stroustrup" c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) indent-tabs-mode:nil fill-column:99 End: */ // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :