Akumuli
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
cursor.h
1 
23 #pragma once
24 
25 #include <memory>
26 #include <vector>
27 
28 #include "akumuli.h"
29 #include "internal_cursor.h"
30 #include "page.h"
31 
32 namespace Akumuli {
33 
34 std::ostream& operator<<(std::ostream& st, aku_Sample res);
35 
36 class CursorFSM {
37  // user data
38  void* usr_buffer_;
39  size_t usr_buffer_len_;
40  // cursor state
41  size_t write_offset_;
42  bool error_;
43  aku_Status error_code_;
44  bool complete_;
45  bool closed_;
46 public:
47  CursorFSM();
48  ~CursorFSM();
49  // modifiers
50  void put(aku_Sample const& result);
51  void complete();
52  void set_error(aku_Status error_code);
53  void update_buffer(void* buf, size_t buf_len);
54  void update_buffer(CursorFSM* other_fsm);
55  bool close();
56  // accessors
57  bool can_put(int size) const;
58  bool is_done() const;
59  bool get_error(aku_Status* error_code) const;
60  size_t get_data_len() const;
61 };
62 
63 
69  //virtual size_t read(aku_Sample* buf, size_t buf_len) = 0;
70 
77  virtual size_t read_ex(void* buffer, size_t buffer_size) = 0;
78 
80  virtual bool is_done() const = 0;
81 
83  virtual bool is_error(aku_Status* out_error_code_or_null = nullptr) const = 0;
84 
86  virtual void close() = 0;
87 
88  virtual ~ExternalCursor() = default;
89 };
90 
91 
94 
95 
97  void allocate(boost::coroutines::stack_context& ctx, size_t size) const;
98  void deallocate(boost::coroutines::stack_context& ctx) const;
99 };
100 
101 struct CoroCursor : Cursor {
102  boost::shared_ptr<Coroutine> coroutine_;
103  CursorFSM cursor_fsm_;
104 
105  // External cursor implementation
106 
107  //virtual size_t read(aku_Sample* buf, size_t buf_len);
108 
109  virtual size_t read_ex(void* buffer, size_t buffer_size);
110 
111  virtual bool is_done() const;
112 
113  virtual bool is_error(aku_Status* out_error_code_or_null = nullptr) const;
114 
115  virtual void close();
116 
117  // Internal cursor implementation
118 
119  void set_error(Caller& caller, aku_Status error_code);
120 
121  bool put(Caller& caller, aku_Sample const& result);
122 
123  void complete(Caller& caller);
124 
125  template <class Fn_1arg_caller> void start(Fn_1arg_caller const& fn) {
126  coroutine_.reset(new Coroutine(fn, boost::coroutines::attributes(AKU_STACK_SIZE),
128  }
129 
130  template <class Fn_1arg> static std::unique_ptr<ExternalCursor> make(Fn_1arg const& fn) {
131  std::unique_ptr<CoroCursor> cursor(new CoroCursor());
132  cursor->start(fn);
133  return std::move(cursor);
134  }
135 
136  template <class Fn_2arg, class Tobj, class T2nd>
137  static std::unique_ptr<ExternalCursor> make(Fn_2arg const& fn, Tobj obj, T2nd const& arg2) {
138  std::unique_ptr<CoroCursor> cursor(new CoroCursor());
139  cursor->start(std::bind(fn, obj, std::placeholders::_1 /*caller*/, cursor.get(), arg2));
140  return std::move(cursor);
141  }
142 
143  template <class Fn_3arg, class Tobj, class T2nd, class T3rd>
144  static std::unique_ptr<ExternalCursor> make(Fn_3arg const& fn, Tobj obj, T2nd const& arg2,
145  T3rd const& arg3) {
146  std::unique_ptr<CoroCursor> cursor(new CoroCursor());
147  cursor->start(
148  std::bind(fn, obj, std::placeholders::_1 /*caller*/, cursor.get(), arg2, arg3));
149  return std::move(cursor);
150  }
151 
152  template <class Fn_4arg, class Tobj, class T2nd, class T3rd, class T4th>
153  static std::unique_ptr<ExternalCursor> make(Fn_4arg const& fn, Tobj obj, T2nd const& arg2,
154  T3rd const& arg3, T4th const& arg4) {
155  std::unique_ptr<CoroCursor> cursor(new CoroCursor());
156  cursor->start(
157  std::bind(fn, obj, std::placeholders::_1 /*caller*/, cursor.get(), arg2, arg3, arg4));
158  return std::move(cursor);
159  }
160 };
161 
162 } // namespace
bool put(Caller &caller, aku_Sample const &result)
Send offset to caller.
Definition: cursor.cpp:151
virtual bool is_error(aku_Status *out_error_code_or_null=nullptr) const =0
Check is error occured and (optionally) get the error code.
virtual bool is_done() const =0
Check is everything done.
Definition: internal_cursor.h:41
Definition: cursor.h:101
virtual void close()
Finalizer.
Definition: cursor.cpp:137
virtual void close()=0
Finalizer.
virtual size_t read_ex(void *buffer, size_t buffer_size)=0
Read portion of the data to the buffer.
virtual bool is_error(aku_Status *out_error_code_or_null=nullptr) const
Check is error occured and (optionally) get the error code.
Definition: cursor.cpp:133
Definition: cursor.h:36
void set_error(Caller &caller, aku_Status error_code)
Set error and stop execution.
Definition: cursor.cpp:146
virtual bool is_done() const
Check is everything done.
Definition: cursor.cpp:129
virtual size_t read_ex(void *buffer, size_t buffer_size)
Read portion of the data to the buffer.
Definition: cursor.cpp:123
Definition: cursor.h:96
Definition: cursor.h:67
CursorFSM()
Used to check that close method was called.
Definition: cursor.cpp:31
Combined cursor interface.
Definition: cursor.h:93