|
7 | 7 | #include <string> |
8 | 8 |
|
9 | 9 | namespace stan { |
10 | | -namespace callbacks { |
| 10 | + namespace callbacks { |
11 | 11 |
|
12 | | -/** |
13 | | - * <code>stream_writer</code> is an implementation |
14 | | - * of <code>writer</code> that writes to a stream. |
15 | | - */ |
16 | | -class stream_writer : public writer { |
17 | | - public: |
18 | | - /** |
19 | | - * Constructs a stream writer with an output stream |
20 | | - * and an optional prefix for comments. |
21 | | - * |
22 | | - * @param[in, out] output stream to write |
23 | | - * @param[in] comment_prefix string to stream before |
24 | | - * each comment line. Default is "". |
25 | | - */ |
26 | | - explicit stream_writer(std::ostream& output, |
27 | | - const std::string& comment_prefix = "") |
28 | | - : output_(output), comment_prefix_(comment_prefix) {} |
| 12 | + /** |
| 13 | + * <code>stream_writer</code> is an implementation |
| 14 | + * of <code>writer</code> that writes to a stream. |
| 15 | + */ |
| 16 | + class stream_writer : public writer { |
| 17 | + public: |
| 18 | + /** |
| 19 | + * Constructs a stream writer with an output stream |
| 20 | + * and an optional prefix for comments. |
| 21 | + * |
| 22 | + * @param[in, out] output stream to write |
| 23 | + * @param[in] comment_prefix string to stream before |
| 24 | + * each comment line. Default is "". |
| 25 | + */ |
| 26 | + stream_writer(std::ostream& output, |
| 27 | + const std::string& comment_prefix = ""): |
| 28 | + output_(output), comment_prefix_(comment_prefix) {} |
29 | 29 |
|
30 | | - /** |
31 | | - * Virtual destructor |
32 | | - */ |
33 | | - virtual ~stream_writer() {} |
| 30 | + /** |
| 31 | + * Virtual destructor |
| 32 | + */ |
| 33 | + virtual ~stream_writer() {} |
34 | 34 |
|
35 | | - /** |
36 | | - * Writes a set of names on a single line in csv format followed |
37 | | - * by a newline. |
38 | | - * |
39 | | - * Note: the names are not escaped. |
40 | | - * |
41 | | - * @param[in] names Names in a std::vector |
42 | | - */ |
43 | | - void operator()(const std::vector<std::string>& names) { |
44 | | - write_vector(names); |
45 | | - } |
| 35 | + /** |
| 36 | + * Writes a set of names on a single line in csv format followed |
| 37 | + * by a newline. |
| 38 | + * |
| 39 | + * Note: the names are not escaped. |
| 40 | + * |
| 41 | + * @param[in] names Names in a std::vector |
| 42 | + */ |
| 43 | + void operator()(const std::vector<std::string>& names) { |
| 44 | + write_vector(names); |
| 45 | + } |
46 | 46 |
|
47 | | - /** |
48 | | - * Writes a set of values in csv format followed by a newline. |
49 | | - * |
50 | | - * Note: the precision of the output is determined by the settings |
51 | | - * of the stream on construction. |
52 | | - * |
53 | | - * @param[in] state Values in a std::vector |
54 | | - */ |
55 | | - void operator()(const std::vector<double>& state) { write_vector(state); } |
| 47 | + /** |
| 48 | + * Writes a set of values in csv format followed by a newline. |
| 49 | + * |
| 50 | + * Note: the precision of the output is determined by the settings |
| 51 | + * of the stream on construction. |
| 52 | + * |
| 53 | + * @param[in] state Values in a std::vector |
| 54 | + */ |
| 55 | + void operator()(const std::vector<double>& state) { |
| 56 | + write_vector(state); |
| 57 | + } |
56 | 58 |
|
57 | | - /** |
58 | | - * Writes the comment_prefix to the stream followed by a newline. |
59 | | - */ |
60 | | - void operator()() { output_ << comment_prefix_ << std::endl; } |
| 59 | + /** |
| 60 | + * Writes the comment_prefix to the stream followed by a newline. |
| 61 | + */ |
| 62 | + void operator()() { |
| 63 | + output_ << comment_prefix_ << std::endl; |
| 64 | + } |
61 | 65 |
|
62 | | - /** |
63 | | - * Writes the comment_prefix then the message followed by a newline. |
64 | | - * |
65 | | - * @param[in] message A string |
66 | | - */ |
67 | | - void operator()(const std::string& message) { |
68 | | - output_ << comment_prefix_ << message << std::endl; |
69 | | - } |
| 66 | + /** |
| 67 | + * Writes the comment_prefix then the message followed by a newline. |
| 68 | + * |
| 69 | + * @param[in] message A string |
| 70 | + */ |
| 71 | + void operator()(const std::string& message) { |
| 72 | + output_ << comment_prefix_ << message << std::endl; |
| 73 | + } |
70 | 74 |
|
71 | | - private: |
72 | | - /** |
73 | | - * Output stream |
74 | | - */ |
75 | | - std::ostream& output_; |
| 75 | + private: |
| 76 | + /** |
| 77 | + * Output stream |
| 78 | + */ |
| 79 | + std::ostream& output_; |
76 | 80 |
|
77 | | - /** |
78 | | - * Comment prefix to use when printing comments: strings and blank lines |
79 | | - */ |
80 | | - std::string comment_prefix_; |
| 81 | + /** |
| 82 | + * Comment prefix to use when printing comments: strings and blank lines |
| 83 | + */ |
| 84 | + std::string comment_prefix_; |
81 | 85 |
|
82 | | - /** |
83 | | - * Writes a set of values in csv format followed by a newline. |
84 | | - * |
85 | | - * Note: the precision of the output is determined by the settings |
86 | | - * of the stream on construction. |
87 | | - * |
88 | | - * @param[in] v Values in a std::vector |
89 | | - */ |
90 | | - template <class T> |
91 | | - void write_vector(const std::vector<T>& v) { |
92 | | - if (v.empty()) |
93 | | - return; |
| 86 | + /** |
| 87 | + * Writes a set of values in csv format followed by a newline. |
| 88 | + * |
| 89 | + * Note: the precision of the output is determined by the settings |
| 90 | + * of the stream on construction. |
| 91 | + * |
| 92 | + * @param[in] v Values in a std::vector |
| 93 | + */ |
| 94 | + template <class T> |
| 95 | + void write_vector(const std::vector<T>& v) { |
| 96 | + if (v.empty()) return; |
94 | 97 |
|
95 | | - typename std::vector<T>::const_iterator last = v.end(); |
96 | | - --last; |
| 98 | + typename std::vector<T>::const_iterator last = v.end(); |
| 99 | + --last; |
97 | 100 |
|
98 | | - for (typename std::vector<T>::const_iterator it = v.begin(); it != last; |
99 | | - ++it) |
100 | | - output_ << *it << ","; |
101 | | - output_ << v.back() << std::endl; |
102 | | - } |
103 | | -}; |
| 101 | + for (typename std::vector<T>::const_iterator it = v.begin(); |
| 102 | + it != last; ++it) |
| 103 | + output_ << *it << ","; |
| 104 | + output_ << v.back() << std::endl; |
| 105 | + } |
| 106 | + }; |
104 | 107 |
|
105 | | -} // namespace callbacks |
106 | | -} // namespace stan |
| 108 | + } |
| 109 | +} |
107 | 110 | #endif |
0 commit comments