bus: reduce forward declaration for friendship

Generally we do not want users to be able to get the underlying `sd_bus`
pointer held by the `bus_t`, but there are a number of classes inside
sdbusplus that do need the bus so that they can make appropriate sd_bus
call.  Previously, we've required every one of these classes to be
forward declared inside `bus.hpp`, which is clutter.  Create a new
class, with a simple static function, which can be inherited by any
internal class and allows access to the underlying `sd_bus*`.

Refactor classes which were previously friends to now inherit from
this class.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ide0e7147801d01dc45b255261ba2cafefabddbb1
diff --git a/include/sdbusplus/bus.hpp b/include/sdbusplus/bus.hpp
index 676a445..874c364 100644
--- a/include/sdbusplus/bus.hpp
+++ b/include/sdbusplus/bus.hpp
@@ -22,37 +22,6 @@
 namespace sdbusplus
 {
 
-// Forward declare.
-namespace server
-{
-namespace interface
-{
-struct interface;
-}
-} // namespace server
-namespace server
-{
-namespace manager
-{
-struct manager;
-}
-} // namespace server
-namespace server
-{
-namespace object
-{
-template <class...>
-struct object;
-}
-} // namespace server
-namespace bus
-{
-namespace match
-{
-struct match;
-}
-} // namespace bus
-
 namespace bus
 {
 
@@ -116,6 +85,8 @@
 /* @brief Alias 'bus' to a unique_ptr type for auto-release. */
 using bus = std::unique_ptr<sd_bus, BusDeleter>;
 
+struct bus_friend;
+
 } // namespace details
 
 /** @class bus
@@ -476,14 +447,10 @@
         return _intf;
     }
 
-    friend struct server::interface::interface;
-    friend struct server::manager::manager;
-    template <class... Args>
-    friend struct server::object::object;
-    friend struct match::match;
+    friend struct details::bus_friend;
 
   protected:
-    busp_t get()
+    busp_t get() noexcept
     {
         return _bus.get();
     }
@@ -586,6 +553,23 @@
     return bus;
 }
 
+namespace details
+{
+
+// Some sdbusplus classes need to be able to pass the underlying bus pointer
+// along to sd_bus calls, but we don't want to make it available for everyone.
+// Define a class which can be inherited explicitly (intended for internal users
+// only) to get the underlying bus pointer.
+struct bus_friend
+{
+    static busp_t get_busp(sdbusplus::bus::bus& b) noexcept
+    {
+        return b.get();
+    }
+};
+
+} // namespace details
+
 } // namespace bus
 
 using bus_t = bus::bus;
diff --git a/include/sdbusplus/bus/match.hpp b/include/sdbusplus/bus/match.hpp
index cc6c19c..3032712 100644
--- a/include/sdbusplus/bus/match.hpp
+++ b/include/sdbusplus/bus/match.hpp
@@ -17,7 +17,7 @@
 namespace match
 {
 
-struct match
+struct match : private sdbusplus::bus::details::bus_friend
 {
     /* Define all of the basic class operations:
      *     Not allowed:
@@ -45,7 +45,7 @@
           sd_bus_message_handler_t handler, void* context = nullptr)
     {
         sd_bus_slot* slot = nullptr;
-        sd_bus_add_match(bus.get(), &slot, match, handler, context);
+        sd_bus_add_match(get_busp(bus), &slot, match, handler, context);
 
         _slot = std::move(slot);
     }
@@ -66,7 +66,7 @@
         _callback(std::make_unique<callback_t>(std::move(callback)))
     {
         sd_bus_slot* slot = nullptr;
-        sd_bus_add_match(bus.get(), &slot, match, callCallback,
+        sd_bus_add_match(get_busp(bus), &slot, match, callCallback,
                          _callback.get());
 
         _slot = std::move(slot);
diff --git a/include/sdbusplus/server/interface.hpp b/include/sdbusplus/server/interface.hpp
index 946c50e..be450ac 100644
--- a/include/sdbusplus/server/interface.hpp
+++ b/include/sdbusplus/server/interface.hpp
@@ -32,7 +32,7 @@
  *  implementation should 'has-a' server::interface with a name sufficiently
  *  unique to prevent name collisions in multiple-inheritence situations.
  */
-struct interface final
+struct interface final : private sdbusplus::bus::details::bus_friend
 {
     /* Define all of the basic class operations:
      *     Not allowed:
diff --git a/include/sdbusplus/server/manager.hpp b/include/sdbusplus/server/manager.hpp
index 32519f4..797fb0f 100644
--- a/include/sdbusplus/server/manager.hpp
+++ b/include/sdbusplus/server/manager.hpp
@@ -19,7 +19,7 @@
  *  Wraps sd_bus_add_object_manager so that an sd-bus owned object manager
  *  can be installed at a path and automatically removed when destructed.
  */
-struct manager
+struct manager : private sdbusplus::bus::details::bus_friend
 {
     /* Define all of the basic class operations:
      *     Not allowed:
@@ -46,7 +46,7 @@
         sd_bus_slot* slot = nullptr;
         sdbusplus::SdBusInterface* intf = bus.getInterface();
 
-        intf->sd_bus_add_object_manager(bus.get(), &slot, path);
+        intf->sd_bus_add_object_manager(get_busp(bus), &slot, path);
 
         _slot = std::move(slot);
     }
diff --git a/include/sdbusplus/server/object.hpp b/include/sdbusplus/server/object.hpp
index 8553bd4..f258435 100644
--- a/include/sdbusplus/server/object.hpp
+++ b/include/sdbusplus/server/object.hpp
@@ -88,7 +88,9 @@
  *
  */
 template <class... Args>
-struct object : details::compose<Args...>
+struct object :
+    details::compose<Args...>,
+    private sdbusplus::bus::details::bus_friend
 {
     /* Define all of the basic class operations:
      *     Not allowed:
@@ -123,7 +125,7 @@
     object(bus_t& bus, const char* path,
            action act = action::emit_object_added) :
         details::compose<Args...>(bus, path),
-        __sdbusplus_server_object_bus(bus.get(), bus.getInterface()),
+        __sdbusplus_server_object_bus(get_busp(bus), bus.getInterface()),
         __sdbusplus_server_object_path(path),
         __sdbusplus_server_object_emitremoved(false),
         __sdbusplus_server_object_intf(bus.getInterface())
@@ -144,7 +146,7 @@
         if (__sdbusplus_server_object_emitremoved)
         {
             __sdbusplus_server_object_intf->sd_bus_emit_object_removed(
-                __sdbusplus_server_object_bus.get(),
+                get_busp(__sdbusplus_server_object_bus),
                 __sdbusplus_server_object_path.c_str());
         }
     }
@@ -155,7 +157,7 @@
         if (!__sdbusplus_server_object_emitremoved)
         {
             __sdbusplus_server_object_intf->sd_bus_emit_object_added(
-                __sdbusplus_server_object_bus.get(),
+                get_busp(__sdbusplus_server_object_bus),
                 __sdbusplus_server_object_path.c_str());
             __sdbusplus_server_object_emitremoved = true;
         }
diff --git a/src/server/interface.cpp b/src/server/interface.cpp
index 653e6d6..e47f6ef 100644
--- a/src/server/interface.cpp
+++ b/src/server/interface.cpp
@@ -12,13 +12,13 @@
 interface::interface(sdbusplus::bus_t& bus, const char* path,
                      const char* interf, const sdbusplus::vtable_t* vtable,
                      void* context) :
-    _bus(bus.get(), bus.getInterface()),
+    _bus(get_busp(bus), bus.getInterface()),
     _path(path), _interf(interf), _intf(bus.getInterface()),
     _interface_added(false)
 {
     sd_bus_slot* slot = nullptr;
-    int r = _intf->sd_bus_add_object_vtable(_bus.get(), &slot, _path.c_str(),
-                                            _interf.c_str(), vtable, context);
+    int r = _intf->sd_bus_add_object_vtable(
+        get_busp(_bus), &slot, _path.c_str(), _interf.c_str(), vtable, context);
     if (r < 0)
     {
         throw exception::SdBusError(-r, "sd_bus_add_object_vtable");
@@ -38,7 +38,7 @@
 
     // Note: Converting to use _strv version, could also mock two pointer
     // use-case explicitly.
-    _intf->sd_bus_emit_properties_changed_strv(_bus.get(), _path.c_str(),
+    _intf->sd_bus_emit_properties_changed_strv(get_busp(_bus), _path.c_str(),
                                                _interf.c_str(), values.data());
 }