/* 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_I_VALUE_STREAM_HPP
#define NMM_MAGIC_NUMBER_I_VALUE_STREAM_HPP NMM_MAGIC_NUMBER_I_VALUE_STREAM_HPP

#include "nmm/nmm_prefix.hpp"

#include "IValueStream.hpp"
#include "GenericMagicNumber.hpp"

#include "visibility.hpp"

namespace NMM {

  class MagicNumberManager;

  /** Input value stream which uses magic numbers.
   * This value stream works with serialized data in binary format.
   * Magic numbers are used for representing type information and structure boundaries.
   *
   * @see MagicNumberOValueStream, MagicNumberManager
   */
  class DLLEXPORT MagicNumberIValueStream
    : public IValueStream,
      public GenericMagicNumber
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Default constructor.
     */
    MagicNumberIValueStream();

    /** Copy constructor.
     *
     * @param iv Value stream object to copy from
     */
    MagicNumberIValueStream(const MagicNumberIValueStream& iv);

    /** Virtual destructor.
     */
    virtual ~MagicNumberIValueStream();

    /** Virtual copy constructor.
     *
     * @return A copy of this object which has the same type
     */
    virtual MagicNumberIValueStream* copy() const { return new MagicNumberIValueStream(*this); }
    //@}


    /** @name Serialization Operator
     */
    //@{
    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_uint16& value)       ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_int16& value)         ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_uint32& value)     ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_int32& value)       ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_uint64& value)     ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_int64& value)       ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_bool& value)          ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_double& value)         ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_float& value)          ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_char& value)           ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual  MagicNumberIValueStream& operator>>(nmm_uchar& value)   ;

    /** Input serialization operator.
     * This is the input serialization operator for a basic type.
     * This implementation parses binary data with magic numbers.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual MagicNumberIValueStream& operator>>(string& value);
    //@}

    /** @name Buffer Reading
     */
    //@{
    /** Read raw data from this stream.
     * This method reads exactly the given number of raw bytes from the stream
     * and stores the read data in a buffer.
     *
     * @param buffer Pointer to buffer that stores the read data
     * @param size Number of bytes to read
     */
    virtual void readBuffer(char* buffer, nmm_uint32 size);

    /** Read raw data from this stream.
     * This method reads exactly the given number of raw bytes from the stream
     * and stores the read data in a buffer.
     *
     * @param buffer Pointer to buffer that stores the read data
     * @param size Number of bytes to read
     */
    virtual void readBuffer(unsigned char* buffer, nmm_uint32 size);

    /** Read data for serialization.
     * This method must be called before using the serialization operators.
     * It fetches data internally.
     * This implementation parses binary data with magic numbers.
     */
    virtual void readData();
    //@}

    /** @name Type Information
     */
    //@{
    /** Begin reading structure attributes.
     * This method begins serialization of attributes of a structure.
     * If the current element is not a structure, then this method fails.
     *
     * @return @c true if this method succeeded, @c false otherwise
     */
    virtual bool goDown();

    /** End reading structure attributes.
     * This method ends serialization of attributes of a structure.
     */
    virtual void goUp();

    /** Check if this stream is empty.
     * This method checks if this stream contains no data to read.
     * If this method returns @c true, serialization operations will fail.
     *
     * @return @c true if this stream is empty, @c false otherwise
     */
    virtual bool isEmpty()          const;

    /** Skip current element.
     * This method skips the current element which would be serialized.
     * It should be used if an element is of the wrong type.
     * It is also used by implementations of serialization operations and goDown()/goUp()
     * in subclasses after serializing an element.
     *
     * @return @c true if the next element is valid, @c false otherwise
     */
    virtual bool nextElement();
    //@}

    /** Get the type of the current element.
     * This method gets the type information for the type of the current element.
     * It should be used before serializing an element to check if the element has the correct type.
     *
     * @return Type information for type of current element
     */
    virtual const TypeInfo& getCurrentTypeInfo() const;

    /** Get the type of the current element.
     * This method gets the type name for the type of the current element.
     *
     * @return Type name for type of current element
     */
    virtual const string& getCurrentType() const;

    /** Get the type of the current element.
     * This method gets the type namespace for the type of the current element.
     *
     * @return Type namespace for type of current element
     */
    virtual const string& getCurrentNamespace() const;

    /** Get the type name of this stream.
     * This method returns the type name of this stream object.
     *
     * @deprecated Use SerializedValue::getTypeInfo() instead
     */
    virtual const char* getIValueStreamType() const { return __i_value_stream_type; }
    //@}
  private:
    const TypeInfo*         __current_type_info;              ///< Type information for current element
    TypeInfo                __complex_type_info;              ///< Type information stored in current element (if no magic number)
    int                     __next_element;                   ///< Magic number of next element
    vector<const TypeInfo*> __open_stack;                     ///< TypeInfo stack for nested structures
    bool                    __empty;                          ///< Structure or stream is empty
    bool                    __stream_empty;                   ///< Stream is empty
    MagicNumberManager*     __magic_number_manager;           ///< Magic number manager

  public:
    static const char*      __i_value_stream_type;            ///< Value stream type name ("MagicNumberIValueStream")
    static const string     __empty_string;                   ///< An empty string
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
