/* 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_SERIALIZED_EXCEPTION_HPP
#define NMM_SERIALIZED_EXCEPTION_HPP NMM_SERIALIZED_EXCEPTION_HPP

#include "nmm/nmm_prefix.hpp"

#include "SerializedValue.hpp"
#include "TValue.hpp"
#include "visibility.hpp"

#include <string>
#include <iosfwd>

#include "visibility.hpp"

namespace NMM {

  /** A serializable exception.
   * This is the base class of all exceptions that are serializable and therefore can be thrown by
   * NMM interface methods.
   *
   * Throwing an exception:
   * @code
   * // In a method of a class derived from NamedObject
   * throw SerializedException(this, "Brief error description");
   *
   * // In all other functions
   * throw SerializedException("Brief error description");
   * @endcode
   *
   * Note that for exceptions which are used frequently and which carry additional information,
   * a subclass of this class should be defined.
   *
   * The reference to the NamedObject is not serialized. It should only be used for debugging
   * of locally thrown exceptions.
   *
   * Catching an exception:
   * @code
   * try {
   *   // ...
   * }
   * catch(const SerializedException& e) {
   *   cerr << e << endl;
   *   // ...
   * }
   * @endcode
   */
  class DLLEXPORT SerializedException
    : public SerializedValue
  {
  private:
    const class NamedObject* parent;    ///< NamedObject from which the exception was thrown
    TValue<std::string> comment;        ///< Exception comment

  public:
    /** Create an exception with default attributes.
     */
    SerializedException();

    /** Create an exception.
     *
     * @param comment Exception comment (a brief error description)
     */
    SerializedException(const std::string& comment);

    /** Create an exception within a method of a NamedObject.
     *
     * @param parent NamedObject from which the exception is thrown
     * @param comment Exception comment (a brief error description)
     */
    SerializedException(const NamedObject* parent, const std::string& comment = std::string());

    /** Copy constructor.
     */
    SerializedException(const SerializedException& exception);

    /** Virtual copy constructor.
     *
     * @return A copy of this exception which has the same type as this exception
     */
    virtual SerializedException* copy() const;

    /** Get the NamedObject from which this exception was thrown.
     *
     * @return A pointer to the NamedObject from which this exception was thrown.
     */
    inline const NamedObject *getParent()      const { return parent; }

    /** Get the exception comment.
     *
     * @return The exception comment
     */
    inline const std::string& getComment()     const { return comment.getRValue();}

    /** Set the exception comment.
     *
     * @param comment The exception comment
     */
    inline void setComment(const std::string& comment) { this->comment.setValue(comment); }

    /** Output insertion operator.
     * This operator writes a string representation of this exception to an output stream.
     * It can be used for displaying exceptions in debug messages.
     *
     * @param os An output stream
     * @param e An exception
     * @return os
     */
    friend DLLEXPORT std::ostream& operator<<(std::ostream& os, const SerializedException& e);
  };

}

#include "nmm/nmm_suffix.hpp"

#endif
