Wt: Wt::WMemoryResource Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions
Wt::WMemoryResource Class Reference

A resource which streams data from memory. More...

`#include <Wt/WMemoryResource>`

Inheritance diagram for Wt::WMemoryResource:
[legend]

List of all members.

## Public Member Functions
| WMemoryResource (WObject *parent=0)
| Creates a new resource.
| WMemoryResource (const std::string &mimeType, WObject *parent=0)
| Creates a new resource with given mime-type.
| WMemoryResource (const std::string &mimeType, const std::vector< unsigned char > &data, WObject *parent=0)
| Creates a new resource with given mime-type and data.
void | setData (const std::vector< unsigned char > &data)
| Sets new data for the resource to serve.
void | setData (const unsigned char *data, int count)
| Sets new data for the resource to serve.
const std::vector< unsigned char > & | data () const
| Returns the data this resource will serve.
const std::string | mimeType () const
| Returns the mime-type.
void | setMimeType (const std::string &mimeType)
| Sets the mime-type.
virtual void | handleRequest (const Http::Request &request, Http::Response &response)
| Handles a request.

## Detailed Description

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:

     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");

See also:
  WFileResource.

## Constructor & Destructor Documentation

Wt::WMemoryResource::WMemoryResource | ( | WObject * | parent = `0` | ) |

Creates a new resource.

You must call setMimeType() and setData() before using the resource.

Wt::WMemoryResource::WMemoryResource | ( | const std::string & | mimeType,
|  | WObject * | parent = `0`
| ) |  |

Creates a new resource with given mime-type.

You must call setData() before using the resource.

## Member Function Documentation

void Wt::WMemoryResource::handleRequest | ( | const Http::Request & | request,
|  | Http::Response & | response
| ) |  | `[virtual]`

Handles a request.

Reimplement this method so that a proper response is generated for the given request. From the `request` object you can access request parameters and whether the request is a continuation request. In the `response` object, you should set the mime type and stream the output data.

A request may also concern a continuation, indicated in Http::Request::continuation(), in which case the next part for a previously created continuation should be served.

While handling a request, which may happen at any time together with event handling, the library makes sure that the resource is not being concurrently deleted, but multiple requests may happend simultaneously for a single resource.

Implements Wt::WResource.

void Wt::WMemoryResource::setData | ( | const unsigned char * | data,
|  | int | count
| ) |  |

Sets new data for the resource to serve.

Sets the data from using the first `count` bytes from the C-style `data` array.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator