|
Boost Users : |
From: Christopher Lux (christopherlux_at_[hidden])
Date: 2007-05-28 19:23:27
Hi,
i am trying to get access to specialized stream attributes in my own filters. I created a specialized stringstream class which holds some kind of log level, which i want to use in my input filters. Therefore i wrote a input device which handles my specialized class. With debug outputs i see that this device is used to read my stream. Now i want to create a filter which is able to access the log level attribute from the specialized stream. I tried to specialize the template function for reading from the source for my stream device and for the specialized stream but none of these functions are getting used. Is there a way to be able to access either the device or the source stream in the filters?
The example code below gives a short example of the mentioned problem.
#include <iostream>
#include <sstream>
#include <string>
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/char_traits.hpp>
#include <boost/iostreams/stream.hpp>
// specialized stream class
class console_out_stream : public std::stringstream
{
public:
console_out_stream() : std::stringstream() {}
virtual ~console_out_stream() {}
int get_log_level() const { return (_log_level); }
void set_log_level(int l) { _log_level = l; }
private:
int _log_level;
};
// ability to set log level using the << operator on the new stream class
class log_level
{
public:
log_level(int l) : _log_level(l) {}
int _log_level;
};
console_out_stream& operator << (console_out_stream& con_str, const log_level& log)
{
con_str.set_log_level(log._log_level);
return (con_str);
}
// source device
class my_console_source : public boost::iostreams::source {
public:
my_console_source(console_out_stream& con)
: _console(con) {}
std::streamsize read(char* s, std::streamsize n)
{
std::cout << "ran through the source" << std::endl;
return (boost::iostreams::read(_console, s, n));
}
const console_out_stream& get_console() const {return _console; }
protected:
console_out_stream& _console;
};
// input filter
// just filters out the character s
class test_in_filter_m : public boost::iostreams::multichar_input_filter {
public:
explicit test_in_filter_m() {}
template<typename Source>
std::streamsize read(Source& src, char* s, std::streamsize n)
{
for (std::streamsize z = 0; z < n; ++z) {
int c;
while (true) {
if ((c = boost::iostreams::get(src)) == EOF)
return z != 0 ? z : -1;
else if (c == boost::iostreams::WOULD_BLOCK)
return z;
if (c != 's')
break;
}
s[z] = c;
}
return n;
}
template<>
std::streamsize read<my_console_source>(my_console_source& src, char* s, std::streamsize n)
{
std::cout << "ran through the source filter: my_console_source" << std::endl;
return (n);
}
template<>
std::streamsize read<console_out_stream>(console_out_stream& src, char* s, std::streamsize n)
{
std::cout << "ran through the source filter: console_out_stream" << std::endl;
return (n);
}
};
int main(int argc, char **argv)
{
console_out_stream console;
console << log_level(20) << "sick, sad world!";
console << log_level(5) << "from mtv daria" << std::endl;
boost::iostreams::stream<my_console_source> in_stream_buf(console);
boost::iostreams::filtering_istream out_stream;
out_stream.push(test_in_filter_m());
out_stream.push(in_stream_buf);
std::string line;
while (std::getline(out_stream, line)) {
std::cout << line << std::endl;
}
return (0);
}
-chris
-- Christopher Lux | | Bauhaus University Weimar | Faculty of Media - Virtual Reality Systems Group
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net