// This may look like C code, but it's really -*- C++ -*-
/*
 * Copyright (C) 2007 Wim Dumon, Leuven, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#ifndef WMEMORY_RESOURCE_H_
#define WMEMORY_RESOURCE_H_

#include <string>
#include <Wt/WResource>

namespace Wt {

/*! \class WMemoryResource Wt/WMemoryResource Wt/WMemoryResource
 *  \brief A resource which streams data from memory
 *
 * Use this resource if you want to serve resource data from memory. This
 * is suitable for relatively small resources, which still require some
 * computation.
 *
 * If creating the data requires computation which you would like to
 * post-pone until the resource is served, then you may want to
 * directly reimplement WResource instead and compute the data on the
 * fly while streaming.
 *
 * Usage examples:
 * \code
 * Wt::WMemoryResource *imageResource = new Wt::WMemoryResource("image/gif", this);
 *
 * static const unsigned char gifData[]
 *    = { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00,
 *        0x80, 0x00, 0x00, 0xdb, 0xdf, 0xef, 0x00, 0x00, 0x00, 0x21,
 *        0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00,
 *        0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44,
 *        0x01, 0x00, 0x3b };
 *
 * imageResource->setData(gifData, 43);
 * Wt::WImage *image = new Wt::WImage(imageResource, "1 transparent pixel");
 * \endcode
 *
 * \sa WFileResource.
 */
class WT_API WMemoryResource : public WResource
{
public:
  /*! \brief Creates a new resource.
   *
   * You must call setMimeType() and setData() before using the resource.
   */
  WMemoryResource(WObject *parent = 0);

  /*! \brief Creates a new resource with given mime-type.
   *
   * You must call setData() before using the resource.
   */
  WMemoryResource(const std::string& mimeType, WObject *parent = 0);

  /*! \brief Creates a new resource with given mime-type and data
   */
  WMemoryResource(const std::string& mimeType,
		  const std::vector<unsigned char> &data,
		  WObject *parent = 0);

  ~WMemoryResource();

  /*! \brief Sets new data for the resource to serve.
   */
  void setData(const std::vector<unsigned char> &data);

  /*! \brief Sets new data for the resource to serve.
   *
   * Sets the data from using the first \p count bytes from the
   * C-style \p data array.
   */
  void setData(const unsigned char *data, int count);

  /*! \brief Returns the data this resource will serve.
   */
  const std::vector<unsigned char>& data() const;

  /*! \brief Returns the mime-type.
   */
  const std::string mimeType() const { return mimeType_; }

  /*! \brief Sets the mime-type.
   */
  void setMimeType(const std::string& mimeType);

  virtual void handleRequest(const Http::Request& request,
			     Http::Response& response);

private:
  std::string mimeType_;
  std::vector<unsigned char> data_;
};

}

#endif // WMEMORY_RESOURCE_H_
