1 /* 2 * Copyright 2016, Rene Gollent, rene@gollent.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 #include "ConnectionConfigHandlerRoster.h" 6 7 #include <AutoDeleter.h> 8 9 #include "NetworkConnectionConfigHandler.h" 10 #include "TargetHostInterfaceInfo.h" 11 12 13 /*static*/ ConnectionConfigHandlerRoster* 14 ConnectionConfigHandlerRoster::sDefaultInstance = NULL; 15 16 17 ConnectionConfigHandlerRoster::ConnectionConfigHandlerRoster() 18 : 19 fLock("config handler roster lock"), 20 fConfigHandlers(10, true) 21 { 22 } 23 24 25 ConnectionConfigHandlerRoster::~ConnectionConfigHandlerRoster() 26 { 27 } 28 29 30 /*static*/ ConnectionConfigHandlerRoster* 31 ConnectionConfigHandlerRoster::Default() 32 { 33 return sDefaultInstance; 34 } 35 36 37 /*static*/ status_t 38 ConnectionConfigHandlerRoster::CreateDefault() 39 { 40 if (sDefaultInstance != NULL) 41 return B_OK; 42 43 ConnectionConfigHandlerRoster* roster 44 = new(std::nothrow) ConnectionConfigHandlerRoster; 45 if (roster == NULL) 46 return B_NO_MEMORY; 47 48 ObjectDeleter<ConnectionConfigHandlerRoster> rosterDeleter(roster); 49 50 status_t error = roster->Init(); 51 if (error != B_OK) 52 return error; 53 54 sDefaultInstance = roster; 55 rosterDeleter.Detach(); 56 return B_OK; 57 } 58 59 60 /*static*/ void 61 ConnectionConfigHandlerRoster::DeleteDefault() 62 { 63 ConnectionConfigHandlerRoster* roster = sDefaultInstance; 64 sDefaultInstance = NULL; 65 delete roster; 66 } 67 68 69 status_t 70 ConnectionConfigHandlerRoster::Init() 71 { 72 return _RegisterHandlers(); 73 } 74 75 76 bool 77 ConnectionConfigHandlerRoster::HasHandlerFor(TargetHostInterfaceInfo* info) 78 const 79 { 80 ConnectionConfigHandler* handler = NULL; 81 return _GetHandler(info->Name(), handler); 82 } 83 84 85 status_t 86 ConnectionConfigHandlerRoster::CreateConfigView(TargetHostInterfaceInfo* info, 87 ConnectionConfigView::Listener* listener, 88 ConnectionConfigView*& _view) const 89 { 90 ConnectionConfigHandler* handler = NULL; 91 92 if (!_GetHandler(info->Name(), handler)) 93 return B_NOT_SUPPORTED; 94 95 return handler->CreateView(info, listener, _view); 96 } 97 98 99 bool 100 ConnectionConfigHandlerRoster::_GetHandler(const BString& name, 101 ConnectionConfigHandler*& _handler) const 102 { 103 ConnectionConfigHandler* handler = NULL; 104 for (int32 i = 0; i < fConfigHandlers.CountItems(); i++) { 105 handler = fConfigHandlers.ItemAt(i); 106 if (handler->Name() == name) { 107 _handler = handler; 108 return true; 109 } 110 } 111 112 return false; 113 } 114 115 116 status_t 117 ConnectionConfigHandlerRoster::_RegisterHandlers() 118 { 119 ConnectionConfigHandler* handler = NULL; 120 ObjectDeleter<ConnectionConfigHandler> handlerDeleter; 121 122 #undef REGISTER_HANDLER_INFO 123 #define REGISTER_HANDLER_INFO(type) \ 124 handler = new(std::nothrow) type##ConnectionConfigHandler; \ 125 if (handler == NULL) \ 126 return B_NO_MEMORY; \ 127 handlerDeleter.SetTo(handler); \ 128 if (!fConfigHandlers.AddItem(handler)) \ 129 return B_NO_MEMORY; \ 130 handlerDeleter.Detach(); \ 131 132 REGISTER_HANDLER_INFO(Network) 133 134 return B_OK; 135 } 136