44 *
55 */
66
7+ #include < iterator>
8+ #include " Common/Cpp/Exceptions.h"
79#include " Common/Cpp/Json/JsonValue.h"
810#include " Common/Cpp/Json/JsonObject.h"
11+ #include " ControllerCapability.h"
912#include " ControllerDescriptor.h"
10- #include " Controllers/SerialPABotBase/SerialPABotBase.h"
11- #include " NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.h"
12- #include " ControllerConnection.h"
13+ #include " NullController.h"
14+
15+ // #include <iostream>
16+ // using std::cout;
17+ // using std::endl;
1318
1419namespace PokemonAutomation {
1520
1621
17- bool NullControllerDescriptor::operator ==(const ControllerDescriptor& x) const {
18- return typeid (*this ) == typeid (x);
19- }
20- const char * NullControllerDescriptor::type_name () const {
21- return " None" ;
22- }
23- std::string NullControllerDescriptor::display_name () const {
24- return " (none)" ;
25- }
26- void NullControllerDescriptor::load_json (const JsonValue& json){
22+ //
23+ // Here we store a map of all controller types in the program.
24+ //
25+ std::map<std::string, std::unique_ptr<ControllerType>> CONTROLLER_TYPES;
2726
27+
28+ void ControllerType::register_factory (
29+ const std::string& name,
30+ std::unique_ptr<ControllerType> factory
31+ ){
32+ auto ret = CONTROLLER_TYPES.emplace (name, std::move (factory));
33+ if (!ret.second ){
34+ throw InternalProgramError (nullptr , PA_CURRENT_FUNCTION, " Duplicate Factory Name: " + name);
35+ }
2836}
29- JsonValue NullControllerDescriptor::to_json () const {
30- return JsonValue ();
31- }
32- std::unique_ptr<ControllerConnection> NullControllerDescriptor::open (
33- Logger& logger,
34- const ControllerRequirements& requirements
35- ) const {
36- return nullptr ;
37+
38+
39+
40+ std::vector<std::shared_ptr<const ControllerDescriptor>>
41+ get_compatible_descriptors (const ControllerRequirements& requirements){
42+ std::vector<std::shared_ptr<const ControllerDescriptor>> ret;
43+
44+ // Find all the devices in common between the supported list and the
45+ // required list. For each of those, enumerate all the descriptors and
46+ // combine them into a single list.
47+ for (const auto & device : requirements.map ()){
48+ auto iter = CONTROLLER_TYPES.find (device.first );
49+ if (iter != CONTROLLER_TYPES.end ()){
50+ std::vector<std::shared_ptr<const ControllerDescriptor>> list = iter->second ->list ();
51+ std::move (list.begin (), list.end (), std::back_inserter (ret));
52+ }
53+ }
54+
55+ return ret;
3756}
3857
3958
4059
4160
61+
62+
63+
4264ControllerOption::ControllerOption ()
4365 : m_current(new NullControllerDescriptor())
4466{}
@@ -51,19 +73,13 @@ void ControllerOption::load_json(const JsonValue& json){
5173 const std::string& type = obj.get_string_throw (" DeviceType" );
5274 const JsonValue& params = obj.get_value_throw (" Parameters" );
5375
54- if (type == " None" ){
55- auto descriptor = std::make_unique<NullControllerDescriptor>();
56- m_current = std::move (descriptor);
57- return ;
58- }
59- if (type == SerialPABotBase::NintendoSwitch_Basic){
60- auto descriptor = std::make_unique<NintendoSwitch::SwitchController_SerialPABotBase_Descriptor>();
61- descriptor->load_json (params);
62- m_current = std::move (descriptor);
76+ auto iter = CONTROLLER_TYPES.find (type);
77+ if (iter == CONTROLLER_TYPES.end ()){
78+ m_current.reset (new NullControllerDescriptor ());
6379 return ;
6480 }
6581
66- m_current. reset ( new NullControllerDescriptor () );
82+ m_current = iter-> second -> make (params );
6783}
6884JsonValue ControllerOption::to_json () const {
6985 if (!m_current){
0 commit comments