ManaPlus
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Friends
ConfigurationObject Class Reference

#include <configuration.h>

Inheritance diagram for ConfigurationObject:
Configuration

Public Member Functions

virtual ~ConfigurationObject ()
 
virtual void setValue (const std::string &key, const std::string &value)
 
void deleteKey (const std::string &key)
 
std::string getValue (const std::string &key, const std::string &deflt) const
 
int getValue (const std::string &key, const int deflt) const
 
int getValueInt (const std::string &key, const int deflt) const
 
bool getValueBool (const std::string &key, const bool deflt) const
 
unsigned getValue (const std::string &key, const unsigned deflt) const
 
double getValue (const std::string &key, const double deflt) const
 
void clear ()
 
template<class IT , class T , class CONT >
void setList (const std::string &name, IT begin, IT end, ConfigurationListManager< T, CONT > *manager)
 
template<class T , class CONT >
CONT getList (const std::string &name, CONT empty, ConfigurationListManager< T, CONT > *manager)
 

Protected Types

typedef std::map< std::string, std::string > Options
 
typedef std::list< ConfigurationObject * > ConfigurationList
 

Protected Member Functions

 ConfigurationObject ()
 
virtual void initFromXML (const xmlNode *const parentNode)
 
virtual void writeToXML (const xmlTextWriterPtr writer)
 
void deleteList (const std::string &name)
 

Protected Attributes

Options mOptions
 
std::map< std::string, ConfigurationListmContainerOptions
 

Friends

class Configuration
 

Detailed Description

Configuration object, mapping values to names and possibly containing lists of further configuration objects

Definition at line 88 of file configuration.h.

Member Typedef Documentation

◆ ConfigurationList

Definition at line 220 of file configuration.h.

◆ Options

typedef std::map<std::string, std::string> ConfigurationObject::Options
protected

Definition at line 217 of file configuration.h.

Constructor & Destructor Documentation

◆ ~ConfigurationObject()

ConfigurationObject::~ConfigurationObject ( )
virtual

Definition at line 357 of file configuration.cpp.

358 {
359  clear();
360 }

References clear().

◆ ConfigurationObject()

ConfigurationObject::ConfigurationObject ( )
protected

Definition at line 344 of file configuration.cpp.

344  :
345  mOptions(),
346 #ifdef DEBUG_CONFIG
348  mLogKeys(false),
349  mIsMain(false)
350 #else // DEBUG_CONFIG
351 
353 #endif // DEBUG_CONFIG
354 {
355 }
std::map< std::string, ConfigurationList > mContainerOptions

Referenced by initFromXML(), and setList().

Member Function Documentation

◆ clear()

void ConfigurationObject::clear ( )

Re-sets all data in the configuration

Definition at line 332 of file configuration.cpp.

333 {
334  for (std::map<std::string, ConfigurationList>::const_iterator
335  it = mContainerOptions.begin();
336  it != mContainerOptions.end(); ++it)
337  {
338  deleteList(it->first);
339  }
340  mOptions.clear();
341  mContainerOptions.clear();
342 }
void deleteList(const std::string &name)

References deleteList(), mContainerOptions, and mOptions.

Referenced by Client::gameClear(), Configuration::init(), TestMain::initConfig(), initFromXML(), setList(), Configuration::unload(), and ~ConfigurationObject().

◆ deleteKey()

void ConfigurationObject::deleteKey ( const std::string &  key)

◆ deleteList()

void ConfigurationObject::deleteList ( const std::string &  name)
protected

Definition at line 320 of file configuration.cpp.

321 {
322  for (ConfigurationList::const_iterator
323  it = mContainerOptions[name].begin();
324  it != mContainerOptions[name].end(); ++it)
325  {
326  delete *it;
327  }
328 
329  mContainerOptions[name].clear();
330 }

References mContainerOptions.

Referenced by clear(), and setList().

◆ getList()

template<class T , class CONT >
CONT ConfigurationObject::getList ( const std::string &  name,
CONT  empty,
ConfigurationListManager< T, CONT > *  manager 
)
inline

Serialises a container into a list of configuration options

Parameters
nameName of the list the elements should be read from under
emptyInitial (empty) container to write to
managerAn object capable of deserialising items into CONT

Definition at line 182 of file configuration.h.

