/*
 *  Open BEAGLE
 *  Copyright (C) 2001-2004 by Christian Gagne and Marc Parizeau
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact:
 *  Laboratoire de Vision et Systemes Numeriques
 *  Departement de genie electrique et de genie informatique
 *  Universite Laval, Quebec, Canada, G1K 7P4
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/Register.hpp
 *  \brief  Definition of the class Register.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.4 $
 *  $Date: 2004/04/25 23:12:21 $
 */

#ifndef Beagle_Register_hpp
#define Beagle_Register_hpp

#include <map>
#include <string>

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Map.hpp"
#include "beagle/WrapperT.hpp"
#include "beagle/String.hpp"
#include "beagle/Operator.hpp"


namespace Beagle {

// Forward declaration
class System;


/*!
 *  \class Register beagle/Register.hpp "beagle/Register.hpp"
 *  \brief Register implementation, where all the parameters of the framework are centralized.
 *  \ingroup ECF
 *  \ingroup Sys
 */
class Register : public Map {

public:

  /*!
   *  \struct Description beagle/Register.hpp "beagle/Register.hpp"
   *  \brief Parameter extensive description.
   */
  struct Description {
    std::string mBrief;         //!< Brief description of the parameter.
    std::string mType;          //!< Type of the parameter.
    std::string mDefaultValue;  //!< Default value of the parameter.
    std::string mDescription;   //!< Long description of the parameter.

             Description();
    explicit Description(std::string inBrief, std::string inType,
                         std::string inDefaultValue, std::string inDescription);
  };

  //! Register allocator type.
  typedef AllocatorT<Register,Map::Alloc>
          Alloc;
  //! Register handle type.
  typedef PointerT<Register,Map::Handle>
          Handle;
  //! Register bag type.
  typedef ContainerT<Register,Map::Bag>
          Bag;

           Register() { }
  virtual ~Register() { }

  void               addEntry(std::string inTag, Object::Handle inEntry,
                              const Description& inDescription);
  Object::Handle     deleteEntry(std::string inTag);
  const Description& getDescription(std::string inTag) const;
  virtual void       initialize(System& ioSystem, std::string inConfigFileName="");
  bool               isRegistered(std::string inTag) const;
  Object::Handle     modifyEntry(std::string inTag, Object::Handle inNewEntry);
  Description        modifyDescription(std::string inTag, const Description& inNewDescription);
  virtual void       parseCommandLine(System& ioSystem, int& ioArgc, char** ioArgv);
  virtual void       read(XMLNode::Handle& inNode);
  virtual void       readParametersFile(std::string inFileName, System& ioSystem);
  virtual void       readWithSystem(XMLNode::Handle& inNode, System& ioSystem);
  virtual void       write(XMLStreamer& ioStreamer) const;
  
  /*!
   *  \brief Get an entry of the register.
   *  \param inTag Tag of the parameter.
   *  \return Smart pointer to the entry. Return a NULL pointer if there is no entry
   *     associated with the tag.
   */
  inline Object::Handle operator[](std::string inTag)
  {
    iterator lIterObj = Map::find(inTag);
    if(lIterObj == end()) return Object::Handle(NULL);
    else return lIterObj->second;
  }

  /*!
   *  \brief Get an entry of the register.
   *  \param inTag Tag of the parameter.
   *  \return Constant smart pointer to the entry. Return a NULL pointer if there is no entry
   *     associated with the tag.
   */
  inline const Object::Handle operator[](std::string inTag) const
  {
    const_iterator lIterObj = Map::find(inTag);
    if(lIterObj == end()) return Object::Handle(NULL);
    else return lIterObj->second;
  }

  /*!
   *  \brief Get an entry of the register.
   *  \param inTag Tag of the parameter.
   *  \return Smart pointer to the entry. Return a NULL pointer if there is no entry
   *     associated with the tag.
   */
  inline Object::Handle getEntry(std::string inTag)
  {
    iterator lIterObj = Map::find(inTag);
    if(lIterObj == end()) return Object::Handle(NULL);
    else return lIterObj->second;
  }

  /*!
   *  \brief Get an entry of the register.
   *  \param inTag Tag of the parameter.
   *  \return Constant smart pointer to the entry. Return a NULL pointer if there is no entry
   *     associated with the tag.
   */
  inline const Object::Handle getEntry(std::string inTag) const
  {
    const_iterator lIterObj = Map::find(inTag);
    if(lIterObj == end()) return Object::Handle(NULL);
    else return lIterObj->second;
  }

  /*!
   *  \brief Calling post-initialization hook of the register.
   *  \param ioSystem Evolutionary system to use for post-initialization.
   */
  virtual void postInit(System& ioSystem) { } 
  
protected:
  void  addDescription(std::string inTag, const Description& inDescription);
  char* eraseArg(int inN, int& ioArgc, char** ioArgv) const;
  void  interpretArgs(System& ioSystem, int& ioArgc, char** ioArgv);
  void  showHelp(char** ioArgv, std::ostream& ioOs=std::cerr) const;
  void  showUsage(char** ioArgv, std::ostream& ioOs=std::cerr) const;

  std::map<std::string, Description>      mDescriptions;   //!< Parameters descriptions.
  std::map<std::string, Operator::Handle> mOperatorMap;    //!< Operator map.
  std::vector<Operator::Handle>           mOperatorUsed;   //!< Operator used.
  String::Handle                          mFileName;       //!< Configuration file name.

};

}

#endif // Beagle_Register_hpp
