1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2019 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2000-2001 Simon White
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22 
23 #ifndef SIDBUILDER_H
24 #define SIDBUILDER_H
25 
26 #include <set>
27 #include <string>
28 
29 #include "sidplayfp/SidConfig.h"
30 
31 namespace libsidplayfp
32 {
33 class sidemu;
34 class EventScheduler;
35 }
36 
37 /**
38  * Base class for sid builders.
39  */
40 class sidbuilder
41 {
42 protected:
43     typedef std::set<libsidplayfp::sidemu*> emuset_t;
44 
45 private:
46     const char * const m_name;
47 
48 protected:
49     std::string m_errorBuffer;
50 
51     emuset_t sidobjs;
52 
53     bool m_status;
54 
55 protected:
56     /**
57      * Utility class for setting emu parameters in builders.
58      */
59     template<class Temu, typename Tparam>
60     class applyParameter
61     {
62     protected:
63         Tparam m_param;
64         void (Temu::*m_method)(Tparam);
65 
66     public:
67         applyParameter(void (Temu::*method)(Tparam), Tparam param) :
68             m_param(param),
69             m_method(method) {}
70         void operator() (libsidplayfp::sidemu *e) { (static_cast<Temu*>(e)->*m_method)(m_param); }
71     };
72 
73 public:
74     sidbuilder(const char * const name) :
75         m_name(name),
76         m_errorBuffer("N/A"),
77         m_status(true) {}
78     virtual ~sidbuilder() {}
79 
80     /**
81      * The number of used devices.
82      *
83      * @return number of used sids, 0 if none.
84      */
85     unsigned int usedDevices() const { return sidobjs.size(); }
86 
87     /**
88      * Available devices.
89      *
90      * @return the number of available sids, 0 = endless.
91      */
92     virtual unsigned int availDevices() const = 0;
93 
94     /**
95      * Create the sid emu.
96      *
97      * @param sids the number of required sid emu
98      * @return the number of actually created sid emus
99      */
100     virtual unsigned int create(unsigned int sids) = 0;
101 
102     /**
103      * Find a free SID of the required specs
104      *
105      * @param env the event context
106      * @param model the required sid model
107      * @param digiboost whether to enable digiboost for 8580
108      * @return pointer to the locked sid emu
109      */
110     libsidplayfp::sidemu *lock(libsidplayfp::EventScheduler *scheduler, SidConfig::sid_model_t model, bool digiboost);
111 
112     /**
113      * Release this SID.
114      *
115      * @param device the sid emu to unlock
116      */
117     void unlock(libsidplayfp::sidemu *device);
118 
119     /**
120      * Remove all SID emulations.
121      */
122     void remove();
123 
124     /**
125      * Get the builder's name.
126      *
127      * @return the name
128      */
129     const char *name() const { return m_name; }
130 
131     /**
132      * Error message.
133      *
134      * @return string error message.
135      */
136     const char *error() const { return m_errorBuffer.c_str(); }
137 
138     /**
139      * Determine current state of object.
140      *
141      * @return true = okay, false = error
142      */
143     bool getStatus() const { return m_status; }
144 
145     /**
146      * Get the builder's credits.
147      *
148      * @return credits
149      */
150     virtual const char *credits() const = 0;
151 
152     /**
153      * Toggle sid filter emulation.
154      *
155      * @param enable true = enable, false = disable
156      */
157     virtual void filter(bool enable) = 0;
158 };
159 
160 #endif // SIDBUILDER_H
161