summaryrefslogtreecommitdiff
path: root/i/marvin/src/config
diff options
context:
space:
mode:
authordufourj2006-03-11 16:10:25 +0000
committerdufourj2006-03-11 16:10:25 +0000
commit15cd6ebf2ae12c601d6f6aef8337aa16effeacc2 (patch)
tree4e384c0fac315177b8aec93dea9c68046daddf71 /i/marvin/src/config
parentc4395e067140b7bbc40a20c4d8d720d30b096033 (diff)
Classe Config en singleton
Diffstat (limited to 'i/marvin/src/config')
-rw-r--r--i/marvin/src/config/Makefile.defs2
-rw-r--r--i/marvin/src/config/config.cc27
-rw-r--r--i/marvin/src/config/config.hh28
-rw-r--r--i/marvin/src/config/test_config_data.cc4
4 files changed, 58 insertions, 3 deletions
diff --git a/i/marvin/src/config/Makefile.defs b/i/marvin/src/config/Makefile.defs
index 500470c..15f72bb 100644
--- a/i/marvin/src/config/Makefile.defs
+++ b/i/marvin/src/config/Makefile.defs
@@ -1,6 +1,6 @@
PROGRAMS += test_config_data
-config_OBJECTS = lexer.o parser.o config_data.o config_parser.o
+config_OBJECTS = lexer.o parser.o config_data.o config_parser.o config.o
test_config_data_OBJECTS = $(config_OBJECTS) test_config_data.o
diff --git a/i/marvin/src/config/config.cc b/i/marvin/src/config/config.cc
new file mode 100644
index 0000000..333bd4a
--- /dev/null
+++ b/i/marvin/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/marvin/src/config/config.hh b/i/marvin/src/config/config.hh
index bcec7d8..ab9cfb7 100644
--- a/i/marvin/src/config/config.hh
+++ b/i/marvin/src/config/config.hh
@@ -25,10 +25,12 @@
//
// }}}
#include "config_data.hh"
+#include <stdexcept>
#include <list>
/// Classe d'accés à la configuration.
+/// Singleton, created by hand.
class Config
{
ConfigData configData_;
@@ -36,15 +38,29 @@ class Config
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
@@ -56,6 +72,14 @@ class Config
{
return configData_.get (id);
}
+ /// 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/marvin/src/config/test_config_data.cc b/i/marvin/src/config/test_config_data.cc
index 2ddb610..5643ce1 100644
--- a/i/marvin/src/config/test_config_data.cc
+++ b/i/marvin/src/config/test_config_data.cc
@@ -32,7 +32,11 @@ main (int argc, char **argv)
{
try
{
+ // Create manually the instance of the config class
Config cd (argc, argv);
+ // Example for getting the unique instance
+ //Config &confInstance = Config::getInstance ();
+
for (int i = 1; i < argc; ++i)
std::cout << cd.get (argv[i]) << std::endl;
}