// 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 you require little computation for creating the data, then
 * you may want to reimplement WResource and compute the data on the fly
 * while streaming.
 *
 * If you have a lot of data, you may want to use a WFileResource instead.
 */
class WT_API WMemoryResource : public WResource
{
public:
  /*! \brief Create a new resource.
   *
   * You must call setMimeType() and setData() before using the resource.
   */
  WMemoryResource(WObject *parent = 0);

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

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

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

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

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

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

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

protected:
  virtual const std::string resourceMimeType() const;
  virtual bool streamResourceData(std::ostream& stream,
				  const ArgumentMap& arguments);
};

}

#endif // WMEMORY_RESOURCE_H_
