#pragma once
#include <string>
#include <ostream>
namespace ggk {
struct DBusObjectPath
{
inline DBusObjectPath() { path = "/"; }
inline DBusObjectPath(const DBusObjectPath &path) : path(path.path) {}
inline explicit DBusObjectPath(const char *pPath) : path(pPath) {}
inline explicit DBusObjectPath(const std::string &path) : path(path) {}
inline const std::string &toString() const { return path; }
inline const char *c_str() const { return path.c_str(); }
inline DBusObjectPath &operator =(const DBusObjectPath &rhs)
{
if (this == &rhs) return *this;
path = rhs.path;
return *this;
}
inline const DBusObjectPath &append(const char *rhs)
{
if (nullptr == rhs || !*rhs) { return *this; }
if (path.empty()) { path = rhs; return *this; }
bool ls = path.back() == '/';
bool rs = *rhs == '/';
if (ls && rs) { path.erase(path.length()-1); }
if (!ls && !rs) { path += "/"; }
path += rhs;
return *this;
}
inline const DBusObjectPath &append(const std::string &rhs)
{
return append(rhs.c_str());
}
inline const DBusObjectPath &append(const DBusObjectPath &rhs)
{
return append(rhs.path.c_str());
}
inline void operator +=(const DBusObjectPath &rhs)
{
append(rhs);
}
inline void operator +=(const char *rhs)
{
append(rhs);
}
inline void operator +=(const std::string &rhs)
{
append(rhs);
}
inline DBusObjectPath operator +(const DBusObjectPath &rhs) const
{
DBusObjectPath result(*this);
result += rhs;
return result;
}
inline DBusObjectPath operator +(const char *rhs) const
{
DBusObjectPath result(*this);
result += rhs;
return result;
}
inline DBusObjectPath operator +(const std::string &rhs) const
{
DBusObjectPath result(*this);
result += rhs;
return result;
}
inline bool operator ==(const DBusObjectPath &rhs) const
{
return path == rhs.path;
}
private:
std::string path;
};
inline DBusObjectPath operator +(const char *lhs, const DBusObjectPath &rhs) { return DBusObjectPath(lhs) + rhs; }
inline DBusObjectPath operator +(const std::string &lhs, const DBusObjectPath &rhs) { return DBusObjectPath(lhs) + rhs; }
inline std::ostream& operator<<(std::ostream &os, const DBusObjectPath &path)
{
os << path.toString();
return os;
}
inline std::ostream& operator +(std::ostream &os, const DBusObjectPath &path)
{
os << path.toString();
return os;
}
};