Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 10:27:22

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #pragma once
0006 
0007 #include <limits>
0008 #include <string>
0009 #include <algorithm>
0010 #include <vector>
0011 #include <array>
0012 #include <map>
0013 #include <cmath>
0014 #include <iomanip>
0015 #include <cstring>
0016 
0017 #include <JANA/JLogger.h>
0018 #include <JANA/JException.h>
0019 #include <JANA/Services/JServiceLocator.h>
0020 #include <JANA/Utils/JEventLevel.h>
0021 
0022 class JParameter {
0023 
0024     std::string m_name;             // A token (no whitespace, colon-prefixed), e.g. "my_plugin:use_mc"
0025     std::string m_value;            // Stringified value, e.g. "1". Can handle comma-separated vectors of strings e.g. "abc, def"
0026     std::string m_default_value;    // Optional default value, which may or may not equal value
0027     std::string m_description;      // One-liner
0028 
0029     bool m_has_default = false;     // Indicates that a default value is present. Does not indicate whether said value is in use.
0030                                     //   We need this because "" is a valid default value.
0031     bool m_is_default = false;      // Indicates that we are actually using this default value.
0032                                     //   We need this because we want to know if the user provided a value that happened to match the default value.
0033     bool m_is_deprecated = false;   // This lets us print a warning if a user provides a deprecated parameter,
0034                                     //   It also lets us suppress printing this parameter in the parameter list.
0035     bool m_is_advanced = false;       // Some JANA parameters control internal details that aren't part of the contract between JANA and its users.
0036                                     //   We want to differentiate these from the parameters that users are meant to control and understand.
0037     bool m_is_used = false;         // If a parameter hasn't been used, it probably contains a typo, and we should warn the user.
0038 
0039     bool m_is_conflicted = false;   // Whether or not this parameter has been registered with inconsistent default values
0040 
0041 public:
0042 
0043     JParameter(std::string key, std::string value, std::string defaultValue, std::string description, bool hasDefault, bool isDefault)
0044     : m_name(std::move(key)),
0045       m_value(std::move(value)),
0046       m_default_value(std::move(defaultValue)),
0047       m_description(std::move(description)),
0048       m_has_default(hasDefault),
0049       m_is_default(isDefault) {}
0050 
0051     inline const std::string& GetKey() const { return m_name; }
0052     inline const std::string& GetValue() const { return m_value; }
0053     inline const std::string& GetDefault() const { return m_default_value; }
0054     inline const std::string& GetDescription() const { return m_description; }
0055 
0056     inline bool HasDefault() const { return m_has_default; }
0057     inline bool IsDefault() const { return m_is_default; }
0058     inline bool IsAdvanced() const { return m_is_advanced; }
0059     inline bool IsUsed() const { return m_is_used; }
0060     inline bool IsDeprecated() const { return m_is_deprecated; }
0061     inline bool IsConflicted() const { return m_is_conflicted; }
0062 
0063     inline void SetKey(std::string key) { m_name = std::move(key); }
0064     inline void SetValue(std::string val) { m_value = std::move(val); }
0065     inline void SetDefault(std::string defaultValue) { m_default_value = std::move(defaultValue); }
0066     inline void SetDescription(std::string desc) { m_description = std::move(desc); }
0067     inline void SetHasDefault(bool hasDefault) { m_has_default = hasDefault; }
0068     inline void SetIsDefault(bool isDefault) { m_is_default = isDefault; }
0069     inline void SetIsAdvanced(bool isHidden) { m_is_advanced = isHidden; }
0070     inline void SetIsUsed(bool isUsed) { m_is_used = isUsed; }
0071     inline void SetIsDeprecated(bool isDeprecated) { m_is_deprecated = isDeprecated; }
0072     inline void SetIsConflicted(bool isConflicted) { m_is_conflicted = isConflicted; }
0073 
0074 };
0075 
0076 class JParameterManager : public JService {
0077 public:
0078 
0079     JParameterManager();
0080 
0081     JParameterManager(const JParameterManager&);
0082 
0083     ~JParameterManager() override;
0084 
0085     inline void SetLogger(JLogger logger) { m_logger = logger; }
0086 
0087     bool Exists(std::string name);
0088 
0089     JParameter* FindParameter(std::string);
0090 
0091     void PrintParameters();
0092     
0093     void PrintParameters(int verbosity, int strictness);
0094 
0095     std::map<std::string, JParameter*> GetAllParameters();
0096 
0097     template<typename T>
0098     JParameter* GetParameter(std::string name, T& val);
0099 
0100     template<typename T>
0101     T GetParameterValue(std::string name);
0102 
0103     template<typename T>
0104     JParameter* SetParameter(std::string name, T val);
0105 
0106     template<typename T>
0107     JParameter* SetDefaultParameter(std::string name, T& val, std::string description = "");
0108 
0109     template <typename T>
0110     T RegisterParameter(std::string name, const T default_val, std::string description = "");
0111 
0112     void FilterParameters(std::map<std::string,std::string> &parms, std::string filter="");
0113 
0114     void ReadConfigFile(std::string name);
0115 
0116     void WriteConfigFile(std::string name);
0117 
0118     template<typename T>
0119     static inline void Parse(const std::string& value, T& out);
0120 
0121     template<typename T>
0122     static inline void Parse(const std::string& value, std::vector<T>& out);
0123 
0124     template<typename T, size_t arrSize>
0125     static inline void Parse(const std::string& value, std::array<T,arrSize>& out); 
0126 
0127     template<size_t arrSize>
0128     static inline void Parse(const std::string& value, std::array<std::string,arrSize>& out);
0129 
0130 
0131     /*
0132     Template specialization done for double and float numbers in order to match the 
0133     precision of the number with the string
0134     */
0135     template<typename T>
0136     static std::string Stringify(const T& value);
0137 
0138     template<typename T>
0139     static std::string Stringify(const std::vector<T>& values);
0140 
0141     template<typename T,size_t N>
0142     static std::string Stringify(const std::array<T,N>& values);
0143  
0144     template<size_t N>
0145     static std::string Stringify(const std::array<std::string,N>& values);
0146 
0147     template<typename T>
0148     static bool Equals(const T& lhs, const T& rhs);
0149 
0150 
0151     static std::string ToLower(const std::string& name);
0152 
0153     JLogger GetLogger(const std::string& prefix);
0154 
0155 private:
0156 
0157     std::map<std::string, JParameter*> m_parameters;
0158 
0159     int m_strictness = 1;
0160     int m_verbosity = 1;
0161 
0162     std::mutex m_mutex;
0163 };
0164 
0165 
0166 
0167 /// @brief Retrieves a JParameter and stores its value
0168 ///
0169 /// @param [in] name    The name of the parameter to retrieve
0170 /// @param [out] val    Reference to where the parameter value should be stored
0171 /// @returns            The corresponding JParameter representation, which provides us with
0172 ///                     additional information such as the default value, or nullptr if not found.
0173 ///
0174 template<typename T>
0175 JParameter* JParameterManager::GetParameter(std::string name, T& val) {
0176 
0177     auto result = m_parameters.find(ToLower(name));
0178     if (result == m_parameters.end()) {
0179         return nullptr;
0180     }
0181     Parse(result->second->GetValue(),val);
0182     result->second->SetIsUsed(true);
0183     return result->second;
0184 }
0185 
0186 
0187 /// @brief Retrieves a parameter value
0188 ///
0189 /// @param [in] name    The name of the parameter to retrieve
0190 /// @returns            The parameter value
0191 /// @throws JException  in case the parameter is not found
0192 ///
0193 template<typename T>
0194 T JParameterManager::GetParameterValue(std::string name) {
0195     T t;
0196     auto result = m_parameters.find(ToLower(name));
0197     if (result == m_parameters.end()) {
0198         throw JException("Unknown parameter \"%s\"", name.c_str());
0199     }
0200     result->second->SetIsUsed(true);
0201     Parse(result->second->GetValue(),t);
0202     return t;
0203 }
0204 
0205 
0206 /// @brief Sets a configuration parameter
0207 /// @param [in] name        The parameter name
0208 /// @param [in] val         The parameter value. This may be typed, or it may be a string.
0209 /// @return                 Pointer to the JParameter that was either created or updated.
0210 ///                         Note that this is owned by this JParameterManager, so do not delete.
0211 template<typename T>
0212 JParameter* JParameterManager::SetParameter(std::string name, T val) {
0213 
0214     auto result = m_parameters.find(ToLower(name));
0215 
0216     if (result == m_parameters.end()) {
0217         auto* param = new JParameter {name, Stringify(val), "", "", false, false};
0218         m_parameters[ToLower(name)] = param;
0219         return param;
0220     }
0221     result->second->SetValue(Stringify(val));
0222     result->second->SetIsDefault(false);
0223     return result->second;
0224 }
0225 
0226 
0227 
0228 /// @brief Retrieve a configuration parameter, if necessary creating it with the provided default value
0229 ///
0230 /// @param [in] name            The parameter name. Must not contain spaces.
0231 /// @param [in,out] val         Reference to a variable which has been set to the desired default value.
0232 /// @param [in] description     Optional description, e.g. units, set or range of valid values, etc.
0233 ///
0234 /// @details Upon entry, the value in "val" should be set to the desired default value. It
0235 /// will be overwritten if a value for the parameter already exists because
0236 /// it was given by the user either on the command line or in a configuration
0237 /// file. If the parameter does not already exist, it is created and its value set
0238 /// to that of "val". Upon exit, "val" will always contain the value that should be used
0239 /// for event processing.
0240 ///
0241 /// If a parameter with the given name already exists, it will be checked to see
0242 /// if the parameter already has a default value assigned (this is kept separate
0243 /// from the actual value of the parameter used and is maintained purely for
0244 /// bookkeeping purposes). If it does not have a default value, then the value
0245 /// of "val" upon entry is saved as the default. If it does have a default, then
0246 /// the value of the default is compared to the value of "val" upon entry. If the
0247 /// two do not match, then a warning message is printed to indicate to the user
0248 /// that two different default values are being set for this parameter.
0249 ///
0250 /// Parameters specified on the command line using the "-Pkey=value" syntax will
0251 /// not have a default value at the time the parameter is created.
0252 ///
0253 template<typename T>
0254 JParameter* JParameterManager::SetDefaultParameter(std::string name, T& val, std::string description) {
0255 
0256     std::lock_guard<std::mutex> lock(m_mutex);
0257     JParameter* param = nullptr;
0258     T t;
0259     auto result = m_parameters.find(ToLower(name));
0260     if (result != m_parameters.end()) {
0261         // We already have a value stored for this parameter
0262         param = result->second;
0263 
0264         if (!param->HasDefault()) {
0265             // We don't have a default value yet. Our existing value is a non-default value.
0266             param->SetHasDefault(true);
0267             param->SetDefault(Stringify(val));
0268             param->SetDescription(std::move(description));
0269         }
0270         else {
0271             // We tried to set the same default parameter twice. This is fine; this happens all the time
0272             // because we construct lots of copies of JFactories, each of which calls SetDefaultParameter on its own.
0273             // However, we still want to warn the user if the same parameter was declared with different values.
0274             Parse(param->GetDefault(),t);
0275             if (!Equals(val, t)) {
0276                 if (!param->IsConflicted()) {
0277                     // Only show this warning once per parameter
0278                     LOG_WARN(m_logger) << "Parameter '" << name << "' has conflicting defaults: '"
0279                                     << Stringify(val) << "' vs '" << param->GetDefault() << "'"
0280                                     << LOG_END;
0281                     param->SetIsConflicted(true);
0282                 }
0283                 // If we tried to set the same default parameter twice with different values, and there is no
0284                 // existing non-default value, we remember the _latest_ default value. This way, we return the
0285                 // default provided by the caller, instead of the default provided by the mysterious interloper.
0286                 param->SetDefault(Stringify(val));
0287             }
0288             if (param->IsDefault()) {
0289                 param->SetValue(Stringify(val)); // Use _latest_ default value
0290             }
0291         }
0292     }
0293     else {
0294         // We are storing a value for this parameter for the first time
0295         auto valstr = Stringify(val);
0296         param = new JParameter {name, valstr, valstr, std::move(description), true, true};
0297 
0298         // Test whether parameter is one-to-one with its string representation.
0299         // If not, warn the user they may have a problem.
0300         
0301         Parse(valstr,t);
0302         if (!Equals(val, t)) {
0303             LOG_WARN(m_logger) << "Parameter '" << name << "' with value '" << valstr
0304                                << "' loses equality with itself after stringification" << LOG_END;
0305         }
0306         m_parameters[ToLower(name)] = param;
0307     }
0308 
0309     // Always put val through the stringification/parsing cycle to be consistent with
0310     // values passed in from config file, accesses from other threads
0311     Parse(param->GetValue(),t);
0312     val = t;
0313     param->SetIsUsed(true);
0314     return param;
0315 }
0316 
0317 /// @brief Retrieve a configuration parameter, if necessary creating it with the provided default value.
0318 ///
0319 /// @param [in] name            The parameter name. Must not contain spaces.
0320 /// @param [in,out] default_val Value to set the desired default value to (returned if no value yet set for parameter).
0321 /// @param [in] description     Optional description, e.g. units, set or range of valid values, etc.
0322 /// @return                     Current value of parameter
0323 ///
0324 /// @details This is a convenience method that wraps SetDefaultParameter. The difference
0325 /// is that this has the default passed by value (not by reference) and the value of the parameter is returned
0326 /// by value. This allows a slightly different form for declaring configuration parameters with a default value.
0327 /// e.g.
0328 ///     auto thresh = jpp->RegisterParameter("SystemA:threshold", 1.3, "threshold in MeV");
0329 ///
0330 template <typename T>
0331 inline T JParameterManager::RegisterParameter(std::string name, const T default_val, std::string description){
0332     T val = default_val;
0333     SetDefaultParameter(name.c_str(), val, description);
0334     return val;
0335 }
0336 
0337 
0338 #if __cplusplus >= 201703L
0339 /// @brief Basic implementation of Parse for C++17 and newer. Provides a helpful error message when attempting to parse a type that doesn't come with a stream operator.
0340 template <typename T>
0341 void JParameterManager::Parse(const std::string& s, T& out) {
0342     constexpr bool parseable = JTypeInfo::is_parseable<T>::value;
0343     static_assert(parseable, "Type is not automatically parseable by JParameterManager. To use, provide a template specialization for JParameterManager::Parse(std::string in, T& out).");
0344     if constexpr (parseable) {
0345         std::stringstream ss(s);
0346         ss >> out;
0347     }
0348 }
0349 #else
0350 /// @brief Basic implementation of Parse for C++14 and earlier.
0351 template <typename T>
0352 void JParameterManager::Parse(const std::string& s, T& out) {
0353     std::stringstream ss(s);
0354     ss >> out;
0355 }
0356 #endif
0357 
0358 
0359 /// @brief Specialization for string. 
0360 /// The stream operator is not only redundant here, but it also splits the string (see Issue #191)
0361 template <>
0362 inline void JParameterManager::Parse(const std::string& value, std::string& out) {
0363     out = value;
0364 }
0365 
0366 
0367 /// @brief Specialization for bool
0368 template <>
0369 inline void JParameterManager::Parse(const std::string& value, bool& val) {
0370     if (value == "0" || value =="false" || value == "off") val = false;
0371     else if (value == "1" || value == "true" || value == "on") val = true;
0372     else throw JException("'%s' not parseable as bool", value.c_str());
0373 }
0374 
0375 // @brief Template to parse a string and return in an array
0376 template<typename T, size_t N>
0377 inline void JParameterManager::Parse(const std::string& value,std::array<T,N> &val) {
0378     std::string s;
0379     std::stringstream ss(value);
0380     int indx = 0;
0381     while (getline(ss, s, ',')) {
0382         T t;
0383         Parse(s, t);
0384         val[indx++]= t;
0385     }
0386 }
0387 
0388 // @brief Template to parse a string and return in an array of strings
0389 template<size_t N>
0390 inline void JParameterManager::Parse(const std::string& value,std::array<std::string,N> &val) {
0391     std::string s;
0392     std::stringstream ss(value);
0393     std::string temp;   // creating a temp var allows us to store the string s where the escape character was used
0394     int indx = 0;
0395     while (getline(ss, s, ',')) {
0396         std::string t;
0397         if (s.back() == '\\') {
0398             s.pop_back();
0399             temp += s + ',';
0400             continue;
0401         }
0402         if (!temp.empty()) {
0403             Parse(temp + s, t);
0404             val[indx++]= t;
0405             temp.clear();
0406         }
0407         else {
0408             Parse(s, t);
0409             val[indx++]= t;
0410         }
0411     }
0412 }
0413 
0414 /// @brief Specialization for std::vector<std::string>
0415 template<typename T>
0416 inline void JParameterManager::Parse(const std::string& value, std::vector<T> &val) {
0417     std::stringstream ss(value);
0418     std::string s;
0419     val.clear(); // clearing the input vector to ensure no dulication which can be caused due to val.push_back(t);
0420     while (getline(ss, s, ',')) {
0421         T t;
0422         Parse(s, t);
0423         val.push_back(t);
0424     }
0425 }
0426 
0427 /// @brief Specialization for std::vector<std::string> with escape commas
0428 template<>
0429 inline void JParameterManager::Parse(const std::string& value, std::vector<std::string> &val) {
0430     std::stringstream ss(value);
0431     std::string s;
0432     val.clear(); // clearing the input vector to ensure no dulication which can be caused due to val.push_back(t);
0433     std::string temp;   // creating a temp var allows us to store the string s where the escape character was used
0434     while (getline(ss, s, ',')) {
0435         std::string t;
0436         if (s.back() == '\\') {
0437             s.pop_back();
0438             temp += s + ',';
0439             continue;
0440         }
0441         if (!temp.empty()) {
0442             Parse(temp + s, t);
0443             val.push_back(t);
0444             temp.clear();
0445         }
0446         else {
0447             Parse(s, t);
0448             val.push_back(t);
0449         }
0450     }
0451 }
0452 
0453 /// @brief Specialization for JLogger::Level enum
0454 template <>
0455 inline void JParameterManager::Parse(const std::string& in, JLogger::Level& out) {
0456     std::string token(in);
0457     std::transform(in.begin(), in.end(), token.begin(), ::tolower);
0458     if (std::strcmp(token.c_str(), "trace") == 0) { 
0459         out = JLogger::Level::TRACE;
0460     }
0461     else if (std::strcmp(token.c_str(), "debug") == 0) { 
0462         out = JLogger::Level::DEBUG;
0463     }
0464     else if (std::strcmp(token.c_str(), "info") == 0) { 
0465         out = JLogger::Level::INFO;
0466     }
0467     else if (std::strcmp(token.c_str(), "warn") == 0) { 
0468         out = JLogger::Level::WARN;
0469     }
0470     else if (std::strcmp(token.c_str(), "warning") == 0) { 
0471         out = JLogger::Level::WARN;
0472     }
0473     else if (std::strcmp(token.c_str(), "error") == 0) { 
0474         out = JLogger::Level::ERROR;
0475     }
0476     else if (std::strcmp(token.c_str(), "err") == 0) { 
0477         out = JLogger::Level::ERROR;
0478     }
0479     else if (std::strcmp(token.c_str(), "fatal") == 0) { 
0480         out = JLogger::Level::FATAL;
0481     }
0482     else if (std::strcmp(token.c_str(), "critical") == 0) { 
0483         out = JLogger::Level::FATAL;
0484     }
0485     else if (std::strcmp(token.c_str(), "off") == 0) { 
0486         out = JLogger::Level::OFF;
0487     }
0488     else {
0489         throw JException("Unable to parse log level: '%s'. Options are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF", in.c_str());
0490     }
0491 }
0492 
0493 template<>
0494 inline void JParameterManager::Parse(const std::string& in, JEventLevel& out) {
0495     std::string token(in);
0496     std::transform(in.begin(), in.end(), token.begin(), ::tolower);
0497     if (std::strcmp(token.c_str(), "run") == 0) { 
0498         out = JEventLevel::Run;
0499     }
0500     else if (std::strcmp(token.c_str(), "subrun") == 0) { 
0501         out = JEventLevel::Subrun;
0502     }
0503     else if (std::strcmp(token.c_str(), "timeframe") == 0) { 
0504         out = JEventLevel::Timeframe;
0505     }
0506     else if (std::strcmp(token.c_str(), "timeslice") == 0) { 
0507         out = JEventLevel::Timeslice;
0508     }
0509     else if (std::strcmp(token.c_str(), "block") == 0) { 
0510         out = JEventLevel::Block;
0511     }
0512     else if (std::strcmp(token.c_str(), "slowcontrols") == 0) { 
0513         out = JEventLevel::SlowControls;
0514     }
0515     else if (std::strcmp(token.c_str(), "physicsevent") == 0) { 
0516         out = JEventLevel::PhysicsEvent;
0517     }
0518     else if (std::strcmp(token.c_str(), "subevent") == 0) { 
0519         out = JEventLevel::Subevent;
0520     }
0521     else if (std::strcmp(token.c_str(), "task") == 0) { 
0522         out = JEventLevel::Task;
0523     }
0524     else {
0525         throw JException("Unable to parse event level: '%s'. Options are: Run, Subrun, Timeframe, Timeslice, Block, SlowControls, PhysicsEvent, Subevent, Task", in.c_str());
0526     }
0527 }
0528 
0529 #if __cplusplus >= 201703L
0530 /// @brief Basic implementation of Stringify for C++17 and newer. Provides a helpful error message when attempting to stringify a type that doesn't come with a stream operator.
0531 template <typename T>
0532 inline std::string JParameterManager::Stringify(const T& value) {
0533     constexpr bool serializable = JTypeInfo::is_serializable<T>::value;
0534     static_assert(serializable, "Type is not automatically serializable by JParameterManager. To use, provide a template specialization for JParameterManager::Stringify(const T& val) -> std::string.");
0535     if constexpr (serializable) {
0536         std::stringstream ss;
0537         ss << value;
0538         return ss.str();
0539     }
0540 }
0541 #else
0542 /// @brief Basic implementation of Parse for C++14 and earlier.
0543 template <typename T>
0544 inline std::string JParameterManager::Stringify(const T& value) {
0545     std::stringstream ss;
0546     ss << value;
0547     return ss.str();
0548 }
0549 #endif
0550 
0551 
0552 template <>
0553 inline std::string JParameterManager::Stringify(const float& value) {
0554     std::stringstream ss;
0555     // Use enough precision to make sure that the "float -> string -> float" roundtrip is exact. 
0556     // The stringstream << operator is smart enough to give us a clean representation when one is available.
0557     ss << std::setprecision(std::numeric_limits<float>::max_digits10) << value;
0558     return ss.str();
0559 }
0560 template <>
0561 inline std::string JParameterManager::Stringify(const double& value) {
0562     std::stringstream ss;
0563     // Use enough precision to make sure that the "double -> string -> double" roundtrip is exact. 
0564     // The stringstream << operator is smart enough to give us a clean representation when one is available.
0565     ss << std::setprecision(std::numeric_limits<double>::max_digits10) << value;
0566     return ss.str();
0567 }
0568 template <>
0569 inline std::string JParameterManager::Stringify(const long double& value) {
0570     std::stringstream ss;
0571     // Use enough precision to make sure that the "long double -> string -> long double" roundtrip is exact. 
0572     // The stringstream << operator is smart enough to give us a clean representation when one is available.
0573     ss << std::setprecision(std::numeric_limits<long double>::max_digits10) << value;
0574     return ss.str();
0575 }
0576 
0577 // @brief Specialization for strings. The stream operator is not only redundant here, but it also splits the string (see Issue #191)
0578 template <>
0579 inline std::string JParameterManager::Stringify(const std::string& value) {
0580     return value;
0581 }
0582 
0583 template <>
0584 inline std::string JParameterManager::Stringify(const JEventLevel& value) {
0585     return toString(value);
0586 }
0587 
0588 template <typename T>
0589 inline std::string JParameterManager::Stringify(const std::vector<T> &values) {
0590     std::stringstream ss;
0591     size_t len = values.size();
0592     for (size_t i = 0; i+1 < len; ++i) {
0593         ss << Stringify(values[i]);
0594         ss << ",";
0595     }
0596     if (len != 0) {
0597         ss << Stringify(values[len-1]);
0598     }
0599     return ss.str();
0600 }
0601 
0602 
0603 template <>
0604 inline std::string JParameterManager::Stringify(const std::vector<std::string> &values) {
0605     size_t len = values.size();
0606     std::stringstream ssv;
0607     for (size_t i = 0; i < len; ++i) {
0608         if (values[i].find(',') != std::string::npos) {
0609             std::stringstream ss(values[i]);
0610             std::string s;
0611             std::string s_end = values[i].substr(values[i].rfind(',')+1);
0612             while (getline(ss, s, ',')) {
0613                 if (s == s_end) {
0614                     ssv << s;
0615                     if (i != len-1) {
0616                         ssv << ',';
0617                     }
0618                     break;
0619                 }
0620                 ssv << s << "\\,";
0621                 continue;
0622             }
0623         } else {
0624             ssv << values[i];
0625             if (i != len-1) {
0626                 ssv << ',';
0627             }
0628         }
0629     }
0630     return ssv.str();
0631 }
0632 
0633 
0634 // @brief Template for generically stringifying an array
0635 template <typename T, size_t N>
0636 inline std::string JParameterManager::Stringify(const std::array<T,N> &values) {
0637     std::stringstream ss;
0638     size_t len = values.size();
0639     for (size_t i = 0; i+1 < N; ++i) {
0640         ss << Stringify(values[i]);
0641         ss << ",";
0642     }
0643     if (len != 0) {
0644         ss << Stringify(values[len-1]);
0645     }
0646     return ss.str();
0647 }
0648 
0649 
0650 template <size_t N>
0651 inline std::string JParameterManager::Stringify(const std::array<std::string,N> &values) {
0652     size_t len = values.size();
0653     std::stringstream ssv;
0654     for (size_t i = 0; i < len; ++i) {
0655         if (values[i].find(',') != std::string::npos) {
0656             std::stringstream ss(values[i]);
0657             std::string s;
0658             std::string s_end = values[i].substr(values[i].rfind(',')+1);
0659             while (getline(ss, s, ',')) {
0660                 if (s == s_end) {
0661                     ssv << s;
0662                     if (i != len-1) {
0663                         ssv << ',';
0664                     }
0665                     break;
0666                 }
0667                 ssv << s << "\\,";
0668                 continue;
0669             }
0670         } else {
0671             ssv << values[i];
0672             if (i != len-1) {
0673                 ssv << ',';
0674             }
0675         }
0676     }
0677     return ssv.str();
0678 }
0679 
0680 // @brief Equals() is called internally by SetDefaultParameter. This allows the user to define equivalence relations on custom data types so that
0681 // they can silence any spurious "loses equality with itself after stringification" warnings. Generally you should try specializing Stringify and Parse
0682 // first to normalize your data representation.
0683 template <typename T>
0684 inline bool JParameterManager::Equals(const T& lhs, const T& rhs) {
0685     return lhs == rhs;
0686 }
0687 
0688 
0689 
0690