summaryrefslogtreecommitdiffstats
path: root/src/util/share.h
blob: e8b9fb12bf86ce3abbee5cb8791fe55536bfda11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 :