Workspace 6.21.5
|
The following are suggested pre-reading for this tutorial:
In the previous tutorial, you learned how to create your own custom data type. However, you may have an existing class and want to include it in Workspace as a custom data type. Or you may want to use a C++ STL class or a built-in data type from an external third-party library. These data types do not need to extend CSIRO::DataExecution::ObjectGroup
like the previous example. This tutorial shows how to import exisitng data types.
In the following example we will learn how to import a class from the C++ STL. We will import std::deque from the C++ STL. For most data types, we really only have to add two lines of code to import the class into Workspace: the DECLARE and DEFINE macros.
The best way to understand this is to see it in action, so let's have a look at this new header file:
The macro:
tells us that std::deque will be made available to Workspace.
The DEFINE_WORKSPACE_DATA_FACTORY or DEFINE_WORKSPACE_DATA_FACTORY_NO_QMETATYPE macro should just about always be at the end of the file and it must sit outside any namespaces. Most importantly, it must never appear in a header file. The macro associates your custom class with your plugin and adds a data factory for it to Workspace. It even takes care of handling whether or not your class supports serialization. It should be used as follows:
getInstance()
function of your plugin.In the following example we will learn how to import a class from a third-party library. We will import QSerialPort from the Qt library. As demonstrated above in Importing a C++ STL class, for most data types, we really only have to add two lines of code to import the class into Workspace: the DECLARE and DEFINE macros.
Let's have a look at this header file:
The QSerialPort disables the use of copy constructors and assignment operators. Hence we need to explicitly add:
The macro:
tells us that QSerialPort will be made available to Workspace except that no QMetaType support is added for the type T (QSerialPort in this case). This macro must be used if T refers to a type for which Qt already provides QMetaType support (built in C++ types and Qt classes). It must also be used if T does not support cloning, since the QMetaType system requires types to be clonable. In this example we can tell that QSerialPort does not support cloning by the presence of the macro Q_DISABLE_COPY(QSerialPort). Q_DISABLE_COPY disables the use of copy constructors and assignment operators for the given class. Another way to recognize this is to check the code for the copy constructor and assignment operator. If the copy constructor and assignment operator are declared in the private section, the class is not clonable and cannot be assigned.
The CPP file, serialport.cpp contains the DEFINE macro:
We also need to modify the plugin CPP file (importdatatypeplugin.cpp) and add the headers and source files to the CMakeLists.txt files.
Include the header files for the newly imported data types.
Add the data factories for the new data types to this plugin.
In this case we need a new Qt package - Qt5SerialPort. Add this to the find_package() directive in CMakeLists.txt and the QT_LIBRARIES. Also add the new .h and .cpp files for the new data types.
Build the plugin again as per the instruction in Writing a Simple Workspace Plugin.
The data types can now be used.
This tutorial has introduced you to the essentials of including an existing class in Workspace as a custom data type.
The following is suggested for further reading: