/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_MAGIC_NUMBER_MANAGER_LINUX_HPP
#define NMM_MAGIC_NUMBER_MANAGER_LINUX_HPP NMM_MAGIC_NUMBER_MANAGER_LINUX_HPP

#include "nmm/nmm_prefix.hpp"

#include "TypeInfo.hpp"

#include "nmm/utils/Singleton.hpp"
#include "nmm/nmm_hash_map.hpp"

namespace NMM {

  /** Magic number manager.
   * This is a helper class used by the MagicNumberIValueStream and MagicNumberOValueStream
   * classes for managing magic numbers that represent types. The streams use this manager
   * to convert between magic numbers and type information objects.
   *
   * This class is a Singleton.
   */
  class MagicNumberManager {
    DECLARE_SINGLETON(MagicNumberManager, CreateUsingNew, DefaultLifetime, MultiThreaded);
    friend class CreateUsingNew<MagicNumberManager>;
  public:
    /** Convert magic number to type information.
     * This method returns the type information that corresponds to the given magic number.
     * If the magic number is not registered, a null pointer is returned.
     * Otherwise, a pointer to a registered TypeInfo object is returned, which must not be deleted.
     *
     * @param magic_number A magic number
     * @return Pointer to type information or 0
     */
    const TypeInfo* getTypeInfo(int magic_number);

    /** Convert type information to magic number.
     * This method returns the magic number that corresponds to the given type information.
     * If the type information is not registered, -1 is returned.
     *
     * @param t_info Type information
     * @return The magic number or -1
     */
    inline int getMagicNumber(const TypeInfo& t_info) { return getMagicNumber(t_info.t_namespace, t_info.t_name); }

    /** Convert type information to magic number.
     * This method returns the magic number that corresponds to the given type information.
     * If the type information is not registered, -1 is returned.
     *
     * @param type_name Type name
     * @param t_namespace Type namespace
     * @return The magic number or -1
     */
    int getMagicNumber(const std::string& type_name, const std::string& t_namespace);

    /** Register a magic number
     * This method registers a pair of type information and a magic number that both represent
     * the same type.
     *
     * @param t_info Type information
     * @param magic_number A magic number
     */
    void addMagicNumber(const TypeInfo& t_info, int magic_number);

  private:
    /** Private singleton constructor.
     */
    MagicNumberManager(){};

    /** Private singleton destructor.
     */
    ~MagicNumberManager();

    /** Disabled copy constructor
     *
     * @param dfhdas Unused
     */
    MagicNumberManager(const MagicNumberManager& dfhdas);
  private:
    /** Hash function for strings.
     */
    struct string_hash
    {
      /** Get hash value of a string.
       *
       * @param __s A string
       * @return hash value of the string
       */
      size_t operator()(const std::string& __s) const { return HASH_MAP_STL::__stl_hash_string(__s.c_str()); }
    };

    HASH_MAP_STL::hash_map<std::string, HASH_MAP_STL::hash_map<std::string, int, string_hash >, string_hash> __type_info_map;     ///< Map from type information to magic numbers
    HASH_MAP_STL::hash_map<int, TypeInfo*>                                           __magic_number_map;  ///< Map from magic numbers to type information
  };

  /** Singleton interface for MagicNumberManager.
   */
  typedef MagicNumberManager::Singleton SMagicNumberManager;

  /** Static registration of magic numbers.
   * This class is used for static registration of magic numbers.
   *
   * Usage:
   * @code
   * // Register 42 as magic number for std::string
   * static MagicNumberRegister __string_magic_number(TypeInfo("string", "std"), 42);
   * @endcode
   */
  class MagicNumberRegister {
  public:
    /** Constructor.
     *
     * @param t_info Type information
     * @param magic_number Magic number
     */
    MagicNumberRegister(const TypeInfo& t_info, int magic_number) { SMagicNumberManager::getInstance().addMagicNumber(t_info, magic_number); }
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
