48 #ifndef OPENMESH_MOSTREAM_HH
49 #define OPENMESH_MOSTREAM_HH
54 #include <OpenMesh/Core/System/config.h>
56 #if defined( OM_CC_GCC ) && OM_CC_VERSION < 30000
57 # include <streambuf.h>
70 #ifndef DOXY_IGNORE_THIS
76 class basic_multiplex_target
79 virtual ~basic_multiplex_target() {}
80 virtual void operator<<(
const std::string& _s) = 0;
85 class multiplex_target :
public basic_multiplex_target
88 multiplex_target(T& _t) : target_(_t) {}
89 virtual void operator<<(
const std::string& _s) { target_ << _s; }
99 #if defined( OM_CC_GCC ) && OM_CC_VERSION < 30000
100 # define STREAMBUF streambuf
101 # define INT_TYPE int
104 # define STREAMBUF std::basic_streambuf<char>
107 class multiplex_streambuf :
public STREAMBUF
111 typedef STREAMBUF base_type;
112 #if defined( OM_CC_GCC ) && OM_CC_VERSION < 30000
113 typedef int int_type;
116 static int_type eof() {
return -1; }
117 static char to_char_type(int_type c) {
return char(c); }
120 typedef base_type::int_type int_type;
121 typedef base_type::traits_type traits_type;
125 multiplex_streambuf() : enabled_(true) { buffer_.reserve(100); }
128 ~multiplex_streambuf()
130 tmap_iter t_it(target_map_.begin()), t_end(target_map_.end());
131 for (; t_it!=t_end; ++t_it)
137 bool is_enabled()
const {
return enabled_; }
138 void enable() { enabled_ =
true; }
139 void disable() { enabled_ =
false; }
143 template <
class T>
bool connect(T& _target)
145 void* key = (
void*) &_target;
147 if (target_map_.find(key) != target_map_.end())
150 target_type* mtarget =
new multiplex_target<T>(_target);
151 target_map_[key] = mtarget;
159 template <
class T>
bool disconnect(T& _target)
161 void* key = (
void*) &_target;
162 tmap_iter t_it = target_map_.find(key);
164 if (t_it != target_map_.end())
166 __disconnect(t_it->second);
167 target_map_.erase(t_it);
180 if (!buffer_.empty())
182 if (enabled_) multiplex();
183 #if defined( OM_CC_GCC ) && OM_CC_VERSION < 30000
189 return base_type::sync();
196 int_type overflow(int_type _c = multiplex_streambuf::traits_type::eof())
198 char c = traits_type::to_char_type(_c);
199 buffer_.push_back(c);
200 if (c ==
'\n') sync();
207 typedef basic_multiplex_target target_type;
208 typedef std::vector<target_type*> target_list;
209 typedef target_list::iterator tlist_iter;
210 typedef std::map<void*, target_type*> target_map;
211 typedef target_map::iterator tmap_iter;
215 void __connect(target_type* _target) { targets_.push_back(_target); }
219 void __disconnect(target_type* _target) {
220 targets_.erase(std::find(targets_.begin(), targets_.end(), _target));
227 tlist_iter t_it(targets_.begin()), t_end(targets_.end());
228 for (; t_it!=t_end; ++t_it)
235 target_list targets_;
236 target_map target_map_;
256 class mostream :
public std::ostream
261 explicit mostream() : std::ostream(NULL) { init(&streambuffer_); }
265 template <
class T>
bool connect(T& _target)
267 return streambuffer_.connect(_target);
272 template <
class T>
bool disconnect(T& _target)
274 return streambuffer_.disconnect(_target);
279 bool is_enabled()
const {
return streambuffer_.is_enabled(); }
282 void enable() { streambuffer_.enable(); }
285 void disable() { streambuffer_.disable(); }
289 multiplex_streambuf streambuffer_;
297 #endif // OPENMESH_MOSTREAM_HH defined
std::ostream & operator<<(std::ostream &_os, const BaseHandle &_hnd)
Write handle _hnd to stream _os.
Definition: Handles.hh:104