diff --git a/include/maxscale/config.hh b/include/maxscale/config.hh index 55545bbf4..2b81f4ba9 100644 --- a/include/maxscale/config.hh +++ b/include/maxscale/config.hh @@ -375,6 +375,10 @@ public: */ static void set_multiple(MXS_CONFIG_PARAMETER** destination, const MXS_CONFIG_PARAMETER* source); + static void set_from_list(MXS_CONFIG_PARAMETER** destination, + std::vector> list, + const MXS_MODULE_PARAM* module_params = NULL); + /** * Remove a key-value pair from the container. * @@ -668,26 +672,3 @@ inline bool config_is_valid_name(const std::string& name, std::string* reason = { return config_is_valid_name(name.c_str()); } - - -namespace maxscale -{ - -// Helper class for allocating temporary configuration parameters -class ParamList -{ -public: - ParamList(const ParamList&) = delete; - ParamList& operator=(const ParamList&) = delete; - - ParamList(std::initializer_list> list, - const MXS_MODULE_PARAM* module_params = NULL); - - ~ParamList(); - - MXS_CONFIG_PARAMETER* params(); - -private: - CONFIG_CONTEXT m_ctx = {(char*)""}; -}; -} diff --git a/server/core/config.cc b/server/core/config.cc index 10804aaab..eb58b832a 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -1966,6 +1966,29 @@ void MXS_CONFIG_PARAMETER::set_multiple(MXS_CONFIG_PARAMETER** destination, cons } } +void MXS_CONFIG_PARAMETER::set_from_list(MXS_CONFIG_PARAMETER** destination, + std::vector> list, + const MXS_MODULE_PARAM* module_params) +{ + // Add custom values. + for (const auto& a : list) + { + MXS_CONFIG_PARAMETER::set(destination, a.first, a.second); + } + + if (module_params) + { + // Add default values for the rest of the parameters. + for (auto module_param = module_params; module_param->name; module_param++) + { + if (module_param->default_value && !(*destination)->contains(module_param->name)) + { + MXS_CONFIG_PARAMETER::set(destination, module_param->name, module_param->default_value); + } + } + } +} + void MXS_CONFIG_PARAMETER::remove(MXS_CONFIG_PARAMETER** ppParams, const string& key) { mxb_assert(ppParams); @@ -4953,34 +4976,6 @@ bool config_parse_disk_space_threshold(SERVER::DiskSpaceLimits* pDisk_space_thre return success; } -namespace maxscale -{ - -ParamList::ParamList(std::initializer_list> list, - const MXS_MODULE_PARAM* module_params) -{ - for (const auto& a : list) - { - config_add_param(&m_ctx, a.first, a.second); - } - - if (module_params) - { - config_add_defaults(&m_ctx, module_params); - } -} - -ParamList::~ParamList() -{ - MXS_CONFIG_PARAMETER::free_all(&m_ctx.parameters); -} - -MXS_CONFIG_PARAMETER* ParamList::params() -{ - return m_ctx.parameters; -} -} - void dump_if_changed(const MXS_MODULE_PARAM* params, int file, const std::string& key, diff --git a/server/core/server.cc b/server/core/server.cc index ce0aa5474..f6f69a7e9 100644 --- a/server/core/server.cc +++ b/server/core/server.cc @@ -119,26 +119,13 @@ public: { for (const auto& elem : parameters) { - // Allocate and add new head element. - MXS_CONFIG_PARAMETER* new_elem = - static_cast(MXS_MALLOC(sizeof(MXS_CONFIG_PARAMETER))); - new_elem->name = MXS_STRDUP(elem.name.c_str()); - new_elem->value = MXS_STRDUP(elem.value.c_str()); - new_elem->next = m_params; - m_params = new_elem; + MXS_CONFIG_PARAMETER::set(&m_params, elem.name, elem.value); } } ~ParamAdaptor() { - while (m_params) - { - auto elem = m_params; - m_params = elem->next; - MXS_FREE(elem->name); - MXS_FREE(elem->value); - MXS_FREE(elem); - } + MXS_CONFIG_PARAMETER::free_all(&m_params); } operator MXS_CONFIG_PARAMETER*() diff --git a/server/core/test/test_server.cc b/server/core/test/test_server.cc index f09431c85..9f4f28da3 100644 --- a/server/core/test/test_server.cc +++ b/server/core/test/test_server.cc @@ -43,13 +43,7 @@ #include "../server.cc" #include "../internal/server.hh" -static mxs::ParamList params( -{ - {"address", "127.0.0.1"}, - {"port", "9876"}, - {"protocol", "HTTPD"}, - {"authenticator", "NullAuthAllow"} -}, config_server_params); +static MXS_CONFIG_PARAMETER* params = nullptr; /** * test1 Allocate a server and do lots of other things @@ -62,7 +56,7 @@ static int test1() /* Server tests */ fprintf(stderr, "testserver : creating server called MyServer"); - Server* server = Server::server_alloc("uniquename", params.params()); + Server* server = Server::server_alloc("uniquename", params); mxb_assert_message(server, "Allocating the server should not fail"); fprintf(stderr, "\t..done\nTest Parameter for Server."); @@ -134,7 +128,7 @@ bool test_serialize() char old_config_name[] = "serialized-server.cnf.old"; char* persist_dir = MXS_STRDUP_A("./"); set_config_persistdir(persist_dir); - Server* server = Server::server_alloc(name, params.params()); + Server* server = Server::server_alloc(name, params); TEST(server, "Server allocation failed"); /** Make sure the files don't exist */ @@ -179,6 +173,13 @@ int main(int argc, char** argv) set_libdir(MXS_STRDUP_A("../../modules/protocol/HTTPD/")); load_module("HTTPD", MODULE_PROTOCOL); + MXS_CONFIG_PARAMETER::set_from_list(¶ms, + { + {"address", "127.0.0.1"}, + {"port", "9876"}, + {"protocol", "HTTPD"}, + {"authenticator", "NullAuthAllow"} + }, config_server_params); int result = 0; result += test1(); diff --git a/server/modules/filter/cache/test/test_cacheoptions.cc b/server/modules/filter/cache/test/test_cacheoptions.cc index f82d6e1f5..6be2fb686 100644 --- a/server/modules/filter/cache/test/test_cacheoptions.cc +++ b/server/modules/filter/cache/test/test_cacheoptions.cc @@ -319,13 +319,13 @@ int test(FilterModule& filter_module, const TEST_CASE& tc) { int rv = 1; - auto_ptr sParameters = filter_module.create_default_parameters(); - sParameters->set_value("cache_in_transactions", to_string(tc.cit)); - sParameters->set_value("debug", "31"); - sParameters->set_value("cached_data", "shared"); - sParameters->set_value("selects", "verify_cacheable"); + auto params = filter_module.create_default_parameters(); + MXS_CONFIG_PARAMETER::set(¶ms, "cache_in_transactions", to_string(tc.cit)); + MXS_CONFIG_PARAMETER::set(¶ms, "debug", "31"); + MXS_CONFIG_PARAMETER::set(¶ms, "cached_data", "shared"); + MXS_CONFIG_PARAMETER::set(¶ms, "selects", "verify_cacheable"); - auto_ptr sInstance = filter_module.createInstance("test", sParameters); + auto_ptr sInstance = filter_module.createInstance("test", params); if (sInstance.get()) { diff --git a/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc b/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc index 19e63bcba..7d26eebdb 100644 --- a/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc +++ b/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc @@ -816,11 +816,11 @@ int test(FilterModule& filter_module, const FW_TEST& t) TempFile file; file.write(t.zRules); - auto_ptr sParameters = filter_module.create_default_parameters(); - sParameters->set_value("action", zAction); - sParameters->set_value("rules", file.name()); + auto params = filter_module.create_default_parameters(); + MXS_CONFIG_PARAMETER::set(¶ms, "action", zAction); + MXS_CONFIG_PARAMETER::set(¶ms, "rules", file.name()); - auto_ptr sInstance = filter_module.createInstance("test", sParameters); + auto_ptr sInstance = filter_module.createInstance("test", params); if (sInstance.get()) { diff --git a/server/modules/filter/test/filtermodule.cc b/server/modules/filter/test/filtermodule.cc index 231fbcadd..e292621e9 100644 --- a/server/modules/filter/test/filtermodule.cc +++ b/server/modules/filter/test/filtermodule.cc @@ -41,12 +41,6 @@ auto_ptr FilterModule::createInstance(const char* zName, return sInstance; } -auto_ptr FilterModule::createInstance(const char* zName, - std::auto_ptr sParameters) -{ - return createInstance(zName, sParameters.get()); -} - // // FilterModule::Instance // diff --git a/server/modules/filter/test/maxscale/filtermodule.hh b/server/modules/filter/test/maxscale/filtermodule.hh index 328c71957..ddaa748f6 100644 --- a/server/modules/filter/test/maxscale/filtermodule.hh +++ b/server/modules/filter/test/maxscale/filtermodule.hh @@ -136,11 +136,7 @@ public: * * @return A new instance or NULL if creation failed. */ - std::auto_ptr createInstance(const char* zName, - MXS_CONFIG_PARAMETER* pParameters); - - std::auto_ptr createInstance(const char* zName, - std::auto_ptr sParameters); + std::auto_ptr createInstance(const char* zName, MXS_CONFIG_PARAMETER* pParameters); private: friend class Instance; diff --git a/server/modules/filter/test/maxscale/module.hh b/server/modules/filter/test/maxscale/module.hh index 39162359c..36f249d00 100644 --- a/server/modules/filter/test/maxscale/module.hh +++ b/server/modules/filter/test/maxscale/module.hh @@ -28,45 +28,6 @@ namespace maxscale class Module { public: - class ConfigParameters : public MXS_CONFIG_PARAMETER - { - ConfigParameters(const ConfigParameters&); - ConfigParameters& operator=(const ConfigParameters&); - - public: - ~ConfigParameters(); - - /** - * Get the value of a parameter - * - * @param zName The name of a parameter. - * - * @return The value of the parameter or NULL if the parameter does not exist. - */ - const char* get(const char* zName) const; - - /** - * Set the value of a parameter - * - * @param zName The name of a parameter. - * @param zValue The value of the parameter. - */ - void set_value(const char* zName, const char* zValue); - - void set_value(const char* zName, const std::string& value); - - private: - friend class Module; - - ConfigParameters(const MXS_MODULE_PARAM* pParams); - - const MXS_CONFIG_PARAMETER* get_param(const char* zName) const; - MXS_CONFIG_PARAMETER* get_param(const char* zName); - - MXS_CONFIG_PARAMETER* get_tail(); - - std::deque m_values; /** Storage for modified parameters. */ - }; /** * Get a ConfigParameters instance containing the default values @@ -74,7 +35,7 @@ public: * * @return A ConfigParameters object. */ - std::auto_ptr create_default_parameters() const; + MXS_CONFIG_PARAMETER* create_default_parameters() const; /** * Load a module with a specific name, assumed to be of a specific type. diff --git a/server/modules/filter/test/module.cc b/server/modules/filter/test/module.cc index e4278c69f..1e6309d82 100644 --- a/server/modules/filter/test/module.cc +++ b/server/modules/filter/test/module.cc @@ -21,131 +21,19 @@ using std::auto_ptr; namespace maxscale { -// -// Module::ConfigParameters -// - -Module::ConfigParameters::ConfigParameters(const MXS_MODULE_PARAM* pParams) +MXS_CONFIG_PARAMETER* Module::create_default_parameters() const { - this->name = NULL; - this->value = NULL; - this->next = NULL; - - MXS_CONFIG_PARAMETER* pCurrent = this; - - while (pParams->name) + MXS_CONFIG_PARAMETER* rval = nullptr; + const MXS_MODULE_PARAM* param_definition = m_module.parameters; + while (param_definition->name) { - if (pParams->name && pParams->default_value) + if (param_definition->default_value) { - if (this->name == NULL) - { - this->name = MXS_STRDUP(pParams->name); - this->value = MXS_STRDUP(pParams->default_value); - } - else - { - MXS_CONFIG_PARAMETER* pNext = new MXS_CONFIG_PARAMETER; - pNext->name = MXS_STRDUP(pParams->name); - pNext->value = MXS_STRDUP(pParams->default_value); - pNext->next = NULL; - - pCurrent->next = pNext; - pCurrent = pNext; - } + MXS_CONFIG_PARAMETER::set(&rval, param_definition->name, param_definition->default_value); } - - ++pParams; + ++param_definition; } -} - -Module::ConfigParameters::~ConfigParameters() -{ - MXS_CONFIG_PARAMETER* pNext = this->next; - - while (pNext) - { - MXS_CONFIG_PARAMETER* pCurrent = pNext; - pNext = pNext->next; - - delete pCurrent; - } -} - -const char* Module::ConfigParameters::get(const char* zName) const -{ - const MXS_CONFIG_PARAMETER* pParam = get_param(zName); - - return pParam ? pParam->value : NULL; -} - -void Module::ConfigParameters::set_value(const char* zName, const char* zValue) -{ - set_value(zName, std::string(zValue)); -} - -void Module::ConfigParameters::set_value(const char* zName, const std::string& value) -{ - MXS_CONFIG_PARAMETER* pParam = get_param(zName); - - if (!pParam) - { - MXS_CONFIG_PARAMETER* pTail = get_tail(); - - pParam = new MXS_CONFIG_PARAMETER; - m_values.push_back(zName); - pParam->name = MXS_STRDUP(m_values.back().c_str()); - pParam->value = NULL; - pParam->next = NULL; - - pTail->next = pParam; - } - - m_values.push_back(value); - - pParam->value = MXS_STRDUP(m_values.back().c_str()); -} - -const MXS_CONFIG_PARAMETER* Module::ConfigParameters::get_param(const char* zName) const -{ - return const_cast(this)->get_param(zName); -} - -MXS_CONFIG_PARAMETER* Module::ConfigParameters::get_param(const char* zName) -{ - MXS_CONFIG_PARAMETER* pParam = NULL; - - if (this->name && (strcmp(this->name, zName) == 0)) - { - pParam = this; - } - else - { - pParam = this->next; - - while (pParam && (strcmp(pParam->name, zName) != 0)) - { - pParam = pParam->next; - } - } - - return pParam; -} - -MXS_CONFIG_PARAMETER* Module::ConfigParameters::get_tail() -{ - MXS_CONFIG_PARAMETER* pTail = this; - - while (pTail->next) - { - pTail = pTail->next; - } - - return pTail; -} - -auto_ptr Module::create_default_parameters() const -{ - return auto_ptr(new ConfigParameters(m_module.parameters)); + return rval; } // diff --git a/server/modules/routing/binlogrouter/blr.cc b/server/modules/routing/binlogrouter/blr.cc index 3859cd23e..ffa1809cc 100644 --- a/server/modules/routing/binlogrouter/blr.cc +++ b/server/modules/routing/binlogrouter/blr.cc @@ -799,16 +799,16 @@ static MXS_ROUTER* createInstance(SERVICE* service, MXS_CONFIG_PARAMETER* params { // Declared in config.cc and needs to be removed if/when blr is refactored extern const MXS_MODULE_PARAM config_server_params[]; + MXS_CONFIG_PARAMETER* params = nullptr; + MXS_CONFIG_PARAMETER::set_from_list(¶ms, + { + {"address", "_none_"}, + {"port", "3306"}, + {"protocol", "mariadbbackend"}, + {"authenticator", "MySQLBackendAuth"} + }, config_server_params); - mxs::ParamList p( - { - {"address", "_none_"}, - {"port", "3306"}, - {"protocol", "mariadbbackend"}, - {"authenticator", "MySQLBackendAuth"} - }, config_server_params); - - Server* server = Server::server_alloc("binlog_router_master_host", p.params()); + Server* server = Server::server_alloc("binlog_router_master_host", params); if (server == NULL) { diff --git a/server/modules/routing/binlogrouter/test/testbinlog.cc b/server/modules/routing/binlogrouter/test/testbinlog.cc index a67e2170d..9f01b6909 100644 --- a/server/modules/routing/binlogrouter/test/testbinlog.cc +++ b/server/modules/routing/binlogrouter/test/testbinlog.cc @@ -138,15 +138,16 @@ int main(int argc, char** argv) // Declared in config.cc and needs to be removed if/when blr is refactored extern const MXS_MODULE_PARAM config_server_params[]; - mxs::ParamList p( - { - {"address", "_none_"}, - {"port", "3306"}, - {"protocol", "MySQLBackend"}, - {"authenticator", "MySQLBackendAuth"} - }, config_server_params); + MXS_CONFIG_PARAMETER* params = nullptr; + MXS_CONFIG_PARAMETER::set_from_list(¶ms, + { + {"address", "_none_"}, + {"port", "3306"}, + {"protocol", "MySQLBackend"}, + {"authenticator", "MySQLBackendAuth"} + }, config_server_params); - Server* server = Server::server_alloc("binlog_router_master_host", p.params()); + Server* server = Server::server_alloc("binlog_router_master_host", params); if (server == NULL) { printf("Failed to allocate 'server' object\n");