// $Id: Config.hh,v 1.7 2005/05/05 15:41:47 thea Exp $
// Author: Daniel De Marco    Jan, 25 2002

/*****************************************************************************
 * ESAF: Euso Simulation and Analysis Framework                              *
 *                                                                           *
 *  Id: Config                                                               *
 *  Package: Base                                                            *
 *  Coordinator: Marco.Pallavicini                                           *
 *                                                                           *
 *****************************************************************************/

#ifndef __CONFIG_HH__
#define __CONFIG_HH__

#include <string>
#include <map>
#include "ConfigFileParser.hh"
#include "EsafConfigurable.hh"

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// Config                                                                     //
//                                                                            //
// ESAF configuration manager                                                 //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

class Config : public EsafMsgSource {
public:
    virtual ~Config();
    static Config *Get();

    void AssociateParent(const string& c_name, const string& p_name);
    // associate one class name with parent name
    // this mechanism allow to ask for base class parameters getting the right file
    // in case of inheritance trees

    void AddUserDefinition(const string &ClassName, const string &expr); 
    // add a definition of a class variable that overtakes the config file one 

    void DefineConfig(const string& standard_config_name);

    inline Bool_t IsStandardConfigDefined() const;
    
    Bool_t LoadUserConfig( const char* );
    // load a set of user definitions from file

    inline const char* GetDumpFileName() { return fDumpFileName.c_str(); }
        
    ConfigFileParser *GetCF(const string &ClassType, const string &ClassName, const string &path="" );
    // returns the ConfigFileParser object corresponding to ClassName and ClassType

    inline ConfigFileParser *GetCF(const EsafConfigurable *obj);
    // returns a standard configuration, i.e. a single ConfigFileParser with
    // all parameters

    ConfigFileParser *GetStandardConfig();
    inline const char* GetStdConfigList() const { return fStdConfigList.c_str(); };
    const map<string,ConfigFileParser*> &GetStandardConfigMap() const {return fStandards;}

	void SaveConfig();
    inline void SetDumpFileName( const string& name ) { SetDumpFileName( name.c_str() ); }
    void SetDumpFileName( const char* name );
    inline void SetStdConfigList( const char* fname ) { fStdConfigList = fname; }
private:
    void Reset();
    // reset; clear internal maps in order to force configuration file reading
    void ResetUserDef();

    Config();
    // private ctor
    
    static Config *fMe;
    // singleton
    
    void LoadStandardConfigs();
    // load standard configurations

    map<string, ConfigFileParser*> fCfs;
    map<string, ConfigFileParser*> fStandards;
    map<string, vector<string>* > fUserDef;
    map<string, string*> fChildTable;
    string fCurrentConfig;
    string fStdConfigList;

    string fDumpFileName;  // config dump filename
        
	friend class EusoApplication;
    ClassDef(Config,0)
};

//______________________________________________________________________________
inline ConfigFileParser *Config::GetCF(const EsafConfigurable *obj) {
    return GetCF( obj->ClassType(), obj->ClassName() );
}

//______________________________________________________________________________
inline Bool_t Config::IsStandardConfigDefined() const {
    //
    // Returns true if a standard configuration was selected
    // 
    return !(fCurrentConfig == "None" );
}
#endif  /* __CONFIG_HH__ */