Actually call on_reset callback
[lsnes.git] / include / library / messagebuffer.hpp
blob70701e70602dd1eaf2bf9a3f2247eb3d2c4869e7
1 #ifndef _library__messagebuffer__hpp__included__
2 #define _library__messagebuffer__hpp__included__
4 #include <stdexcept>
5 #include <map>
6 #include <set>
7 #include <cstdint>
8 #include <string>
10 class messagebuffer
12 public:
13 /**
14 * Update handler.
16 class update_handler
18 public:
19 /**
20 * Destructor.
22 virtual ~update_handler() throw();
23 /**
24 * Handle update.
26 virtual void messagebuffer_update() = 0;
28 /**
29 * Create new message buffer with specified maximum message count.
31 * Parameter maxmessages: The maximum number of messages.
32 * Parameter windowsize: The initial window size.
33 * Throws std::bad_alloc: Not enough memory.
34 * Throws std::logic_error: Windowsize is greater than maxmessages or maxmessages is zero.
36 messagebuffer(size_t maxmessages, size_t windowsize);
38 /**
39 * Add a new message to the buffer.
41 * Parameter msg: The new message to add.
42 * Throws std::bad_alloc: Not enough memory.
43 * Throws std::runtime_error: Thrown through from update handler.
45 void add_message(const std::string& msg);
47 /**
48 * Read a message.
50 * Parameter msgnum: Number of message to read.
51 * Returns: The read message.
52 * Throws std::bad_alloc: Not enough memory.
53 * Throws std::logic_error: Invalid message number.
55 const std::string& get_message(size_t msgnum);
57 /**
58 * Get the number of first message present.
60 * Returns: The number of first message present.
62 size_t get_msg_first() throw();
64 /**
65 * Get the number of messages present.
67 * Returns: The number of messages present.
69 size_t get_msg_count() throw();
71 /**
72 * Get the number of first message visible.
74 * Returns: The number of first message visible.
76 size_t get_visible_first() throw();
78 /**
79 * Get the number of messages visible.
81 * Returns: The number of messages visible.
83 size_t get_visible_count() throw();
85 /**
86 * Is there more messages after the current window?
88 * Returns: True if there is, false if not.
90 bool is_more_messages() throw();
92 /**
93 * Freeze scrolling
95 void freeze_scrolling() throw();
97 /**
98 * Unfreeze scrolling
100 void unfreeze_scrolling() throw();
103 * Freeze updates
105 void freeze_updates() throw();
108 * Unfreeze updates
110 * Returns: True if update is needed.
112 bool unfreeze_updates() throw();
115 * Scroll to beginning.
117 * Throws std::bad_alloc: Not enough memory.
118 * Throws std::runtime_error: Thrown through from update handler.
120 void scroll_beginning();
123 * Scroll up one page.
125 * Throws std::bad_alloc: Not enough memory.
126 * Throws std::runtime_error: Thrown through from update handler.
128 void scroll_up_page();
131 * Scroll up one line.
133 * Throws std::bad_alloc: Not enough memory.
134 * Throws std::runtime_error: Thrown through from update handler.
136 void scroll_up_line();
139 * Scroll down one line.
141 * Throws std::bad_alloc: Not enough memory.
142 * Throws std::runtime_error: Thrown through from update handler.
144 void scroll_down_line();
147 * Scroll down one page.
149 * Throws std::bad_alloc: Not enough memory.
150 * Throws std::runtime_error: Thrown through from update handler.
152 void scroll_down_page();
155 * Scroll to beginning.
157 * Throws std::bad_alloc: Not enough memory.
158 * Throws std::runtime_error: Thrown through from update handler.
160 void scroll_end();
163 * Register an update handler.
165 * Parameter handler: The new handler.
166 * Throws std::bad_alloc: Not enough memory.
168 void register_handler(update_handler& handler);
171 * Unregister an update handler.
173 * Parameter handler: The handler to remove.
174 * Throws std::bad_alloc: Not enough memory.
176 void unregister_handler(update_handler& handler) throw();
179 * Change the window size.
181 * Parameter windowsize: The new window size.
182 * Throws std::bad_alloc: Not enough memory.
183 * Throws std::logic_error: Windowsize is greater than maxmessages or maxmessages is zero.
185 void set_max_window_size(size_t windowsize);
188 * Read the window size.
190 size_t get_max_window_size() throw();
192 * Read the last message.
194 std::string get_last_message();
195 private:
196 void send_notifications();
197 std::map<uint64_t, std::string> messages_buf;
198 uint64_t first_present_message;
199 uint64_t next_message_number;
200 uint64_t window_start;
201 size_t max_messages;
202 size_t window_size;
203 bool scroll_frozen;
204 bool updates_frozen;
205 uint64_t window_start_at_freeze;
206 uint64_t window_size_at_freeze;
207 uint64_t next_message_number_at_freeze;
208 std::set<update_handler*> handlers;
211 #endif