184  {
185  ConfigurationList *const list = &(mContainerOptions[name]);
186  CONT container = empty;
187 
188  if (!manager)
189  return container;
190 
191  for (ConfigurationList::const_iterator it = list->begin();
192  it != list->end();
193  ++ it)
194  {
195  container = manager->readConfigItem(*it, container);
196  }
197 
198  return container;
199  }
virtual CONT readConfigItem(const ConfigurationObject *const obj, CONT container) const =0
std::list< ConfigurationObject * > ConfigurationList
std::string empty
Definition: podict.cpp:26

References empty, mContainerOptions, and ConfigurationListManager< T, CONT >::readConfigItem().

Referenced by PlayerRelationsManager::load().

◆ getValue() [1/4]

double ConfigurationObject::getValue ( const std::string &  key,
const double  deflt 
) const

Definition at line 312 of file configuration.cpp.

314 {
315  GETLOG();
316  const Options::const_iterator iter = mOptions.find(key);
317  return (iter != mOptions.end()) ? atof(iter->second.c_str()) : deflt;
318 }
#define GETLOG()

References GETLOG, and mOptions.

◆ getValue() [2/4]

int ConfigurationObject::getValue ( const std::string &  key,
const int  deflt 
) const

Definition at line 277 of file configuration.cpp.

279 {
280  GETLOG();
281  const Options::const_iterator iter = mOptions.find(key);
282  return (iter != mOptions.end()) ? atoi(iter->second.c_str()) : deflt;
283 }

References GETLOG, and mOptions.

◆ getValue() [3/4]

std::string ConfigurationObject::getValue ( const std::string &  key,
const std::string &  deflt 
) const

◆ getValue() [4/4]

unsigned ConfigurationObject::getValue ( const std::string &  key,
const unsigned  deflt 
) const

Definition at line 303 of file configuration.cpp.

305 {
306  GETLOG();
307  const Options::const_iterator iter = mOptions.find(key);
308  return (iter != mOptions.end()) ? CAST_U32(
309  atol(iter->second.c_str())) : deflt;
310 }
#define CAST_U32
Definition: cast.h:31

References CAST_U32, GETLOG, and mOptions.

◆ getValueBool()

bool ConfigurationObject::getValueBool ( const std::string &  key,
const bool  deflt 
) const

Definition at line 293 of file configuration.cpp.

295 {
296  GETLOG();
297  const Options::const_iterator iter = mOptions.find(key);
298  if (iter != mOptions.end())
299  return atoi(iter->second.c_str()) != 0 ? true : false;
300  return deflt;
301 }

References GETLOG, and mOptions.

Referenced by PacketLimiter::checkPackets(), PacketLimiter::limitPackets(), OutfitWindow::load(), Window::loadWindowState(), Minimap::Minimap(), MiniStatusWindow::MiniStatusWindow(), Joystick::open(), and StatusWindow::StatusWindow().

◆ getValueInt()

int ConfigurationObject::getValueInt ( const std::string &  key,
const int  deflt 
) const

Definition at line 285 of file configuration.cpp.

287 {
288  GETLOG();
289  const Options::const_iterator iter = mOptions.find(key);
290  return (iter != mOptions.end()) ? atoi(iter->second.c_str()) : deflt;
291 }

References GETLOG, and mOptions.

Referenced by Setup_Video::action(), UserPalette::addColor(), GraphicsManager::detectVideoSettings(), Window::loadWindowState(), and GraphicsManager::setVideoMode().

◆ initFromXML()

void ConfigurationObject::initFromXML ( const xmlNode *const  parentNode)
protectedvirtual

Definition at line 699 of file configuration.cpp.

700 {
701  clear();
702 
703  if (parentNode == nullptr)
704  return;
705 
706  for_each_xml_child_node(node, parentNode)
707  {
708  if (xmlNameEqual(node, "list"))
709  {
710  // list option handling
711  const std::string name = XML::getProperty(node,
712  "name", std::string());
713 
714  for_each_xml_child_node(subnode, node)
715  {
716  if (xmlNameEqual(subnode, name.c_str()) &&
717  xmlTypeEqual(subnode, XML_ELEMENT_NODE))
718  {
719  ConfigurationObject *const cobj = new ConfigurationObject;
720  cobj->initFromXML(subnode); // recurse
721  mContainerOptions[name].push_back(cobj);
722  }
723  }
724  }
725  else if (xmlNameEqual(node, "option"))
726  {
727  // single option handling
728  const std::string name = XML::getProperty(node,
729  "name", std::string());
730  if (!name.empty())
731  {
732  mOptions[name] = XML::getProperty(node,
733  "value", std::string());
734  }
735  } // otherwise ignore
736  }
737 }
virtual void initFromXML(const xmlNode *const parentNode)
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174

