ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
CustomBackend.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include <ChimeraTK/BackendFactory.h>
5#include <ChimeraTK/DeviceAccessVersion.h>
6#include <ChimeraTK/DummyBackend.h> // Would probably be DeviceBackendImpl or NumericAddressedBackend in a real application
7
8#include <boost/make_shared.hpp>
9
10/*
11 * A custom backend which is registered to the factory.
12 * This example only shows how to register a new type of backend to the factory.
13 * It does not show how to write a new backend. We are lazy and derrive from
14 * DummyBackend to have a fully working backend. In a real example you would
15 * either derrive from DeviceBackendImpl or NumericAddressedBackend, unless you
16 * want to write a custom dummy for testing.
17 *
18 * Custom backends are always created as a shared library which can be loaded at
19 * run time.
20 */
22 public:
23 // C++11 shorthand syntax that we want a constructor with the same parameters
24 // as the parent class.
26
27 /*
28 * You have to implement a static function createInstance() with this exact
29 * signature. This function is later given to the BackendFactory to create
30 * this type of backend when it is requested.
31 */
32 static boost::shared_ptr<ChimeraTK::DeviceBackend> createInstance(
33 std::string /*address*/, std::map<std::string, std::string> parameters) {
34 /*
35 * Inside createInstance the parameters are interpreted and passed on to the
36 * constructor. Like this the backend constructor can have arbitrary
37 parameters
38 * while the factory can always call a function with the same signature.
39
40 * In this example we have to convert the "map" parameter to an absolute
41 path
42 * (there is already a function for it in the DummyBackend parent class),
43 * and pass it on to the constructor, which has the same signature as
44 DummyBackend
45 * (see 'using' clause above).
46 *
47 * This part will vary, depending on the requirements of the particular
48 backend.
49 */
50 std::string absolutePath = convertPathRelativeToDmapToAbs(parameters["map"]);
51
52 /*
53 * Now we have all parameters for the constructor. We just have to create a
54 * shared pointer of the CustomBackend with it.
55 */
56 return boost::make_shared<CustomBackend>(absolutePath);
57 }
58
59 /*
60 * The task of the BackendRegister is to call the function which tells the
61 * factory about the new type of backend. This is happening in the constructor
62 * of the class, so you just have to create an instance of the class and the
63 * code is executed.
64 */
67 /*
68 * The first parameter is the backend type string. It is the name by which
69 * the factory knows which type of backend to create. The name has to be
70 * unique. It shows up in the ChimeraTK device descriptor, in this case
71 * (CUSTOM?map=example.map)
72 * (example.map is the parameter which is passed on to createInstance, see
73 * above)
74 *
75 * The second parameter is the pointer to the createInstance function.
76 * The factory stores this pointer together with the type name and call
77 * the functions when this type if backed needs to be created.
78 */
80 }
81 };
82};
83
84// We have one global instance if the BackendRegisterer. Whenever the library
85// containing this backend is loaded, this object is instantiated. As the
86// constructor of this class is registering the device, the backend is
87// automatically known to the factory when the library is loaded.
88static CustomBackend::BackendRegisterer gCustomBackendRegisterer;
static BackendFactory & getInstance()
Static function to get an instance of factory.
void registerBackendType(const std::string &backendType, boost::shared_ptr< DeviceBackend >(*creatorFunction)(std::string address, std::map< std::string, std::string > parameters), const std::vector< std::string > &sdmParameterNames={}, const std::string &deviceAccessVersion=CHIMERATK_DEVICEACCESS_VERSION)
Register a backend by the name backendType with the given creatorFunction.
The dummy device opens a mapping file instead of a device, and implements all registers defined in th...
DummyBackend(const std::string &mapFileName, const std::string &dataConsistencyKeyDescriptor="")
static std::string convertPathRelativeToDmapToAbs(std::string const &mapfileName)
static boost::shared_ptr< ChimeraTK::DeviceBackend > createInstance(std::string, std::map< std::string, std::string > parameters)