diff --git a/libshvbroker/src/brokeraclnode.cpp b/libshvbroker/src/brokeraclnode.cpp index d1d35af9c..1bca4680d 100644 --- a/libshvbroker/src/brokeraclnode.cpp +++ b/libshvbroker/src/brokeraclnode.cpp @@ -588,7 +588,7 @@ unsigned AccessAclNode::keyToRuleIndex(const std::string &key) if(std::regex_search(key, color_match, color_regex)) { if (color_match.size() > 1) { bool ok; - unsigned ix = static_cast(shv::core::String::toInt(color_match[1], &ok)); + unsigned ix = static_cast(shv::core::string::toInt(color_match[1], &ok)); if(ok) return ix; } diff --git a/libshvbroker/src/brokerapp.cpp b/libshvbroker/src/brokerapp.cpp index 933f9243e..3bcbd88b5 100644 --- a/libshvbroker/src/brokerapp.cpp +++ b/libshvbroker/src/brokerapp.cpp @@ -984,7 +984,7 @@ void BrokerApp::onClientLogin(int connection_id) conn->setParent(client_app_node); { - std::string mount_point = client_id_node->shvPath() + "/subscriptions"; + std::string mount_point = client_id_node->shvPath().asString() + "/subscriptions"; auto *nd = new SubscriptionsNode(conn); if(!m_nodesTree->mount(mount_point, nd)) SHV_EXCEPTION("Cannot mount connection subscription list to device tree, connection id: " + std::to_string(connection_id) @@ -1010,7 +1010,7 @@ void BrokerApp::onClientLogin(int connection_id) SHV_EXCEPTION("Cannot mount connection to device tree, connection id: " + std::to_string(connection_id)); connect(cli_nd, &ClientShvNode::destroyed, cli_nd->parentNode(), &shv::iotqt::node::ShvNode::deleteIfEmptyWithParents, Qt::QueuedConnection); } - mount_point = cli_nd->shvPath(); + mount_point = cli_nd->shvPath().asString(); QTimer::singleShot(0, this, [this, mount_point]() { sendNotifyToSubscribers(mount_point, cp::Rpc::SIG_MOUNTED_CHANGED, "", true); }); diff --git a/libshvbroker/src/rpc/masterbrokerconnection.cpp b/libshvbroker/src/rpc/masterbrokerconnection.cpp index 2420bf9a5..d21327570 100644 --- a/libshvbroker/src/rpc/masterbrokerconnection.cpp +++ b/libshvbroker/src/rpc/masterbrokerconnection.cpp @@ -114,7 +114,7 @@ std::string MasterBrokerConnection::masterExportedToLocalPath(const std::string static const std::string DIR_BROKER = cp::Rpc::DIR_BROKER; if(shv::core::utils::ShvPath::startsWithPath(master_path, DIR_BROKER)) return master_path; - return shv::core::utils::ShvPath(m_exportedShvPath).appendPath(master_path); + return shv::core::utils::ShvPath(m_exportedShvPath).appendPath(master_path).asString(); } std::string MasterBrokerConnection::localPathToMasterExported(const std::string &local_path) const diff --git a/libshvcore/include/shv/core/string.h b/libshvcore/include/shv/core/string.h index a9310d019..cbbe8d712 100644 --- a/libshvcore/include/shv/core/string.h +++ b/libshvcore/include/shv/core/string.h @@ -7,49 +7,36 @@ #include #include -namespace shv::core { - -class SHVCORE_DECL_EXPORT String : public std::string -{ - using Super = std::string; -public: +namespace shv::core::string { enum CaseSensitivity {CaseSensitive, CaseInsensitive}; enum SplitBehavior {KeepEmptyParts, SkipEmptyParts}; - using Super::Super; - String(); - String(const std::string &o); - String(std::string &&o); - - static bool equal(const std::string &a, const std::string &b, String::CaseSensitivity case_sensitivity); - static std::string::size_type indexOf(const std::string & str_haystack, const std::string &str_needle, String::CaseSensitivity case_sensitivity = CaseSensitive); - static std::string::size_type indexOf(const std::string &haystack, char needle); - std::string::size_type indexOf(const std::string &needle, String::CaseSensitivity case_sensitivity = CaseSensitive) const; - std::string::size_type indexOf(char needle) const; - size_t lastIndexOf(char c) const; + SHVCORE_DECL_EXPORT bool equal(const std::string &a, const std::string &b, CaseSensitivity case_sensitivity); + SHVCORE_DECL_EXPORT std::string::size_type indexOf(const std::string & str_haystack, const std::string &str_needle, CaseSensitivity case_sensitivity = CaseSensitive); + SHVCORE_DECL_EXPORT std::string::size_type indexOf(const std::string &haystack, char needle); - static constexpr auto WhiteSpaceChars = " \t\n\r\f\v"; - static std::string& rtrim(std::string& s, const char* t = WhiteSpaceChars); - static std::string& ltrim(std::string& s, const char* t = WhiteSpaceChars); - static std::string& trim(std::string& s, const char* t = WhiteSpaceChars); + constexpr auto WhiteSpaceChars = " \t\n\r\f\v"; + SHVCORE_DECL_EXPORT std::string& rtrim(std::string& s, const char* t = WhiteSpaceChars); + SHVCORE_DECL_EXPORT std::string& ltrim(std::string& s, const char* t = WhiteSpaceChars); + SHVCORE_DECL_EXPORT std::string& trim(std::string& s, const char* t = WhiteSpaceChars); - std::string mid(size_t pos, size_t cnt = std::string::npos) const; + SHVCORE_DECL_EXPORT std::string mid(const std::string& str, size_t pos, size_t cnt = std::string::npos); - static std::pair indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket); - static std::vector split(const std::string &str, char delim, SplitBehavior split_behavior = SkipEmptyParts); - static std::string join(const std::vector &lst, const std::string &delim); - static std::string join(const std::vector &lst, char delim); - static std::string join(const std::vector &lst, char delim); - static int replace(std::string &str, const std::string &from, const std::string &to); - static int replace(std::string &str, const char from, const char to); + SHVCORE_DECL_EXPORT std::pair indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket); + SHVCORE_DECL_EXPORT std::vector split(const std::string &str, char delim, SplitBehavior split_behavior = SkipEmptyParts); + SHVCORE_DECL_EXPORT std::string join(const std::vector &lst, const std::string &delim); + SHVCORE_DECL_EXPORT std::string join(const std::vector &lst, char delim); + SHVCORE_DECL_EXPORT std::string join(const std::vector &lst, char delim); + SHVCORE_DECL_EXPORT int replace(std::string &str, const std::string &from, const std::string &to); + SHVCORE_DECL_EXPORT int replace(std::string &str, const char from, const char to); - static std::string& upper(std::string& s); - static std::string toUpper(const std::string& s); - static std::string& lower(std::string& s); - static std::string toLower(const std::string& s); + SHVCORE_DECL_EXPORT std::string& upper(std::string& s); + SHVCORE_DECL_EXPORT std::string toUpper(const std::string& s); + SHVCORE_DECL_EXPORT std::string& lower(std::string& s); + SHVCORE_DECL_EXPORT std::string toLower(const std::string& s); template - static std::string toString(T i, size_t width = 0, char fill_char = ' ') + std::string toString(T i, size_t width = 0, char fill_char = ' ') { std::ostringstream ss; ss << i; @@ -58,9 +45,7 @@ class SHVCORE_DECL_EXPORT String : public std::string ret = fill_char + ret; return ret; } - static int toInt(const std::string &str, bool *ok = nullptr); - static double toDouble(const std::string &str, bool *ok); - -}; + SHVCORE_DECL_EXPORT int toInt(const std::string &str, bool *ok = nullptr); + SHVCORE_DECL_EXPORT double toDouble(const std::string &str, bool *ok); } diff --git a/libshvcore/include/shv/core/utils/shvpath.h b/libshvcore/include/shv/core/utils/shvpath.h index 97875f157..c28f3df47 100644 --- a/libshvcore/include/shv/core/utils/shvpath.h +++ b/libshvcore/include/shv/core/utils/shvpath.h @@ -15,18 +15,16 @@ SHVCORE_DECL_EXPORT bool startsWithPath(const std::string_view &str, const std:: } -class SHVCORE_DECL_EXPORT ShvPath : public shv::core::String +class SHVCORE_DECL_EXPORT ShvPath { - using Super = shv::core::String; public: static constexpr auto SHV_PATH_QUOTE = '\''; static constexpr auto SHV_PATH_DELIM = '/'; static constexpr auto SHV_PATH_METHOD_DELIM = ':'; public: ShvPath(); - ShvPath(shv::core::String &&o); - ShvPath(const shv::core::String &o); - using Super::Super; + ShvPath(std::string &&o); + ShvPath(const std::string &o); const std::string &asString() const; @@ -92,5 +90,8 @@ class SHVCORE_DECL_EXPORT ShvPath : public shv::core::String } } } + +private: + std::string m_str; }; } diff --git a/libshvcore/src/string.cpp b/libshvcore/src/string.cpp index 37dfe030c..cf185d4f3 100644 --- a/libshvcore/src/string.cpp +++ b/libshvcore/src/string.cpp @@ -3,21 +3,9 @@ #include -namespace shv::core { +namespace shv::core::string { -String::String() = default; - -String::String(const std::string &o) - : Super(o) -{ -} - -String::String(std::string &&o) - : Super(o) -{ -} - -std::string::size_type String::indexOf(const std::string & str_haystack, const std::string &str_needle, String::CaseSensitivity case_sensitivity) +std::string::size_type indexOf(const std::string & str_haystack, const std::string &str_needle, CaseSensitivity case_sensitivity) { auto it = std::search( str_haystack.begin(), str_haystack.end(), @@ -29,7 +17,7 @@ std::string::size_type String::indexOf(const std::string & str_haystack, const s return (it == str_haystack.end())? std::string::npos: static_cast(it - str_haystack.begin()); } -std::string::size_type String::indexOf(const std::string &haystack, char needle) +std::string::size_type indexOf(const std::string &haystack, char needle) { for (std::string::size_type i = 0; i < haystack.length(); i++) if(haystack[i] == needle) @@ -37,54 +25,31 @@ std::string::size_type String::indexOf(const std::string &haystack, char needle) return std::string::npos; } -std::string::size_type String::indexOf(const std::string &needle, String::CaseSensitivity case_sensitivity) const +std::string mid(const std::string& str, size_t pos, size_t cnt) { - return indexOf(*this, needle, case_sensitivity); -} - -std::string::size_type String::indexOf(char needle) const -{ - return indexOf(*this, needle); -} - -size_t String::lastIndexOf(char c) const -{ - if(empty()) - return std::string::npos; - for (size_t i = size() - 1; ; i--) { - if(at(i) == c) - return i; - if(i == 0) - break; - } - return std::string::npos; -} - -std::string String::mid(size_t pos, size_t cnt) const -{ - if(pos < size()) - return substr(pos, cnt); + if(pos < str.size()) + return str.substr(pos, cnt); return {}; } -std::string& String::rtrim(std::string& s, const char* t) +std::string& rtrim(std::string& s, const char* t) { s.erase(s.find_last_not_of(t) + 1); return s; } -std::string& String::ltrim(std::string& s, const char* t) +std::string& ltrim(std::string& s, const char* t) { s.erase(0, s.find_first_not_of(t)); return s; } -std::string& String::trim(std::string& s, const char* t) +std::string& trim(std::string& s, const char* t) { return ltrim(rtrim(s, t), t); } -bool String::equal(std::string const& a, std::string const& b, String::CaseSensitivity case_sensitivity) +bool equal(std::string const& a, std::string const& b, CaseSensitivity case_sensitivity) { if (a.length() == b.length()) { return std::equal( @@ -99,24 +64,24 @@ bool String::equal(std::string const& a, std::string const& b, String::CaseSensi return false; } -std::vector String::split(const std::string &str, char delim, SplitBehavior split_behavior) +std::vector split(const std::string &str, char delim, SplitBehavior split_behavior) { using namespace std; - vector ret; + vector ret; size_t pos = 0; while(true) { size_t pos2 = str.find_first_of(delim, pos); - string s = (pos2 == string::npos)? str.substr(pos): str.substr(pos, pos2 - pos); + std::string s = (pos2 == std::string::npos)? str.substr(pos): str.substr(pos, pos2 - pos); if(split_behavior == KeepEmptyParts || !s.empty()) ret.push_back(s); - if(pos2 == string::npos) + if(pos2 == std::string::npos) break; pos = pos2 + 1; } return ret; } -std::string String::join(const std::vector &lst, const std::string &delim) +std::string join(const std::vector &lst, const std::string &delim) { std::string ret; for(const auto &s : lst) { @@ -127,7 +92,7 @@ std::string String::join(const std::vector &lst, const std::string return ret; } -std::string String::join(const std::vector &lst, char delim) +std::string join(const std::vector &lst, char delim) { std::string ret; for(const auto &s : lst) { @@ -138,7 +103,7 @@ std::string String::join(const std::vector &lst, char delim) return ret; } -std::string String::join(const std::vector &lst, char delim) +std::string join(const std::vector &lst, char delim) { std::string ret; for(const auto &s : lst) { @@ -149,7 +114,7 @@ std::string String::join(const std::vector &lst, char delim) return ret; } -int String::replace(std::string &str, const std::string &from, const std::string &to) +int replace(std::string &str, const std::string &from, const std::string &to) { int i = 0; size_t pos = 0; @@ -163,7 +128,7 @@ int String::replace(std::string &str, const std::string &from, const std::string return i; } -int String::replace(std::string& str, const char from, const char to) +int replace(std::string& str, const char from, const char to) { int n = 0; for (char& i : str) { @@ -175,33 +140,33 @@ int String::replace(std::string& str, const char from, const char to) return n; } -std::string &String::upper(std::string &s) +std::string &upper(std::string &s) { for (char& i : s) i = static_cast(std::toupper(i)); return s; } -std::string String::toUpper(const std::string& s) +std::string toUpper(const std::string& s) { std::string ret(s); return upper(ret); } -std::string &String::lower(std::string &s) +std::string &lower(std::string &s) { for (char& i : s) i = static_cast(std::tolower(i)); return s; } -std::string String::toLower(const std::string& s) +std::string toLower(const std::string& s) { std::string ret(s); return lower(ret); } -int String::toInt(const std::string &str, bool *ok) +int toInt(const std::string &str, bool *ok) { int ret = 0; bool is_ok = false; @@ -221,7 +186,7 @@ int String::toInt(const std::string &str, bool *ok) return ret; } -double String::toDouble(const std::string &str, bool *ok) +double toDouble(const std::string &str, bool *ok) { double ret = 0; bool is_ok = false; @@ -246,15 +211,15 @@ size_t find_str(const std::string &haystack, size_t begin_pos, size_t end_pos, c { using namespace std; if(needle.empty()) - return string::npos; + return std::string::npos; if(begin_pos > haystack.size()) - return string::npos; + return std::string::npos; if(end_pos > haystack.size()) - return string::npos; + return std::string::npos; if(end_pos < begin_pos) - return string::npos; + return std::string::npos; if(needle.size() > (end_pos - begin_pos)) - return string::npos; + return std::string::npos; auto pos1 = begin_pos; auto pos2 = end_pos - needle.size() + 1; while(pos1 < pos2) { @@ -267,11 +232,11 @@ size_t find_str(const std::string &haystack, size_t begin_pos, size_t end_pos, c return pos1; ++pos1; } - return string::npos; + return std::string::npos; } } -std::pair String::indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket) +std::pair indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket) { if(begin_pos + open_bracket.size() > haystack.size()) return std::pair(std::string::npos, std::string::npos); diff --git a/libshvcore/src/utils/clioptions.cpp b/libshvcore/src/utils/clioptions.cpp index 5c0b8641d..e0b1560b7 100644 --- a/libshvcore/src/utils/clioptions.cpp +++ b/libshvcore/src/utils/clioptions.cpp @@ -119,14 +119,14 @@ CLIOptions::Option& CLIOptions::Option::setValueString(const std::string &val_st } else { bool ok; - int n = String::toInt(val_str, &ok); + int n = string::toInt(val_str, &ok); if(ok) { setValue(n != 0); } else { bool is_true = true; for(const char * const s : {"n", "no", "false"}) { - if(String::equal(val_str, s, String::CaseInsensitive)) { + if(string::equal(val_str, s, string::CaseInsensitive)) { is_true = false; break; } @@ -140,7 +140,7 @@ CLIOptions::Option& CLIOptions::Option::setValueString(const std::string &val_st case RpcValue::Type::UInt: { bool ok; - setValue(String::toInt(val_str, &ok)); + setValue(string::toInt(val_str, &ok)); if(!ok) shvWarning() << "Value:" << val_str << "cannot be converted to Int."; break; @@ -148,7 +148,7 @@ CLIOptions::Option& CLIOptions::Option::setValueString(const std::string &val_st case(RpcValue::Type::Double): { bool ok; - setValue(String::toDouble(val_str, &ok)); + setValue(string::toDouble(val_str, &ok)); if(!ok) shvWarning() << "Value:" << val_str << "cannot be converted to Double."; break; @@ -399,7 +399,7 @@ std::tuple CLIOptions::applicationDirAndName() const #endif if(app_name.size() > ext.size()) { std::string app_ext = app_name.substr(app_name.size() - ext.size()); - if(String::equal(ext, app_ext, String::CaseInsensitive)) + if(string::equal(ext, app_ext, string::CaseInsensitive)) app_name = app_name.substr(0, app_name.size() - ext.size()); } } @@ -425,7 +425,7 @@ void CLIOptions::printHelp(std::ostream &os) const os << "OPTIONS:" << endl << endl; for(const auto &kv : m_options) { const Option &opt = kv.second; - os << String::join(opt.names(), ", "); + os << string::join(opt.names(), ", "); if(opt.type() != RpcValue::Type::Bool) { if(opt.type() == RpcValue::Type::Int || opt.type() == RpcValue::Type::UInt @@ -457,7 +457,7 @@ void CLIOptions::dump(std::ostream &os) const { for(const auto &kv : m_options) { const Option &opt = kv.second; - os << kv.first << '(' << String::join(opt.names(), ", ") << ')' << ": " << opt.value().asString() << std::endl; + os << kv.first << '(' << string::join(opt.names(), ", ") << ')' << ": " << opt.value().asString() << std::endl; } } diff --git a/libshvcore/src/utils/shvfilejournal.cpp b/libshvcore/src/utils/shvfilejournal.cpp index 7486a73ea..e1f2af338 100644 --- a/libshvcore/src/utils/shvfilejournal.cpp +++ b/libshvcore/src/utils/shvfilejournal.cpp @@ -124,9 +124,9 @@ const std::string &ShvFileJournal::journalDir() } else { std::string id = m_journalContext.deviceId; - String::replace(id, '/', '-'); - String::replace(id, ':', '-'); - String::replace(id, '.', '-'); + string::replace(id, '/', '-'); + string::replace(id, ':', '-'); + string::replace(id, '.', '-'); d += id; } m_journalContext.journalDir = d; diff --git a/libshvcore/src/utils/shvjournalfilereader.cpp b/libshvcore/src/utils/shvjournalfilereader.cpp index 9ba4adcce..15d1de7c7 100644 --- a/libshvcore/src/utils/shvjournalfilereader.cpp +++ b/libshvcore/src/utils/shvjournalfilereader.cpp @@ -119,13 +119,13 @@ bool ShvJournalFileReader::next() } else { bool ok; - int short_time = shv::core::String::toInt(std::string{fld}, &ok); + int short_time = shv::core::string::toInt(std::string{fld}, &ok); m_currentEntry.shortTime = ok && short_time >= 0? short_time: ShvJournalEntry::NO_SHORT_TIME; } break; } case Column::ValueFlags: { - auto value_flags = fld.empty()? 0: shv::core::String::toInt(std::string{fld}); + auto value_flags = fld.empty()? 0: shv::core::string::toInt(std::string{fld}); m_currentEntry.valueFlags = static_cast(value_flags); break; } diff --git a/libshvcore/src/utils/shvpath.cpp b/libshvcore/src/utils/shvpath.cpp index 574adf914..e7bbe1008 100644 --- a/libshvcore/src/utils/shvpath.cpp +++ b/libshvcore/src/utils/shvpath.cpp @@ -27,24 +27,24 @@ bool shvpath::startsWithPath(const std::string_view &str, const std::string_view ShvPath::ShvPath() = default; -ShvPath::ShvPath(shv::core::String &&o) - : Super(std::move(o)) +ShvPath::ShvPath(std::string &&o) + : m_str(std::move(o)) { } -ShvPath::ShvPath(const shv::core::String &o) - : Super(o) +ShvPath::ShvPath(const std::string &o) + : m_str(o) { } const std::string &ShvPath::asString() const { - return *this; + return m_str; } bool ShvPath::startsWithPath(const StringView &path, size_t *pos) const { - return startsWithPath(*this, path, pos); + return startsWithPath(m_str, path, pos); } bool ShvPath::startsWithPath(const StringView &str, const StringView &path, size_t *pos) @@ -54,7 +54,7 @@ bool ShvPath::startsWithPath(const StringView &str, const StringView &path, size ShvPath ShvPath::appendPath(const StringView &path) const { - return shv::core::utils::joinPath(*this, path); + return shv::core::utils::joinPath(m_str, path); } core::StringViewList ShvPath::splitPath(const shv::core::StringView &shv_path) @@ -86,7 +86,7 @@ StringView ShvPath::takeFirsDir(StringView &shv_path) ShvPath ShvPath::joinDirs(const std::vector &dirs) { - ShvPath ret; + std::string ret; for(const std::string &s : dirs) { if(s.empty()) continue; @@ -123,7 +123,7 @@ bool need_quotes(const StringView &dir) ShvPath ShvPath::joinDirs(std::vector::const_iterator first, std::vector::const_iterator last) { - ShvPath ret; + std::string ret; for(auto it = first; it != last; ++it) { if(it->empty()) continue; @@ -162,7 +162,7 @@ ShvPath ShvPath::appendDir(StringView path1, StringView dir) ShvPath ShvPath::appendDir(StringView dir) const { - return appendDir(*this, dir); + return appendDir(m_str, dir); } StringViewList ShvPath::split(const StringView &shv_path) @@ -178,7 +178,7 @@ bool ShvPath::matchWild(const std::string &pattern) const bool ShvPath::matchWild(const shv::core::StringViewList &pattern_lst) const { - const shv::core::StringViewList path_lst = shv::core::utils::split(*this, SHV_PATH_DELIM); + const shv::core::StringViewList path_lst = shv::core::utils::split(m_str, SHV_PATH_DELIM); return matchWild(path_lst, pattern_lst); } diff --git a/libshvcore/src/utils/shvtypeinfo.cpp b/libshvcore/src/utils/shvtypeinfo.cpp index 7420d0e54..690150138 100644 --- a/libshvcore/src/utils/shvtypeinfo.cpp +++ b/libshvcore/src/utils/shvtypeinfo.cpp @@ -8,7 +8,6 @@ #include -using namespace std; using namespace shv::chainpack; namespace shv::core::utils { @@ -46,7 +45,7 @@ ShvDescriptionBase::ShvDescriptionBase(const chainpack::RpcValue &v) { } -string ShvDescriptionBase::name() const +std::string ShvDescriptionBase::name() const { return m_data.asMap().value(KEY_NAME).asString(); } @@ -123,17 +122,17 @@ ShvFieldDescr::ShvFieldDescr(const std::string &name, const std::string &type_na setDataValue(KEY_VALUE, value); } -string ShvFieldDescr::typeName() const +std::string ShvFieldDescr::typeName() const { return dataValue(KEY_TYPE_NAME).asString(); } -string ShvFieldDescr::label() const +std::string ShvFieldDescr::label() const { return dataValue(KEY_LABEL).asString(); } -string ShvFieldDescr::description() const +std::string ShvFieldDescr::description() const { return dataValue(KEY_DESCRIPTION).asString(); } @@ -143,12 +142,12 @@ RpcValue ShvFieldDescr::value() const return dataValue(KEY_VALUE); } -string ShvFieldDescr::visualStyleName() const +std::string ShvFieldDescr::visualStyleName() const { return dataValue(KEY_VISUAL_STYLE).asString(); } -string ShvFieldDescr::alarm() const +std::string ShvFieldDescr::alarm() const { return dataValue(KEY_ALARM).asString(); } @@ -158,42 +157,42 @@ int ShvFieldDescr::alarmLevel() const return dataValue(KEY_ALARM_LEVEL).toInt(); } -string ShvFieldDescr::unit() const +std::string ShvFieldDescr::unit() const { return dataValue(KEY_UNIT).asString(); } -ShvFieldDescr &ShvFieldDescr::setTypeName(const string &type_name) +ShvFieldDescr &ShvFieldDescr::setTypeName(const std::string &type_name) { setDataValue(KEY_TYPE_NAME, type_name); return *this; } -ShvFieldDescr &ShvFieldDescr::setLabel(const string &label) +ShvFieldDescr &ShvFieldDescr::setLabel(const std::string &label) { setDataValue(KEY_LABEL, label); return *this; } -ShvFieldDescr &ShvFieldDescr::setDescription(const string &description) +ShvFieldDescr &ShvFieldDescr::setDescription(const std::string &description) { setDataValue(KEY_DESCRIPTION, description); return *this; } -ShvFieldDescr &ShvFieldDescr::setUnit(const string &unit) +ShvFieldDescr &ShvFieldDescr::setUnit(const std::string &unit) { setDataValue(KEY_UNIT, unit); return *this; } -ShvFieldDescr &ShvFieldDescr::setVisualStyleName(const string &visual_style_name) +ShvFieldDescr &ShvFieldDescr::setVisualStyleName(const std::string &visual_style_name) { setDataValue(KEY_VISUAL_STYLE, visual_style_name); return *this; } -ShvFieldDescr &ShvFieldDescr::setAlarm(const string &alarm) +ShvFieldDescr &ShvFieldDescr::setAlarm(const std::string &alarm) { setDataValue(KEY_ALARM, alarm); return *this; @@ -346,7 +345,7 @@ ShvTypeDescr &ShvTypeDescr::setSampleType(ShvTypeDescr::SampleType st) return *this; } -string ShvTypeDescr::restrictionOfType() const +std::string ShvTypeDescr::restrictionOfType() const { return dataValue(KEY_RESTRICTION_OF_TYPE).asString(); } @@ -488,7 +487,7 @@ RpcValue ShvTypeDescr::toRpcValue() const map.setValue(KEY_SAMPLE_TYPE, {}); else map.setValue(KEY_SAMPLE_TYPE, sampleTypeToString(sampleType())); - auto remove_empty_list = [&map](const string &name) { + auto remove_empty_list = [&map](const std::string &name) { if(map.hasKey(name) && map.value(name).asList().empty()) map.erase(name); }; @@ -615,7 +614,7 @@ RpcValue ShvPropertyDescr::toRpcValue() const ShvPropertyDescr ShvPropertyDescr::fromRpcValue(const RpcValue &v, RpcValue::Map *extra_tags) { - static const vector known_tags{ + static const std::vector known_tags{ KEY_DEVICE_TYPE, //KEY_SUPER_DEVICE_TYPE, KEY_NAME, @@ -737,22 +736,22 @@ ShvDeviceDescription &ShvDeviceDescription::setPropertyDescription(const ShvProp // ShvLogTypeInfo //===================================================================== template -typename map::const_iterator find_longest_prefix(const map &map, const string &path) +typename std::map::const_iterator find_longest_prefix(const std::map &map, const std::string &path) { - shv::core::String prefix = path; + std::string prefix = path; while(true) { auto it = map.find(prefix); if(it == map.end()) { // if not found, cut last path part - size_t ix = prefix.lastIndexOf('/'); - if(ix == string::npos) { + size_t ix = prefix.find_last_of('/'); + if(ix == std::string::npos) { if(prefix.empty()) { return map.cend(); } prefix = ""; } else { - prefix = prefix.mid(0, ix); + prefix = shv::core::string::mid(prefix, 0, ix); } } else { @@ -903,23 +902,23 @@ ShvTypeInfo &ShvTypeInfo::setTypeDescription(const std::string &type_name, const } namespace { -string cut_prefix(const string &path, const string &prefix) { +std::string cut_prefix(const std::string &path, const std::string &prefix) { if(prefix.empty()) { return path; } if(prefix.size() < path.size()) { return path.substr(prefix.size() + 1); } - return string(); + return std::string(); } -string cut_sufix(const string &path, const string &postfix) { +std::string cut_sufix(const std::string &path, const std::string &postfix) { if(postfix.empty()) { return path; } if(!postfix.empty() && postfix.size() < path.size()) { return path.substr(0, path.size() - postfix.size() - 1); } - return string(); + return std::string(); } } @@ -929,7 +928,7 @@ ShvTypeInfo::PathInfo ShvTypeInfo::pathInfo(const std::string &shv_path) const bool deviation_found = false; if(auto it3 = find_longest_prefix(m_propertyDeviations, shv_path); it3 != m_propertyDeviations.cend()) { deviation_found = true; - const string own_property_path = it3->first; + const std::string own_property_path = it3->first; ret.fieldPath = cut_prefix(shv_path, own_property_path); ret.propertyDescription = it3->second; } @@ -949,7 +948,7 @@ ShvTypeInfo::PathInfo ShvTypeInfo::pathInfo(const std::string &shv_path) const return ret; } -ShvPropertyDescr ShvTypeInfo::propertyDescriptionForPath(const std::string &shv_path, string *p_field_name) const +ShvPropertyDescr ShvTypeInfo::propertyDescriptionForPath(const std::string &shv_path, std::string *p_field_name) const { auto info = pathInfo(shv_path); if(p_field_name) @@ -957,26 +956,26 @@ ShvPropertyDescr ShvTypeInfo::propertyDescriptionForPath(const std::string &shv_ return info.propertyDescription; } -std::tuple ShvTypeInfo::findDeviceType(const std::string &shv_path) const +std::tuple ShvTypeInfo::findDeviceType(const std::string &shv_path) const { auto it = find_longest_prefix(m_devicePaths, shv_path); if(it == m_devicePaths.cend()) { return {}; } - const string prefix = it->first; - const string device_type = it->second; - const string property_path = prefix.empty()? shv_path: String(shv_path).mid(prefix.size() + 1); + const std::string prefix = it->first; + const std::string device_type = it->second; + const std::string property_path = prefix.empty()? shv_path: shv::core::string::mid(shv_path, prefix.size() + 1); return make_tuple(prefix, device_type, property_path); } -std::tuple ShvTypeInfo::findPropertyDescription(const std::string &device_type, const std::string &property_path) const +std::tuple ShvTypeInfo::findPropertyDescription(const std::string &device_type, const std::string &property_path) const { if(auto it2 = m_deviceDescriptions.find(device_type); it2 != m_deviceDescriptions.end()) { const auto &dev_descr = it2->second; if(auto it3 = dev_descr.findLongestPropertyPrefix(property_path); it3 != dev_descr.properties.cend()) { - const string own_property_path = it3->name(); - const string field_path = cut_prefix(property_path, own_property_path); + const std::string own_property_path = it3->name(); + const std::string field_path = cut_prefix(property_path, own_property_path); return make_tuple(*it3, field_path); } } @@ -1008,8 +1007,8 @@ RpcValue ShvTypeInfo::extraTagsForPath(const std::string &shv_path) const std::string ShvTypeInfo::findSystemPath(const std::string &shv_path) const { - string current_root; - string ret; + std::string current_root; + std::string ret; for(const auto& [shv_root_path, system_path] : m_systemPathsRoots) { if(shv_path.starts_with(shv_root_path)) { if(shv_root_path.empty() || shv_path.size() == shv_root_path.size() || shv_path[shv_root_path.size()] == '/') { @@ -1284,24 +1283,24 @@ void ShvTypeInfo::fromNodesTree_helper(const RpcValue::Map &node_types, return; } - string current_device_type = device_type; - string current_device_path = device_path; - string current_property_path = property_path; + std::string current_device_type = device_type; + std::string current_device_path = device_path; + std::string current_property_path = property_path; ShvDeviceDescription new_device_description; ShvDeviceDescription *current_device_description = device_description? device_description: &new_device_description; RpcValue::Map property_descr_map; RpcList property_methods; bool new_device_type_entered = device_description == nullptr; - static const string CREATE_FROM_TYPE_NAME = "createFromTypeName"; - static const string SYSTEM_PATH = "systemPath"; + static const std::string CREATE_FROM_TYPE_NAME = "createFromTypeName"; + static const std::string SYSTEM_PATH = "systemPath"; for(const RpcValue &rv : node.metaData().valref("methods").asList()) { const auto mm = MetaMethod::fromRpcValue(rv); - const string &method_name = mm.name(); + const std::string &method_name = mm.name(); if(method_name == Rpc::METH_LS || method_name == Rpc::METH_DIR) continue; property_methods.push_back(mm.toRpcValue()); if(!mm.extra().empty()) { - string key = shv::core::utils::joinPath(property_path, "method"s); + std::string key = shv::core::utils::joinPath(property_path, std::string("method")); key = shv::core::utils::joinPath(key, mm.name()); setExtraTags(key, mm.extra()); } @@ -1309,11 +1308,11 @@ void ShvTypeInfo::fromNodesTree_helper(const RpcValue::Map &node_types, const RpcValue::Map &node_tags = node.metaData().valref(KEY_TAGS).asMap(); if(!node_tags.empty()) { RpcValue::Map tags_map = node_tags; - const string &dtype = tags_map.valref(KEY_DEVICE_TYPE).asString(); + const std::string &dtype = tags_map.valref(KEY_DEVICE_TYPE).asString(); if(!dtype.empty()) { current_device_type = dtype; current_device_path = utils::joinPath(device_path, property_path); - current_property_path = string(); + current_property_path = std::string(); current_device_description = &new_device_description; new_device_type_entered = true; } @@ -1351,7 +1350,7 @@ void ShvTypeInfo::fromNodesTree_helper(const RpcValue::Map &node_types, if(child_name.empty()) continue; ShvPath child_property_path = shv::core::utils::joinPath(current_property_path, child_name); - fromNodesTree_helper(node_types, child_node, current_device_type, current_device_path, child_property_path, current_device_description); + fromNodesTree_helper(node_types, child_node, current_device_type, current_device_path, child_property_path.asString(), current_device_description); } } if(new_device_type_entered) { diff --git a/libshvcore/src/utils/versioninfo.cpp b/libshvcore/src/utils/versioninfo.cpp index 90bea2993..83335c74d 100644 --- a/libshvcore/src/utils/versioninfo.cpp +++ b/libshvcore/src/utils/versioninfo.cpp @@ -16,7 +16,7 @@ VersionInfo::VersionInfo(int major, int minor, int patch, const std::string &bra VersionInfo::VersionInfo(const std::string &version, const std::string &branch) : m_branch(branch) { - std::vector parts = String::split(version, '.'); + std::vector parts = shv::core::string::split(version, '.'); while (parts.size() < 3) { parts.emplace_back("0"); } diff --git a/libshvcore/tests/test_clioptions.cpp b/libshvcore/tests/test_clioptions.cpp index 7cd7da448..306c8126c 100644 --- a/libshvcore/tests/test_clioptions.cpp +++ b/libshvcore/tests/test_clioptions.cpp @@ -11,10 +11,9 @@ using namespace shv::core::utils; using namespace shv::core; -using namespace std; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -vector cmdline; +std::vector cmdline; int main(int argc, char** argv) { @@ -25,12 +24,13 @@ int main(int argc, char** argv) DOCTEST_TEST_CASE("CliOptions") { + using namespace std::string_literals; DOCTEST_SUBCASE("Make config dir and config file absolute") { - string req_abs_config_dir; - string req_abs_config_file; - string config_dir; - string config_file; + std::string req_abs_config_dir; + std::string req_abs_config_file; + std::string config_dir; + std::string config_file; const auto cwd = std::filesystem::current_path().string(); const auto default_config_file_name = "test_core_clioptions.conf"s; ConfigCLIOptions cliopts; diff --git a/libshvcore/tests/test_shvpath.cpp b/libshvcore/tests/test_shvpath.cpp index 7cec2a9e1..84f863107 100644 --- a/libshvcore/tests/test_shvpath.cpp +++ b/libshvcore/tests/test_shvpath.cpp @@ -8,15 +8,15 @@ using namespace shv::core::utils; using namespace shv::core; -using namespace std; DOCTEST_TEST_CASE("ShvPath") { + using namespace std::string_literals; DOCTEST_SUBCASE("ShvPath::startsWithPath()") { { - string path = "status/errorCommunication"; - array paths = { + std::string path = "status/errorCommunication"; + std::array paths = { "status/errorCommunication"s, "status/errorCommunication/"s, "status/errorCommunication/foo"s, @@ -27,8 +27,8 @@ DOCTEST_TEST_CASE("ShvPath") } } { - string path = "foo/bar"; - array paths = { + std::string path = "foo/bar"; + std::array paths = { "bar/baz"s, "foo/barbaz"s, }; @@ -39,29 +39,29 @@ DOCTEST_TEST_CASE("ShvPath") } DOCTEST_SUBCASE("ShvPath::firstDir()") { - string dir = "'a/b/c'/d"; - string dir1 = "a/b/c"; + std::string dir = "'a/b/c'/d"; + std::string dir1 = "a/b/c"; REQUIRE(ShvPath::firstDir(dir) == StringView(dir1)); } DOCTEST_SUBCASE("ShvPath::takeFirsDir()") { - string dir = "'a/b/c'/d"; + std::string dir = "'a/b/c'/d"; StringView dir_view(dir); - string dir1 = "a/b/c"; - string dir2 = "d"; + std::string dir1 = "a/b/c"; + std::string dir2 = "d"; auto dir_view1 = ShvPath::takeFirsDir(dir_view); REQUIRE(dir_view1 == StringView(dir1)); REQUIRE(dir_view == StringView(dir2)); } DOCTEST_SUBCASE("ShvPath join & split") { - string dir1 = "foo"; - string dir2 = "bar/baz"; - string joined = "foo/'bar/baz'"; + std::string dir1 = "foo"; + std::string dir2 = "bar/baz"; + std::string joined = "foo/'bar/baz'"; DOCTEST_SUBCASE("ShvPath::joinDirs()") { - REQUIRE(ShvPath::joinDirs(dir1, dir2) == joined); + REQUIRE(ShvPath::joinDirs(dir1, dir2).asString() == joined); } DOCTEST_SUBCASE("ShvPath::splitPath()") { @@ -82,7 +82,7 @@ DOCTEST_TEST_CASE("ShvPath") {"a/c/d", "a"}, }; ShvPath::forEachDirAndSubdirs(m, "a/b", [](Map::const_iterator it) { - REQUIRE(ShvPath::startsWithPath(it->first, string("a/b"))); + REQUIRE(ShvPath::startsWithPath(it->first, std::string("a/b"))); }); } DOCTEST_SUBCASE("pattern match") diff --git a/libshviotqt/include/shv/iotqt/node/propertynode.h b/libshviotqt/include/shv/iotqt/node/propertynode.h index 67bf2d5cb..d44c8708c 100644 --- a/libshviotqt/include/shv/iotqt/node/propertynode.h +++ b/libshviotqt/include/shv/iotqt/node/propertynode.h @@ -83,7 +83,7 @@ class BasePropertyNode : public shv::iotqt::node::MethodsTableNode m_value = val; shv::chainpack::RpcSignal sig; sig.setMethod(shv::chainpack::Rpc::SIG_VAL_CHANGED); - sig.setShvPath(shvPath()); + sig.setShvPath(shvPath().asString()); sig.setParams(m_toRpcValue(m_value)); emitSendRpcMessage(sig); } diff --git a/libshviotqt/src/node/shvnode.cpp b/libshviotqt/src/node/shvnode.cpp index 068d573a9..98998cc7b 100644 --- a/libshviotqt/src/node/shvnode.cpp +++ b/libshviotqt/src/node/shvnode.cpp @@ -55,7 +55,7 @@ ShvNode *ShvNode::childNode(const ShvNode::String &name, bool throw_exc) const { auto *nd = findChild(QString::fromStdString(name), Qt::FindDirectChildrenOnly); if(throw_exc && !nd) - SHV_EXCEPTION("Child node id: " + name + " doesn't exist, parent node: " + shvPath()); + SHV_EXCEPTION("Child node id: " + name + " doesn't exist, parent node: " + shvPath().asString()); return nd; } @@ -85,7 +85,7 @@ void ShvNode::setNodeId(const ShvNode::String &n) shv::core::utils::ShvPath ShvNode::shvPath() const { - shv::core::utils::ShvPath ret; + std::string ret; const ShvNode *nd = this; while(nd) { if(!nd->isRootNode()) { @@ -142,15 +142,15 @@ void ShvNode::handleRpcFrame(RpcFrame &&frame) ShvNode *nd = childNode(std::string{shv_path_list.at(0)}, !shv::core::Exception::Throw); if(nd) { shvDebug() << "Child node:" << shv_path_list.at(0) << "on path:" << shv_path_list.join('/') << "FOUND"; - std::string new_path = ShvPath::joinDirs(++shv_path_list.begin(), shv_path_list.end()); - RpcMessage::setShvPath(frame.meta, new_path); + ShvPath new_path = ShvPath::joinDirs(++shv_path_list.begin(), shv_path_list.end()); + RpcMessage::setShvPath(frame.meta, new_path.asString()); nd->handleRpcFrame(std::move(frame)); return; } } const chainpack::MetaMethod *mm = metaMethod(shv_path_list, method); if(mm) { - shvDebug() << "Metamethod:" << method << "on path:" << ShvPath::joinDirs(shv_path_list) << "FOUND"; + shvDebug() << "Metamethod:" << method << "on path:" << ShvPath::joinDirs(shv_path_list).asString() << "FOUND"; std::string errmsg; RpcMessage rpc_msg = frame.toRpcMessage(&errmsg); if(!errmsg.empty()) @@ -163,7 +163,7 @@ void ShvNode::handleRpcFrame(RpcFrame &&frame) } } else { - string path = shv::core::utils::joinPath(shvPath(), shv_path_str); + std::string path = shv::core::utils::joinPath(shvPath().asString(), shv_path_str); throw chainpack::RpcException(RpcResponse::Error::MethodNotFound, "Method: '" + method + "' on path '" + path + "' doesn't exist", std::string(__FILE__) + ":" + std::to_string(__LINE__)); @@ -241,10 +241,10 @@ chainpack::RpcValue ShvNode::handleRpcRequestImpl(const chainpack::RpcRequest &r if(!shv_path.empty()) { ShvNode *nd = childNode(std::string{shv_path.at(0)}, !shv::core::Exception::Throw); if(nd) { - shvDebug() << "Child node:" << shv_path.at(0) << "on path:" << ShvPath::joinDirs(shv_path) << "FOUND"; - std::string new_path = ShvPath::joinDirs(++shv_path.begin(), shv_path.end()); + shvDebug() << "Child node:" << shv_path.at(0) << "on path:" << ShvPath::joinDirs(shv_path).asString() << "FOUND"; + ShvPath new_path = ShvPath::joinDirs(++shv_path.begin(), shv_path.end()); chainpack::RpcRequest rq2(rq); - rq2.setShvPath(new_path); + rq2.setShvPath(new_path.asString()); return nd->handleRpcRequestImpl(rq2); } } @@ -260,19 +260,19 @@ chainpack::RpcValue ShvNode::processRpcRequest(const chainpack::RpcRequest &rq) const chainpack::MetaMethod *mm = metaMethod(shv_path, method); if (!mm) { throw chainpack::RpcException(RpcResponse::Error::MethodNotFound, - "Method: '" + method + "' on path '" + shvPath() + '/' + rq.shvPath().toString() + "' doesn't exist", + "Method: '" + method + "' on path '" + shvPath().asString() + '/' + rq.shvPath().toString() + "' doesn't exist", std::string(__FILE__) + ":" + std::to_string(__LINE__)); } auto rq_grant = rq.accessGrant(); auto rq_access_level = rq_grant.accessLevel; auto mm_access_level = mm->accessLevel(); if(mm_access_level > rq_access_level) - SHV_EXCEPTION(std::string("Call method: '") + method + "' on path '" + shvPath() + '/' + rq.shvPath().toString() + SHV_EXCEPTION(std::string("Call method: '") + method + "' on path '" + shvPath().asString() + '/' + rq.shvPath().toString() + "' permission denied, grant: " + rq_grant.toPrettyString() + " required: " + accessLevelToAccessString(mm_access_level)); if(mm_access_level > AccessLevel::Write) { - shv::core::utils::ShvJournalEntry e(shv::core::utils::joinPath(shvPath(), rq.shvPath().asString()) + shv::core::utils::ShvJournalEntry e(shv::core::utils::joinPath(shvPath().asString(), rq.shvPath().asString()) , method + '(' + rq.params().toCpon() + ')' , shv::chainpack::Rpc::SIG_COMMAND_LOGGED , shv::core::utils::ShvJournalEntry::NO_SHORT_TIME @@ -322,7 +322,7 @@ ShvNode::StringList ShvNode::childNames(const StringViewList &shv_path) if(nd) ret = nd->childNames(StringViewList()); } - shvDebug() << "\tret:" << shv::core::String::join(ret, '+'); + shvDebug() << "\tret:" << shv::core::string::join(ret, '+'); return ret; } @@ -466,7 +466,7 @@ chainpack::RpcValue ShvNode::callMethod(const ShvNode::StringViewList &shv_path, if(method == Rpc::METH_LS) return ls(shv_path, params); - SHV_EXCEPTION("Node: " + shvPath() + " - method: " + method + " not exists on path: " + shv_path.join('/') + " user id: " + user_id.toCpon()); + SHV_EXCEPTION("Node: " + shvPath().asString() + " - method: " + method + " not exists on path: " + shv_path.join('/') + " user id: " + user_id.toCpon()); } ShvNode *ShvNode::rootNode() @@ -1029,7 +1029,7 @@ void ValueProxyShvNode::onShvValueChanged(int value_id, chainpack::RpcValue val) if(value_id == m_valueId && isSignal()) { RpcSignal sig; sig.setMethod(Rpc::SIG_VAL_CHANGED); - sig.setShvPath(shvPath()); + sig.setShvPath(shvPath().asString()); sig.setParams(val); emitSendRpcMessage(sig); } diff --git a/libshviotqt/src/node/shvnodetree.cpp b/libshviotqt/src/node/shvnodetree.cpp index 425252e44..8cd678c40 100644 --- a/libshviotqt/src/node/shvnodetree.cpp +++ b/libshviotqt/src/node/shvnodetree.cpp @@ -89,7 +89,7 @@ ShvNode *ShvNodeTree::mdcd(const ShvNode::StringViewList &path, bool create_dirs } if(path_rest) { auto path2 = ShvNode::StringViewList(path.begin() + static_cast(ix), path.end()); - *path_rest = shv::core::String::join(path2, '/'); + *path_rest = shv::core::string::join(path2, '/'); } return ret; } diff --git a/libshviotqt/src/rpc/deviceconnection.cpp b/libshviotqt/src/rpc/deviceconnection.cpp index 2d35b8d72..f9b6ea7b7 100644 --- a/libshviotqt/src/rpc/deviceconnection.cpp +++ b/libshviotqt/src/rpc/deviceconnection.cpp @@ -49,8 +49,8 @@ void DeviceConnection::setCliOptions(const DeviceAppCliOptions *cli_opts) device_id = device_id_from_file; } else { - shv::core::String::replace(device_id, "{{deviceIdFile}}", device_id_from_file); - shv::core::String::replace(device_id, "{{appName}}", QCoreApplication::applicationName().toStdString()); + shv::core::string::replace(device_id, "{{deviceIdFile}}", device_id_from_file); + shv::core::string::replace(device_id, "{{appName}}", QCoreApplication::applicationName().toStdString()); } if(!device_id.empty()) dev[cp::Rpc::KEY_DEVICE_ID] = device_id;