Pages: 1
  Print  
Author Topic: Help coding something...  (Read 851 times)
Offline (Male) RetroX
Posted on: July 02, 2010, 12:25:43 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Remember that mixed type class that I was working on a long time ago?

Here's my scenario:
- The value for the variable is stored in a void pointer with an integer to decide which type is currently stored.  When the variable is typecasted, the value is casted the the proper type using a function (returning an error if it can't be).
- To cast types, a 2-dimensional array of "cast functions" is provided.  Each type that is supported is given an ID, and cast_functions[from][to] will provide the proper function to cast a void pointer from one type to another.  Invalid IDs, etc. return errors.

Here's the problem:
- For operators, you don't know what the type is, and thus, you can't exactly add two types together.  If you're adding mixed to a different type, you can use that type, but mixed and mixed?  It can't be done.

The only solution that I can think of for this is to create an array of functions for each operation, and that would result in a massive size; is there anyone with other suggestions for this?
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #1 Posted on: July 02, 2010, 06:45:59 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2959

View Profile Email
Not while treating it as a plain variable. Allowing operator behavior of -two- types is the difficult part; if it was one or the other, you could use the template magic to make it work, and it would only error if an operator was called that was unsupported. But since, in the case of string and double, only some 8 operators are shared in common, that's problematic.

You could look at boost::variant better than I did. From what I can tell, they scraped something together with accessors. If I were you, I'd recommend overloading operator+(void) and operator-(void) to fetch either type explicitly (Has to be as good as or better than what boost did, but I didn't read very far in).
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #2 Posted on: July 04, 2010, 11:28:08 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
And after reading that, I did think of a decent idea.  I'll just order the types by increasing complexity (bool and int are at the bottom, double and string are at the top), and then cast to the most complex type, and perform the operation.

Types could also be equal in complexity; it doesn't really matter.  In that case, the second is casted to the first.

Example:
mixed (stored as int) + mixed (stored as double)
double is more complex than int, and the int is casted to a double, then the operation is performed.

mixed + int returns mixed, which can be casted to int (this avoids loss of data when types like string are stored).
mixed + mixed returns mixed.

...and that wouldn't work because I can't just randomly pull a type definition out of nowhere.  Damn.
« Last Edit: July 04, 2010, 11:53:08 AM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #3 Posted on: July 04, 2010, 03:39:45 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2959

View Profile Email
Yes, you can. It won't work because you can't assume that all types passed to a generic mixed() will have even operator+. If you could presume all available operators for both types, you'd be fine.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #4 Posted on: July 05, 2010, 02:30:07 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Yes, you can. It won't work because you can't assume that all types passed to a generic mixed() will have even operator+. If you could presume all available operators for both types, you'd be fine.
No, what I meant was how I could use a function to add the two doubles.  I would have two void*s, but that wouldn't do me much good.  I can't exactly cast them to double without using a template.

But I will.

And for operators and casts, it just shows an error if the function is NULL.  And when GCC 4.6 comes out, I'll change that to nullptr.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: 1
  Print