[Iostreams] Safely returning filtering_istream from function
Hi, I would like to return filtering_istream via std::unique_ptrstd::iostream so that client code can just use iostream interface without knowing what is actual stream type. Inside the factory function filtering_istream is prepared with possible filters attached. Problem is filters are attached by reference. My question is: how to elegantly attach filters to a filtering_istream so that they live as long as filtering_istream itself? How to attach a filter via shared_ptr for example? Regards, Szymon Gatner
On 06/05/13 07:37, Szymon Gatner wrote:
Hi,
I would like to return filtering_istream via std::unique_ptrstd::iostream so that client code can just use iostream interface without knowing what is actual stream type.
Inside the factory function filtering_istream is prepared with possible filters attached. Problem is filters are attached by reference. My question is: how to elegantly attach filters to a filtering_istream so that they live as long as filtering_istream itself? How to attach a filter via shared_ptr for example?
Regards, Szymon Gatner
Hi Szymon, Could you not create a derived version of filtering_istream where the 1st base class contained all the filters and the 2nd base class was a filtering_istream. Then, I'd guess you could just pass references to filters in 1st base class to the 2nd base class, and they would live *almost* as long as the 2nd base class. I say almost because the 1st base class would be destroyed 1st. Would that be a problem? Hmm. Maybe the 1st base could be just a wrapped pointer which would be deleted in the derived class? -regards, Larry
2013/6/5 Larry Evans
Hi Szymon,
Could you not create a derived version of filtering_istream where the 1st base class contained all the filters and the 2nd base class was a filtering_istream. Then, I'd guess you could just pass references to filters in 1st base class to the 2nd base class, and they would live *almost* as long as the 2nd base class. I say almost because the 1st base class would be destroyed 1st. Would that be a problem? Hmm. Maybe the 1st base could be just a wrapped pointer which would be deleted in the derived class?
-regards, Larry
I actually thought about the same thing, possibly using boost base-from-member utility. Still, this feels like working against Iostreams not with it :( Thanks, SImon
On 06/05/13 07:37, Szymon Gatner wrote:
Hi,
I would like to return filtering_istream via std::unique_ptrstd::iostream so that client code can just use iostream interface without knowing what is actual stream type.
Inside the factory function filtering_istream is prepared with possible filters attached. Problem is filters are attached by reference.
Do you mean that the filter is passed to the filtering_istream CTOR with a ref_wrapper, as described in item 1 of: A filtering_stream may be constructed from an instance of a Filter or Device type T which is not CopyConstructible in one of two ways: * by using a reference wrapper, or * if T is a standard stream or stream buffer type, by using the constructor taking a non-const reference. from: http://www.boost.org/doc/libs/1_53_0/libs/iostreams/doc/classes/filtering_st... ? [snip] -regards, Larry
participants (2)
-
Larry Evans
-
Szymon Gatner