ENIGMA Forums

Sharing is caring => Tips, Tutorials, Examples => Topic started by: RetroX on February 24, 2010, 01:47:59 pm

Title: lol streams
Post by: RetroX on February 24, 2010, 01:47:59 pm
Something useful for ENIGMA:

Code: [Select]
std::ostream &operator <<(std::ostream &outstream,__variant &variable)
 {
 if (variable.type)
  {
  return outstream << variable.doubleval;
  }
 return outstream << variable.stringval;
 }
std::istream &operator >>(std::istream &instream,__variant &variable)
 {
 char c,c2;
 c=instream.get();
 if (c>=48 && c<=57)
  {
  instream.putback(c);
  return instream >> variable.doubleval;
  }
 else if (c==46)
  {
  c2=instream.get();
  if (c2>=48 && c2<=57)
   {
   instream.putback(c2);
   instream.putback(c);
   return instream >> variable.doubleval;
   }
  }
 instream.putback(c);
 return instream >> variable.stringval;
 }

You probably should add a typecast operator for var to cast to __variant.

This is from r3's source, though, so, IDK what you've changed.

Also, because I wouldn't know, would this also work for iostream, fstream, and stringstream because they are inherited from istream and ostream, or would it not because this is defined after that?
Title: Re: lol streams
Post by: Josh @ Dreamland on February 24, 2010, 07:38:55 pm
Does... that work? It should error that your function should be a member of std::ostream rather than declared in the global scope...
Title: Re: lol streams
Post by: Rusky on February 25, 2010, 08:15:42 am
No, you can declare new versions of << without it being a member. That was the whole point of switching to the brain-dead system.
Title: Re: lol streams
Post by: Josh @ Dreamland on February 25, 2010, 08:23:50 am
Duly noted.
Title: Re: lol streams
Post by: RetroX on February 25, 2010, 07:59:47 pm
Duly noted.
Yeah, I figured that out recently, and decided to mess around with it.

Code: [Select]
typedef char int8;
typedef unsigned char uint8;
std::ostream &operator <<(std::ostream &outstream,int8 value)
 {
 return outstream << (int)value;
 }
std::ostream &operator <<(std::ostream &outstream,uint8 value)
 {
 return outstream << (unsigned int)value;
 }
Title: Re: lol streams
Post by: RetroX on March 04, 2010, 06:38:54 pm
Additionally, here's something else that might be useful:
Code: [Select]
// Operators for strings (addition)
string &operator +(string &str1,string &str2)
 {
 string temp=str1;
 return temp+=str2;
 }
string &operator +(string &str1,cstring str2)
 {
 string temp=str1;
 return temp+=str2;
 }

// Operators for strings (comparison)
bool operator ==(string &str1,string &str2)
 {
 string temp=str1;
 return (temp.compare(str2));
 }
bool operator ==(string &str1,cstring str2)
 {
 string temp=str1;
 return (temp.compare(str2));
 }

// Operators for strings (comparison)
bool operator !=(string &str1,string &str2)
 {
 return !(str1==str2);
 }
bool operator !=(string &str1,cstring str2)
 {
 return !(str1==str2);
 }

Simple operators for strings.
Title: Re: lol streams
Post by: Josh @ Dreamland on March 04, 2010, 06:44:39 pm
String already defines those...
And then some...
Title: Re: lol streams
Post by: RetroX on March 04, 2010, 06:52:23 pm
I hate it when cplusplus.com doesn't provide an accurate reference.

I learned something new, today, I guess.
Title: Re: lol streams
Post by: Rusky on March 04, 2010, 07:18:52 pm
Those functions are on cplusplus.com:
http://cplusplus.com/reference/string/operator+/ (http://cplusplus.com/reference/string/operator+/)
http://cplusplus.com/reference/string/operators/ (http://cplusplus.com/reference/string/operators/)
Title: Re: lol streams
Post by: RetroX on March 04, 2010, 07:23:09 pm
Those functions are on cplusplus.com:
http://cplusplus.com/reference/string/operator+/ (http://cplusplus.com/reference/string/operator+/)
http://cplusplus.com/reference/string/operators/ (http://cplusplus.com/reference/string/operators/)
I was looking at <cstring>; for whatever reason, it didn't occur to me to look at <string>.
Title: Re: lol streams
Post by: Micah on March 04, 2010, 09:06:41 pm
Could you implement those operators for char *s?
Title: Re: lol streams
Post by: RetroX on March 04, 2010, 09:19:37 pm
Could you implement those operators for char *s?
string operator+(string str,char *str2)
 {
 return str+const_cast<const char*>(str2);
 }

?
Title: Re: lol streams
Post by: luiscubal on March 05, 2010, 02:48:17 pm
Not for two char*s though. C++ doesn't allow that.
Title: Re: lol streams
Post by: RetroX on March 05, 2010, 09:30:16 pm
Erm, why would you want to do that?  That's kind of the point of string - to add things like that.
Title: Re: lol streams
Post by: Josh @ Dreamland on March 05, 2010, 09:33:35 pm
He's thinking about 'situations where you need to quote "' + "that's" + '," or other situations requiring both "' + " and '".
Title: Re: lol streams
Post by: RetroX on March 05, 2010, 09:37:59 pm
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Title: Re: lol streams
Post by: Josh @ Dreamland on March 05, 2010, 09:54:21 pm
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is.

"Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them.

Also, you don't need to use const_cast to get it represented as a const char. :P
Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
Title: Re: lol streams
Post by: RetroX on March 05, 2010, 10:04:53 pm
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is.

"Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them.

Also, you don't need to use const_cast to get it represented as a const char. :P
Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
Whenever I have tried to do it without const_cast, G++ bugs me about a depreciated conversion (that works).  I always use -Wall and purge all warnings and errors, and that is one that I happen to get into the habit of doing.
Title: Re: lol streams
Post by: score_under on March 06, 2010, 08:59:16 am
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Title: Re: lol streams
Post by: RetroX on March 06, 2010, 03:25:17 pm
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Exactly.  It's pointless.
Title: Re: lol streams
Post by: score_under on March 06, 2010, 08:30:41 pm
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Exactly.  It's pointless.
Tell me...
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
printf("%c",z[2]);
...What happens when you run that? If you say "prints l" I'll shoot your face off. If you say "access violation because you added 2 random pointers", then I'll forgive you... for now.
Title: Re: lol streams
Post by: Micah on March 06, 2010, 09:45:48 pm
Of course it's adding two random pointers. He was explaining what I meant when I asked if you could overload operator+ for two char *s.
Title: Re: lol streams
Post by: score_under on March 06, 2010, 10:15:45 pm
I see, though overloading the + for a char* is really pointless IMO.
Title: Re: lol streams
Post by: Josh @ Dreamland on March 06, 2010, 10:18:28 pm
Yeah, compiler presently concatenates "" + "" automatically. For "" + anything else, I want it to cast first "" to variant.
Title: Re: lol streams
Post by: Rusky on March 06, 2010, 11:19:35 pm
It concatenates "" "" automatically. someCharPtr + "" is what might be useful to overload, to avoid std::string constructor noise. However, I agree that it's not very useful. Allowing operator overloading on arbitrary types would make the language completely unpredictable; it'd be worse than monkeypatching in Ruby.