Sign in to follow this  
Toni Petrina

Urgent! operator (ostream) Not anymore

Recommended Posts

Toni Petrina    123
I am at public library but I need to know what code is exactly called when executing. I need function definition. ostream &operator<<(ostream &os, const float &fVal); And please hurry up. [Edited by - ffx on November 12, 2004 9:15:41 AM]

Share this post


Link to post
Share on other sites
Roboguy    794
if I'm understanding the question correctly, you want the code for that operator? that's implementation specific, and with Microsoft implementations, I believe it's copyrighted

Share this post


Link to post
Share on other sites
Fruny    1658
Since you demand a fast answer: GNU libstdc++, bits/ostream.tcc


template<typename _CharT, typename _Traits>
basic_ostream<_CharT, _Traits>&
basic_ostream<_CharT, _Traits>::
operator<<(double __n)
{
sentry __cerb(*this);
if (__cerb)
{
ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
try
{
_M_check_facet(this->_M_fnumput);
const __numput_type& __np = *this->_M_fnumput;
if (__np.put(*this, *this, this->fill(), __n).failed())
__err |= ios_base::badbit;
}
catch(...)
{ this->_M_setstate(ios_base::badbit); }
if (__err)
this->setstate(__err);
}
return *this;
}



Happy?

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by antareus
I dunno if his homework will let him put all those underscores on there, Fruny. :)


Not my problem (technically, it's a "no", since only the compiler and standard library people are allowed to use them so). His question rubbed me the wrong way. I just hope he's up to snuff with locales, because that's where the magic's happening (that const __numput_type& __np thingy).

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by Fruny
Since you demand a fast answer: GNU libstdc++, bits/ostream.tcc


i knew that was going to come sooner or later [lol]

Share this post


Link to post
Share on other sites
Toni Petrina    123
Quote:
Original post by Fruny
Since you demand a fast answer: GNU libstdc++, bits/ostream.tcc

*** Source Snippet Removed ***

Happy?


Absolutely, only too late. Quite interesting code.

But thanks anyway.

Share this post


Link to post
Share on other sites
Toni Petrina    123
Quote:
Original post by antareus
I dunno if his homework will let him put all those underscores on there, Fruny. :)


Not my homework. I was trying to create system that would invoke operator<< for ostream class even when sent through pointer to some class. Got me? It is for my serialization article that I'm writing and I'm trying to make it as standard compatible as I can.

Idea is stream indpendant streaming.

oarchive ar;
//..ignore initialization
ar << fVal;

This can either call ostream operator<< or ostream.write depending on archive type (text or binary).

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by ffx
Quote:
Original post by antareus
I dunno if his homework will let him put all those underscores on there, Fruny. :)


Not my homework. I was trying to create system that would invoke operator<< for ostream class even when sent through pointer to some class. Got me? It is for my serialization article that I'm writing and I'm trying to make it as standard compatible as I can.


Let me get this straight: you're writing an article when you can't be arsed to look up standard header sources in [google], and assume everybody else is your research monkey? I've seen enough badly written tutorials and articles made by newbies who post tutorials explaining how to create badly written code (the ones that horrify me the most are the ones that teach you how to use C to create security holes), and you need it now (and it's now "too late")? I'm sorry, but this screams of homework.

Quote:
Idea is stream indpendant streaming.

oarchive ar;
//..ignore initialization
ar << fVal;

This can either call ostream operator<< or ostream.write depending on archive type (text or binary).


So, you're effectively creating a wrapper like so:

class oarchive
{
std::ostream & os;
bool binary;
public:
//...

template < typename T > oarchive & operator<<( const T & value )
{
if (binary)
{
os.write(reinterpret_cast<const char *>(&value),sizeof(T));
}
else
{
os << value;
}
return *this;
}
};


?

Share this post


Link to post
Share on other sites
Toni Petrina    123
Quote:
Original post by MaulingMonkeyLet me get this straight: you're writing an article when you can't be arsed to look up standard header sources in [google], and assume everybody else is your research monkey? I've seen enough badly written tutorials and articles made by newbies who post tutorials explaining how to create badly written code (the ones that horrify me the most are the ones that teach you how to use C to create security holes), and you need it now (and it's now "too late")? I'm sorry, but this screams of homework.


Well if you want to know I've made serialization system for my last engine. It was similar to serialization system found in utpub432 (I got my inspiration from there). Then I decided to write article about serialization by showing my work but I didn't just copy and paste my source code. Instead, I browsed through tons of articles on web (e.g. codeproject) in order to write good system, not just some hack. I sent article to gamedev but I stopped it from publishing when I realized how many errors there are in there. If you want, I could send it to you.

I currently don't have a computer. I've moved to another town but I'll get one (computer, not town) in a matter of weeks. However, spending free time designing and by using public internet acess in my library I was designing my serialization system.

I just wanted to see what does operator << do for float data type, just that. Why is it too late? Its because I've thought of another solution in mean time.


Quote:

[qoute]
oarchive ar;
//..ignore initialization
ar << fVal;

This can either call ostream operator<< or ostream.write depending on archive type (text or binary).


So, you're effectively creating a wrapper like so:

class oarchive
{
std::ostream & os;
bool binary;
public:
//...

template < typename T > oarchive & operator<<( const T & value )
{
if (binary)
{
os.write(reinterpret_cast<const char *>(&value),sizeof(T));
}
else
{
os << value;
}
return *this;
}
};


?[/quote]

Not like that. Consider code :
ostream os(/* here goes archive type, binary, text or xml or any other*/);
os << fVal;
My problem is that if user wanted to save to standard ofstream I need to call istream operator<< but if he wanted to save in binary I need to call member function write. You see my problem? Anyway, I've come to solution.

Don't worry, I don't plan to release bad article. I've seen to many of those.

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by ffx
Not like that. Consider code :
ostream os(/* here goes archive type, binary, text or xml or any other*/);
os << fVal;
My problem is that if user wanted to save to standard ofstream I need to call istream operator<< but if he wanted to save in binary I need to call member function write. You see my problem? Anyway, I've come to solution.

Don't worry, I don't plan to release bad article. I've seen to many of those.


Well, changing the behavior of std::ostream isn't a good idea IMHO - if you meant oarchive as in your original example, then my code example should work as intended assuming the reference refers to the ostream the user wants to work with - although my code example depends on the boolean "binary" (which would be set in the oarchive ctor as the flags were read). If you want to go with an ostream inheriting class (so you can pass an oarcive to an "ostream &") things might be slightly trickier since then you WILL have to work with the compiler-specific standard headers. A more complete example, using a class-local fstream:

class oarchive
{
std::ofstream fs;
bool binary;
public:
oarchive( const char * filename , std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc ) : fs(filename,mode)
{
binary = ( mode & std::ios_base::binary );
}

void open( const char * filename , std::ios_base::openmode = std::ios_base::out | std::ios_base::trunc )
{
binary = ( mode & std::ios_base::binary );
fs.open( filename , openmode );
}

template < typename T > oarchive & operator<<( const T & value )
{
if (binary)
{
os.write(reinterpret_cast<const char *>(&value),sizeof(T));
}
else
{
os << value;
}
return *this;
}
};

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by ffx
Why is it too late? Its because I've thought of another solution in mean time.

Quick, hurry before I come up with a better solution... ?!?!?!

That's new!

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by iMalc
Quote:
Original post by ffx
Why is it too late? Its because I've thought of another solution in mean time.

Quick, hurry before I come up with a better solution... ?!?!?!

That's new!


My thought, precisely.

ffs, there is a book on IOStreams you would be well advised to buy.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this