summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eina_cxx/eina_strbuf.hh
blob: c81a98f95d69ebac7243efb6d27e4dfa00cdff47 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#ifndef EINA_CXX_STRBUF_HH
#define EINA_CXX_STRBUF_HH

#include <Eina.h>
#include <eina_type_traits.hh>
#include <eina_throw.hh>
#include <eina_error.hh>

#include <cstring>

// FIXME: Needs doc, I guess :)

namespace efl { namespace eina {

template <typename strbuf_type, typename basic_type>
struct _strbuf_view_trait
{
   typedef basic_type value_type;
   typedef value_type& reference;
   typedef value_type* pointer;
   typedef value_type const& const_reference;
   typedef value_type const* const_pointer;
   typedef const_pointer const_iterator;
   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
   typedef std::ptrdiff_t difference_type;
   typedef std::size_t size_type;

   _strbuf_view_trait() = delete;

   /** Create a new view from another _strbuf_view_trait */
   _strbuf_view_trait(_strbuf_view_trait const& other)
      : _sb(other._sb)
   {
   }

   /** Create a new view from an existing Eina_Strbuf */
   _strbuf_view_trait(Eina_Strbuf const* sb)
      : _sb(const_cast<Eina_Strbuf *>(sb))
   {
   }

   /**
    * @brief Tells whether this object contains a real Eina_Strbuf or not
    * @return true if wrapping an Eina_Strbuf, false if contains nullptr.
    */
   bool empty() const
   {
      return (_sb != nullptr);
   }

   /**
    * @brief Get the contained C string
    * @return A C-style string (const char*)
    */
   const char * c_str() const
   {
      return ::eina_strbuf_string_get(_sb);
   }

   /**
    * @brief Convert to a string
    * @return A std::string copy
    */
   operator std::string() const
   {
      return std::string(c_str());
   }

   /**
    * @brief Get the size of the string.
    * @return Number of characters in the string.
    */
   size_type size() const
   {
     return eina_strbuf_length_get(_sb);
   }

   /**
    * @brief Alias to @ref size() const.
    */
   size_type length() const
   {
     return size();
   }

   /**
    * @brief Get a constant iterator pointing to the first character of the string.
    * @return Constant iterator to the initial position of the string.
    *
    * This member function returns a constant iterator pointing to the
    * first character of the string. If the string is empty the iterator
    * is equal to the one returned by @ref end() const.
    */
   const_iterator begin() const
   {
      return ::eina_strbuf_string_get(_sb);
   }

   /**
    * @brief Get a constant iterator to the position following the last character of the string.
    * @return Constant iterator to the final position of the string.
    *
    * This member function returns an constant iterator to the position
    * following the last character in the string. If the string is empty
    * the iterator is equal to the one returned by @ref begin().
    *
    * @note Note that attempting to access this position causes undefined
    * behavior.
    */
   const_iterator end() const
   {
      return begin() + size();
   }

   /**
    * @brief Get a constant reverse iterator pointing to the reverse begin of the string.
    * @return Constant reverse iterator pointing to the reverse begin of the string.
    *
    * This member function returns a constant reverse iterator pointing
    * to the last character of the string. If the string is empty the
    * returned reverse iterator is the same as the one returned by
    * @ref rend() const.
    */
   const_reverse_iterator rbegin() const
   {
      return const_reverse_iterator(end());
   }

   /**
    * @brief Get a constant reverse iterator pointing to the reverse end of the string.
    * @return Constant reverse iterator pointing to the reverse end of the string.
    *
    * This member function returns a constant reverse iterator pointing
    * to the position before the first character of the string. If the
    * string is empty the returned iterator is the same as the one
    * returned by @ref rbegin() const.
    *
    * @note Note that attempting to access this position causes undefined
    * behavior.
    */
   const_reverse_iterator rend() const
   {
      return const_reverse_iterator(begin());
   }

   /**
    * @brief Get a constant iterator to the position following the last character of the string.
    * @return Constant iterator to the final position of the string.
    *
    * This member function works just like @ref end() const. But it is
    * granted to always return a constant iterator.
    */
   const_iterator cend() const
   {
      return end();
   }

   /**
    * @brief Get a constant reverse iterator pointing to the reverse begin of the string.
    * @return Constant reverse iterator pointing to the reverse begin of the string.
    *
    * This member function works just like @ref rbegin() const. But it is
    * granted to always return a constant reverse iterator.
    */
   const_reverse_iterator crbegin() const
   {
      return rbegin();
   }

   /**
    * @brief Get a constant reverse iterator pointing to the reverse end of the string.
    * @return Constant reverse iterator pointing to the reverse end of the string.
    *
    * This member function works just like @ref rend() const. But it is
    * granted to always return a constant reverse iterator.
    */
   const_reverse_iterator crend() const
   {
      return rend();
   }

