我正在使用该qdbusxml2cpp工具为我的D-Bus服务器生成D-Bus适配器类.但是,它有以下缺点:
代码生成一次,然后您不应该修改它.但是,如果我们必须进行更改(见下文)然后更改XML(当然是以向后兼容的方式),该怎么办?
假设"适配器"具有与D-Bus接口完全相同的功能和签名.就我而言,这并不完全正确,例如,某些方法的命名方式不同.由于生成的代码使用QMetaObject::invokeMethod,因此仅在运行时检测到.如果我们将来需要重新生成代码,我们就无法明智地修改生成的代码.
在我看来,如果qdbusxml2cpp生成一个抽象类,只是一个标题,所有方法都是纯虚拟的,那就更好了.然后我可以编写一个类的实现,只需调用适配器上的正确方法,而无需通过Qt元类型系统.这解决了两个问题:
如果XML发生变化,我们只需重新生成标题.现在编译器会抱怨,直到我们正确实现新接口.
我们可以自由地在"adaptee"类中调用我们喜欢的任何函数,而不是保持与公共D-Bus接口中完全相同的签名.
我找不到上述任何工具或qdbusxml2cpp叉子.在我自己编写之前,上述方法是否存在任何问题,我可能会忽略,设计方面还是技术方面?也许元类型系统的局限性与抽象类或纯虚函数有关?
请注意,我需要这不仅可以使用方法,还可以使用属性和信号.
我还考虑编写一个包含"adaptee"的"中间"适配器,并提供D-Bus适配器所需的确切接口,但D-Bus适配器仍将使用元类型系统和运行时检查.当然,我们可以做得更好.
使用qdbusxml2cpp程序将以下xml转换为Qt类时,我收到此错误:
qdbusxml2cpp -c ObjectManager -a ObjectManager:ObjectManager.cpp xml/object_manager.xml
Got unknown type `a{oa{sa{sv}}}'
You should add <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="<type>"/> to the XML description
Run Code Online (Sandbox Code Playgroud)
D-Feet描述:

XML:
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node><interface name="org.freedesktop.DBus.Introspectable"><method name="Introspect"><arg name="xml" type="s" direction="out"/>
</method></interface><interface name="org.freedesktop.DBus.ObjectManager"><method name="GetManagedObjects"><arg name="objects" type="a{oa{sa{sv}}}" direction="out"/>
</method><signal name="InterfacesAdded"><arg name="object" type="o"/>
<arg name="interfaces" type="a{sa{sv}}"/>
</signal>
<signal name="InterfacesRemoved"><arg name="object" type="o"/>
<arg name="interfaces" type="as"/>
</signal>
</interface><node name="org"/></node>
Run Code Online (Sandbox Code Playgroud)
从这个网站(http://techbase.kde.org/Development/Tutorials/D-Bus/CustomTypes)我明白我需要在XML中添加一个注释才能使工具正常工作.
这是我到目前为止:
a{oa{sa{sv}}}
https://alteeve.ca/w/List_of_DBus_data_types
o == A UTF-8 string whose value is a valid DBus object …Run Code Online (Sandbox Code Playgroud)