MXS-1512 Create parameters object from default parameters
This commit is contained in:
parent
7ae2acecf7
commit
9aa4a2d1ff
@ -42,6 +42,13 @@ auto_ptr<FilterModule::Instance> FilterModule::createInstance(const char* zName,
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
auto_ptr<FilterModule::Instance> FilterModule::createInstance(const char* zName,
|
||||
char** pzOptions,
|
||||
std::auto_ptr<ConfigParameters> sParameters)
|
||||
{
|
||||
return createInstance(zName, pzOptions, sParameters.get());
|
||||
}
|
||||
|
||||
//
|
||||
// FilterModule::Instance
|
||||
//
|
||||
|
@ -140,6 +140,10 @@ public:
|
||||
char** pzOptions,
|
||||
MXS_CONFIG_PARAMETER* pParameters);
|
||||
|
||||
std::auto_ptr<Instance> createInstance(const char* zName,
|
||||
char** pzOptions,
|
||||
std::auto_ptr<ConfigParameters> sParameters);
|
||||
|
||||
private:
|
||||
friend class Instance;
|
||||
|
||||
|
@ -14,6 +14,9 @@
|
||||
|
||||
#include <maxscale/cppdefs.hh>
|
||||
#include <memory>
|
||||
#include <deque>
|
||||
#include <maxscale/config.h>
|
||||
#include <maxscale/modinfo.h>
|
||||
|
||||
namespace maxscale
|
||||
{
|
||||
@ -25,6 +28,54 @@ 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<std::string> m_values; /** Storage for modified parameters. */
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a ConfigParameters instance containing the default values
|
||||
* of all parameters.
|
||||
*
|
||||
* @return A ConfigParameters object.
|
||||
*/
|
||||
std::auto_ptr<ConfigParameters> create_default_parameters() const;
|
||||
|
||||
/**
|
||||
* Load a module with a specific name, assumed to be of a specific type.
|
||||
*
|
||||
|
@ -14,9 +14,142 @@
|
||||
#include "maxscale/module.hh"
|
||||
#include "../../../core/internal/modules.h"
|
||||
|
||||
using std::auto_ptr;
|
||||
|
||||
namespace maxscale
|
||||
{
|
||||
|
||||
//
|
||||
// Module::ConfigParameters
|
||||
//
|
||||
|
||||
Module::ConfigParameters::ConfigParameters(const MXS_MODULE_PARAM* pParams)
|
||||
{
|
||||
this->name = NULL;
|
||||
this->value = NULL;
|
||||
this->next = NULL;
|
||||
|
||||
MXS_CONFIG_PARAMETER* pCurrent = this;
|
||||
|
||||
while (pParams->name)
|
||||
{
|
||||
if (pParams->name && pParams->default_value)
|
||||
{
|
||||
if (this->name == NULL)
|
||||
{
|
||||
this->name = const_cast<char*>(pParams->name);
|
||||
this->value = const_cast<char*>(pParams->default_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_CONFIG_PARAMETER* pNext = new MXS_CONFIG_PARAMETER;
|
||||
pNext->name = const_cast<char*>(pParams->name);
|
||||
pNext->value = const_cast<char*>(pParams->default_value);
|
||||
pNext->next = NULL;
|
||||
|
||||
pCurrent->next = pNext;
|
||||
pCurrent = pNext;
|
||||
}
|
||||
}
|
||||
|
||||
++pParams;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return 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 = const_cast<char*>(m_values.back().c_str());
|
||||
pParam->value = NULL;
|
||||
pParam->next = NULL;
|
||||
|
||||
pTail->next = pParam;
|
||||
}
|
||||
|
||||
m_values.push_back(value);
|
||||
|
||||
pParam->value = const_cast<char*>(m_values.back().c_str());
|
||||
}
|
||||
|
||||
const MXS_CONFIG_PARAMETER* Module::ConfigParameters::get_param(const char* zName) const
|
||||
{
|
||||
return const_cast<Module::ConfigParameters*>(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::ConfigParameters> Module::create_default_parameters() const
|
||||
{
|
||||
return auto_ptr<ConfigParameters>(new ConfigParameters(m_module.parameters));
|
||||
}
|
||||
|
||||
//
|
||||
// Module
|
||||
//
|
||||
|
||||
//static
|
||||
void* Module::load(const char* zName, const char* zType)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user