Lua: Fix type confusion between signed and unsigned
[lsnes.git] / include / library / rrdata.hpp
blob5e8c0487785d8e2b0c251702d2dcc600c9fb4a30
1 #ifndef _library_rrdata__hpp__included__
2 #define _library_rrdata__hpp__included__
4 #define RRDATA_BYTES 32
5 #include <cstdint>
6 #include <stdexcept>
7 #include <vector>
8 #include <string>
9 #include <sstream>
10 #include <iostream>
11 #include <fstream>
12 #include <set>
15 class rrdata_set
17 public:
18 struct instance
20 /**
21 * Create new all zero load ID.
23 instance() throw();
24 /**
25 * Create new load id from bytes.
27 * parameter b: 32 byte array containing the new ID.
29 instance(const unsigned char* b) throw();
30 /**
31 * Create load id from string (mainly intended for debugging).
33 instance(const std::string& id) throw();
34 /**
35 * The load ID.
37 unsigned char bytes[RRDATA_BYTES];
38 /**
39 * Is this ID before another one?
41 * parameter i: Another ID.
42 * returns: True if this ID is before another one, false otherwise.
44 bool operator<(const struct instance& i) const throw();
45 bool operator<=(const struct instance& i) const throw() { return !(i < *this); }
46 bool operator>=(const struct instance& i) const throw() { return !(*this < i); }
47 bool operator>(const struct instance& i) const throw() { return (i < *this); }
48 /**
49 * Is this ID equal to another one?
51 * parameter i: Another ID.
52 * returns: True if this ID is equal to another one, false otherwise.
54 bool operator==(const struct instance& i) const throw();
55 bool operator!=(const struct instance& i) const throw() { return !(*this == i); }
56 /**
57 * Increment this ID.
59 * returns: Copy of the ID before the increment.
61 const struct instance operator++(int) throw();
62 /**
63 * Increment this ID.
65 * returns: Reference to this.
67 struct instance& operator++() throw();
68 /**
69 * Increment this ID by specified amount.
71 * returns: The incremented id.
73 struct instance operator+(unsigned inc) const throw();
74 /**
75 * Difference.
77 * Returns: The difference, or UINT_MAX if too great.
79 unsigned operator-(const struct instance& m) const throw();
81 /**
82 * State block for emergency save.
84 struct esave_state
86 esave_state()
88 initialized = false;
90 void init(const std::set<std::pair<instance, instance>>& obj)
92 if(initialized) return;
93 initialized = true;
94 itr = obj.begin();
95 eitr = obj.end();
97 std::set<std::pair<instance, instance>>::const_iterator next()
99 if(itr == eitr) return itr;
100 return itr++;
102 bool finished()
104 return (itr == eitr);
106 void reset()
108 initialized = false;
109 segptr = segend = pred = instance();
111 instance segptr;
112 instance segend;
113 instance pred;
114 private:
115 bool initialized;
116 std::set<std::pair<instance, instance>>::const_iterator itr;
117 std::set<std::pair<instance, instance>>::const_iterator eitr;
120 * Ctor
122 rrdata_set() throw();
124 * Read the saved set of load IDs for specified project and switch to that project.
126 * parameter projectfile: The name of project backing file.
127 * parameter lazy: If true, just switch to project, don't read the IDs.
128 * throws std::bad_alloc: Not enough memory
130 void read_base(const std::string& projectfile, bool lazy) throw(std::bad_alloc);
132 * Is lazy?
134 bool is_lazy() throw() { return lazy_mode; }
136 * Switch to no project, closing the load IDs.
138 void close() throw();
140 * Add new specified instance to current project.
142 * Not allowed if there is no project open.
144 * parameter i: The load ID to add.
146 void add(const struct instance& i) throw(std::bad_alloc);
148 * Write compressed representation of current load ID set to stream.
150 * parameter strm: The stream to write to.
151 * returns: Rerecord count.
152 * throws std::bad_alloc: Not enough memory.
154 uint64_t write(std::vector<char>& strm) throw(std::bad_alloc);
156 * Get size for compressed representation.
158 * Returns: The size of compressed representation.
159 * Note: Uses no memory.
161 uint64_t size_emerg() const throw();
163 * Write part of compressed representation to buffer.
165 * Parameter state: The state variable (initially, pass esave_state()).
166 * Parameter buf: The buffer to write to.
167 * Parameter bufsize: The buffer size (needs to be at least 36).
168 * Returns: The number of bytes written (0 if at the end).
169 * Note: Uses no memory.
171 size_t write_emerg(struct esave_state& state, char* buf, size_t bufsize) const throw();
173 * Load compressed representation of load ID set from stream and union it with current set to form new current
174 * set.
176 * parameter strm: The stream to read from.
177 * parameter dummy: If true, don't actually do it, just simulate.
178 * returns: Rerecord count.
179 * throws std::bad_alloc: Not enough memory.
181 uint64_t read(std::vector<char>& strm) throw(std::bad_alloc);
183 * Load compressed representation of load ID set from stream, but don't do anything to it.
185 * parameter strm: The stream to read from.
186 * returns: Rerecord count.
187 * throws std::bad_alloc: Not enough memory.
189 static uint64_t count(std::vector<char>& strm) throw(std::bad_alloc);
191 * Count number of rerecords.
193 * returns: Rerecord count.
195 uint64_t count() throw();
197 * Debugging functions.
199 std::string debug_dump();
200 bool debug_add(const instance& b) { return _add(b); }
201 void debug_add(const instance& b, const instance& e) { return _add(b, e); }
202 bool debug_in_set(const instance& b) { return _in_set(b); }
203 bool debug_in_set(const instance& b, const instance& e) { return _in_set(b, e); }
204 uint64_t debug_nodecount(std::set<std::pair<instance, instance>>& set);
205 private:
206 bool _add(const instance& b);
207 void _add(const instance& b, const instance& e);
208 void _add(const instance& b, const instance& e, std::set<std::pair<instance, instance>>& set,
209 uint64_t& cnt);
210 bool _in_set(const instance& b) { return _in_set(b, b + 1); }
211 bool _in_set(const instance& b, const instance& e);
212 uint64_t emerg_action(struct esave_state& state, char* buf, size_t bufsize, uint64_t& scount) const;
214 std::set<std::pair<instance, instance>> data;
215 std::ofstream ohandle;
216 bool handle_open;
217 std::string current_projectfile;
218 bool lazy_mode;
219 uint64_t rcount;
222 std::ostream& operator<<(std::ostream& os, const struct rrdata_set::instance& j);
224 #endif