I just came across some situation where it was handy to have a std::ostream operator<< for the var class that just adds the string representation of the var to the stream. I implemented it like that:
std::ostream& operator<< (std::ostream& os, const var& v)
{
return os << v.toString();
}
Would it make sense to the juce team to add this to the library?
Alternatively a version with additional support for arrays
std::ostream& operator<< (std::ostream& os, const var& v)
{
if (v.isArray())
{
os << "[ ";
const int arraySize = v.size();
for (int arrayField = 0; arrayField < arraySize; ++arrayField)
os << v[arrayField] << " ";
return os << "]";
}
return os << v.toString();
}
daniel
July 16, 2019, 4:45pm
3
The juce::OutputStream doesn’t do it for you?
Also there is the operator for String: std::basic_ostream<char, traits>& operator<<(std::basic_ostream<char, traits>& stream, const String& stringToWrite) ,
which should allow you to do (haven’t tried though):
var v;
std::cout << v.toString() << std::endl;
Yes I know both and this is how I do it currently, as you can see in my code snippet I already make use of the String operator you mentioned.
However, my intention was primarily to write clean looking print statements in command in a line application context, e.g.
var a ({1, 2, 3, 4});
var b ("foo");
std::cout << "a contains: " << a << ", b contains: " << b << std::endl;
which would result in an output like a contains: [ 1 2 3 4 ], b contains: foo.
I just thinks this syntax look nice & clean, there is nothing that couldn’t be done without it