Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
share.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Inkscape::Util::ptr_shared<T> - like T const *, but stronger.
4 * Used to hold c-style strings for objects that are managed by the gc.
5 *
6 * Authors:
7 * MenTaLguY <mental@rydia.net>
8 *
9 * Copyright (C) 2006 MenTaLguY
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#ifndef SEEN_INKSCAPE_UTIL_SHARE_H
15#define SEEN_INKSCAPE_UTIL_SHARE_H
16
17#include "inkgc/gc-core.h"
18#include <cstring>
19#include <cstddef>
20
21namespace Inkscape {
22namespace Util {
23
25public:
26
27 ptr_shared() : _string(nullptr) {}
28 ptr_shared(ptr_shared const &other) = default;
29
30 operator char const *() const { return _string; }
31 operator bool() const { return _string; }
32
33 char const *pointer() const { return _string; }
34 char const &operator[](int i) const { return _string[i]; }
35
36 ptr_shared operator+(int i) const {
37 return share_unsafe(_string+i);
38 }
39 ptr_shared operator-(int i) const {
40 return share_unsafe(_string-i);
41 }
42 //WARNING: No bounds checking in += and -= functions. Moving the pointer
43 //past the end of the string and then back could probably cause the garbage
44 //collector to deallocate the string inbetween, as there's temporary no
45 //valid reference pointing into the allocated space.
47 _string += i;
48 return *this;
49 }
51 _string -= i;
52 return *this;
53 }
54 std::ptrdiff_t operator-(ptr_shared const &other) {
55 return _string - other._string;
56 }
57
58 ptr_shared &operator=(ptr_shared const &other) = default;
59
60 bool operator==(ptr_shared const &other) const {
61 return _string == other._string;
62 }
63 bool operator!=(ptr_shared const &other) const {
64 return _string != other._string;
65 }
66 bool operator>(ptr_shared const &other) const {
67 return _string > other._string;
68 }
69 bool operator<(ptr_shared const &other) const {
70 return _string < other._string;
71 }
72
73 friend ptr_shared share_unsafe(char const *string);
74
75private:
76 ptr_shared(char const *string) : _string(string) {}
77 static ptr_shared share_unsafe(char const *string) {
78 return ptr_shared(string);
79 }
80
81 //This class (and code using it) assumes that it never has to free this
82 //pointer, and that the memory it points to will not be freed as long as a
83 //ptr_shared pointing to it exists.
84 char const *_string;
85};
86
87ptr_shared share_string(char const *string);
88ptr_shared share_string(char const *string, std::size_t length);
89
90inline ptr_shared share_unsafe(char const *string) {
91 return ptr_shared::share_unsafe(string);
92}
93
94//TODO: Do we need this function?
95inline ptr_shared share_static_string(char const *string) {
96 return share_unsafe(string);
97}
98
99}
100}
101
102#endif
103/*
104 Local Variables:
105 mode:c++
106 c-file-style:"stroustrup"
107 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
108 indent-tabs-mode:nil
109 fill-column:99
110 End:
111*/
112// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
bool operator<(ptr_shared const &other) const
Definition share.h:69
ptr_shared & operator=(ptr_shared const &other)=default
ptr_shared(ptr_shared const &other)=default
char const * _string
Definition share.h:84
static ptr_shared share_unsafe(char const *string)
Definition share.h:77
std::ptrdiff_t operator-(ptr_shared const &other)
Definition share.h:54
char const & operator[](int i) const
Definition share.h:34
ptr_shared & operator-=(int i)
Definition share.h:50
ptr_shared operator+(int i) const
Definition share.h:36
ptr_shared(char const *string)
Definition share.h:76
bool operator>(ptr_shared const &other) const
Definition share.h:66
friend ptr_shared share_unsafe(char const *string)
Definition share.h:90
bool operator!=(ptr_shared const &other) const
Definition share.h:63
ptr_shared & operator+=(int i)
Definition share.h:46
char const * pointer() const
Definition share.h:33
bool operator==(ptr_shared const &other) const
Definition share.h:60
ptr_shared operator-(int i) const
Definition share.h:39
Wrapper for Boehm GC.
Miscellaneous supporting code.
Definition document.h:93
ptr_shared share_unsafe(char const *string)
Definition share.h:90
ptr_shared share_string(char const *string)
Definition share.cpp:20
ptr_shared share_static_string(char const *string)
Definition share.h:95
Helper class to stream background task notifications as a series of messages.