summaryrefslogtreecommitdiff
path: root/src/utils.hh
blob: 3c68f1cdfaa2619c7cdad4e5e1982cc7af8516b6 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef utils_hh_INCLUDED
#define utils_hh_INCLUDED

#include "exception.hh"
#include "assert.hh"

#include <memory>
#include <algorithm>

namespace Kakoune
{
// *** Singleton ***
//
// Singleton helper class, every singleton type T should inherit
// from Singleton<T> to provide a consistent interface.
template<typename T>
class Singleton
{
public:
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

    static T& instance()
    {
        assert (ms_instance);
        return *ms_instance;
    }

    static void delete_instance()
    {
        delete ms_instance;
        ms_instance = nullptr;
    }

protected:
    Singleton()
    {
        assert(not ms_instance);
        ms_instance = static_cast<T*>(this);
    }

    ~Singleton()
    {
        assert(ms_instance == this);
        ms_instance = nullptr;
    }

private:
    static T* ms_instance;
};

template<typename T>
T* Singleton<T>::ms_instance = nullptr;

// *** safe_ptr: objects that assert nobody references them when they die ***

template<typename T>
class safe_ptr
{
public:
    safe_ptr() : m_ptr(nullptr) {}
    explicit safe_ptr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->inc_safe_count(); }
    safe_ptr(const safe_ptr& other) : safe_ptr(other.m_ptr) {}
    safe_ptr(safe_ptr&& other) : m_ptr(other.m_ptr) { other.m_ptr = nullptr; }
    ~safe_ptr() { if (m_ptr) m_ptr->dec_safe_count(); }

    safe_ptr& operator=(const safe_ptr& other)
    {
        if (m_ptr != other.m_ptr)
        {
            if (m_ptr)
                m_ptr->dec_safe_count();
            m_ptr = other.m_ptr;
            if (m_ptr)
                m_ptr->inc_safe_count();
        }
        return *this;
   }

   safe_ptr& operator=(safe_ptr&& other)
   {
       if (m_ptr != other.m_ptr)
       {
           if (m_ptr)
               m_ptr->dec_safe_count();
           m_ptr = other.m_ptr;
           other.m_ptr = nullptr;
       }
       return *this;
   }

   void reset(T* ptr)
   {
       *this = safe_ptr(ptr);
   }

   bool operator== (const safe_ptr& other) const { return m_ptr == other.m_ptr; }
   bool operator!= (const safe_ptr& other) const { return m_ptr != other.m_ptr; }
   bool operator== (T* ptr) const { return m_ptr == ptr; }
   bool operator!= (T* ptr) const { return m_ptr != ptr; }

   T& operator*  () const { return *m_ptr; }
   T* operator-> () const { return m_ptr; }

   T* get() const { return m_ptr; }

   explicit operator bool() const { return m_ptr; }

private:
   T* m_ptr;
};

class SafeCountable
{
public:
   SafeCountable() : m_count(0) {}
   ~SafeCountable() { assert(m_count == 0); }

   void inc_safe_count() const { ++m_count; }
   void dec_safe_count() const { --m_count; assert(m_count >= 0); }

private:
   mutable int m_count;
};

// *** Containers helpers ***

template<typename Container>
struct ReversedContainer
{
    ReversedContainer(Container& container) : container(container) {}
    Container& container;

    decltype(container.rbegin()) begin() { return container.rbegin(); }
    decltype(container.rend())   end()   { return container.rend(); }
};

template<typename Container>
ReversedContainer<Container> reversed(Container& container)
{
    return ReversedContainer<Container>(container);
}


template<typename Container, typename T>
auto find(Container& container, const T& value) -> decltype(container.begin())
{
    return std::find(container.begin(), container.end(), value);
}

template<typename Container, typename T>
auto find_if(Container& container, T op) -> decltype(container.begin())
{
    return std::find_if(container.begin(), container.end(), op);
}


template<typename Container, typename T>
bool contains(const Container& container, const T& value)
{
    return find(container, value) != container.end();
}

// *** On scope end ***
//
// on_scope_end provides a way to register some code to be
// executed when current scope closes.
//
// usage:
// auto cleaner = on_scope_end([]() { ... });
//
// This permits to cleanup c-style resources without implementing
// a wrapping class
template<typename T>
class OnScopeEnd
{
public:
    OnScopeEnd(T func) : m_func(func) {}
    ~OnScopeEnd() { m_func(); }
private:
    T m_func;
};

template<typename T>
OnScopeEnd<T> on_scope_end(T t)
{
    return OnScopeEnd<T>(t);
}

// *** Misc helper functions ***

template<typename T>
bool operator== (const std::unique_ptr<T>& lhs, T* rhs)
{
    return lhs.get() == rhs;
}

inline String escape(const String& name)
{
    return name.replace("([ \\t;])", R"(\\\1)");
}

template<typename T>
const T& clamp(const T& val, const T& min, const T& max)
{
    return (val < min ? min : (val > max ? max : val));
}

}

#endif // utils_hh_INCLUDED