Add <functional> to files that use std::function
[lsnes.git] / include / core / romimage.hpp
blob021d233bfefaa7b8a5fea6c9e61e2970facb22f4
1 #ifndef _romimage__hpp__included__
2 #define _romimage__hpp__included__
4 #include <functional>
5 #include "core/rom-small.hpp"
6 #include "interface/romtype.hpp"
7 #include "library/fileimage.hpp"
8 #include "library/threads.hpp"
10 //ROM request.
11 struct rom_request
13 //List of core types.
14 std::vector<core_type*> cores;
15 //Selected core (default core on call).
16 bool core_guessed;
17 size_t selected;
18 //Filename selected (on entry, filename hint).
19 bool has_slot[ROM_SLOT_COUNT];
20 bool guessed[ROM_SLOT_COUNT];
21 std::string filename[ROM_SLOT_COUNT];
22 std::string hash[ROM_SLOT_COUNT];
23 std::string hashxml[ROM_SLOT_COUNT];
24 //Canceled flag.
25 bool canceled;
28 /**
29 * A collection of files making up a ROM image.
31 class rom_image
33 public:
34 /**
35 * Create blank ROM
37 rom_image() throw();
38 /**
39 * Take in ROM filename (or a bundle) and load it to memory.
41 * parameter file: The file to load
42 * parameter tmpprefer: The core name to prefer.
43 * throws std::bad_alloc: Not enough memory.
44 * throws std::runtime_error: Loading ROM file failed.
46 rom_image(const std::string& file, const std::string& tmpprefer = "") throw(std::bad_alloc,
47 std::runtime_error);
48 /**
49 * Take a ROM and load it.
51 rom_image(const std::string& file, const std::string& core, const std::string& type,
52 const std::string& region);
53 /**
54 * Load a multi-file ROM.
56 rom_image(const std::string file[ROM_SLOT_COUNT], const std::string& core, const std::string& type,
57 const std::string& region);
58 /**
59 * Take in ROM filename and load it to memory with specified type.
61 * parameter file: The file to load
62 * parameter ctype: The core type to use.
63 * throws std::bad_alloc: Not enough memory.
64 * throws std::runtime_error: Loading ROM file failed.
66 rom_image(const std::string& file, core_type& ctype) throw(std::bad_alloc, std::runtime_error);
67 /**
68 * Destroy ROM image.
70 ~rom_image();
71 /**
72 * Get ROM type.
74 core_type& get_type() { return *rtype; }
75 /**
76 * Get ROM region.
78 core_region& get_region() { return *orig_region; }
79 /**
80 * Do region setup. Changes orig_region to specified if NULL.
82 void setup_region(core_region& reg);
83 /**
84 * Get image.
86 fileimage::image& get_image(size_t index, bool xml)
88 if(index < ROM_SLOT_COUNT) {
89 if(xml)
90 return romxml[index];
91 else
92 return romimg[index];
93 } else
94 return null_img;
96 /**
97 * Get filename of ROM pack, if any.
99 const std::string& get_pack_filename() { return load_filename; }
101 * Get MSU-1 base fileaname.
103 const std::string& get_msu1_base() { return msu1_base; }
105 * Is same ROM type?
107 bool is_of_type(core_type& type) { return (rtype == &type); }
109 * Is file a gamepak?
111 * parameter filename: The file to probe.
112 * retruns: True if gamepak, false if not.
113 * throws std::runtime_error: No such file.
115 static bool is_gamepak(const std::string& filename) throw(std::bad_alloc, std::runtime_error);
116 //ROM functions.
117 std::list<core_region*> get_regions() { return rtype->get_regions(); }
118 const std::string& get_hname() { return rtype->get_hname(); }
119 private:
120 rom_image(const rom_image&);
121 rom_image& operator=(const rom_image&);
122 //Account images.
123 void account_images();
124 //Static NULL image.
125 static fileimage::image null_img;
126 //Loaded ROM images.
127 fileimage::image romimg[ROM_SLOT_COUNT];
128 //Loaded ROM XML (markup) images.
129 fileimage::image romxml[ROM_SLOT_COUNT];
130 //MSU-1 base filename.
131 std::string msu1_base;
132 //Load filename.
133 std::string load_filename;
134 //ROM type.
135 core_type* rtype;
136 //ROM region.
137 core_region* region;
138 //Region ROM was loaded as.
139 core_region* orig_region;
140 //Reference count.
141 threads::lock usage_lock;
142 size_t usage_count;
143 void get() { threads::alock l(usage_lock); usage_count++; }
144 bool put() { threads::alock l(usage_lock); return !--usage_count; }
145 friend class rom_image_handle;
146 //Handle bundle load case.
147 void load_bundle(const std::string& file, std::istream& spec, const std::string& tmpprefer)
148 throw(std::bad_alloc, std::runtime_error);
149 //Tracker.
150 memtracker::autorelease tracker;
154 * A handle for ROM imageset.
156 class rom_image_handle
158 public:
160 * Create a handle to NULL imageset.
162 rom_image_handle()
164 img = &null_img;
167 * Create a new handle with refcount 1. The specified ROM imageset has to be allocated using new.
169 rom_image_handle(rom_image* _img)
171 img = _img;
172 img->usage_count = 1;
175 * Copy-construct a handle.
177 rom_image_handle(const rom_image_handle& h)
179 h.get();
180 img = h.img;
183 * Assign a handle.
185 rom_image_handle& operator=(const rom_image_handle& h)
187 if(img == h.img) return *this;
188 h.get();
189 put();
190 img = h.img;
191 return *this;
194 * Destroy a handle.
196 ~rom_image_handle()
198 put();
201 * Access the handle.
203 rom_image& operator*()
205 return *img;
208 * Access the handle.
210 rom_image* operator->()
212 return img;
214 private:
215 static rom_image null_img;
216 mutable rom_image* img;
217 void get() const { if(img != &null_img) img->get(); }
218 void put() const { if(img != &null_img && img->put()) delete img; }
222 void record_filehash(const std::string& file, uint64_t prefix, const std::string& hash);
223 void set_hasher_callback(std::function<void(uint64_t, uint64_t)> cb);
224 rom_image_handle construct_rom(const std::string& movie_filename, const std::vector<std::string>& cmdline);
226 //Map of preferred cores for each extension and type.
227 extern std::map<std::string, core_type*> preferred_core;
228 //Main hasher
229 extern fileimage::hash lsnes_image_hasher;
231 #endif