Skip to content

Commit d676898

Browse files
committed
Always set ConfigurableParam provenance, add provenance getter
1 parent a1e0cbb commit d676898

File tree

3 files changed

+63
-29
lines changed

3 files changed

+63
-29
lines changed

Common/Utils/include/CommonUtils/ConfigurableParam.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ class ConfigurableParam
143143
/* can add more modes here */
144144
};
145145

146+
enum class EParamUpdateStatus {
147+
Changed, // param was successfully changed
148+
Unchanged, // param was not changed: new value is the same as previous
149+
Failed // failed to update param
150+
};
151+
146152
static std::string toString(EParamProvenance p)
147153
{
148154
static std::array<std::string, 3> names = {"CODE", "CCDB", "RT"};
@@ -155,6 +161,11 @@ class ConfigurableParam
155161
// print the current keys and values to screen (optionally with provenance information)
156162
virtual void printKeyValues(bool showprov = true) const = 0;
157163

164+
// return the provenance of the member key
165+
virtual EParamProvenance getMemberProvenance(const std::string& key) const = 0;
166+
167+
static EParamProvenance getProvenance(const std::string& key);
168+
158169
static void printAllRegisteredParamNames();
159170
static void printAllKeyValuePairs();
160171

@@ -189,7 +200,7 @@ class ConfigurableParam
189200
if (sPtree->get_optional<std::string>(key).is_initialized()) {
190201
sPtree->put(key, x);
191202
auto changed = updateThroughStorageMap(mainkey, subkey, typeid(T), (void*)&x);
192-
if (changed) {
203+
if (changed != EParamUpdateStatus::Failed) {
193204
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
194205
}
195206
}
@@ -210,7 +221,7 @@ class ConfigurableParam
210221
if (sPtree->get_optional<std::string>(key).is_initialized()) {
211222
sPtree->put(key, valuestring);
212223
auto changed = updateThroughStorageMapWithConversion(key, valuestring);
213-
if (changed) {
224+
if (changed != EParamUpdateStatus::Failed) {
214225
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
215226
}
216227
}
@@ -252,8 +263,8 @@ class ConfigurableParam
252263
friend std::ostream& operator<<(std::ostream& out, const ConfigurableParam& me);
253264

254265
static void initPropertyTree();
255-
static bool updateThroughStorageMap(std::string, std::string, std::type_info const&, void*);
256-
static bool updateThroughStorageMapWithConversion(std::string const&, std::string const&);
266+
static EParamUpdateStatus updateThroughStorageMap(std::string, std::string, std::type_info const&, void*);
267+
static EParamUpdateStatus updateThroughStorageMapWithConversion(std::string const&, std::string const&);
257268

258269
virtual ~ConfigurableParam() = default;
259270

Common/Utils/include/CommonUtils/ConfigurableParamHelper.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
8282
return P::sKey;
8383
}
8484

85+
// ----------------------------------------------------------------
86+
// get the provenace of the member with given key
87+
EParamProvenance getMemberProvenance(const std::string& key) const final
88+
{
89+
return getProvenance(getName() + '.' + key);
90+
}
91+
8592
// ----------------------------------------------------------------
8693

8794
// one of the key methods, using introspection to print itself

Common/Utils/src/ConfigurableParam.cxx

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,20 @@ void ConfigurableParam::printAllKeyValuePairs()
313313

314314
// ------------------------------------------------------------------
315315

316+
ConfigurableParam::EParamProvenance ConfigurableParam::getProvenance(const std::string& key)
317+
{
318+
if (!sIsFullyInitialized) {
319+
initialize();
320+
}
321+
auto iter = sValueProvenanceMap->find(key);
322+
if (iter == sValueProvenanceMap->end()) {
323+
throw std::runtime_error(fmt::format("provenace of unknown {:s} parameter is requested", key));
324+
}
325+
return iter->second;
326+
}
327+
328+
// ------------------------------------------------------------------
329+
316330
// evidently this could be a local file or an OCDB server
317331
// ... we need to generalize this ... but ok for demonstration purposes
318332
void ConfigurableParam::toCCDB(std::string filename)
@@ -612,24 +626,24 @@ bool isMemblockDifferent(void const* block1, void const* block2)
612626
// copies data from one place to other and returns
613627
// true of data was actually changed
614628
template <typename T>
615-
bool Copy(void const* addr, void* targetaddr)
629+
ConfigurableParam::EParamUpdateStatus Copy(void const* addr, void* targetaddr)
616630
{
617631
if (isMemblockDifferent<T>(addr, targetaddr)) {
618632
std::memcpy(targetaddr, addr, sizeof(T));
619-
return true;
633+
return ConfigurableParam::EParamUpdateStatus::Changed;
620634
}
621-
return false;
635+
return ConfigurableParam::EParamUpdateStatus::Unchanged;
622636
}
623637

624-
bool ConfigurableParam::updateThroughStorageMap(std::string mainkey, std::string subkey, std::type_info const& tinfo,
625-
void* addr)
638+
ConfigurableParam::EParamUpdateStatus ConfigurableParam::updateThroughStorageMap(std::string mainkey, std::string subkey, std::type_info const& tinfo,
639+
void* addr)
626640
{
627641
// check if key_exists
628642
auto key = mainkey + "." + subkey;
629643
auto iter = sKeyToStorageMap->find(key);
630644
if (iter == sKeyToStorageMap->end()) {
631645
LOG(WARN) << "Cannot update parameter " << key << " not found";
632-
return false;
646+
return ConfigurableParam::EParamUpdateStatus::Failed;
633647
}
634648

635649
// the type we need to convert to
@@ -638,7 +652,7 @@ bool ConfigurableParam::updateThroughStorageMap(std::string mainkey, std::string
638652
// check that type matches
639653
if (iter->second.first != tinfo) {
640654
LOG(WARN) << "Types do not match; cannot update value";
641-
return false;
655+
return ConfigurableParam::EParamUpdateStatus::Failed;
642656
}
643657

644658
auto targetaddress = iter->second.second;
@@ -749,72 +763,74 @@ bool ConfigurableParam::updateThroughStorageMap(std::string mainkey, std::string
749763
break;
750764
}
751765
}
752-
return false;
766+
return ConfigurableParam::EParamUpdateStatus::Failed;
753767
}
754768

755769
template <typename T>
756-
bool ConvertAndCopy(std::string const& valuestring, void* targetaddr)
770+
ConfigurableParam::EParamUpdateStatus ConvertAndCopy(std::string const& valuestring, void* targetaddr)
757771
{
758772
auto addr = boost::lexical_cast<T>(valuestring);
759773
if (isMemblockDifferent<T>(targetaddr, (void*)&addr)) {
760774
std::memcpy(targetaddr, (void*)&addr, sizeof(T));
761-
return true;
775+
return ConfigurableParam::EParamUpdateStatus::Changed;
762776
}
763-
return false;
777+
return ConfigurableParam::EParamUpdateStatus::Unchanged;
764778
}
779+
765780
// special version for std::string
766781
template <>
767-
bool ConvertAndCopy<std::string>(std::string const& valuestring, void* targetaddr)
782+
ConfigurableParam::EParamUpdateStatus ConvertAndCopy<std::string>(std::string const& valuestring, void* targetaddr)
768783
{
769784
std::string& target = *((std::string*)targetaddr);
770785
if (target.compare(valuestring) != 0) {
771786
// the targetaddr is a std::string to which we can simply assign
772787
// and all the magic will happen internally
773788
target = valuestring;
774-
return true;
789+
return ConfigurableParam::EParamUpdateStatus::Changed;
775790
}
776-
return false;
791+
return ConfigurableParam::EParamUpdateStatus::Unchanged;
777792
}
778793
// special version for char and unsigned char since we are interested in the numeric
779794
// meaning of char as an 8-bit integer (boost lexical cast is assigning the string as a character i// nterpretation
780795
template <>
781-
bool ConvertAndCopy<char>(std::string const& valuestring, void* targetaddr)
796+
ConfigurableParam::EParamUpdateStatus ConvertAndCopy<char>(std::string const& valuestring, void* targetaddr)
782797
{
783798
int intvalue = boost::lexical_cast<int>(valuestring);
784799
if (intvalue > std::numeric_limits<char>::max() || intvalue < std::numeric_limits<char>::min()) {
785800
LOG(ERROR) << "Cannot assign " << valuestring << " to a char variable";
786-
return false;
801+
return ConfigurableParam::EParamUpdateStatus::Failed;
787802
}
788803
char addr = intvalue;
789804
if (isMemblockDifferent<char>(targetaddr, (void*)&addr)) {
790805
std::memcpy(targetaddr, (void*)&addr, sizeof(char));
791-
return true;
806+
return ConfigurableParam::EParamUpdateStatus::Changed;
792807
}
793-
return false;
808+
return ConfigurableParam::EParamUpdateStatus::Unchanged;
794809
}
810+
795811
template <>
796-
bool ConvertAndCopy<unsigned char>(std::string const& valuestring, void* targetaddr)
812+
ConfigurableParam::EParamUpdateStatus ConvertAndCopy<unsigned char>(std::string const& valuestring, void* targetaddr)
797813
{
798814
unsigned int intvalue = boost::lexical_cast<int>(valuestring);
799815
if (intvalue > std::numeric_limits<unsigned char>::max() || intvalue < std::numeric_limits<unsigned char>::min()) {
800816
LOG(ERROR) << "Cannot assign " << valuestring << " to an unsigned char variable";
801-
return false;
817+
return ConfigurableParam::EParamUpdateStatus::Failed;
802818
}
803819
unsigned char addr = intvalue;
804820
if (isMemblockDifferent<unsigned char>(targetaddr, (void*)&addr)) {
805821
std::memcpy(targetaddr, (void*)&addr, sizeof(unsigned char));
806-
return true;
822+
return ConfigurableParam::EParamUpdateStatus::Changed;
807823
}
808-
return false;
824+
return ConfigurableParam::EParamUpdateStatus::Unchanged;
809825
}
810826

811-
bool ConfigurableParam::updateThroughStorageMapWithConversion(std::string const& key, std::string const& valuestring)
827+
ConfigurableParam::EParamUpdateStatus ConfigurableParam::updateThroughStorageMapWithConversion(std::string const& key, std::string const& valuestring)
812828
{
813829
// check if key_exists
814830
auto iter = sKeyToStorageMap->find(key);
815831
if (iter == sKeyToStorageMap->end()) {
816832
LOG(WARN) << "Cannot update parameter " << key << " (parameter not found) ";
817-
return false;
833+
return ConfigurableParam::EParamUpdateStatus::Failed;
818834
}
819835

820836
auto targetaddress = iter->second.second;
@@ -936,7 +952,7 @@ bool ConfigurableParam::updateThroughStorageMapWithConversion(std::string const&
936952
break;
937953
}
938954
}
939-
return false;
955+
return ConfigurableParam::EParamUpdateStatus::Failed;
940956
}
941957

942958
} // namespace conf

0 commit comments

Comments
 (0)