diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/Makefile_insert | 4 | ||||
| -rw-r--r-- | src/util/share.cpp | 43 | ||||
| -rw-r--r-- | src/util/share.h | 149 | ||||
| -rw-r--r-- | src/util/shared-c-string-ptr.cpp | 50 | ||||
| -rw-r--r-- | src/util/shared-c-string-ptr.h | 77 |
5 files changed, 194 insertions, 129 deletions
diff --git a/src/util/Makefile_insert b/src/util/Makefile_insert index 1b7119180..bdd613385 100644 --- a/src/util/Makefile_insert +++ b/src/util/Makefile_insert @@ -14,8 +14,8 @@ util_libinkutil_a_SOURCES = \ util/list-container.h \ util/map-list.h \ util/reverse-list.h \ - util/shared-c-string-ptr.h \ - util/shared-c-string-ptr.cpp \ + util/share.h \ + util/share.cpp \ util/tuple.h \ util/units.cpp \ util/units.h diff --git a/src/util/share.cpp b/src/util/share.cpp new file mode 100644 index 000000000..7f2d09bf6 --- /dev/null +++ b/src/util/share.cpp @@ -0,0 +1,43 @@ +/* + * Inkscape::Util::shared_ptr<T> - like T const *, but stronger + * + * Authors: + * MenTaLguY <mental@rydia.net> + * + * Copyright (C) 2006 MenTaLguY + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "util/share.h" +#include <glib/gmessages.h> + +namespace Inkscape { +namespace Util { + +shared_ptr<char> share_string(char const *string) { + g_return_val_if_fail(string != NULL, share_unsafe<char>(NULL)); + return share_string(string, std::strlen(string)); +} + +shared_ptr<char> share_string(char const *string, std::size_t length) { + g_return_val_if_fail(string != NULL, share_unsafe<char>(NULL)); + char *new_string=new (GC::ATOMIC) char[length+1]; + std::memcpy(new_string, string, length); + new_string[length] = 0; + return share_unsafe(new_string); +} + +} +} + +/* + 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 : diff --git a/src/util/share.h b/src/util/share.h new file mode 100644 index 000000000..e9e6c5eb0 --- /dev/null +++ b/src/util/share.h @@ -0,0 +1,149 @@ +/* + * Inkscape::Util::shared_ptr<T> - like T const *, but stronger + * + * Authors: + * MenTaLguY <mental@rydia.net> + * + * 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 <cstring> + +namespace Inkscape { +namespace Util { + +template <typename T> +class shared_ptr { +public: + shared_ptr() : _obj(NULL) {} + + template <typename T1> + shared_ptr(shared_ptr<T1> const &other) : _obj(other._obj) {} + + T const *pointer() const { return _obj; } + + template <typename T1> + 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<T> operator+(int i) const { + return share_unsafe(_obj+i); + } + shared_ptr<T> operator-(int i) const { + return share_unsafe(_obj-i); + } + + shared_ptr<T> &operator+=(int i) const { + _obj += i; + return *this; + } + shared_ptr<T> &operator-=(int i) const { + _obj -= i; + return *this; + } + + template <typename T1> + std::ptrdiff_t operator-(shared_ptr<T1> const &other) { + return _obj - other._obj; + } + + template <typename T1> + shared_ptr<T> &operator=(shared_ptr<T1> const &other) { + _obj = other._obj; + return *this; + } + + template <typename T1> + bool operator==(shared_ptr<T1> const &other) const { + return _obj == other._obj; + } + + template <typename T1> + bool operator!=(shared_ptr<T1> const &other) const { + return _obj != other._obj; + } + + template <typename T1> + bool operator>(shared_ptr<T1> const &other) const { + return _obj > other._obj; + } + + template <typename T1> + bool operator<(shared_ptr<T1> const &other) const { + return _obj < other._obj; + } + + static shared_ptr<T> share_unsafe(T const *obj) { + return shared_ptr<T>(obj); + } + +protected: + explicit shared_ptr(T const *obj) : _obj(obj) {} + +private: + T const *_obj; +}; + +template <typename T> +inline shared_ptr<T> share(T const *obj) { + return share_unsafe(obj ? new T(*obj) : NULL); +} + +shared_ptr<char> share_string(char const *string); +shared_ptr<char> share_string(char const *string, std::size_t length); + +template <typename T> +inline shared_ptr<T> reshare(T const *obj) { + return shared_ptr<T>::share_unsafe(obj); +} + +template <typename T> +inline shared_ptr<T> share_unsafe(T const *obj) { + return shared_ptr<T>::share_unsafe(obj); +} + +template <typename T> +inline shared_ptr<T> share_static(T const *obj) { + return shared_ptr<T>::share_unsafe(obj); +} + +template <typename T1, typename T2> +inline shared_ptr<T1> static_cast_shared(shared_ptr<T2> const &ref) { + return reshare(static_cast<T1 const *>(ref.pointer())); +} + +template <typename T1, typename T2> +inline shared_ptr<T1> dynamic_cast_shared(shared_ptr<T2> const &ref) { + return reshare(dynamic_cast<T1 const *>(ref.pointer())); +} + +template <typename T1, typename T2> +inline shared_ptr<T1> reinterpret_cast_shared(shared_ptr<T2> const &ref) { + return reshare(reinterpret_cast<T1 const *>(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 : diff --git a/src/util/shared-c-string-ptr.cpp b/src/util/shared-c-string-ptr.cpp deleted file mode 100644 index cedf18df4..000000000 --- a/src/util/shared-c-string-ptr.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Inkscape::Util::SharedCStringPtr - shared and immutable strings - * - * Authors: - * MenTaLguY <mental@rydia.net> - * - * Copyright (C) 2004 MenTaLguY - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - -#include <cstring> -#include <glib/gmessages.h> -#include "gc-core.h" -#include "util/shared-c-string-ptr.h" - -namespace Inkscape { - -namespace Util { - -SharedCStringPtr SharedCStringPtr::copy(char const *string) { - g_return_val_if_fail(string != NULL, SharedCStringPtr::coerce(NULL)); - - return SharedCStringPtr::copy(string, std::strlen(string)); -} - -SharedCStringPtr SharedCStringPtr::copy(char const *string, size_t len) { - g_return_val_if_fail(string != NULL, SharedCStringPtr::coerce(NULL)); - - char *dup=new (GC::ATOMIC) gchar[len+1]; - std::memcpy(dup, string, len); - dup[len] = '\000'; - - return SharedCStringPtr::coerce(dup); -} - -} - -} - -/* - 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 : diff --git a/src/util/shared-c-string-ptr.h b/src/util/shared-c-string-ptr.h deleted file mode 100644 index b88f4cf99..000000000 --- a/src/util/shared-c-string-ptr.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Inkscape::Util::SharedCStringPtr - shared and immutable strings - * - * Authors: - * MenTaLguY <mental@rydia.net> - * - * Copyright (C) 2004 MenTaLguY - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - -#ifndef SEEN_INKSCAPE_UTIL_SHARED_C_STRING_PTR_H -#define SEEN_INKSCAPE_UTIL_SHARED_C_STRING_PTR_H - -#include <sys/types.h> -#include <glib/gtypes.h> - -namespace Inkscape { - -namespace Util { - -class SharedCStringPtr { -public: - SharedCStringPtr() : _str(NULL) {} - - operator char const *() const { return cString(); } - - char operator[](size_t i) const { return cString()[i]; } - - char const *cString() const { return _str; } - - static SharedCStringPtr coerce(char const *s) { return SharedCStringPtr(s); } - static SharedCStringPtr copy(char const *s); - static SharedCStringPtr copy(char const *s, size_t len); - - operator bool() const { return _str; } - - bool operator==(SharedCStringPtr const &other) { return _str == other._str; } - bool operator!=(SharedCStringPtr const &other) { return _str != other._str; } - -private: - SharedCStringPtr(char const *s) : _str(s) {} - - char const *_str; -}; - -inline bool operator==(SharedCStringPtr const &ss, char const *s) { - return ss.cString() == s; -} - -inline bool operator==(char const *s, SharedCStringPtr const &ss) { - return operator==(ss, s); -} - -inline bool operator!=(SharedCStringPtr const &ss, char const *s) { - return !operator==(ss, s); -} - -inline bool operator!=(char const *s, SharedCStringPtr const &ss) { - return !operator==(s, ss); -} - -} - -} - -#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 : |
