/* * Inkscape::Util::ptr_shared - 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 #include namespace Inkscape { namespace Util { template class ptr_shared { public: ptr_shared() : _obj(NULL) {} template ptr_shared(ptr_shared 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]; } ptr_shared operator+(int i) const { return share_unsafe(_obj+i); } ptr_shared operator-(int i) const { return share_unsafe(_obj-i); } ptr_shared &operator+=(int i) const { _obj += i; return *this; } ptr_shared &operator-=(int i) const { _obj -= i; return *this; } template std::ptrdiff_t operator-(ptr_shared const &other) { return _obj - other._obj; } template ptr_shared &operator=(ptr_shared const &other) { _obj = other._obj; return *this; } template bool operator==(ptr_shared const &other) const { return _obj == other._obj; } template bool operator!=(ptr_shared const &other) const { return _obj != other._obj; } template bool operator>(ptr_shared const &other) const { return _obj > other._obj; } template bool operator<(ptr_shared const &other) const { return _obj < other._obj; } static ptr_shared share_unsafe(T const *obj) { return ptr_shared(obj); } protected: explicit ptr_shared(T const *obj) : _obj(obj) {} private: T const *_obj; }; template inline ptr_shared share(T const *obj) { return share_unsafe(obj ? new T(*obj) : NULL); } ptr_shared share_string(char const *string); ptr_shared share_string(char const *string, std::size_t length); template inline ptr_shared reshare(T const *obj) { return ptr_shared::share_unsafe(obj); } template inline ptr_shared share_unsafe(T const *obj) { return ptr_shared::share_unsafe(obj); } inline ptr_shared share_static_string(char const *string) { return share_unsafe(string); } template inline ptr_shared static_cast_shared(ptr_shared const &ref) { return reshare(static_cast(ref.pointer())); } template inline ptr_shared dynamic_cast_shared(ptr_shared const &ref) { return reshare(dynamic_cast(ref.pointer())); } template inline ptr_shared reinterpret_cast_shared(ptr_shared 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:fileencoding=utf-8:textwidth=99 :