libime
inputbuffer.h
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 #ifndef _FCITX_LIBIME_CORE_INPUTBUFFER_H_
7 #define _FCITX_LIBIME_CORE_INPUTBUFFER_H_
8 
9 #include <cstddef>
10 #include <string_view>
11 #include <boost/iterator/iterator_categories.hpp>
12 #include <boost/iterator/iterator_facade.hpp>
13 #include <fcitx-utils/inputbuffer.h>
14 #include <libime/core/libimecore_export.h>
15 
16 namespace libime {
17 class InputBufferPrivate;
18 
19 class LIBIMECORE_EXPORT InputBuffer : public fcitx::InputBuffer {
20 public:
21  class iterator
22  : public boost::iterator_facade<iterator, std::string_view,
23  boost::bidirectional_traversal_tag,
24  std::string_view> {
25  public:
26  iterator() = default;
27  iterator(const InputBuffer *buffer, size_t idx)
28  : buffer_(buffer), idx_(idx) {}
29 
30  bool equal(iterator const &other) const {
31  return buffer_ == other.buffer_ && idx_ == other.idx_;
32  }
33 
34  void increment() { idx_++; }
35 
36  void decrement() { idx_--; }
37 
38  std::string_view dereference() const { return buffer_->at(idx_); }
39 
40  private:
41  const InputBuffer *buffer_ = nullptr;
42  size_t idx_ = 0;
43  };
44 
45  using fcitx::InputBuffer::InputBuffer;
46 
47  using fcitx::InputBuffer::type;
48  // add one overload for string_view
49  bool type(std::string_view s) { return type(s.data(), s.length()); }
50  std::string_view at(size_t i) const;
51 
52  std::string_view operator[](size_t i) const { return at(i); }
53 
54  iterator begin() { return {this, 0}; }
55 
56  iterator end() { return {this, size()}; }
57 };
58 } // namespace libime
59 
60 #endif // _FCITX_LIBIME_CORE_INPUTBUFFER_H_