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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
|
/*
* SPDX-FileCopyrightText: All Contributors to the PyTango project
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#ifndef _DEVICE_IMPL_H
#define _DEVICE_IMPL_H
#include <boost/python.hpp>
#include <tango/tango.h>
#include "pytgutils.h"
#include "exception.h"
#include "server/device_class.h"
#define __AUX_DECL_CALL_DEVICE_METHOD AutoPythonGIL __py_lock;
#define __AUX_CATCH_EXCEPTIONS(name) \
catch(bopy::error_already_set & eas) \
{ \
handle_python_exception(eas); \
} \
catch(...) \
{ \
Tango::Except::throw_exception("CppException", "An unexpected C++ exception occurred", #name); \
}
#define CALL_DEVICE_METHOD(cls, name) \
__AUX_DECL_CALL_DEVICE_METHOD \
try \
{ \
if(bopy::override name = this->get_override(#name)) \
{ \
name(); \
} \
else \
{ \
cls ::name(); \
} \
} \
__AUX_CATCH_EXCEPTIONS(name)
#define CALL_DEVICE_METHOD_VARGS(cls, name, ...) \
__AUX_DECL_CALL_DEVICE_METHOD \
try \
{ \
if(bopy::override name = this->get_override(#name)) \
{ \
name(__VA_ARGS__); \
} \
else \
{ \
cls ::name(__VA_ARGS__); \
} \
} \
__AUX_CATCH_EXCEPTIONS(name)
#define CALL_DEVICE_METHOD_RET(cls, name) \
__AUX_DECL_CALL_DEVICE_METHOD \
try \
{ \
if(bopy::override name = this->get_override(#name)) \
{ \
return name(); \
} \
else \
{ \
return cls ::name(); \
} \
} \
__AUX_CATCH_EXCEPTIONS(name)
/**
* A wrapper around the Tango::DeviceImpl class
*/
class DeviceImplWrap : public Tango::DeviceImpl, public bopy::wrapper<Tango::DeviceImpl>
{
public:
/** a reference to itself */
PyObject *m_self;
/**
* Constructor
*
* @param[in] self
* @param[in] cl
* @param[in] st
*/
DeviceImplWrap(PyObject *self, CppDeviceClass *cl, std::string &st);
/**
* Constructor
*
* @param[in] self
* @param[in] cl
* @param[in] name
* @param[in] desc
* @param[in] sta
* @param[in] status
*/
DeviceImplWrap(PyObject *self,
CppDeviceClass *cl,
const char *name,
const char *desc = "A Tango device",
Tango::DevState sta = Tango::UNKNOWN,
const char *status = Tango::StatusNotSet);
/**
* Destructor
*/
virtual ~DeviceImplWrap() { }
/**
* Invokes the actual init_device
*/
void init_device();
bool _is_attribute_polled(const std::string &att_name);
bool _is_command_polled(const std::string &cmd_name);
int _get_attribute_poll_period(const std::string &att_name);
int _get_command_poll_period(const std::string &cmd_name);
void _poll_attribute(const std::string &att_name, int period);
void _poll_command(const std::string &cmd_name, int period);
void _stop_poll_attribute(const std::string &att_name);
void _stop_poll_command(const std::string &cmd_name);
};
/**
* A wrapper around the Tango::Device_2Impl class
*/
class Device_2ImplWrap : public Tango::Device_2Impl, public bopy::wrapper<Tango::Device_2Impl>
{
public:
/** a reference to itself */
PyObject *m_self;
/**
* Constructor
*
* @param[in] self
* @param[in] cl
* @param[in] st
*/
Device_2ImplWrap(PyObject *self, CppDeviceClass *cl, std::string &st);
/**
* Constructor
*
* @param[in] self
* @param[in] cl
* @param[in] name
* @param[in] desc
* @param[in] sta
* @param[in] status
*/
Device_2ImplWrap(PyObject *self,
CppDeviceClass *cl,
const char *name,
const char *desc = "A Tango device",
Tango::DevState sta = Tango::UNKNOWN,
const char *status = Tango::StatusNotSet);
/**
* Destructor
*/
virtual ~Device_2ImplWrap() { }
/**
* Invokes the actual init_device
*/
void init_device();
};
class PyDeviceImplBase
{
public:
/** a reference to itself */
PyObject *the_self;
std::string the_status;
PyDeviceImplBase(PyObject *self) :
the_self(self)
{
Py_INCREF(the_self);
}
virtual ~PyDeviceImplBase() { }
virtual void py_delete_dev(){};
};
/**
* A wrapper around the Tango::Device_XImpl class
*/
template <typename TangoDeviceImpl>
class Device_XImplWrap : public TangoDeviceImpl, public PyDeviceImplBase, public bopy::wrapper<TangoDeviceImpl>
{
public:
/**
* Constructor
*
* @param[in] self
* @param[in] cl
* @param[in] st
*/
Device_XImplWrap(PyObject *_self, CppDeviceClass *cl, std::string &st) :
TangoDeviceImpl(cl, st),
PyDeviceImplBase(_self)
{
_init();
}
/**
* Constructor
*
* @param[in] self
* @param[in] cl
* @param[in] name
* @param[in] desc
* @param[in] sta
* @param[in] status
*/
Device_XImplWrap(PyObject *_self,
CppDeviceClass *cl,
const char *name,
const char *_desc = "A Tango device",
Tango::DevState sta = Tango::UNKNOWN,
const char *status = Tango::StatusNotSet) :
TangoDeviceImpl(cl, name, _desc, sta, status),
PyDeviceImplBase(_self)
{
_init();
}
/**
* Destructor
*/
virtual ~Device_XImplWrap()
{
delete_device();
}
/**
* Necessary init_device implementation to call python
*/
virtual void init_device()
{
AutoPythonGIL __py_lock;
try
{
this->get_override("init_device")();
}
catch(bopy::error_already_set &eas)
{
handle_python_exception(eas);
}
}
/**
* Necessary server_init_hook implementation to call python
*/
virtual void server_init_hook(){CALL_DEVICE_METHOD(TangoDeviceImpl, server_init_hook)};
/**
* Executes default server_init_hook implementation
*/
void default_server_init_hook()
{
this->TangoDeviceImpl::server_init_hook();
}
/**
* Necessary delete_device implementation to call python
*/
virtual void delete_device()
{
CALL_DEVICE_METHOD(TangoDeviceImpl, delete_device)
}
/**
* Executes default delete_device implementation
*/
void default_delete_device()
{
this->TangoDeviceImpl::delete_device();
}
/**
* called to ask Python to delete a device by decrementing the Python
* reference count
*/
virtual void delete_dev()
{
// Call here the delete_device method. It is defined in Device_3ImplWrap
// class which is already destroyed when the Tango kernel call the
// delete_device method
try
{
delete_device();
}
catch(Tango::DevFailed &e)
{
Tango::Except::print_exception(e);
}
}
void py_delete_dev()
{
Device_XImplWrap::delete_dev();
PyDeviceImplBase::py_delete_dev();
}
/**
* Necessary always_executed_hook implementation to call python
*/
virtual void always_executed_hook(){CALL_DEVICE_METHOD(TangoDeviceImpl, always_executed_hook)};
/**
* Executes default always_executed_hook implementation
*/
void default_always_executed_hook()
{
this->TangoDeviceImpl::always_executed_hook();
}
/**
* Necessary read_attr_hardware implementation to call python
*/
virtual void read_attr_hardware(std::vector<long> &attr_list){
CALL_DEVICE_METHOD_VARGS(TangoDeviceImpl, read_attr_hardware, attr_list)};
/**
* Executes default read_attr_hardware implementation
*/
void default_read_attr_hardware(std::vector<long> &attr_list)
{
this->TangoDeviceImpl::read_attr_hardware(attr_list);
}
/**
* Necessary write_attr_hardware implementation to call python
*/
virtual void write_attr_hardware(std::vector<long> &attr_list){
CALL_DEVICE_METHOD_VARGS(TangoDeviceImpl, write_attr_hardware, attr_list)};
/**
* Executes default write_attr_hardware implementation
*/
void default_write_attr_hardware(std::vector<long> &attr_list)
{
this->TangoDeviceImpl::write_attr_hardware(attr_list);
}
/**
* Necessary dev_state implementation to call python
*/
virtual Tango::DevState dev_state()
{
CALL_DEVICE_METHOD_RET(TangoDeviceImpl, dev_state)
// Keep the compiler quiet
return Tango::UNKNOWN;
}
/**
* Executes default dev_state implementation
*/
Tango::DevState default_dev_state()
{
return this->TangoDeviceImpl::dev_state();
}
/**
* Necessary dev_status implementation to call python
*/
virtual Tango::ConstDevString dev_status()
{
__AUX_DECL_CALL_DEVICE_METHOD
try
{
if(bopy::override dev_status = this->get_override("dev_status"))
{
this->the_status = bopy::call<const std::string>(dev_status.ptr());
}
else
{
this->the_status = TangoDeviceImpl::dev_status();
}
}
__AUX_CATCH_EXCEPTIONS(dev_status)
return this->the_status.c_str();
}
/**
* Executes default dev_status implementation
*/
Tango::ConstDevString default_dev_status()
{
return this->TangoDeviceImpl::dev_status();
}
/**
* Necessary signal_handler implementation to call python
*/
virtual void signal_handler(long signo)
{
try
{
CALL_DEVICE_METHOD_VARGS(TangoDeviceImpl, signal_handler, signo)
}
catch(Tango::DevFailed &df)
{
long nb_err = df.errors.length();
df.errors.length(nb_err + 1);
df.errors[nb_err].reason = CORBA::string_dup("PyDs_UnmanagedSignalHandlerException");
df.errors[nb_err].desc =
CORBA::string_dup("An unmanaged Tango::DevFailed exception occurred in signal_handler");
TangoSys_OMemStream origin;
origin << TANGO_EXCEPTION_ORIGIN << std::ends;
df.errors[nb_err].origin = CORBA::string_dup(origin.str().c_str());
df.errors[nb_err].severity = Tango::ERR;
Tango::Except::print_exception(df);
}
}
/**
* Executes default signal_handler implementation
*/
void default_signal_handler(long signo)
{
this->TangoDeviceImpl::signal_handler(signo);
}
protected:
/**
* internal method used to initialize the class. Called by the constructors
*/
void _init()
{
// Make sure the wrapper contains a valid pointer to the self
// I found out this is needed by inspecting the boost wrapper_base.hpp code
initialize_wrapper(the_self, this);
}
};
/**
* A wrapper around the Tango::Device_3Impl class
*/
class Device_3ImplWrap : public Device_XImplWrap<Tango::Device_3Impl>
{
public:
Device_3ImplWrap(PyObject *_self, CppDeviceClass *cl, std::string &st) :
Device_XImplWrap(_self, cl, st){};
Device_3ImplWrap(PyObject *_self,
CppDeviceClass *cl,
const char *name,
const char *_desc = "A Tango device",
Tango::DevState sta = Tango::UNKNOWN,
const char *status = Tango::StatusNotSet) :
Device_XImplWrap(_self, cl, name, _desc, sta, status){};
/**
* I do not know why, but boost does link methods from template,
* so we must have them implemented in wrapper (or I do something wrong)
* so I just add simplest realization, which just call the parent method
*/
void default_server_init_hook()
{
Device_XImplWrap<Tango::Device_3Impl>::default_server_init_hook();
}
void default_delete_device()
{
Device_XImplWrap<Tango::Device_3Impl>::default_delete_device();
}
void default_always_executed_hook()
{
Device_XImplWrap<Tango::Device_3Impl>::default_always_executed_hook();
}
void default_read_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_3Impl>::default_read_attr_hardware(attr_list);
}
void default_write_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_3Impl>::default_write_attr_hardware(attr_list);
}
Tango::DevState default_dev_state()
{
return Device_XImplWrap<Tango::Device_3Impl>::default_dev_state();
}
Tango::ConstDevString default_dev_status()
{
return Device_XImplWrap<Tango::Device_3Impl>::default_dev_status();
}
void default_signal_handler(long signo)
{
Device_XImplWrap<Tango::Device_3Impl>::default_signal_handler(signo);
}
};
/**
* A wrapper around the Tango::Device_4Impl class
*/
class Device_4ImplWrap : public Device_XImplWrap<Tango::Device_4Impl>
{
public:
Device_4ImplWrap(PyObject *_self, CppDeviceClass *cl, std::string &st) :
Device_XImplWrap(_self, cl, st){};
Device_4ImplWrap(PyObject *_self,
CppDeviceClass *cl,
const char *name,
const char *_desc = "A Tango device",
Tango::DevState sta = Tango::UNKNOWN,
const char *status = Tango::StatusNotSet) :
Device_XImplWrap(_self, cl, name, _desc, sta, status){};
/**
* I do not know why, but boost does link methods from template,
* so we must have them implemented in wrapper (or I do something wrong)
* so I just add simplest realization, which just call the parent method
*/
void default_server_init_hook()
{
Device_XImplWrap<Tango::Device_4Impl>::default_server_init_hook();
}
void default_delete_device()
{
Device_XImplWrap<Tango::Device_4Impl>::default_delete_device();
}
void default_always_executed_hook()
{
Device_XImplWrap<Tango::Device_4Impl>::default_always_executed_hook();
}
void default_read_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_4Impl>::default_read_attr_hardware(attr_list);
}
void default_write_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_4Impl>::default_write_attr_hardware(attr_list);
}
Tango::DevState default_dev_state()
{
return Device_XImplWrap<Tango::Device_4Impl>::default_dev_state();
}
Tango::ConstDevString default_dev_status()
{
return Device_XImplWrap<Tango::Device_4Impl>::default_dev_status();
}
void default_signal_handler(long signo)
{
Device_XImplWrap<Tango::Device_4Impl>::default_signal_handler(signo);
}
};
/**
* A wrapper around the Tango::Device_5Impl class
*/
class Device_5ImplWrap : public Device_XImplWrap<Tango::Device_5Impl>
{
public:
Device_5ImplWrap(PyObject *_self, CppDeviceClass *cl, std::string &st) :
Device_XImplWrap(_self, cl, st){};
Device_5ImplWrap(PyObject *_self,
CppDeviceClass *cl,
const char *name,
const char *_desc = "A Tango device",
Tango::DevState sta = Tango::UNKNOWN,
const char *status = Tango::StatusNotSet) :
Device_XImplWrap(_self, cl, name, _desc, sta, status){};
/**
* I do not know why, but boost does link methods from template,
* so we must have them implemented in wrapper (or I do something wrong)
* so I just add simplest realization, which just call the parent method
*/
void default_server_init_hook()
{
Device_XImplWrap<Tango::Device_5Impl>::default_server_init_hook();
}
void default_delete_device()
{
Device_XImplWrap<Tango::Device_5Impl>::default_delete_device();
}
void default_always_executed_hook()
{
Device_XImplWrap<Tango::Device_5Impl>::default_always_executed_hook();
}
void default_read_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_5Impl>::default_read_attr_hardware(attr_list);
}
void default_write_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_5Impl>::default_write_attr_hardware(attr_list);
}
Tango::DevState default_dev_state()
{
return Device_XImplWrap<Tango::Device_5Impl>::default_dev_state();
}
Tango::ConstDevString default_dev_status()
{
return Device_XImplWrap<Tango::Device_5Impl>::default_dev_status();
}
void default_signal_handler(long signo)
{
Device_XImplWrap<Tango::Device_5Impl>::default_signal_handler(signo);
}
};
/**
* A wrapper around the Tango::Device_6Impl class
*/
class Device_6ImplWrap : public Device_XImplWrap<Tango::Device_6Impl>
{
public:
Device_6ImplWrap(PyObject *_self, CppDeviceClass *cl, std::string &st) :
Device_XImplWrap(_self, cl, st){};
Device_6ImplWrap(PyObject *_self,
CppDeviceClass *cl,
const char *name,
const char *_desc = "A Tango device",
Tango::DevState sta = Tango::UNKNOWN,
const char *status = Tango::StatusNotSet) :
Device_XImplWrap(_self, cl, name, _desc, sta, status){};
/**
* I do not know why, but boost does link methods from template,
* so we must have them implemented in wrapper (or I do something wrong)
* so I just add simplest realization, which just call the parent method
*/
void default_server_init_hook()
{
Device_XImplWrap<Tango::Device_6Impl>::default_server_init_hook();
}
void default_delete_device()
{
Device_XImplWrap<Tango::Device_6Impl>::default_delete_device();
}
void default_always_executed_hook()
{
Device_XImplWrap<Tango::Device_6Impl>::default_always_executed_hook();
}
void default_read_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_6Impl>::default_read_attr_hardware(attr_list);
}
void default_write_attr_hardware(std::vector<long> &attr_list)
{
Device_XImplWrap<Tango::Device_6Impl>::default_write_attr_hardware(attr_list);
}
Tango::DevState default_dev_state()
{
return Device_XImplWrap<Tango::Device_6Impl>::default_dev_state();
}
Tango::ConstDevString default_dev_status()
{
return Device_XImplWrap<Tango::Device_6Impl>::default_dev_status();
}
void default_signal_handler(long signo)
{
Device_XImplWrap<Tango::Device_6Impl>::default_signal_handler(signo);
}
};
#endif // _DEVICE_IMPL_H
|