object_path.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef DBUS_OBJECT_PATH_H_
  5. #define DBUS_OBJECT_PATH_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include "dbus/dbus_export.h"
  9. namespace dbus {
  10. // ObjectPath is a type used to distinguish D-Bus object paths from simple
  11. // strings, especially since normal practice is that these should be only
  12. // initialized from static constants or obtained from remote objects and no
  13. // assumptions about their value made.
  14. class CHROME_DBUS_EXPORT ObjectPath {
  15. public:
  16. // Permit initialization without a value for passing to
  17. // dbus::MessageReader::PopObjectPath to fill in and from std::string
  18. // objects.
  19. //
  20. // The compiler synthesised copy constructor and assignment operator are
  21. // sufficient for our needs, as is implicit initialization of a std::string
  22. // from a string constant.
  23. ObjectPath() {}
  24. explicit ObjectPath(const std::string& value) : value_(value) {}
  25. // Retrieves value as a std::string.
  26. const std::string& value() const { return value_; }
  27. // Returns true if the value is a valid object path.
  28. bool IsValid() const;
  29. // Permit sufficient comparison to allow an ObjectPath to be used as a
  30. // key in a std::map.
  31. bool operator<(const ObjectPath&) const;
  32. // Permit testing for equality, required for mocks to work and useful for
  33. // observers.
  34. bool operator==(const ObjectPath&) const;
  35. bool operator!=(const ObjectPath&) const;
  36. private:
  37. std::string value_;
  38. };
  39. // This is required by gtest to print a readable output on test failures.
  40. CHROME_DBUS_EXPORT void PrintTo(const ObjectPath& path, std::ostream* out);
  41. } // namespace dbus
  42. #endif // DBUS_OBJECT_PATH_H_