summaryrefslogtreecommitdiff
path: root/i/chuck/src/config
diff options
context:
space:
mode:
Diffstat (limited to 'i/chuck/src/config')
-rw-r--r--i/chuck/src/config/Makefile.defs5
-rw-r--r--i/chuck/src/config/config.cc27
-rw-r--r--i/chuck/src/config/config.hh98
-rw-r--r--i/chuck/src/config/config_data.cc148
-rw-r--r--i/chuck/src/config/config_data.hh68
-rw-r--r--i/chuck/src/config/config_data.tcc59
-rw-r--r--i/chuck/src/config/test_config.cc48
7 files changed, 453 insertions, 0 deletions
diff --git a/i/chuck/src/config/Makefile.defs b/i/chuck/src/config/Makefile.defs
new file mode 100644
index 0000000..a06f2a7
--- /dev/null
+++ b/i/chuck/src/config/Makefile.defs
@@ -0,0 +1,5 @@
+PROGRAMS += test_config
+
+config_OBJECTS = config_data.o config.o $(parser_OBJECTS)
+
+test_config_OBJECTS = $(config_OBJECTS) test_config.o
diff --git a/i/chuck/src/config/config.cc b/i/chuck/src/config/config.cc
new file mode 100644
index 0000000..333bd4a
--- /dev/null
+++ b/i/chuck/src/config/config.cc
@@ -0,0 +1,27 @@
+// config.cc
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Dufour Jérémy
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Contact :
+// Web: %WEB%
+// Email: <dufourj@efrei.fr>
+// }}}
+#include "config.hh"
+
+
+Config *Config::instance_ = 0;
diff --git a/i/chuck/src/config/config.hh b/i/chuck/src/config/config.hh
new file mode 100644
index 0000000..3034b1a
--- /dev/null
+++ b/i/chuck/src/config/config.hh
@@ -0,0 +1,98 @@
+#ifndef config_hh
+#define config_hh
+// config.hh
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2006.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "config_data.hh"
+#include <stdexcept>
+
+#include <list>
+
+/// Classe d'accés à la configuration.
+/// Singleton, created by hand.
+class Config
+{
+ ConfigData configData_;
+ public:
+ typedef std::list<int> IntList;
+ typedef std::list<double> FloatList;
+ typedef std::list<std::string> StringList;
+ private:
+ static Config *instance_;
+ public:
+ /// Boa constricteur, voir ConfigData.
+ Config (int &argc, char **&argv)
+ : configData_ (argc, argv)
+ {
+ create ();
+ }
+ /// Boa constricteur, voir ConfigData.
+ Config (int &argc, char **&argv, const std::string &file)
+ : configData_ (argc, argv, file)
+ {
+ create ();
+ }
+ /// Check and fill the private instance_ variables for singleton
+ void create ()
+ {
+ if (instance_)
+ throw std::runtime_error ("Config already created");
+ instance_ = this;
+ }
+
+ /// Récupère une valeur de configuration, fonction générique.
+ template<typename T>
+ const T &get (const std::string &id) const
+ {
+ return configData_.get<T> (id);
+ }
+ /// Récupère une valeur de configuration, fonction générique avec valeur
+ /// par défaut pour éviter l'exception en cas d'abscence de valeur.
+ template<typename T>
+ const T &get (const std::string &id, const T &defaut) const
+ {
+ return configData_.get<T> (id, defaut);
+ }
+ /// Récupère une valeur de configuration.
+ const any &get (const std::string &id) const
+ {
+ return configData_.get (id);
+ }
+ /// Récupère une valeur de configuration avec valeur par défaut pour
+ /// éviter le renvoie d'exception.
+ const any &get (const std::string &id, const any &defaut) const
+ {
+ return configData_.get (id, defaut);
+ }
+ /// Get the unique instance of the class
+ static inline Config &
+ getInstance ()
+ {
+ if (!instance_)
+ throw std::runtime_error ("Config not created");
+ return *instance_;
+ }
+};
+
+#endif // config_hh
diff --git a/i/chuck/src/config/config_data.cc b/i/chuck/src/config/config_data.cc
new file mode 100644
index 0000000..e4dcf75
--- /dev/null
+++ b/i/chuck/src/config/config_data.cc
@@ -0,0 +1,148 @@
+// config_data.cc
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2006.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "config.hh"
+#include "config_data.hh"
+#include "parser/parser.hh"
+
+#include <stdexcept>
+
+/// Constructeur. Traite les arguments de la ligne de commande et lit le
+/// fichier de configuration. Les arguments traités sont retiré de la
+/// ligne de commande.
+ConfigData::ConfigData (int &argc, char **&argv)
+{
+ init (argc, argv, "rc/config");
+}
+
+/// Constructeur. Fourni en plus un fichier de configuration différent de
+/// celui par défaut.
+ConfigData::ConfigData (int &argc, char **&argv, const std::string &file)
+{
+ init (argc, argv, file);
+}
+
+/// Récupère une valeur de configuration.
+const any &
+ConfigData::get (const std::string &id) const
+{
+ Data::const_iterator i = data_.find (id);
+ if (i == data_.end ())
+ throw std::runtime_error (id + ": config item not found");
+ return i->second;
+}
+
+/// Récupère une valeur de configuration avec une valeur par défaut. Ne
+/// renvoye pas d'exception.
+const any &
+ConfigData::get (const std::string &id, const any &defaut) const
+{
+ Data::const_iterator i = data_.find (id);
+ // If not found, return default values
+ if (i == data_.end ())
+ return defaut;
+ else
+ return i->second;
+}
+
+/// Ajoute une valeur de configuration. VAL prend l'ancienne valeur ou un any
+/// vide.
+void
+ConfigData::add (const std::string &id, any &val)
+{
+ any &a = data_[id];
+ a.swap (val);
+}
+
+/// Classe dérivé de Parser pour le parsing de conf.
+class ConfigParser : public Parser
+{
+ ConfigData &data_;
+ public:
+ /// Constructeur.
+ ConfigParser (ConfigData &data) : data_ (data) { }
+ /// Fonction appelée lors d'une affectation. VAL peut être modifié, il est
+ /// détruit suite à l'appel.
+ virtual void assign (const std::string &id, any &val)
+ {
+ data_.add (id, val);
+ }
+};
+
+/// Initialise (lit la ligne de commande et les fichiers de config.
+void
+ConfigData::init (int &argc, char **&argv, const std::string &file)
+{
+ std::string configFile (file);
+ std::string configString;
+ // Parse la ligne de commande.
+ for (int i = 1; i < argc; )
+ {
+ int del = 0;
+ // -F CONFIG_FILE
+ if (argv[i][0] == '-' && argv[i][1] == 'F')
+ {
+ if (argv[i][2] != '\0')
+ {
+ del = 1;
+ configFile = &argv[i][2];
+ }
+ else if (i + 1 < argc)
+ {
+ del = 2;
+ configFile = argv[i + 1];
+ }
+ }
+ // -C CONFIG_STRING
+ if (argv[i][0] == '-' && argv[i][1] == 'C')
+ {
+ configString += ';';
+ if (argv[i][2] != '\0')
+ {
+ del = 1;
+ configString += &argv[i][2];
+ }
+ else if (i + 1 < argc)
+ {
+ del = 2;
+ configString += argv[i + 1];
+ }
+ }
+ // Supprime ces arguments de la liste.
+ if (del)
+ {
+ for (int j = i; j < argc - del + 1; j++)
+ argv[j] = argv[j + del];
+ argc -= del;
+ }
+ else
+ i++;
+ }
+ // Lit le fichier de config.
+ ConfigParser p (*this);
+ p.parseFile (configFile);
+ // Parse les chaînes supplémentaires.
+ p.parseString (configString);
+}
+
diff --git a/i/chuck/src/config/config_data.hh b/i/chuck/src/config/config_data.hh
new file mode 100644
index 0000000..51caabd
--- /dev/null
+++ b/i/chuck/src/config/config_data.hh
@@ -0,0 +1,68 @@
+#ifndef config_data_hh
+#define config_data_hh
+// config_data.hh
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2006.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "utils/any.hh"
+
+#include <map>
+#include <string>
+
+/// Classe contenant les informations de configuration.
+class ConfigData
+{
+ typedef std::map<std::string, any> Data;
+ Data data_;
+ public:
+ /// Constructeur. Traite les arguments de la ligne de commande et lit le
+ /// fichier de configuration. Les arguments traités sont retiré de la
+ /// ligne de commande.
+ ConfigData (int &argc, char **&argv);
+ /// Constructeur. Fourni en plus un fichier de configuration différent de
+ /// celui par défaut.
+ ConfigData (int &argc, char **&argv, const std::string &file);
+ /// Récupère une valeur de configuration, fonction générique.
+ template<typename T>
+ const T &get (const std::string &id) const;
+ /// Récupère une valeur de configuration, fonction générique avec valeur
+ /// par défaut. Cette fonction ne renvoie d'exception que si il y a un
+ /// problème de type et non de valeur non existante.
+ template<typename T>
+ const T &get (const std::string &id, const T &defaut) const;
+ /// Récupère une valeur de configuration.
+ const any &get (const std::string &id) const;
+ /// Récupère une valeur de configuration avec une valeur par défaut. Ne
+ /// renvoye pas d'exception.
+ const any &get (const std::string &id, const any &defaut) const;
+ /// Ajoute une valeur de configuration. VAL prend l'ancienne valeur ou un
+ /// any vide.
+ void add (const std::string &id, any &val);
+ private:
+ /// Initialise (lit la ligne de commande et les fichiers de config.
+ void init (int &argc, char **&argv, const std::string &file);
+};
+
+#include "config_data.tcc"
+
+#endif // config_data_hh
diff --git a/i/chuck/src/config/config_data.tcc b/i/chuck/src/config/config_data.tcc
new file mode 100644
index 0000000..71fa6d6
--- /dev/null
+++ b/i/chuck/src/config/config_data.tcc
@@ -0,0 +1,59 @@
+// config_data.tcc
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2006.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+#include <stdexcept>
+
+/// Récupère une valeur de configuration, fonction générique.
+template<typename T>
+const T &
+ConfigData::get (const std::string &id) const
+{
+ const any &a = get (id);
+ const T *v = any_cast<T> (&a);
+ if (!v)
+ throw std::runtime_error (id + ": config item with bad type");
+ return *v;
+}
+
+/// Récupère une valeur de configuration, fonction générique avec valeur
+/// par défaut. Cette fonction ne renvoie d'exception que si il y a un
+/// problème de type et non de valeur non existante.
+template<typename T>
+const T &
+ConfigData::get (const std::string &id, const T &defaut) const
+{
+ // Construct an empty any
+ any empty;
+ // Get the value with an empty one by default
+ const any &a = get (id, empty);
+ // Empty value returned, not found
+ if (a.empty ())
+ return defaut;
+ const T *v = any_cast<T> (&a);
+ if (!v)
+ throw std::runtime_error (id + ": config item with bad type");
+ return *v;
+}
+
diff --git a/i/chuck/src/config/test_config.cc b/i/chuck/src/config/test_config.cc
new file mode 100644
index 0000000..3b3e368
--- /dev/null
+++ b/i/chuck/src/config/test_config.cc
@@ -0,0 +1,48 @@
+// test_config.cc
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2006.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "config.hh"
+
+#include <iterator>
+#include <algorithm>
+
+int
+main (int argc, char **argv)
+{
+ try
+ {
+ // Create manually the instance of the config class
+ Config c (argc, argv);
+ // Example for getting the unique instance
+ Config &ci = Config::getInstance ();
+ for (int i = 1; i < argc; ++i)
+ std::cout << ci.get (argv[i]) << std::endl;
+ }
+ catch (const std::exception &e)
+ {
+ std::cerr << e.what () << std::endl;
+ return 1;
+ }
+ return 0;
+}