diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:24:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:24:48 +0000 |
commit | cca66b9ec4e494c1d919bff0f71a820d8afab1fa (patch) | |
tree | 146f39ded1c938019e1ed42d30923c2ac9e86789 /src/util/share.h | |
parent | Initial commit. (diff) | |
download | inkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.tar.xz inkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.zip |
Adding upstream version 1.2.2.upstream/1.2.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/util/share.h | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/util/share.h b/src/util/share.h new file mode 100644 index 0000000..e8b9fb1 --- /dev/null +++ b/src/util/share.h @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Util::ptr_shared<T> - like T const *, but stronger. + * Used to hold c-style strings for objects that are managed by the gc. + * + * Authors: + * MenTaLguY <mental@rydia.net> + * + * Copyright (C) 2006 MenTaLguY + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_UTIL_SHARE_H +#define SEEN_INKSCAPE_UTIL_SHARE_H + +#include "inkgc/gc-core.h" +#include <cstring> +#include <cstddef> + +namespace Inkscape { +namespace Util { + +class ptr_shared { +public: + + ptr_shared() : _string(nullptr) {} + ptr_shared(ptr_shared const &other) = default; + + operator char const *() const { return _string; } + operator bool() const { return _string; } + + char const *pointer() const { return _string; } + char const &operator[](int i) const { return _string[i]; } + + ptr_shared operator+(int i) const { + return share_unsafe(_string+i); + } + ptr_shared operator-(int i) const { + return share_unsafe(_string-i); + } + //WARNING: No bounds checking in += and -= functions. Moving the pointer + //past the end of the string and then back could probably cause the garbage + //collector to deallocate the string inbetween, as there's temporary no + //valid reference pointing into the allocated space. + ptr_shared &operator+=(int i) { + _string += i; + return *this; + } + ptr_shared &operator-=(int i) { + _string -= i; + return *this; + } + std::ptrdiff_t operator-(ptr_shared const &other) { + return _string - other._string; + } + + ptr_shared &operator=(ptr_shared const &other) = default; + + bool operator==(ptr_shared const &other) const { + return _string == other._string; + } + bool operator!=(ptr_shared const &other) const { + return _string != other._string; + } + bool operator>(ptr_shared const &other) const { + return _string > other._string; + } + bool operator<(ptr_shared const &other) const { + return _string < other._string; + } + + friend ptr_shared share_unsafe(char const *string); + +private: + ptr_shared(char const *string) : _string(string) {} + static ptr_shared share_unsafe(char const *string) { + return ptr_shared(string); + } + + //This class (and code using it) assumes that it never has to free this + //pointer, and that the memory it points to will not be freed as long as a + //ptr_shared pointing to it exists. + char const *_string; +}; + +ptr_shared share_string(char const *string); +ptr_shared share_string(char const *string, std::size_t length); + +inline ptr_shared share_unsafe(char const *string) { + return ptr_shared::share_unsafe(string); +} + +//TODO: Do we need this function? +inline ptr_shared share_static_string(char const *string) { + return share_unsafe(string); +} + +} +} + +#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 : |