Workspace 6.21.5
Writing a Custom Enum - looking at the code

In this section, we're going to understand the code that was automatically generated by Workspace so that we know how to customise our enum if we need to change it in the future.


Rebuilding (continue on with tutorial Writing a Custom Enum )


priority.h

\#ifndef CSIRO_PRIORITY_H
\#define CSIRO_PRIORITY_H
\#include "Workspace/DataExecution/DataObjects/datafactorytraits.h"
\#include "Workspace/DataExecution/DataObjects/enumtointadaptor.h"
\#include "simpleplugin.h"
namespace CSIRO
{
//
// Specifies the relative priority of an item.
//
enum CSIRO_API Priority
{
low = 0,
medium = 1,
high = 2
};
}
Top level namespace for all Workspace code.
Definition: applicationsupportplugin.cpp:32

If we look in our priority.h file, we'll see the standard header guards and a number of includes, most notably for "Workspace/DataExecution/DataObjects/enumtointadaptor.h". This is important and will be covered later. We also include our simpleplugin.h file, which gives us access to our export symbol (CSIRO_API), in order to make our enum visible to other plugins / DLLs if necessary.

Further down, we can see the definition of the enum itself, including the symbol names we entered, each of which is associated with a specific integer value. Our brief description is atop this definition.

namespace CSIRO
{
namespace DataExecution
{
template<> inline void getEnumNames<CSIRO::Priority>(QStringList& names)
{
names.push_back("Low");
names.push_back("Medium");
names.push_back("High");
}
}
}
QStringList
Definition: vectornumbertostringlistadaptor.cpp:133

Next, we see a template specialisation of the function, "getEnumNames". This function is what Workspace uses to identify the visible names associated with each enum value. It is critical that the number of names in this list matches the number of elements in the enum itself.

DECLARE_WORKSPACE_DATA_FACTORY(CSIRO::Priority, CSIRO_API)
DECLARE_WORKSPACE_ENUMTOINTADAPTOR(CSIRO::Priority, CSIRO_API)
#define DECLARE_WORKSPACE_DATA_FACTORY(T, WORKSPACE_EXPORT_SYMBOL)
Definition: datafactorytraits.h:759
#define DECLARE_WORKSPACE_ENUMTOINTADAPTOR(T, WORKSPACE_EXPORT_SYMBOL)
Definition: enumtointadaptor.h:183

Finally, we see that the enum itself is declared as a Workspace Data Factory (see Writing a Workspace Datatype for more information), and then more interestingly, we see a special Enum To Int Adaptor is declared. This will allow Workspace to automatically convert the enum to an integer wherever this is required, such as in the case it is used to drive a DataExecution::SelectInput or similar operation.

priority.cpp

Our source file is very straightforward. It contains only includes as well as the definition macros for our Workspace Data Factory and Enum To Int Adaptor.

\#include "Workspace/Application/LanguageUtils/errorchecks.h"
\#include "Workspace/DataExecution/DataObjects/typeddatafactory.h"
\#include "simpleplugin.h"
\#include "priority.h"
DEFINE_WORKSPACE_DATA_FACTORY(CSIRO::Priority, CSIRO::SimplePlugin::getInstance())
DEFINE_WORKSPACE_ENUMTOINTADAPTOR(CSIRO::Priority, CSIRO::SimplePlugin::getInstance())
#define DEFINE_WORKSPACE_ENUMTOINTADAPTOR(T, P)
Definition: enumtointadaptor.h:143
#define DEFINE_WORKSPACE_DATA_FACTORY(T, P)
Definition: typeddatafactory.h:1426

Rebuilding (continue on with tutorial Writing a Custom Enum )


simpleplugin.cpp

If we look at our simpleplugin.cpp file, we will see that the wizard has made a number of changes. The following code has been added to the setup() function.

addFactory(priority);
Traits class for data objects of type T.
Definition: datafactorytraits.h:143
A TypeAdaptorFactory class for an adaptor converting an enum to an int.
Definition: enumtointadaptor.h:51
Definition: enumcomboboxfactory.h:45

These lines do the following and in the order specified:

  1. Add the Priority data factory to the plugin, so that Workspace knows how to create it.
  2. Add an EnumToIntAdaptorFactory for Priority to the plugin, so that Workspace knows how to convert it to an integer when told to do so.
  3. Create an EnumComboBoxFactory for Priority so that Workspace knows how to display the enum values to the user in a simple combo box. This saves us the trouble of having to create a custom combo box widget for every different enum that we add to our plugin.
  4. Add the new EnumComboBoxFactory to the plugin.

Changes to the CMakeLists file

If we look at our CMakeLists.txt file, we will see that the wizard will have made minimal changes. The priority header and source files have simply been added to the HEADERS, INSTALL_HEADERS and SOURCES lists, thus making them source files of our plugin.

set(HEADERS
${SIMPLE_SOURCE_DIR}/priority.h
...
)
set(INSTALL_HEADERS
${SIMPLE_SOURCE_DIR}/priority.h
...
)
set(SOURCES
${SIMPLE_SOURCE_DIR}/priority.cpp
...
)

Rebuilding (continue on with tutorial Writing a Custom Enum )