forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_filter.h
More file actions
72 lines (66 loc) · 1.94 KB
/
Copy pathcopy_filter.h
File metadata and controls
72 lines (66 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <[email protected]>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_COPY_FILTER_H
#define CPPCMS_COPY_FILTER_H
#include <booster/streambuf.h>
#include <booster/noncopyable.h>
#include <booster/hold_ptr.h>
#include <cppcms/defs.h>
#include <list>
#include <ostream>
namespace cppcms {
///
/// \brief Copy the output stream part - "tee" filter
///
/// This simple class designed to "copy" all the output that comes to the stream
/// to internal buffer and the stream itself and return the copied data on detaching
/// the filter.
///
/// It is very useful to use with caching, for example:
///
/// \code
/// std::string frame;
/// if(cache().fetch_frame("key",frame)) {
/// out() << frame;
/// }
/// else {
/// cppcms::copy_filter tee(out());
/// ...
/// // generate something heavy
/// ...
/// cache().store_frame("key",tee.detach());
/// }
/// \endcode
///
class CPPCMS_API copy_filter : public booster::noncopyable {
public:
///
/// Create a filter copying all output to internal buffer
///
copy_filter(std::ostream &output);
///
/// When destroyed, if the stream wasn't detached aborts coping the data,
/// making it exception safe.
///
~copy_filter();
///
/// Stop the coping process and return all collected data as string.
///
std::string detach();
private:
class tee_device;
struct data;
booster::hold_ptr<data> d;
booster::streambuf copy_buffer_;
std::ostream &output_;
std::ostream real_output_stream_;
std::list<std::string> data_;
bool detached_;
};
} // cppcms
#endif