   /**
    * @brief Get the maximum number of characters a string can hold.
    * @return Maximum number of characters a string can hold.
    */
   size_type max_size() const
   {
      return static_cast<size_type>(-1);
   }

   typedef strbuf_type* native_handle_type;
   native_handle_type native_handle() const
   {
      return _sb;
   }

protected:
   strbuf_type *_sb;
};


/** A read-only view of an existing Eina_Strbuf. */
typedef _strbuf_view_trait<const Eina_Strbuf, const char> strbuf_view;

static struct _strbuf_empty_t {} _strbuf_empty;


/** Allocates and manages an Eina_Strbuf, this is a mutable object. */
struct strbuf : public _strbuf_view_trait<Eina_Strbuf, char>
{
   strbuf(const char *str = nullptr)
      : _strbuf_view_trait(::eina_strbuf_new())
   {
      if (!_sb) EFL_CXX_THROW(std::make_error_code(std::errc::not_enough_memory));
      eina_strbuf_append(_sb, str);
   }

   explicit strbuf(_strbuf_empty_t)
      : _strbuf_view_trait(nullptr)
   {
   }

   explicit strbuf(Eina_Strbuf* sb) = delete;

   strbuf(Eina_Strbuf const* sb)
      : strbuf(eina_strbuf_string_get(sb))
   {
   }

   strbuf(Eina_Strbuf const& sb)
      : strbuf(eina_strbuf_string_get(&sb))
   {
   }

   strbuf(strbuf_view const& other)
      : strbuf(other.native_handle())
   {
   }

   strbuf(strbuf&& other)
      : strbuf(_strbuf_empty)
   {
      swap(other);
   }

   template <typename S>
   strbuf(S const& str)
      : strbuf(std::string(str).c_str())
   {
   }

   ~strbuf()
   {
      ::eina_strbuf_free(_sb);
   }

   void swap(strbuf& other)
   {
      std::swap(_sb, other._sb);
   }

   strbuf dup()
   {
      return strbuf(c_str());
   }

   strbuf& reset()
   {
      ::eina_strbuf_reset(_sb);
      return *this;
   }

   template <typename S>
   strbuf& append(S const& str)
   {
      ::eina_strbuf_append(_sb, std::string(str).c_str());
      return *this;
   }

   template <typename S, typename... Args>
   strbuf& append_printf(S const& fmt, Args... args)
   {
      ::eina_strbuf_append_printf(_sb, std::string(fmt).c_str(), args...);
      return *this;
   }

   template <typename S, typename... Args>
   strbuf& insert_printf(S const& fmt, size_t pos, Args... args)
   {
      ::eina_strbuf_insert_printf(_sb, std::string(fmt).c_str(), pos, args...);
      return *this;
   }

   template <typename S>
   strbuf& append_strftime(S const& fmt, struct tm const& time)
   {
      ::eina_strbuf_append_strftime(_sb, std::string(fmt).c_str(), &time);
      return *this;
   }

   template <typename S, typename... Args>
   strbuf& insert_strftime(S const& fmt, size_t pos, struct tm const& time)
   {
      ::eina_strbuf_insert_printf(_sb, std::string(fmt).c_str(), pos, time);
      return *this;
   }

   strbuf& trim()
   {
      ::eina_strbuf_trim(_sb);
      return *this;
   }

   strbuf& ltrim()
   {
      ::eina_strbuf_ltrim(_sb);
      return *this;
   }

   strbuf& rtrim()
   {
      ::eina_strbuf_rtrim(_sb);
      return *this;
   }

   // FIXME: add toupper!!
   strbuf& tolower()
   {
      ::eina_strbuf_tolower(_sb);
      return *this;
   }

   strbuf substr_get(size_t pos, size_t len)
   {
      strbuf sb(_strbuf_empty);
      sb._sb = ::eina_strbuf_substr_get(_sb, pos, len);
      return sb;
   }

   char * steal()
   {
      return ::eina_strbuf_string_steal(_sb);
   }

   template <typename S>
   strbuf& operator=(S const& other)
   {
      return reset().append(other);
   }

   template <typename S>
   strbuf& operator+=(S const& other)
   {
      return append(other);
   }
};


/** A writeable Eina_Strbuf wrapper, does not allocate or destroy the Eina_Strbuf. */
struct strbuf_wrapper : public strbuf
{
   strbuf_wrapper() = delete;
   strbuf_wrapper(strbuf_view const& other) = delete;

   explicit strbuf_wrapper(Eina_Strbuf* sb)
      : strbuf(_strbuf_empty)
   {
      _sb = sb;
   }

   strbuf_wrapper(Eina_Strbuf& sb)
      : strbuf_wrapper(&sb)
   {
   }

   ~strbuf_wrapper()
   {
      _sb = nullptr;
   }
};

} }

#endif