References clear(), ConfigurationObject(), for_each_xml_child_node, XML::getProperty(), initFromXML(), mContainerOptions, and mOptions.

Referenced by Configuration::init(), initFromXML(), and Configuration::reInit().

◆ setList()

template<class IT , class T , class CONT >
void ConfigurationObject::setList ( const std::string &  name,
IT  begin,
IT  end,
ConfigurationListManager< T, CONT > *  manager 
)
inline

Serialises a container into a list of configuration options

Parameters
nameName of the list the elements should be stored under
beginIterator start
endIterator end
managerAn object capable of serialising T items

Definition at line 146 of file configuration.h.

148  {
149  if (!manager)
150  return;
151 
153  deleteList(name);
154  ConfigurationList *list = &(mContainerOptions[name]);
155 
156  for (IT it = begin; it != end; it++)
157  {
158  ConfigurationObject *const wrobj
159  = manager->writeConfigItem(*it, nextobj);
160  if (wrobj)
161  { // wrote something
162  nextobj = new ConfigurationObject;
163  list->push_back(wrobj);
164  }
165  else
166  {
167  nextobj->clear(); // you never know...
168  }
169  }
170 
171  delete nextobj;
172  }
virtual ConfigurationObject * writeConfigItem(const T &value, ConfigurationObject *const obj) const =0

References clear(), ConfigurationObject(), deleteList(), mContainerOptions, and ConfigurationListManager< T, CONT >::writeConfigItem().

Referenced by PlayerRelationsManager::store().

◆ setValue()

void ConfigurationObject::setValue ( const std::string &  key,
const std::string &  value 
)
virtual

Sets an option using a string value.

Parameters
keyOption identifier.
valueValue.

Reimplemented in Configuration.

Definition at line 228 of file configuration.cpp.

230 {
231  mOptions[key] = value;
232 }

References mOptions.

Referenced by Configuration::setSilent(), and Configuration::setValue().

◆ writeToXML()

void ConfigurationObject::writeToXML ( const xmlTextWriterPtr  writer)
protectedvirtual

Definition at line 813 of file configuration.cpp.

814 {
815  FOR_EACH (Options::const_iterator, i, mOptions)
816  {
817 #ifdef DEBUG_CONFIG
818  if (mLogKeys)
819  {
820  if (optionsCount.find(i->first) == optionsCount.end())
821  logger->log("unused configuration option: " + i->first);
822  }
823 #endif // DEBUG_CONFIG
824 
825  XmlTextWriterStartElement(writer, "option");
826  XmlTextWriterWriteAttribute(writer, "name", i->first.c_str());
827  XmlTextWriterWriteAttribute(writer, "value", i->second.c_str());
828  XmlTextWriterEndElement(writer);
829  }
830 
831  for (std::map<std::string, ConfigurationList>::const_iterator
832  it = mContainerOptions.begin(), it_fend = mContainerOptions.end();
833  it != it_fend; ++ it)
834  {
835  const char *const name = it->first.c_str();
836 
837  XmlTextWriterStartElement(writer, "list");
838  XmlTextWriterWriteAttribute(writer, "name", name);
839 
840  // recurse on all elements
841  FOR_EACH (ConfigurationList::const_iterator, elt_it, it->second)
842  {
843  XmlTextWriterStartElement(writer, name);
844  if (*elt_it != nullptr)
845  (*elt_it)->writeToXML(writer);
846  XmlTextWriterEndElement(writer);
847  }
848 
849  XmlTextWriterEndElement(writer);
850  }
851 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Logger * logger
Definition: logger.cpp:89

References FOR_EACH, Logger::log(), logger, mContainerOptions, and mOptions.

Referenced by Configuration::write().

Friends And Related Function Documentation

◆ Configuration

friend class Configuration
friend

Definition at line 90 of file configuration.h.

Field Documentation

◆ mContainerOptions

std::map<std::string, ConfigurationList> ConfigurationObject::mContainerOptions
protected

Definition at line 221 of file configuration.h.

Referenced by clear(), deleteList(), getList(), initFromXML(), setList(), and writeToXML().

◆ mOptions

Options ConfigurationObject::mOptions
protected

The documentation for this class was generated from the following files: