/* outputbuffer.h
 * this file is part of libxml++
 *
 * copyright (C) 2003 by libxml++ developer's team
 *
 * this file is covered by the GNU Lesser General Public License,
 * which should be included with libxml++ as the file COPYING.
 */

#ifndef __LIBXMLPP_OUTPUTBUFFER_H
#define __LIBXMLPP_OUTPUTBUFFER_H

#include <glibmm/ustring.h>
#include <libxml++/noncopyable.h>

#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C"
{
  struct _xmlOutputBuffer;
}
#endif //DOXYGEN_SHOULD_SKIP_THIS

namespace xmlpp
{
  struct OutputBufferCallback;

  /** Base class for xmlOutputBuffer wrapper
   *
   * It can be derived from to create a new output buffer.
   * A child class has to override do_write(), and possibly
   * do_close() if some actions are required before buffer closing.
   */
  class OutputBuffer: public NonCopyable
  {
    public:
      /**
       * @param encoding The encoding that the derived class waits for in do_write. If
       * not provided, UTF-8 will be sent to do_write.
       *
       * @warning The encoding is done by libxml. As a consequence, libxml must
       * have a translator to the target encoding.
       */
      LIBXMLPP_API
      OutputBuffer(const Glib::ustring& encoding = Glib::ustring());
      LIBXMLPP_API ~OutputBuffer() override;

    public:
      /** gives an access to the underlying libxml structure to the children
       */
      LIBXMLPP_API _xmlOutputBuffer* cobj();

      /** gives an access to the underlying libxml structure to the children
       */
      LIBXMLPP_API const _xmlOutputBuffer* cobj() const;

    private:
      LIBXMLPP_API bool on_write(const char * buffer, int len);
      LIBXMLPP_API bool on_close();

      /** Function called when some data are sent to the buffer.
       * @param buffer The datas encoded in the charset given to the constructor
       * @param len Buffer length
       *
       * This function MUST be overriden in derived classes.
       */
      LIBXMLPP_API
      virtual bool do_write(const char * buffer, int len) = 0;

      /** Function called before closing the buffer.
       * Derived classes should override it if some actions are required before
       * closing the buffer, instead of doing them in the destructor.
       */
      LIBXMLPP_API virtual bool do_close();

      /**
       * Underlying libxml2 structure.
       */
      _xmlOutputBuffer* impl_;

      friend struct LIBXMLPP_API OutputBufferCallback;
  };

}

#endif
