Implement additional operators for the Value and String classes.

Refs #2710
This commit is contained in:
Gunnar Beutner 2013-11-07 13:41:24 +01:00 committed by Gunnar Beutner
parent e93d3f4c61
commit 038be974e4
4 changed files with 109 additions and 0 deletions

View File

@ -18,6 +18,7 @@
******************************************************************************/ ******************************************************************************/
#include "base/qstring.h" #include "base/qstring.h"
#include "base/value.h"
#include <boost/algorithm/string/trim.hpp> #include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/join.hpp> #include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/compare.hpp> #include <boost/algorithm/string/compare.hpp>
@ -86,6 +87,12 @@ String& String::operator+=(const char *rhs)
return *this; return *this;
} }
String& String::operator+=(const Value& rhs)
{
m_Data += static_cast<String>(rhs);
return *this;
}
String& String::operator+=(char rhs) String& String::operator+=(char rhs)
{ {
m_Data += rhs; m_Data += rhs;

View File

@ -28,6 +28,8 @@
namespace icinga { namespace icinga {
class Value;
/** /**
* String class. * String class.
* *
@ -64,6 +66,7 @@ public:
String& operator+=(const String& rhs); String& operator+=(const String& rhs);
String& operator+=(const char *rhs); String& operator+=(const char *rhs);
String& operator+=(const Value& rhs);
String& operator+=(char rhs); String& operator+=(char rhs);
bool IsEmpty(void) const; bool IsEmpty(void) const;

View File

@ -241,6 +241,77 @@ ValueType Value::GetType(void) const
return static_cast<ValueType>(m_Value.which()); return static_cast<ValueType>(m_Value.which());
} }
bool Value::operator==(int rhs)
{
if (!IsScalar())
return false;
return static_cast<double>(*this) == rhs;
}
bool Value::operator!=(int rhs)
{
return !(*this == rhs);
}
bool Value::operator==(double rhs)
{
if (!IsScalar())
return false;
return static_cast<double>(*this) == rhs;
}
bool Value::operator!=(double rhs)
{
return !(*this == rhs);
}
bool Value::operator==(const char *rhs)
{
return static_cast<String>(*this) == rhs;
}
bool Value::operator!=(const char *rhs)
{
return !(*this == rhs);
}
bool Value::operator==(const String& rhs)
{
return static_cast<String>(*this) == rhs;
}
bool Value::operator!=(const String& rhs)
{
return !(*this == rhs);
}
bool Value::operator==(const Value& rhs)
{
if (IsEmpty() != rhs.IsEmpty())
return false;
if (IsEmpty())
return true;
if (IsObject() != rhs.IsObject())
return false;
if (IsObject())
return static_cast<Object::Ptr>(*this) == static_cast<Object::Ptr>(rhs);
if (GetType() == ValueNumber || rhs.GetType() == ValueNumber)
return static_cast<double>(*this) == static_cast<double>(rhs);
else
return static_cast<String>(*this) == static_cast<String>(rhs);
}
bool Value::operator!=(const Value& rhs)
{
return !(*this == rhs);
}
Value icinga::operator+(const Value& lhs, const char *rhs) Value icinga::operator+(const Value& lhs, const char *rhs)
{ {
return static_cast<String>(lhs) + rhs; return static_cast<String>(lhs) + rhs;
@ -251,6 +322,16 @@ Value icinga::operator+(const char *lhs, const Value& rhs)
return lhs + static_cast<String>(rhs); return lhs + static_cast<String>(rhs);
} }
Value icinga::operator+(const Value& lhs, const String& rhs)
{
return static_cast<String>(lhs) + rhs;
}
Value icinga::operator+(const String& lhs, const Value& rhs)
{
return lhs + static_cast<String>(rhs);
}
std::ostream& icinga::operator<<(std::ostream& stream, const Value& value) std::ostream& icinga::operator<<(std::ostream& stream, const Value& value)
{ {
stream << static_cast<String>(value); stream << static_cast<String>(value);

View File

@ -73,6 +73,21 @@ public:
operator double(void) const; operator double(void) const;
operator String(void) const; operator String(void) const;
bool operator==(int rhs);
bool operator!=(int rhs);
bool operator==(double rhs);
bool operator!=(double rhs);
bool operator==(const char *rhs);
bool operator!=(const char *rhs);
bool operator==(const String& rhs);
bool operator!=(const String& rhs);
bool operator==(const Value& rhs);
bool operator!=(const Value& rhs);
template<typename T> template<typename T>
operator shared_ptr<T>(void) const operator shared_ptr<T>(void) const
{ {
@ -117,6 +132,9 @@ static Value Empty;
I2_BASE_API Value operator+(const Value& lhs, const char *rhs); I2_BASE_API Value operator+(const Value& lhs, const char *rhs);
I2_BASE_API Value operator+(const char *lhs, const Value& rhs); I2_BASE_API Value operator+(const char *lhs, const Value& rhs);
I2_BASE_API Value operator+(const Value& lhs, const String& rhs);
I2_BASE_API Value operator+(const String& lhs, const Value& rhs);
I2_BASE_API std::ostream& operator<<(std::ostream& stream, const Value& value); I2_BASE_API std::ostream& operator<<(std::ostream& stream, const Value& value);
I2_BASE_API std::istream& operator>>(std::istream& stream, Value& value); I2_BASE_API std::istream& operator>>(std::istream& stream, Value& value);