/* 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_O_VALUE_STREAM_HPP
#define NMM_O_VALUE_STREAM_HPP NMM_O_VALUE_STREAM_HPP

#include "nmm/nmm_prefix.hpp"

#include "OStream.hpp"
#include "visibility.hpp"

#include <string>

namespace NMM {

  class Value;
  class TypeInfo;

  /** An output value stream.
   * A value stream is a stream extended for type-safe serialization.
   * It stores type information along with serialized data and has additional support for
   * serializing structures and polymorphic types.
   *
   * This is an abstract class which defines the interface of a value stream.
   * All extended features of a value stream must be implemented in subclasses.
   */
  class DLLEXPORT OValueStream
    : public OStream
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Default constructor.
     */
    OValueStream();

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

    /** Virtual destructor.
     */
    virtual ~OValueStream() {};

    /** Virtual copy constructor.
     *
     * @return pointer to copy of this OValueStream
     */
    virtual OValueStream* copy() const = 0;
    //@}

    /** @name Serialization Operator
     */
    //@{

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual OStream& operator<<(const nmm_uint16& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual OStream& operator<<(const nmm_int16& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual OStream& operator<<(const nmm_uint32& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual OStream& operator<<(const nmm_int32& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual OStream& operator<<(const nmm_uint64& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual OStream& operator<<(const nmm_int64& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual OStream& operator<<(const nmm_bool& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual OStream& operator<<(const nmm_double& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this 
     */
    virtual OStream& operator<<(const nmm_float& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual OStream& operator<<(const nmm_char& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual OStream& operator<<(const nmm_uchar& value) = 0;

    /** Output serialization operator.
     * This is the abstract output serialization operator for a basic type.
     * It must be implemented by subclasses.
     * Subclass implementations must have covariant return types.
     *
     * @param value Reference to variable to be serialized
     * @return *this
     */
    virtual OValueStream& operator<<(const string& value) = 0;
    //@}

    /** @name Buffer Writing
     */
    //@{
    /** Write raw data to this stream.
     * This method writes the given number of raw bytes from the buffer to the stream.
     *
     * @param buffer Pointer to buffer that stores the data to be written
     * @param size Number of bytes to write
     */
    virtual void writeBuffer(const char* buffer, nmm_uint32 size) = 0;

    /** Write raw data to this stream.
     * This method writes the given number of raw bytes from the buffer to the stream.
     *
     * @param buffer Pointer to buffer that stores the data to be written
     * @param size Number of bytes to write
     */
    virtual void writeBuffer(const unsigned char* buffer, nmm_uint32 size) = 0;
    //@}

    /** @name Type Information
     */
    //@{
    /** Begin writing structure elements.
     * This method begins serialization of attributes of a structure.
     *
     * @param type Type name of the structure
     * @param ns Type namespace of the structure
     */
    virtual void beginType(const string& type, const string& ns = "std") = 0;

    /** Begin writing structure elements.
     * This method begins serialization of attributes of a structure.
     *
     * @param type Type information if the structure
     */
    virtual void beginType(const TypeInfo& type);

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

    /** 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* getOValueStreamType() const = 0;
    //@}
  protected:
    /** Subclass constructor.
     * This constructor must be used by subclasses when initializing the base class.
     * Every subclass must also define this subclass constructor as a protected constructor.
     *
     * @param ti Type information for this object
     */
    OValueStream(const TypeInfo& ti);
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
