From e7231b3a09538500bdd93e9cddece76900cddc86 Mon Sep 17 00:00:00 2001 From: MALFREYT <alexandre.malfreyt@synchrotron-soleil.fr> Date: Wed, 23 Apr 2025 14:30:03 +0200 Subject: [PATCH] refactor: improve clarity by removing redundancy in get_device_property() for default properties values --- src/SingleShotAO.cpp | 59 ++++++++++++++++++++------------------------ src/SingleShotAO.h | 20 +++++++++++++-- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/SingleShotAO.cpp b/src/SingleShotAO.cpp index bea9ea5..5c9244e 100755 --- a/src/SingleShotAO.cpp +++ b/src/SingleShotAO.cpp @@ -670,6 +670,7 @@ void SingleShotAO::get_device_property() dev_prop.push_back(Tango::DbDatum("EnableRamps")); dev_prop.push_back(Tango::DbDatum("OutputMemorizedChannelsAtInit")); + // Call database and extract values //-------------------------------------------- if (Tango::Util::instance()->_UseDb==true) @@ -677,7 +678,7 @@ void SingleShotAO::get_device_property() get_db_device()->get_property(dev_prop); } - Tango::DbDatum def_prop, cl_prop; + Tango::DbDatum cl_prop; SingleShotAOClass *ds_class = (static_cast<SingleShotAOClass *>(get_device_class())); int i = -1; @@ -687,8 +688,9 @@ void SingleShotAO::get_device_property() if (cl_prop.is_empty()==false) cl_prop >> boardNum; else { // Try to initialize BoardNum from default device value - def_prop = ds_class->get_default_device_property(dev_prop[i].name); - if (def_prop.is_empty()==false) def_prop >> boardNum; + boardNum_default = ds_class->get_default_device_property(dev_prop[i].name); + if (boardNum_default.is_empty() == false) + boardNum_default >> boardNum; } // And try to extract BoardNum value from database if (dev_prop[i].is_empty()==false) dev_prop[i] >> boardNum; @@ -700,8 +702,9 @@ void SingleShotAO::get_device_property() if (cl_prop.is_empty()==false) cl_prop >> boardType; else { // Try to initialize BoardType from default device value - def_prop = ds_class->get_default_device_property(dev_prop[i].name); - if (def_prop.is_empty()==false) def_prop >> boardType; + boardType_default = ds_class->get_default_device_property(dev_prop[i].name); + if (boardType_default.is_empty() == false) + boardType_default >> boardType; } // And try to extract BoardType value from database if (dev_prop[i].is_empty()==false) dev_prop[i] >> boardType; @@ -713,8 +716,9 @@ void SingleShotAO::get_device_property() if (cl_prop.is_empty() == false) cl_prop >> enableRamps; else { // Try to initialize EnableRamps from default device value - def_prop = ds_class->get_default_device_property(dev_prop[i].name); - if (def_prop.is_empty() == false) def_prop >> enableRamps; + enableRamps_default = ds_class->get_default_device_property(dev_prop[i].name); + if (enableRamps_default.is_empty() == false) + enableRamps_default >> enableRamps; } // And try to extract EnableRamps value from database if (dev_prop[i].is_empty() == false) dev_prop[i] >> enableRamps; @@ -726,22 +730,23 @@ void SingleShotAO::get_device_property() if (cl_prop.is_empty() == false) cl_prop >> outputMemorizedChannelsAtInit; else { // Try to initialize OutputMemorizedChannelsAtInit from default device value - def_prop = ds_class->get_default_device_property(dev_prop[i].name); - if (def_prop.is_empty() == false) def_prop >> outputMemorizedChannelsAtInit; + outputMemorizedChannelsAtInit_default = ds_class->get_default_device_property(dev_prop[i].name); + if (outputMemorizedChannelsAtInit_default.is_empty() == false) + outputMemorizedChannelsAtInit_default >> outputMemorizedChannelsAtInit; } // And try to extract OutputMemorizedChannelsAtInit value from database if (dev_prop[i].is_empty() == false) dev_prop[i] >> outputMemorizedChannelsAtInit; INFO_STREAM << "OutputMemorizedChannelsAtInit parsed: " << (outputMemorizedChannelsAtInit ? "true" : "false") << endl; + // Create properties if empty and set default values //-------------------------------------------- DEBUG_STREAM << "Creating properties if empty" << endl; - // boardNum, boardType, enableRamps and outputMemorizedChannelsAtInit variables - // contain the default values at this point if the properties are not set in the database. - yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, boardNum, "BoardNum"); - yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, boardType, "BoardType"); - yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, enableRamps, "EnableRamps"); - yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, outputMemorizedChannelsAtInit, "OutputMemorizedChannelsAtInit"); + yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, boardNum_default, "BoardNum"); + yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, boardType_default, "BoardType"); + yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, enableRamps_default, "EnableRamps"); + yat4tango::PropertyHelper::create_property_if_empty(this, dev_prop, outputMemorizedChannelsAtInit_default, "OutputMemorizedChannelsAtInit"); + // Check critical properties being valid //-------------------------------------------- @@ -752,15 +757,9 @@ void SingleShotAO::get_device_property() //- <BoardNum> ----------------------- // Case 1 (error): boardNum is set to the default value in the database - // (compare the current value with the default value) - Tango::DbDatum &boardNumDatum = Tango::DbDatum("BoardNum"); - std::string defaultBoardNum_str; - def_prop = ds_class->get_default_device_property(boardNumDatum.name); - if (def_prop.is_empty() == false) def_prop >> defaultBoardNum_str; - Tango::DevShort defaultBoardNum = atoi(defaultBoardNum_str.c_str()); - if (boardNum == defaultBoardNum) - { - errorMessages << "Device property <BoardNum> is not set (default value " << defaultBoardNum << " needs to be replaced)" << endl; + if (boardNum == boardNum_default) + { + errorMessages << "Device property <BoardNum> is not set (default value " << boardNum_default << " needs to be replaced)" << endl; critical_properties_missing = true; } // Case 2 (error): boardNum is set to a value outside the valid range @@ -778,14 +777,9 @@ void SingleShotAO::get_device_property() //- <BoardType> ----------------------- // Case 1 (error): boardType is set to the default value in the database - // (compare the current value with the default value) - Tango::DbDatum &boardTypeDatum = Tango::DbDatum("BoardType"); - std::string defaultBoardType; - def_prop = ds_class->get_default_device_property(boardTypeDatum.name); - if (def_prop.is_empty() == false) def_prop >> defaultBoardType; - if (boardType == defaultBoardType) - { - errorMessages << "Device property <BoardType> is not set (default value " << defaultBoardType << " needs to be replaced)" << endl; + if (boardType == boardType_default) + { + errorMessages << "Device property <BoardType> is not set (default value " << boardType_default << " needs to be replaced)" << endl; critical_properties_missing = true; } // Case 2 (ok): boardType is set to a valid @@ -810,6 +804,7 @@ void SingleShotAO::get_device_property() critical_properties_missing = true; } + // If any critical property is missing or invalid, throw an exception if (critical_properties_missing) { diff --git a/src/SingleShotAO.h b/src/SingleShotAO.h index 17e2b74..2d680ca 100755 --- a/src/SingleShotAO.h +++ b/src/SingleShotAO.h @@ -121,10 +121,26 @@ public : //@} /** - * @name Device properties - * Device property member data. + * @name Device properties default values + * Device properties default values. */ //@{ +/** + * Default value for boardNum property + */ + Tango::DevShort boardNum_default; +/** + * Default value for boardType property + */ + string boardType_default; +/** + * Default value for enableRamps property + */ + Tango::DevBoolean enableRamps_default; +/** + * Default value for outputMemorizedChannelsAtInit property + */ + Tango::DevBoolean outputMemorizedChannelsAtInit_default; //@} /**@name Constructors -- GitLab