summaryrefslogtreecommitdiffstats
path: root/intl/icu_capi/cpp/include/ICU4XList.hpp
blob: e2212c021ad9df237c6a218dcb995cad1c9187a9 (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
#ifndef ICU4XList_HPP
#define ICU4XList_HPP
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <algorithm>
#include <memory>
#include <variant>
#include <optional>
#include "diplomat_runtime.hpp"

#include "ICU4XList.h"

class ICU4XList;

/**
 * A destruction policy for using ICU4XList with std::unique_ptr.
 */
struct ICU4XListDeleter {
  void operator()(capi::ICU4XList* l) const noexcept {
    capi::ICU4XList_destroy(l);
  }
};

/**
 * A list of strings
 */
class ICU4XList {
 public:

  /**
   * Create a new list of strings
   */
  static ICU4XList create();

  /**
   * Create a new list of strings with preallocated space to hold
   * at least `capacity` elements
   */
  static ICU4XList create_with_capacity(size_t capacity);

  /**
   * Push a string to the list
   * 
   * For C++ users, potentially invalid UTF8 will be handled via
   * REPLACEMENT CHARACTERs
   */
  void push(const std::string_view val);

  /**
   * The number of elements in this list
   */
  size_t len() const;
  inline const capi::ICU4XList* AsFFI() const { return this->inner.get(); }
  inline capi::ICU4XList* AsFFIMut() { return this->inner.get(); }
  inline ICU4XList(capi::ICU4XList* i) : inner(i) {}
  ICU4XList() = default;
  ICU4XList(ICU4XList&&) noexcept = default;
  ICU4XList& operator=(ICU4XList&& other) noexcept = default;
 private:
  std::unique_ptr<capi::ICU4XList, ICU4XListDeleter> inner;
};


inline ICU4XList ICU4XList::create() {
  return ICU4XList(capi::ICU4XList_create());
}
inline ICU4XList ICU4XList::create_with_capacity(size_t capacity) {
  return ICU4XList(capi::ICU4XList_create_with_capacity(capacity));
}
inline void ICU4XList::push(const std::string_view val) {
  capi::ICU4XList_push(this->inner.get(), val.data(), val.size());
}
inline size_t ICU4XList::len() const {
  return capi::ICU4XList_len(this->inner.get());
}
#endif