473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

passing around iostreams

Hi,

I'd like advise on passing ownership of an iostream.

The idea is that my factory class/function should open a file,
read enough to detect file type (eg which soundfile format),
then create a Reader object of the appropriate type for the file.

Of course I can call close(), then pass the filename to
the reader constructor and reopen the file there.
But I'd prefer to let the Reader class have an istream (no "f")
- then, if the file is small, the factory could perhaps read it
into a string and pass a stringstream instead of the file handle
(just an example, didn't look at this yet).

So is it safe to let the factory create an ifstream by new,
pass the pointer to reader and dereference it so the Reader
can use an istream& (reference)?
Or would it be better to pass just the streambuf?
- or should I go for reopening (which pretty much rules out
having an istream& in the Reader

Thanks,

Sören



Jul 19 '05 #1
3 4132

"Sören" <sq********@kryphal.nu> wrote in message
news:bf**********@oden.abc.se...
Hi,

I'd like advise on passing ownership of an iostream.

The idea is that my factory class/function should open a file,
read enough to detect file type (eg which soundfile format),
then create a Reader object of the appropriate type for the file.

Of course I can call close(), then pass the filename to
the reader constructor and reopen the file there.
But I'd prefer to let the Reader class have an istream (no "f")
- then, if the file is small, the factory could perhaps read it
into a string and pass a stringstream instead of the file handle
(just an example, didn't look at this yet).

So is it safe to let the factory create an ifstream by new,
pass the pointer to reader and dereference it so the Reader
can use an istream& (reference)?
Or would it be better to pass just the streambuf?
- or should I go for reopening (which pretty much rules out
having an istream& in the Reader

Thanks,

Sören


A stream is a resource your class would have to manage. If you create one
with new you will have memory to manage also. I can't see why you would want
to do that. The filename can just be a std::string. No muss no fuss.

Is your concern about reopening based on performance?

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #2
Sören wrote:
Hi,

I'd like advise on passing ownership of an iostream.

The idea is that my factory class/function should open a file,
read enough to detect file type (eg which soundfile format),
then create a Reader object of the appropriate type for the file.
I don't get it. Does the factory then return the Reader object?
In that case, the Reader would have to take over ownership of the
istream. You can express this by passing a pointer-to-istream,
allocated by new, to the Reader. You would delete the istream in the
Reader, perhaps in the destructor. The factory can forget about it.

But you still have to copy the Reader at least once in order to return
it by value, and you need to make sure the 'right' copy owns the
stream. I think you would need an auto_ptr <istream> as a member
of Reader, initialized in the constructor (maybe even by the copy
constructor from an auto_ptr in the factory, just to show off).

Alternatively, and more likely, does the factory return the object
created by the Reader? Then unless I'm missing something, you can use
automatic variables. If the ifstream's scope encloses that of the
Reader, you can simply pass by reference-to-istream, and leave ownership
of the ifstream with the factory.
Of course I can call close(), then pass the filename to
the reader constructor and reopen the file there.
But I'd prefer to let the Reader class have an istream (no "f")
- then, if the file is small, the factory could perhaps read it
into a string and pass a stringstream instead of the file handle
(just an example, didn't look at this yet).

So is it safe to let the factory create an ifstream by new,
pass the pointer to reader and dereference it so the Reader
can use an istream& (reference)?
Or would it be better to pass just the streambuf?
- or should I go for reopening (which pretty much rules out
having an istream& in the Reader

Thanks,

Sören


Regards,
Buster

Jul 19 '05 #3
Thanks for your comments!

Cy Edmunds wrote:

A stream is a resource your class would have to manage. If you create one
with new you will have memory to manage also. I can't see why you would want
to do that. The filename can just be a std::string. No muss no fuss.

Is your concern about reopening based on performance?


No, generality. I'll rephrase below.
My Reader (or Decoder) class should (i think) have an istream member for the bytes,
not an ifstream.
Its task is to decode these bytes into meaningful data and deliver on request.
The *typical* case is that the bytes come from a file, but they might also
be stored in an array - if the file was so small it could be loaded into memory.

General question: How to handle this?
(- I have now a sketch of solution as follows)

More specific question: Is the following a good idea?

As mentioned in my first post, my factory method opens a file, decodes file type,
then creates a Reader and returns a pointer-to-Reader (well, a smart ptr).
Optionally it may load the entire file, but usually it would pass file ownership.

Now, to pass ifstream (file) ownership from factory to product I think I could pass
the factory ifstream streambuf into the Reader (product) class constructor,
and construct the Reader's istream member around that.
I understand that the Reader-istream then does take ownership,
and will close and destroy the file when Reader is destroyed?
(or free memory if the streambuf is a stringstreambuf, etc)

But the istream in the factory will of course also be destroyed when it goes out
of scope. To prevent that I guess I could switch the streambuf of the factory-istream.
But is this a good idea?
Thanks for your patience.

Sören

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Dave O'Hearn | last post by:
I want to open a file for both reading and writing, but when I do "ios::in|ios::out" with an fstream, it creates the file if it doesn't already exist. I don't like that last part. C's fopen has the...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: | last post by:
There are two methods of snagging a 'line' from a stream (with an attached streambuf that reads/writes from a socket). One is the std function "getline" and the other is the "getline" method of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
5
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
6
by: ganesh.kundapur | last post by:
Hi all, I need to know the complexity of implementing iostreams natively such as dependency on underlying OS. Regards, Ganesh
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.