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/asserv/asserv.cc | 9 ++++++--- i/marvin/src/asserv/asserv.hh | 5 ++--- i/marvin/src/asserv/test_asserv.cc | 2 +- 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 ++++ i/marvin/src/motor/motor.cc | 7 +++++-- i/marvin/src/motor/motor.hh | 3 +-- i/marvin/src/motor/test_motor.cc | 4 ++-- 10 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 i/marvin/src/config/config.cc (limited to 'i') diff --git a/i/marvin/src/asserv/asserv.cc b/i/marvin/src/asserv/asserv.cc index 082f39b..2c36887 100644 --- a/i/marvin/src/asserv/asserv.cc +++ b/i/marvin/src/asserv/asserv.cc @@ -24,15 +24,16 @@ // }}} #include "asserv.hh" +#include "config/config.hh" #include /// Constructeur -Asserv::Asserv (const Config & config, Receiver & receiver) +Asserv::Asserv (Receiver & receiver) :proto_(*this), receiver_(receiver), log_("asserv") { // Rechargement des paramètres - loadConfig(config); + loadConfig (); //Ouverture du port série proto_.open(ttyName_); } @@ -428,8 +429,10 @@ void Asserv::receive(char command, const Proto::Frame &frame) } /// Charge les données de la classe config -void Asserv::loadConfig(const Config & config) +void Asserv::loadConfig() { + // Get the config instance + Config &config = Config::getInstance (); ttyName_ = config.get("asserv.tty"); footing_ = config.get("asserv.footing"); epsilon_ = config.get("asserv.epsilon"); diff --git a/i/marvin/src/asserv/asserv.hh b/i/marvin/src/asserv/asserv.hh index 4064537..b3d3183 100644 --- a/i/marvin/src/asserv/asserv.hh +++ b/i/marvin/src/asserv/asserv.hh @@ -26,7 +26,6 @@ // }}} #include "utils/non_copyable.hh" -#include "config/config.hh" #include "proto/proto.hh" #include "log/log.hh" @@ -94,7 +93,7 @@ class Asserv : public NonCopyable, public Proto::Receiver Log log_; public: /// Constructeur. - Asserv (const Config & config, Asserv::Receiver & receiver); + Asserv (Asserv::Receiver & receiver); /// Reset la carte et envois les paramètres. void reset (void); /// Essaie de purger la liste d'émission et indique si elle est vide. @@ -147,7 +146,7 @@ class Asserv : public NonCopyable, public Proto::Receiver private: /// Charge les données de la classe config - void loadConfig(const Config & config); + void loadConfig (); /// Fonctions de conversion int mm2pas(double dist, bool format24eme = false); //Format 24/8 ou 8 int radTo256(double angle, bool format24eme = false); //Format 8 ou 8/24 diff --git a/i/marvin/src/asserv/test_asserv.cc b/i/marvin/src/asserv/test_asserv.cc index 6d4e672..7cd2f95 100644 --- a/i/marvin/src/asserv/test_asserv.cc +++ b/i/marvin/src/asserv/test_asserv.cc @@ -119,7 +119,7 @@ main (int argc, char **argv) } Config config(argc, argv); TestAsserv testAsserv; - Asserv asserv(config, testAsserv); + Asserv asserv (testAsserv); i = 1; while (i < argc) { 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; } diff --git a/i/marvin/src/motor/motor.cc b/i/marvin/src/motor/motor.cc index 7e78aff..e9c0937 100644 --- a/i/marvin/src/motor/motor.cc +++ b/i/marvin/src/motor/motor.cc @@ -24,14 +24,17 @@ // }}} #include "motor.hh" +#include "config/config.hh" #include /// Constructeur -Motor::Motor (const Config & config) - :asserv_(config, *this), log_("Motor"),idle_(true), doneDone_(false), +Motor::Motor (void) + : asserv_(*this), log_("Motor"),idle_(true), doneDone_(false), f0Sended_(false), pinUpdated_(false) { + // Get Config + Config &config = Config::getInstance (); // XXX Quant est-il des positions de départ du robot??? pStatPosition_ = config.get("motor.pStatPosition"); pStatMotor_ = config.get("motor.pStatMotor"); diff --git a/i/marvin/src/motor/motor.hh b/i/marvin/src/motor/motor.hh index ad67f84..1325c64 100644 --- a/i/marvin/src/motor/motor.hh +++ b/i/marvin/src/motor/motor.hh @@ -25,7 +25,6 @@ // // }}} -#include "config/config.hh" #include "asserv/asserv.hh" #include @@ -68,7 +67,7 @@ class Motor : public Asserv::Receiver public: /// Constructeur - Motor (const Config & config); + Motor (void); /// Initialise les moteurs void init(void); ///Arrête les moteurs diff --git a/i/marvin/src/motor/test_motor.cc b/i/marvin/src/motor/test_motor.cc index f01e299..c901d69 100644 --- a/i/marvin/src/motor/test_motor.cc +++ b/i/marvin/src/motor/test_motor.cc @@ -52,8 +52,8 @@ main (int argc, char **argv) syntax (); return 1; } - Config config(argc, argv); - Motor motor(config); + Config config (argc, argv); + Motor motor; i = 1; while (i < argc) { -- cgit v1.2.3