stream / bitshift overloads
I'm having trouble doing some overloads.
Be prepared for a lot of code:
Code:
template <ostream>
ostream& operator <<(ostream & os, const any & operand)
//postcondition: operand is written to output stream os
{
if (operand.type() == typeid(int))
return os << any_cast<int>(operand);
else if (operand.type() == typeid(char))
return os << any_cast<char>(operand);
else if (operand.type() == typeid(float))
return os << any_cast<float>(operand);
else if (operand.type() == typeid(double))
return os << any_cast<double>(operand);
else if (operand.type() == typeid(bool))
return os << any_cast<bool>(operand);
else if (operand.type() == typeid(char*))
return os << any_cast<char*>(operand);
#ifdef _STRING_ // make sure that you won't get an error if not included
else if (operand.type() == typeid(std::string))
return os << any_cast<std::string>(operand);
#endif
return os; // if it wasn't one of the other types, it ignores it.
}
Code:
template <istream>
istream& operator >>(istream & is, any & operand)
//precondition: input stream is open for reading
//postcondition: the next string from input stream is has been read
// and stored in operand
{
char temp[82]; // not longer than screen length
char ch;
int i = 0;
while (i <= 80 && is.get(ch) && ch != '\n')
temp[i++] = ch;
temp[i] = '\0';
//cout << any_cast<char*>(operand) << endl;
if (isint(temp)) // if it is an integer, convert it to one
operand = atoi(temp);
else if (isfloat(temp)) // if it is a float or double, convert it to one
operand = atof(temp);
else // otherwise, just store a string
{
char * newtemp = new char[strlen(temp)];
strcpy(newtemp,temp);
operand = newtemp;
}
// operand = strdup(temp); // allocate memory for this so that it isn't lost
//free(static_cast<any::holder<char *> *>(operand.content)->held);
return is;
}
Code:
template <any, typename UnknownType>
any operator <<(const any & lhs, const UnknownType & rhs)
{
any result;
if (lhs.type() == typeid(UnknownType)) // check that types are same.
// don't use float or double!
result = any_cast<UnknownType>(lhs) << rhs;
return result; // returns a copy of result
}
Code:
template <typename UnknownType>
UnknownType operator <<(const UnknownType & lhs, const any & rhs)
{
UnknownType result;
if (rhs.type() == typeid(UnknownType)) // check that types are same.
// don't use float or double!
result = lhs << any_cast<UnknownType>(rhs);
return result; // returns a copy of result
}
Code:
template <any, typename UnknownType>
any operator >>(const any & lhs, const UnknownType & rhs)
{
any result;
if (lhs.type() == typeid(UnknownType)) // check that types are same.
// don't use float or double!
result = any_cast<UnknownType>(lhs) >> rhs;
return result; // returns a copy of result
}
Code:
template <typename UnknownType>
UnknownType operator >>(const UnknownType & lhs, const any & rhs)
{
UnknownType result;
if (rhs.type() == typeid(UnknownType)) // check that types are same.
// don't use float or double!
result = lhs >> any_cast<UnknownType>(rhs);
return result; // returns a copy of result
}
Its having trouble figuring out which overloads to do.
I believe this is because it tries to do the one with UnknownType as parameter 1 instead of the ostream/istream...
What am I doing wrong?