1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
#ifndef TANGO_TESTS_CATCH2_UTILS_AUTO_DEVICE_CLASS_H
#define TANGO_TESTS_CATCH2_UTILS_AUTO_DEVICE_CLASS_H
#include <tango/tango.h>
#include <functional>
#include <type_traits>
#include "utils/type_traits.h"
namespace TangoTest
{
namespace detail
{
template <typename T>
using attribute_factory_t = decltype(T::attribute_factory);
template <typename T>
constexpr bool has_attribute_factory = is_detected_v<attribute_factory_t, T>;
template <typename T>
using command_factory_t = decltype(T::command_factory);
template <typename T>
constexpr bool has_command_factory = is_detected_v<command_factory_t, T>;
template <typename F>
struct member_fn_traits;
template <>
struct member_fn_traits<std::nullptr_t>
{
using class_type = void;
};
template <typename C, typename R, typename... Args>
struct member_fn_traits<R (C::*)(Args... args)>
{
using class_type = C;
using return_type = R;
constexpr static int arity = std::tuple_size<std::tuple<Args...>>();
template <int N>
using argument_type = std::tuple_element_t<N, std::tuple<Args...>>;
};
} // namespace detail
/** @brief Automatically generate a Tango::DeviceClass from a Tango::Device.
*
* If the following static member functions are defined then they will be called
* during device instantiation:
*
* - static void attribute_factory(std::vector<Tango::Attr *> &attrs);
* - static void command_factory(std::vector<Tango::Command *> &cmds)
*
* Use the TANGO_TEST_AUTO_DEV_CLASS_INSTANTIATE macro (in a single
* implementation file per Device) to instantiate AutoDeviceClass's static
* members and to register the device class with Tango.
*
* The `init_device` method is called automatically just after the constructor.
* For internal reasons the `delete_device` method, if present, must be called
* explicitly by the test code.
*
* Example:
*
* class MyDevice : public Tango::Device
* {
* static void attribute_factory(std::vector<Tango::Attr *> attrs)
* {
* // ... Add attributes here
* }
* };
*
* TANGO_TEST_AUTO_DEV_CLASS_INSTANTIATE(MyDevice)
*
* @tparam Device C++ class for Tango device
* @param s name of device class
*/
template <typename Device>
class AutoDeviceClass : public Tango::DeviceClass
{
public:
using Tango::DeviceClass::DeviceClass;
static AutoDeviceClass *init(const char *s)
{
if(!_instance)
{
_instance = new AutoDeviceClass{s};
}
return _instance;
}
~AutoDeviceClass() override { }
void device_factory(const Tango::DevVarStringArray *devlist_ptr) override
{
auto *tg = Tango::Util::instance();
for(CORBA::ULong i = 0; i < devlist_ptr->length(); ++i)
{
const char *name = (*devlist_ptr)[i];
// "NoName" means no device with this class was specified
// on the CLI (in nodb mode). We do not create this device as it is
// not needed for the test.
if(strcmp(name, "NoName") == 0)
{
continue;
}
auto dev = new Device{this, name};
dev->init_device();
device_list.push_back(dev);
if(tg->use_db() && !tg->use_file_db())
{
export_device(device_list.back());
}
else
{
export_device(device_list.back(), name);
}
}
}
protected:
void command_factory() override
{
if constexpr(detail::has_command_factory<Device>)
{
Device::command_factory(command_list);
}
}
void attribute_factory(std::vector<Tango::Attr *> &attrs) override
{
if constexpr(detail::has_attribute_factory<Device>)
{
Device::attribute_factory(attrs);
}
}
static AutoDeviceClass *_instance;
};
template <auto cmd_fn>
class AutoCommand : public Tango::Command
{
public:
using traits = detail::member_fn_traits<decltype(cmd_fn)>;
using Device = typename traits::class_type;
using Tango::Command::Command;
AutoCommand(const std::string &name) :
Tango::Command(name, type_in(), type_out())
{
}
~AutoCommand() override { }
CORBA::Any *execute(Tango::DeviceImpl *dev, const CORBA::Any &in_any) override
{
if constexpr(traits::arity == 0 && std::is_same_v<void, typename traits::return_type>)
{
std::invoke(cmd_fn, static_cast<Device *>(dev));
return insert();
}
else if constexpr(traits::arity == 0)
{
auto ret = std::invoke(cmd_fn, static_cast<Device *>(dev));
return insert(ret);
}
else if constexpr(std::is_same_v<void, typename traits::return_type>)
{
using arg_type = std::remove_cv_t<std::remove_reference_t<typename traits::template argument_type<0>>>;
if constexpr(std::is_same_v<arg_type, typename Tango::tango_type_traits<arg_type>::ArrayType>)
{
const arg_type *arg;
extract(in_any, arg);
std::invoke(cmd_fn, static_cast<Device *>(dev), *arg);
}
else
{
arg_type arg;
extract(in_any, arg);
std::invoke(cmd_fn, static_cast<Device *>(dev), arg);
}
return insert();
}
else
{
using arg_type = std::remove_cv_t<std::remove_reference_t<typename traits::template argument_type<0>>>;
auto ret = [this, &dev, &in_any]()
{
if constexpr(std::is_same_v<arg_type, typename Tango::tango_type_traits<arg_type>::ArrayType>)
{
arg_type *arg;
extract(in_any, arg);
return std::invoke(cmd_fn, static_cast<Device *>(dev), *arg);
}
else
{
arg_type arg;
extract(in_any, arg);
return std::invoke(cmd_fn, static_cast<Device *>(dev), arg);
}
}();
return insert(ret);
}
}
private:
constexpr static Tango::CmdArgType type_out()
{
if constexpr(std::is_same_v<void, typename traits::return_type>)
{
return Tango::DEV_VOID;
}
else
{
using ret_type = std::remove_cv_t<std::remove_reference_t<typename traits::return_type>>;
return Tango::tango_type_traits<ret_type>::type_value();
}
}
constexpr static Tango::CmdArgType type_in()
{
if constexpr(traits::arity == 0)
{
return Tango::DEV_VOID;
}
else
{
using arg_type = std::remove_cv_t<std::remove_reference_t<typename traits::template argument_type<0>>>;
return Tango::tango_type_traits<arg_type>::type_value();
}
}
};
template <auto read_fn, auto write_fn = nullptr>
class AutoAttr : public Tango::Attr
{
public:
using ReadDevice = typename detail::member_fn_traits<decltype(read_fn)>::class_type;
using WriteDevice = typename detail::member_fn_traits<decltype(write_fn)>::class_type;
constexpr static bool has_write_fn = static_cast<bool>(write_fn);
using Tango::Attr::Attr;
// do we care about other possible parameters to Tango::Attr()?
AutoAttr(const char *name, long data_type) :
Tango::Attr(name, data_type, has_write_fn ? Tango::READ_WRITE : Tango::READ)
{
}
~AutoAttr() override { }
void read(Tango::DeviceImpl *dev, Tango::Attribute &att) override
{
std::invoke(read_fn, static_cast<ReadDevice *>(dev), att);
}
void write(Tango::DeviceImpl *dev, Tango::WAttribute &att) override
{
if constexpr(has_write_fn)
{
std::invoke(write_fn, static_cast<WriteDevice *>(dev), att);
}
}
};
template <typename EnumType, auto read_fn, auto write_fn = nullptr>
class AutoEnumAttr : public Tango::Attr
{
public:
using ReadDevice = typename detail::member_fn_traits<decltype(read_fn)>::class_type;
using WriteDevice = typename detail::member_fn_traits<decltype(write_fn)>::class_type;
constexpr static bool has_write_fn = static_cast<bool>(write_fn);
using Tango::Attr::Attr;
// do we care about other possible parameters to Tango::Attr()?
AutoEnumAttr(const char *name) :
Tango::Attr(name, Tango::DEV_ENUM, has_write_fn ? Tango::READ_WRITE : Tango::READ)
{
}
~AutoEnumAttr() override { }
void read(Tango::DeviceImpl *dev, Tango::Attribute &att) override
{
std::invoke(read_fn, static_cast<ReadDevice *>(dev), att);
}
void write(Tango::DeviceImpl *dev, Tango::WAttribute &att) override
{
if constexpr(has_write_fn)
{
std::invoke(write_fn, static_cast<WriteDevice *>(dev), att);
}
}
virtual bool same_type(const std::type_info &in_type) override
{
return typeid(EnumType) == in_type;
}
virtual std::string get_enum_type() override
{
return typeid(EnumType).name();
}
};
template <auto read_fn, auto write_fn = nullptr>
class AutoSpectrumAttr : public Tango::SpectrumAttr
{
public:
using ReadDevice = typename detail::member_fn_traits<decltype(read_fn)>::class_type;
using WriteDevice = typename detail::member_fn_traits<decltype(write_fn)>::class_type;
constexpr static bool has_write_fn = static_cast<bool>(write_fn);
using Tango::SpectrumAttr::SpectrumAttr;
// do we care about other possible parameters to Tango::Attr()?
AutoSpectrumAttr(const char *name, long data_type, long max_x) :
Tango::SpectrumAttr(name, data_type, has_write_fn ? Tango::READ_WRITE : Tango::READ, max_x)
{
}
~AutoSpectrumAttr() override { }
void read(Tango::DeviceImpl *dev, Tango::Attribute &att) override
{
std::invoke(read_fn, static_cast<ReadDevice *>(dev), att);
}
void write(Tango::DeviceImpl *dev, Tango::WAttribute &att) override
{
if constexpr(has_write_fn)
{
std::invoke(write_fn, static_cast<WriteDevice *>(dev), att);
}
}
};
template <typename EnumType, auto read_fn, auto write_fn = nullptr>
class AutoEnumSpectrumAttr : public Tango::SpectrumAttr
{
public:
using ReadDevice = typename detail::member_fn_traits<decltype(read_fn)>::class_type;
using WriteDevice = typename detail::member_fn_traits<decltype(write_fn)>::class_type;
constexpr static bool has_write_fn = static_cast<bool>(write_fn);
using Tango::SpectrumAttr::SpectrumAttr;
// do we care about other possible parameters to Tango::Attr()?
AutoEnumSpectrumAttr(const char *name, long max_x) :
Tango::SpectrumAttr(name, Tango::DEV_ENUM, has_write_fn ? Tango::READ_WRITE : Tango::READ, max_x)
{
}
~AutoEnumSpectrumAttr() override { }
void read(Tango::DeviceImpl *dev, Tango::Attribute &att) override
{
std::invoke(read_fn, static_cast<ReadDevice *>(dev), att);
}
void write(Tango::DeviceImpl *dev, Tango::WAttribute &att) override
{
if constexpr(has_write_fn)
{
std::invoke(write_fn, static_cast<WriteDevice *>(dev), att);
}
}
virtual bool same_type(const std::type_info &in_type) override
{
return typeid(EnumType) == in_type;
}
virtual std::string get_enum_type() override
{
return typeid(EnumType).name();
}
};
template <auto read_fn, auto write_fn = nullptr>
class AutoImageAttr : public Tango::ImageAttr
{
public:
using ReadDevice = typename detail::member_fn_traits<decltype(read_fn)>::class_type;
using WriteDevice = typename detail::member_fn_traits<decltype(write_fn)>::class_type;
constexpr static bool has_write_fn = static_cast<bool>(write_fn);
using Tango::ImageAttr::ImageAttr;
// do we care about other possible parameters to Tango::Attr()?
AutoImageAttr(const char *name, long data_type, long max_x, long max_y) :
Tango::ImageAttr(name, data_type, has_write_fn ? Tango::READ_WRITE : Tango::READ, max_x, max_y)
{
}
~AutoImageAttr() override { }
void read(Tango::DeviceImpl *dev, Tango::Attribute &att) override
{
std::invoke(read_fn, static_cast<ReadDevice *>(dev), att);
}
void write(Tango::DeviceImpl *dev, Tango::WAttribute &att) override
{
if constexpr(has_write_fn)
{
std::invoke(write_fn, static_cast<WriteDevice *>(dev), att);
}
}
};
template <typename EnumType, auto read_fn, auto write_fn = nullptr>
class AutoEnumImageAttr : public Tango::ImageAttr
{
public:
using ReadDevice = typename detail::member_fn_traits<decltype(read_fn)>::class_type;
using WriteDevice = typename detail::member_fn_traits<decltype(write_fn)>::class_type;
constexpr static bool has_write_fn = static_cast<bool>(write_fn);
using Tango::ImageAttr::ImageAttr;
// do we care about other possible parameters to Tango::Attr()?
AutoEnumImageAttr(const char *name, long max_x, long max_y) :
Tango::ImageAttr(name, Tango::DEV_ENUM, has_write_fn ? Tango::READ_WRITE : Tango::READ, max_x, max_y)
{
}
~AutoEnumImageAttr() override { }
void read(Tango::DeviceImpl *dev, Tango::Attribute &att) override
{
std::invoke(read_fn, static_cast<ReadDevice *>(dev), att);
}
void write(Tango::DeviceImpl *dev, Tango::WAttribute &att) override
{
if constexpr(has_write_fn)
{
std::invoke(write_fn, static_cast<WriteDevice *>(dev), att);
}
}
virtual bool same_type(const std::type_info &in_type) override
{
return typeid(EnumType) == in_type;
}
virtual std::string get_enum_type() override
{
return typeid(EnumType).name();
}
};
namespace detail
{
constexpr const char *k_enabled_classes_env_var = "TANGO_TEST_ENABLED_CLASSES";
struct ClassRegistrarBase
{
ClassRegistrarBase(const char *n) :
name{n}
{
if(registrars == nullptr)
{
registrars = new std::vector<ClassRegistrarBase *>();
}
registrars->push_back(this);
}
virtual Tango::DeviceClass *init_class() = 0;
// We use a pointer here so that the first `ClassRegistrarBase` constructs
// the vector. This avoids the static initialisation order fiasco.
static std::vector<ClassRegistrarBase *> *registrars;
const char *name;
};
template <typename DeviceClass>
struct ClassRegistrar : ClassRegistrarBase
{
using ClassRegistrarBase::ClassRegistrarBase;
Tango::DeviceClass *init_class() override
{
return DeviceClass::init(name);
}
};
} // namespace detail
} // namespace TangoTest
/**
* @brief Instantiate a TangoTest::AutoDeviceClass for DEVICE.
*
* For each DEVICE, this macro must be used in a single implementation file to
* instantiate static data members.
*
* The device class will be registered with Tango with the name #DEVICE.
*
* @param DEVICE class
* @param NAME name of class
*/
#define TANGO_TEST_AUTO_DEV_CLASS_INSTANTIATE(DEVICE, NAME) \
template <> \
TangoTest::AutoDeviceClass<DEVICE> *TangoTest::AutoDeviceClass<DEVICE>::_instance = nullptr; \
namespace \
{ \
TangoTest::detail::ClassRegistrar<TangoTest::AutoDeviceClass<DEVICE>> NAME##_registrar{#NAME}; \
}
#endif
|