From 15cd6ebf2ae12c601d6f6aef8337aa16effeacc2 Mon Sep 17 00:00:00 2001 From: dufourj Date: Sat, 11 Mar 2006 16:10:25 +0000 Subject: Classe Config en singleton --- i/marvin/src/config/Makefile.defs | 2 +- i/marvin/src/config/config.cc | 27 +++++++++++++++++++++++++++ i/marvin/src/config/config.hh | 28 ++++++++++++++++++++++++++-- i/marvin/src/config/test_config_data.cc | 4 ++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 i/marvin/src/config/config.cc (limited to 'i/marvin/src/config') 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: +// }}} +#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 #include /// Classe d'accés à la configuration. +/// Singleton, created by hand. class Config { ConfigData configData_; @@ -36,15 +38,29 @@ class Config typedef std::list IntList; typedef std::list FloatList; typedef std::list 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 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; } -- cgit v1.2.3