From aa2de65bad663d3d72e08fddb1c166e01cefa3e8 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 24 Nov 2017 20:13:00 -0800 Subject: [PATCH] Use Twine instead of StringRef where appropriate. (#259) Deprecated interfaces were not updated. --- src/main/native/cpp/Dispatcher.cpp | 14 +- src/main/native/cpp/Dispatcher.h | 7 +- src/main/native/cpp/IStorage.h | 6 +- src/main/native/cpp/Storage.cpp | 49 ++++-- src/main/native/cpp/Storage.h | 27 ++-- src/main/native/cpp/Storage_load.cpp | 11 +- src/main/native/cpp/Storage_save.cpp | 22 +-- src/main/native/cpp/jni/NetworkTablesJNI.cpp | 49 +++--- .../native/cpp/networktables/NetworkTable.cpp | 153 +++++++++--------- .../networktables/NetworkTableInstance.cpp | 19 +-- src/main/native/cpp/ntcore_cpp.cpp | 24 +-- .../include/networktables/NetworkTable.h | 35 ++-- .../include/networktables/NetworkTableEntry.h | 8 +- .../networktables/NetworkTableEntry.inl | 6 +- .../networktables/NetworkTableInstance.h | 24 +-- .../networktables/NetworkTableInstance.inl | 18 +-- .../include/networktables/NetworkTableValue.h | 6 +- src/main/native/include/ntcore_cpp.h | 31 ++-- src/main/native/include/tables/ITable.h | 16 +- 19 files changed, 282 insertions(+), 243 deletions(-) diff --git a/src/main/native/cpp/Dispatcher.cpp b/src/main/native/cpp/Dispatcher.cpp index 048b6daa69..31b6a4e0df 100644 --- a/src/main/native/cpp/Dispatcher.cpp +++ b/src/main/native/cpp/Dispatcher.cpp @@ -20,7 +20,7 @@ using namespace nt; -void Dispatcher::StartServer(llvm::StringRef persist_filename, +void Dispatcher::StartServer(const Twine& persist_filename, const char* listen_address, unsigned int port) { DispatcherBase::StartServer( persist_filename, @@ -115,7 +115,7 @@ DispatcherBase::~DispatcherBase() { Stop(); } unsigned int DispatcherBase::GetNetworkMode() const { return m_networkMode; } void DispatcherBase::StartServer( - StringRef persist_filename, + const Twine& persist_filename, std::unique_ptr acceptor) { { std::lock_guard lock(m_user_mutex); @@ -123,11 +123,13 @@ void DispatcherBase::StartServer( m_active = true; } m_networkMode = NT_NET_MODE_SERVER | NT_NET_MODE_STARTING; - m_persist_filename = persist_filename; + m_persist_filename = persist_filename.str(); m_server_acceptor = std::move(acceptor); // Load persistent file. Ignore errors, but pass along warnings. - if (!persist_filename.empty()) { + if (!persist_filename.isTriviallyEmpty() && + (!persist_filename.isSingleStringRef() || + !persist_filename.getSingleStringRef().empty())) { bool first = true; m_storage.LoadPersistent( persist_filename, [&](std::size_t line, const char* msg) { @@ -198,9 +200,9 @@ void DispatcherBase::SetUpdateRate(double interval) { m_update_rate = static_cast(interval * 1000); } -void DispatcherBase::SetIdentity(llvm::StringRef name) { +void DispatcherBase::SetIdentity(const Twine& name) { std::lock_guard lock(m_user_mutex); - m_identity = name; + m_identity = name.str(); } void DispatcherBase::Flush() { diff --git a/src/main/native/cpp/Dispatcher.h b/src/main/native/cpp/Dispatcher.h index 07bcdfcbd7..288dc51c8f 100644 --- a/src/main/native/cpp/Dispatcher.h +++ b/src/main/native/cpp/Dispatcher.h @@ -17,6 +17,7 @@ #include #include "llvm/StringRef.h" +#include "llvm/Twine.h" #include "support/condition_variable.h" #include "support/mutex.h" @@ -46,12 +47,12 @@ class DispatcherBase : public IDispatcher { virtual ~DispatcherBase(); unsigned int GetNetworkMode() const; - void StartServer(llvm::StringRef persist_filename, + void StartServer(const Twine& persist_filename, std::unique_ptr acceptor); void StartClient(); void Stop(); void SetUpdateRate(double interval); - void SetIdentity(llvm::StringRef name); + void SetIdentity(const Twine& name); void Flush(); std::vector GetConnections() const; bool IsConnected() const; @@ -133,7 +134,7 @@ class Dispatcher : public DispatcherBase { wpi::Logger& logger) : DispatcherBase(storage, notifier, logger) {} - void StartServer(StringRef persist_filename, const char* listen_address, + void StartServer(const Twine& persist_filename, const char* listen_address, unsigned int port); void SetServer(const char* server_name, unsigned int port); diff --git a/src/main/native/cpp/IStorage.h b/src/main/native/cpp/IStorage.h index 5e5a4d4e79..0d872f55b2 100644 --- a/src/main/native/cpp/IStorage.h +++ b/src/main/native/cpp/IStorage.h @@ -13,7 +13,7 @@ #include #include "llvm/ArrayRef.h" -#include "llvm/StringRef.h" +#include "llvm/Twine.h" #include "Message.h" #include "ntcore_cpp.h" @@ -53,10 +53,10 @@ class IStorage { // Filename-based save/load functions. Used both by periodic saves and // accessible directly via the user API. - virtual const char* SavePersistent(StringRef filename, + virtual const char* SavePersistent(const Twine& filename, bool periodic) const = 0; virtual const char* LoadPersistent( - StringRef filename, + const Twine& filename, std::function warn) = 0; }; diff --git a/src/main/native/cpp/Storage.cpp b/src/main/native/cpp/Storage.cpp index 17284eda67..b70f0b8dc2 100644 --- a/src/main/native/cpp/Storage.cpp +++ b/src/main/native/cpp/Storage.cpp @@ -757,30 +757,36 @@ void Storage::DeleteAllEntries() { dispatcher->QueueOutgoing(Message::ClearEntries(), nullptr, nullptr); } -Storage::Entry* Storage::GetOrNew(StringRef name) { - auto& entry = m_entries[name]; +Storage::Entry* Storage::GetOrNew(const Twine& name) { + llvm::SmallString<128> nameBuf; + StringRef nameStr = name.toStringRef(nameBuf); + auto& entry = m_entries[nameStr]; if (!entry) { - m_localmap.emplace_back(new Entry(name)); + m_localmap.emplace_back(new Entry(nameStr)); entry = m_localmap.back().get(); entry->local_id = m_localmap.size() - 1; } return entry; } -unsigned int Storage::GetEntry(StringRef name) { - if (name.empty()) return UINT_MAX; +unsigned int Storage::GetEntry(const Twine& name) { + if (name.isTriviallyEmpty() || + (name.isSingleStringRef() && name.getSingleStringRef().empty())) + return UINT_MAX; std::unique_lock lock(m_mutex); return GetOrNew(name)->local_id; } -std::vector Storage::GetEntries(StringRef prefix, +std::vector Storage::GetEntries(const Twine& prefix, unsigned int types) { + llvm::SmallString<128> prefixBuf; + StringRef prefixStr = prefix.toStringRef(prefixBuf); std::lock_guard lock(m_mutex); std::vector ids; for (auto& i : m_entries) { Entry* entry = i.getValue(); auto value = entry->value.get(); - if (!value || !i.getKey().startswith(prefix)) continue; + if (!value || !i.getKey().startswith(prefixStr)) continue; if (types != 0 && (types & value->type()) == 0) continue; ids.push_back(entry->local_id); } @@ -829,14 +835,16 @@ unsigned long long Storage::GetEntryLastChange(unsigned int local_id) const { return entry->value->last_change(); } -std::vector Storage::GetEntryInfo(int inst, StringRef prefix, +std::vector Storage::GetEntryInfo(int inst, const Twine& prefix, unsigned int types) { + llvm::SmallString<128> prefixBuf; + StringRef prefixStr = prefix.toStringRef(prefixBuf); std::lock_guard lock(m_mutex); std::vector infos; for (auto& i : m_entries) { Entry* entry = i.getValue(); auto value = entry->value.get(); - if (!value || !i.getKey().startswith(prefix)) continue; + if (!value || !i.getKey().startswith(prefixStr)) continue; if (types != 0 && (types & value->type()) == 0) continue; EntryInfo info; info.entry = Handle(inst, entry->local_id, Handle::kEntry); @@ -850,16 +858,18 @@ std::vector Storage::GetEntryInfo(int inst, StringRef prefix, } unsigned int Storage::AddListener( - StringRef prefix, + const Twine& prefix, std::function callback, unsigned int flags) const { + llvm::SmallString<128> prefixBuf; + StringRef prefixStr = prefix.toStringRef(prefixBuf); std::lock_guard lock(m_mutex); - unsigned int uid = m_notifier.Add(callback, prefix, flags); + unsigned int uid = m_notifier.Add(callback, prefixStr, flags); // perform immediate notifications if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0) { for (auto& i : m_entries) { Entry* entry = i.getValue(); - if (!entry->value || !i.getKey().startswith(prefix)) continue; + if (!entry->value || !i.getKey().startswith(prefixStr)) continue; m_notifier.NotifyEntry(entry->local_id, i.getKey(), entry->value, NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW, uid); } @@ -885,14 +895,17 @@ unsigned int Storage::AddListener( return uid; } -unsigned int Storage::AddPolledListener(unsigned int poller, StringRef prefix, +unsigned int Storage::AddPolledListener(unsigned int poller, + const Twine& prefix, unsigned int flags) const { + llvm::SmallString<128> prefixBuf; + StringRef prefixStr = prefix.toStringRef(prefixBuf); std::lock_guard lock(m_mutex); - unsigned int uid = m_notifier.AddPolled(poller, prefix, flags); + unsigned int uid = m_notifier.AddPolled(poller, prefixStr, flags); // perform immediate notifications if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0) { for (auto& i : m_entries) { - if (!i.getKey().startswith(prefix)) continue; + if (!i.getKey().startswith(prefixStr)) continue; Entry* entry = i.getValue(); if (!entry->value) continue; m_notifier.NotifyEntry(entry->local_id, i.getKey(), entry->value, @@ -949,9 +962,11 @@ bool Storage::GetPersistentEntries( } bool Storage::GetEntries( - StringRef prefix, + const Twine& prefix, std::vector>>* entries) const { + llvm::SmallString<128> prefixBuf; + StringRef prefixStr = prefix.toStringRef(prefixBuf); // copy values out of storage as quickly as possible so lock isn't held { std::lock_guard lock(m_mutex); @@ -959,7 +974,7 @@ bool Storage::GetEntries( for (auto& i : m_entries) { Entry* entry = i.getValue(); // only write values with given prefix - if (!entry->value || !i.getKey().startswith(prefix)) continue; + if (!entry->value || !i.getKey().startswith(prefixStr)) continue; entries->emplace_back(i.getKey(), entry->value); } } diff --git a/src/main/native/cpp/Storage.h b/src/main/native/cpp/Storage.h index 625bd92218..1d9937d053 100644 --- a/src/main/native/cpp/Storage.h +++ b/src/main/native/cpp/Storage.h @@ -97,11 +97,11 @@ class Storage : public IStorage { void DeleteAllEntries(); - std::vector GetEntryInfo(int inst, StringRef prefix, + std::vector GetEntryInfo(int inst, const Twine& prefix, unsigned int types); unsigned int AddListener( - StringRef prefix, + const Twine& prefix, std::function callback, unsigned int flags) const; unsigned int AddListener( @@ -109,14 +109,14 @@ class Storage : public IStorage { std::function callback, unsigned int flags) const; - unsigned int AddPolledListener(unsigned int poller_uid, StringRef prefix, + unsigned int AddPolledListener(unsigned int poller_uid, const Twine& prefix, unsigned int flags) const; unsigned int AddPolledListener(unsigned int poller_uid, unsigned int local_id, unsigned int flags) const; // Index-only - unsigned int GetEntry(StringRef name); - std::vector GetEntries(StringRef prefix, unsigned int types); + unsigned int GetEntry(const Twine& name); + std::vector GetEntries(const Twine& prefix, unsigned int types); EntryInfo GetEntryInfo(int inst, unsigned int local_id) const; std::string GetEntryName(unsigned int local_id) const; NT_Type GetEntryType(unsigned int local_id) const; @@ -124,23 +124,24 @@ class Storage : public IStorage { // Filename-based save/load functions. Used both by periodic saves and // accessible directly via the user API. - const char* SavePersistent(StringRef filename, bool periodic) const override; + const char* SavePersistent(const Twine& filename, + bool periodic) const override; const char* LoadPersistent( - StringRef filename, + const Twine& filename, std::function warn) override; - const char* SaveEntries(StringRef filename, StringRef prefix) const; + const char* SaveEntries(const Twine& filename, const Twine& prefix) const; const char* LoadEntries( - StringRef filename, StringRef prefix, + const Twine& filename, const Twine& prefix, std::function warn); // Stream-based save/load functions (exposed for testing purposes). These // implement the guts of the filename-based functions. void SavePersistent(llvm::raw_ostream& os, bool periodic) const; - bool LoadEntries(wpi::raw_istream& is, StringRef prefix, bool persistent, + bool LoadEntries(wpi::raw_istream& is, const Twine& prefix, bool persistent, std::function warn); - void SaveEntries(llvm::raw_ostream& os, StringRef prefix) const; + void SaveEntries(llvm::raw_ostream& os, const Twine& prefix) const; // RPC configuration needs to come through here as RPC definitions are // actually special Storage value types. @@ -237,7 +238,7 @@ class Storage : public IStorage { bool periodic, std::vector>>* entries) const; - bool GetEntries(StringRef prefix, + bool GetEntries(const Twine& prefix, std::vector>>* entries) const; void SetEntryValueImpl(Entry* entry, std::shared_ptr value, @@ -251,7 +252,7 @@ class Storage : public IStorage { template void DeleteAllEntriesImpl(bool local, F should_delete); void DeleteAllEntriesImpl(bool local); - Entry* GetOrNew(StringRef name); + Entry* GetOrNew(const Twine& name); }; } // namespace nt diff --git a/src/main/native/cpp/Storage_load.cpp b/src/main/native/cpp/Storage_load.cpp index b1e1955e68..72bcd5349b 100644 --- a/src/main/native/cpp/Storage_load.cpp +++ b/src/main/native/cpp/Storage_load.cpp @@ -362,13 +362,16 @@ std::shared_ptr LoadPersistentImpl::ReadStringArrayValue() { } bool Storage::LoadEntries( - wpi::raw_istream& is, StringRef prefix, bool persistent, + wpi::raw_istream& is, const Twine& prefix, bool persistent, std::function warn) { + llvm::SmallString<128> prefixBuf; + StringRef prefixStr = prefix.toStringRef(prefixBuf); + // entries to add std::vector entries; // load file - if (!LoadPersistentImpl(is, warn).Load(prefix, &entries)) return false; + if (!LoadPersistentImpl(is, warn).Load(prefixStr, &entries)) return false; // copy values into storage as quickly as possible so lock isn't held std::vector> msgs; @@ -431,7 +434,7 @@ bool Storage::LoadEntries( } const char* Storage::LoadPersistent( - StringRef filename, + const Twine& filename, std::function warn) { std::error_code ec; wpi::raw_fd_istream is(filename, ec); @@ -441,7 +444,7 @@ const char* Storage::LoadPersistent( } const char* Storage::LoadEntries( - StringRef filename, StringRef prefix, + const Twine& filename, const Twine& prefix, std::function warn) { std::error_code ec; wpi::raw_fd_istream is(filename, ec); diff --git a/src/main/native/cpp/Storage_save.cpp b/src/main/native/cpp/Storage_save.cpp index 0e72d4db9e..601adccc68 100644 --- a/src/main/native/cpp/Storage_save.cpp +++ b/src/main/native/cpp/Storage_save.cpp @@ -181,11 +181,13 @@ void Storage::SavePersistent(llvm::raw_ostream& os, bool periodic) const { SavePersistentImpl(os).Save(entries); } -const char* Storage::SavePersistent(StringRef filename, bool periodic) const { - llvm::SmallString<128> fn = filename; - llvm::SmallString<128> tmp = filename; +const char* Storage::SavePersistent(const Twine& filename, + bool periodic) const { + llvm::SmallString<128> fn; + filename.toVector(fn); + llvm::SmallString<128> tmp = fn; tmp += ".tmp"; - llvm::SmallString<128> bak = filename; + llvm::SmallString<128> bak = fn; bak += ".bak"; // Get entries before creating file @@ -225,17 +227,19 @@ done: return err; } -void Storage::SaveEntries(llvm::raw_ostream& os, StringRef prefix) const { +void Storage::SaveEntries(llvm::raw_ostream& os, const Twine& prefix) const { std::vector entries; if (!GetEntries(prefix, &entries)) return; SavePersistentImpl(os).Save(entries); } -const char* Storage::SaveEntries(StringRef filename, StringRef prefix) const { - llvm::SmallString<128> fn = filename; - llvm::SmallString<128> tmp = filename; +const char* Storage::SaveEntries(const Twine& filename, + const Twine& prefix) const { + llvm::SmallString<128> fn; + filename.toVector(fn); + llvm::SmallString<128> tmp = fn; tmp += ".tmp"; - llvm::SmallString<128> bak = filename; + llvm::SmallString<128> bak = fn; bak += ".bak"; // Get entries before creating file diff --git a/src/main/native/cpp/jni/NetworkTablesJNI.cpp b/src/main/native/cpp/jni/NetworkTablesJNI.cpp index 5bb3e99e4a..9f01a0ec42 100644 --- a/src/main/native/cpp/jni/NetworkTablesJNI.cpp +++ b/src/main/native/cpp/jni/NetworkTablesJNI.cpp @@ -387,7 +387,7 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntr nullPointerEx.Throw(env, "key cannot be null"); return false; } - return nt::GetEntry(inst, JStringRef{env, key}); + return nt::GetEntry(inst, JStringRef{env, key}.str()); } /* @@ -402,8 +402,8 @@ JNIEXPORT jintArray JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_ge nullPointerEx.Throw(env, "prefix cannot be null"); return nullptr; } - return MakeJIntArray(env, - nt::GetEntries(inst, JStringRef{env, prefix}, types)); + return MakeJIntArray( + env, nt::GetEntries(inst, JStringRef{env, prefix}.str(), types)); } /* @@ -484,12 +484,12 @@ JNIEXPORT jboolean JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_set return false; } if (force) { - nt::SetEntryTypeValue(entry, - nt::Value::MakeString(JStringRef{env, value}, time)); + nt::SetEntryTypeValue( + entry, nt::Value::MakeString(JStringRef{env, value}.str(), time)); return JNI_TRUE; } - return nt::SetEntryValue(entry, - nt::Value::MakeString(JStringRef{env, value}, time)); + return nt::SetEntryValue( + entry, nt::Value::MakeString(JStringRef{env, value}.str(), time)); } /* @@ -737,7 +737,7 @@ JNIEXPORT jboolean JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_set return false; } return nt::SetDefaultEntryValue( - entry, nt::Value::MakeString(JStringRef{env, defaultValue}, time)); + entry, nt::Value::MakeString(JStringRef{env, defaultValue}.str(), time)); } /* @@ -871,7 +871,7 @@ JNIEXPORT jobjectArray JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI nullPointerEx.Throw(env, "prefix cannot be null"); return nullptr; } - auto arr = nt::GetEntryInfo(inst, JStringRef{env, prefix}, types); + auto arr = nt::GetEntryInfo(inst, JStringRef{env, prefix}.str(), types); jobjectArray jarr = env->NewObjectArray(arr.size(), entryInfoCls, nullptr); if (!jarr) return nullptr; for (size_t i = 0; i < arr.size(); ++i) { @@ -915,7 +915,8 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_addPoll nullPointerEx.Throw(env, "prefix cannot be null"); return 0; } - return nt::AddPolledEntryListener(poller, JStringRef{env, prefix}, flags); + return nt::AddPolledEntryListener(poller, JStringRef{env, prefix}.str(), + flags); } /* @@ -1280,7 +1281,7 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_setNetw nullPointerEx.Throw(env, "name cannot be null"); return; } - nt::SetNetworkIdentity(inst, JStringRef{env, name}); + nt::SetNetworkIdentity(inst, JStringRef{env, name}.str()); } /* @@ -1310,7 +1311,7 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_startSe nullPointerEx.Throw(env, "listenAddress cannot be null"); return; } - nt::StartServer(inst, JStringRef{env, persistFilename}, + nt::StartServer(inst, JStringRef{env, persistFilename}.str(), JStringRef{env, listenAddress}.c_str(), port); } @@ -1573,7 +1574,7 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_savePer nullPointerEx.Throw(env, "filename cannot be null"); return; } - const char *err = nt::SavePersistent(inst, JStringRef{env, filename}); + const char *err = nt::SavePersistent(inst, JStringRef{env, filename}.str()); if (err) persistentEx.Throw(env, err); } @@ -1590,7 +1591,7 @@ JNIEXPORT jobjectArray JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI return nullptr; } std::vector warns; - const char* err = nt::LoadPersistent(inst, JStringRef{env, filename}, + const char* err = nt::LoadPersistent(inst, JStringRef{env, filename}.str(), [&](size_t line, const char* msg) { llvm::SmallString<128> warn; llvm::raw_svector_ostream oss(warn); @@ -1620,8 +1621,8 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI_saveEnt nullPointerEx.Throw(env, "prefix cannot be null"); return; } - const char* err = - nt::SaveEntries(inst, JStringRef{env, filename}, JStringRef{env, prefix}); + const char* err = nt::SaveEntries(inst, JStringRef{env, filename}.str(), + JStringRef{env, prefix}.str()); if (err) persistentEx.Throw(env, err); } @@ -1642,14 +1643,14 @@ JNIEXPORT jobjectArray JNICALL Java_edu_wpi_first_networktables_NetworkTablesJNI return nullptr; } std::vector warns; - const char* err = - nt::LoadEntries(inst, JStringRef{env, filename}, JStringRef{env, prefix}, - [&](size_t line, const char* msg) { - llvm::SmallString<128> warn; - llvm::raw_svector_ostream oss(warn); - oss << line << ": " << msg; - warns.emplace_back(oss.str()); - }); + const char* err = nt::LoadEntries(inst, JStringRef{env, filename}.str(), + JStringRef{env, prefix}.str(), + [&](size_t line, const char* msg) { + llvm::SmallString<128> warn; + llvm::raw_svector_ostream oss(warn); + oss << line << ": " << msg; + warns.emplace_back(oss.str()); + }); if (err) { persistentEx.Throw(env, err); return nullptr; diff --git a/src/main/native/cpp/networktables/NetworkTable.cpp b/src/main/native/cpp/networktables/NetworkTable.cpp index 138cfcea76..69d8e4ea32 100644 --- a/src/main/native/cpp/networktables/NetworkTable.cpp +++ b/src/main/native/cpp/networktables/NetworkTable.cpp @@ -28,35 +28,40 @@ StringRef NetworkTable::BasenameKey(StringRef key) { return key.substr(slash + 1); } -std::string NetworkTable::NormalizeKey(StringRef key, bool withLeadingSlash) { +std::string NetworkTable::NormalizeKey(const Twine& key, + bool withLeadingSlash) { llvm::SmallString<128> buf; return NormalizeKey(key, buf, withLeadingSlash); } -StringRef NetworkTable::NormalizeKey(StringRef key, +StringRef NetworkTable::NormalizeKey(const Twine& key, llvm::SmallVectorImpl& buf, bool withLeadingSlash) { buf.clear(); if (withLeadingSlash) buf.push_back(PATH_SEPARATOR_CHAR); // for each path element, add it with a slash following + llvm::SmallString<128> keyBuf; + StringRef keyStr = key.toStringRef(keyBuf); llvm::SmallVector parts; - key.split(parts, PATH_SEPARATOR_CHAR, -1, false); + keyStr.split(parts, PATH_SEPARATOR_CHAR, -1, false); for (auto i = parts.begin(); i != parts.end(); ++i) { buf.append(i->begin(), i->end()); buf.push_back(PATH_SEPARATOR_CHAR); } // remove trailing slash if the input key didn't have one - if (!key.empty() && key.back() != PATH_SEPARATOR_CHAR) buf.pop_back(); + if (!keyStr.empty() && keyStr.back() != PATH_SEPARATOR_CHAR) buf.pop_back(); return StringRef(buf.data(), buf.size()); } -std::vector NetworkTable::GetHierarchy(StringRef key) { +std::vector NetworkTable::GetHierarchy(const Twine& key) { std::vector hierarchy; hierarchy.emplace_back(1, PATH_SEPARATOR_CHAR); // for each path element, add it to the end of what we built previously + llvm::SmallString<128> keyBuf; + StringRef keyStr = key.toStringRef(keyBuf); llvm::SmallString<128> path; llvm::SmallVector parts; - key.split(parts, PATH_SEPARATOR_CHAR, -1, false); + keyStr.split(parts, PATH_SEPARATOR_CHAR, -1, false); if (!parts.empty()) { for (auto i = parts.begin(); i != parts.end(); ++i) { path += PATH_SEPARATOR_CHAR; @@ -64,7 +69,7 @@ std::vector NetworkTable::GetHierarchy(StringRef key) { hierarchy.emplace_back(path.str()); } // handle trailing slash - if (key.back() == PATH_SEPARATOR_CHAR) { + if (keyStr.back() == PATH_SEPARATOR_CHAR) { path += PATH_SEPARATOR_CHAR; hierarchy.emplace_back(path.str()); } @@ -116,7 +121,7 @@ void NetworkTable::SetIPAddress(StringRef address) { inst.StartDSClient(s_port); } -void NetworkTable::SetIPAddress(llvm::ArrayRef addresses) { +void NetworkTable::SetIPAddress(ArrayRef addresses) { auto inst = NetworkTableInstance::GetDefault(); llvm::SmallVector servers; for (const auto& ip_address : addresses) servers.emplace_back(ip_address); @@ -159,12 +164,12 @@ void NetworkTable::SetUpdateRate(double interval) { NetworkTableInstance::GetDefault().SetUpdateRate(interval); } -const char* NetworkTable::SavePersistent(llvm::StringRef filename) { +const char* NetworkTable::SavePersistent(StringRef filename) { return NetworkTableInstance::GetDefault().SavePersistent(filename); } const char* NetworkTable::LoadPersistent( - llvm::StringRef filename, + StringRef filename, std::function warn) { return NetworkTableInstance::GetDefault().LoadPersistent(filename, warn); } @@ -174,8 +179,8 @@ std::shared_ptr NetworkTable::GetTable(StringRef key) { return NetworkTableInstance::GetDefault().GetTable(key); } -NetworkTable::NetworkTable(NT_Inst inst, StringRef path, const private_init&) - : m_inst(inst), m_path(path) {} +NetworkTable::NetworkTable(NT_Inst inst, const Twine& path, const private_init&) + : m_inst(inst), m_path(path.str()) {} NetworkTable::~NetworkTable() { for (auto& i : m_listeners) RemoveEntryListener(i.second); @@ -185,25 +190,22 @@ NetworkTableInstance NetworkTable::GetInstance() const { return NetworkTableInstance{m_inst}; } -NetworkTableEntry NetworkTable::GetEntry(llvm::StringRef key) const { +NetworkTableEntry NetworkTable::GetEntry(const Twine& key) const { + llvm::SmallString<128> keyBuf; + StringRef keyStr = key.toStringRef(keyBuf); std::lock_guard lock(m_mutex); - NT_Entry& entry = m_entries[key]; + NT_Entry& entry = m_entries[keyStr]; if (entry == 0) { - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - path += key; - entry = nt::GetEntry(m_inst, path); + entry = nt::GetEntry(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR) + keyStr); } return NetworkTableEntry{entry}; } NT_EntryListener NetworkTable::AddEntryListener(TableEntryListener listener, unsigned int flags) const { - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - std::size_t prefix_len = path.size(); + std::size_t prefix_len = m_path.size() + 1; return nt::AddEntryListener( - m_inst, path, + m_inst, m_path + Twine(PATH_SEPARATOR_CHAR), [=](const EntryNotification& event) { StringRef relative_key = event.name.substr(prefix_len); if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) return; @@ -213,7 +215,7 @@ NT_EntryListener NetworkTable::AddEntryListener(TableEntryListener listener, flags); } -NT_EntryListener NetworkTable::AddEntryListener(StringRef key, +NT_EntryListener NetworkTable::AddEntryListener(const Twine& key, TableEntryListener listener, unsigned int flags) const { std::size_t prefix_len = m_path.size() + 1; @@ -269,14 +271,11 @@ void NetworkTable::AddTableListener(StringRef key, ITableListener* listener, void NetworkTable::AddTableListenerEx(StringRef key, ITableListener* listener, unsigned int flags) { std::lock_guard lock(m_mutex); - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - std::size_t prefix_len = path.size(); - path += key; + std::size_t prefix_len = m_path.size() + 1; + auto entry = GetEntry(key); NT_EntryListener id = nt::AddEntryListener( - m_inst, path, + entry.GetHandle(), [=](const EntryNotification& event) { - if (event.name != path) return; listener->ValueChangedEx(this, event.name.substr(prefix_len), event.value, event.flags); }, @@ -291,9 +290,7 @@ void NetworkTable::AddSubTableListener(ITableListener* listener) { void NetworkTable::AddSubTableListener(ITableListener* listener, bool localNotify) { std::lock_guard lock(m_mutex); - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - std::size_t prefix_len = path.size(); + std::size_t prefix_len = m_path.size() + 1; // The lambda needs to be copyable, but StringMap is not, so use // a shared_ptr to it. @@ -302,7 +299,7 @@ void NetworkTable::AddSubTableListener(ITableListener* listener, unsigned int flags = NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE; if (localNotify) flags |= NT_NOTIFY_LOCAL; NT_EntryListener id = nt::AddEntryListener( - m_inst, path, + m_inst, m_path + Twine(PATH_SEPARATOR_CHAR), [=](const EntryNotification& event) { StringRef relative_key = event.name.substr(prefix_len); auto end_sub_table = relative_key.find(PATH_SEPARATOR_CHAR); @@ -328,33 +325,34 @@ void NetworkTable::RemoveTableListener(ITableListener* listener) { m_listeners.erase(matches_begin, m_listeners.end()); } -std::shared_ptr NetworkTable::GetSubTable(StringRef key) const { - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - path += key; - return std::make_shared(m_inst, path, private_init{}); +std::shared_ptr NetworkTable::GetSubTable( + const Twine& key) const { + return std::make_shared( + m_inst, m_path + Twine(PATH_SEPARATOR_CHAR) + key, private_init{}); } -bool NetworkTable::ContainsKey(StringRef key) const { - return !key.empty() && GetEntry(key).Exists(); +bool NetworkTable::ContainsKey(const Twine& key) const { + if (key.isTriviallyEmpty() || + (key.isSingleStringRef() && key.getSingleStringRef().empty())) + return false; + return GetEntry(key).Exists(); } -bool NetworkTable::ContainsSubTable(StringRef key) const { - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - path += key; - path += PATH_SEPARATOR_CHAR; - return !GetEntryInfo(m_inst, path, 0).empty(); +bool NetworkTable::ContainsSubTable(const Twine& key) const { + return !GetEntryInfo(m_inst, + m_path + Twine(PATH_SEPARATOR_CHAR) + key + + Twine(PATH_SEPARATOR_CHAR), + 0) + .empty(); } std::vector NetworkTable::GetKeys(int types) const { std::vector keys; - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - auto infos = GetEntryInfo(m_inst, path, types); + size_t prefix_len = m_path.size() + 1; + auto infos = GetEntryInfo(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR), types); std::lock_guard lock(m_mutex); for (auto& info : infos) { - auto relative_key = StringRef(info.name).substr(path.size()); + auto relative_key = StringRef(info.name).substr(prefix_len); if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) continue; keys.push_back(relative_key); m_entries[relative_key] = info.entry; @@ -364,10 +362,10 @@ std::vector NetworkTable::GetKeys(int types) const { std::vector NetworkTable::GetSubTables() const { std::vector keys; - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - for (auto& entry : GetEntryInfo(m_inst, path, 0)) { - auto relative_key = StringRef(entry.name).substr(path.size()); + size_t prefix_len = m_path.size() + 1; + for (auto& entry : + GetEntryInfo(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR), 0)) { + auto relative_key = StringRef(entry.name).substr(prefix_len); std::size_t end_subtable = relative_key.find(PATH_SEPARATOR_CHAR); if (end_subtable == StringRef::npos) continue; keys.push_back(relative_key.substr(0, end_subtable)); @@ -399,7 +397,7 @@ unsigned int NetworkTable::GetFlags(StringRef key) const { return GetEntry(key).GetFlags(); } -void NetworkTable::Delete(StringRef key) { GetEntry(key).Delete(); } +void NetworkTable::Delete(const Twine& key) { GetEntry(key).Delete(); } bool NetworkTable::PutNumber(StringRef key, double value) { return GetEntry(key).SetDouble(value); @@ -438,52 +436,49 @@ bool NetworkTable::GetBoolean(StringRef key, bool defaultValue) const { return GetEntry(key).GetBoolean(defaultValue); } -bool NetworkTable::PutBooleanArray(llvm::StringRef key, - llvm::ArrayRef value) { +bool NetworkTable::PutBooleanArray(StringRef key, ArrayRef value) { return GetEntry(key).SetBooleanArray(value); } bool NetworkTable::SetDefaultBooleanArray(StringRef key, - llvm::ArrayRef defaultValue) { + ArrayRef defaultValue) { return GetEntry(key).SetDefaultBooleanArray(defaultValue); } std::vector NetworkTable::GetBooleanArray( - llvm::StringRef key, llvm::ArrayRef defaultValue) const { + StringRef key, ArrayRef defaultValue) const { return GetEntry(key).GetBooleanArray(defaultValue); } -bool NetworkTable::PutNumberArray(llvm::StringRef key, - llvm::ArrayRef value) { +bool NetworkTable::PutNumberArray(StringRef key, ArrayRef value) { return GetEntry(key).SetDoubleArray(value); } bool NetworkTable::SetDefaultNumberArray(StringRef key, - llvm::ArrayRef defaultValue) { + ArrayRef defaultValue) { return GetEntry(key).SetDefaultDoubleArray(defaultValue); } std::vector NetworkTable::GetNumberArray( - llvm::StringRef key, llvm::ArrayRef defaultValue) const { + StringRef key, ArrayRef defaultValue) const { return GetEntry(key).GetDoubleArray(defaultValue); } -bool NetworkTable::PutStringArray(llvm::StringRef key, - llvm::ArrayRef value) { +bool NetworkTable::PutStringArray(StringRef key, ArrayRef value) { return GetEntry(key).SetStringArray(value); } bool NetworkTable::SetDefaultStringArray( - StringRef key, llvm::ArrayRef defaultValue) { + StringRef key, ArrayRef defaultValue) { return GetEntry(key).SetDefaultStringArray(defaultValue); } std::vector NetworkTable::GetStringArray( - llvm::StringRef key, llvm::ArrayRef defaultValue) const { + StringRef key, ArrayRef defaultValue) const { return GetEntry(key).GetStringArray(defaultValue); } -bool NetworkTable::PutRaw(llvm::StringRef key, llvm::StringRef value) { +bool NetworkTable::PutRaw(StringRef key, StringRef value) { return GetEntry(key).SetRaw(value); } @@ -491,36 +486,32 @@ bool NetworkTable::SetDefaultRaw(StringRef key, StringRef defaultValue) { return GetEntry(key).SetDefaultRaw(defaultValue); } -std::string NetworkTable::GetRaw(llvm::StringRef key, - llvm::StringRef defaultValue) const { +std::string NetworkTable::GetRaw(StringRef key, StringRef defaultValue) const { return GetEntry(key).GetRaw(defaultValue); } -bool NetworkTable::PutValue(StringRef key, std::shared_ptr value) { +bool NetworkTable::PutValue(const Twine& key, std::shared_ptr value) { return GetEntry(key).SetValue(value); } -bool NetworkTable::SetDefaultValue(StringRef key, +bool NetworkTable::SetDefaultValue(const Twine& key, std::shared_ptr defaultValue) { return GetEntry(key).SetDefaultValue(defaultValue); } -std::shared_ptr NetworkTable::GetValue(StringRef key) const { +std::shared_ptr NetworkTable::GetValue(const Twine& key) const { return GetEntry(key).GetValue(); } StringRef NetworkTable::GetPath() const { return m_path; } -const char* NetworkTable::SaveEntries(StringRef filename) const { - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - return nt::SaveEntries(m_inst, filename, path); +const char* NetworkTable::SaveEntries(const Twine& filename) const { + return nt::SaveEntries(m_inst, filename, m_path + Twine(PATH_SEPARATOR_CHAR)); } const char* NetworkTable::LoadEntries( - StringRef filename, + const Twine& filename, std::function warn) { - llvm::SmallString<128> path(m_path); - path += PATH_SEPARATOR_CHAR; - return nt::LoadEntries(m_inst, filename, path, warn); + return nt::LoadEntries(m_inst, filename, m_path + Twine(PATH_SEPARATOR_CHAR), + warn); } diff --git a/src/main/native/cpp/networktables/NetworkTableInstance.cpp b/src/main/native/cpp/networktables/NetworkTableInstance.cpp index 1e11edcbc7..39b790825f 100644 --- a/src/main/native/cpp/networktables/NetworkTableInstance.cpp +++ b/src/main/native/cpp/networktables/NetworkTableInstance.cpp @@ -11,19 +11,20 @@ using namespace nt; std::shared_ptr NetworkTableInstance::GetTable( - StringRef key) const { - if (key.empty() || key == "/") { + const Twine& key) const { + StringRef simple; + bool isSimple = key.isSingleStringRef(); + if (isSimple) simple = key.getSingleStringRef(); + if (isSimple && (simple.empty() || simple == "/")) { return std::make_shared(m_handle, "", NetworkTable::private_init{}); - } else if (key[0] == NetworkTable::PATH_SEPARATOR_CHAR) { + } else if (isSimple && simple[0] == NetworkTable::PATH_SEPARATOR_CHAR) { return std::make_shared(m_handle, key, NetworkTable::private_init{}); } else { - llvm::SmallString<128> path; - path += NetworkTable::PATH_SEPARATOR_CHAR; - path += key; - return std::make_shared(m_handle, path, - NetworkTable::private_init{}); + return std::make_shared( + m_handle, Twine(NetworkTable::PATH_SEPARATOR_CHAR) + key, + NetworkTable::private_init{}); } } @@ -44,7 +45,7 @@ void NetworkTableInstance::SetServer(ArrayRef servers, } NT_EntryListener NetworkTableInstance::AddEntryListener( - StringRef prefix, + const Twine& prefix, std::function callback, unsigned int flags) const { return ::nt::AddEntryListener(m_handle, prefix, callback, flags); diff --git a/src/main/native/cpp/ntcore_cpp.cpp b/src/main/native/cpp/ntcore_cpp.cpp index 4b93e36404..6077e94d1a 100644 --- a/src/main/native/cpp/ntcore_cpp.cpp +++ b/src/main/native/cpp/ntcore_cpp.cpp @@ -52,7 +52,7 @@ NT_Inst GetInstanceFromHandle(NT_Handle handle) { * Table Functions */ -NT_Entry GetEntry(NT_Inst inst, StringRef name) { +NT_Entry GetEntry(NT_Inst inst, const Twine& name) { int i = Handle{inst}.GetTypedInst(Handle::kInstance); auto ii = InstanceImpl::Get(i); if (!ii) return 0; @@ -62,7 +62,7 @@ NT_Entry GetEntry(NT_Inst inst, StringRef name) { return Handle(i, id, Handle::kEntry); } -std::vector GetEntries(NT_Inst inst, StringRef prefix, +std::vector GetEntries(NT_Inst inst, const Twine& prefix, unsigned int types) { int i = Handle{inst}.GetTypedInst(Handle::kInstance); auto ii = InstanceImpl::Get(i); @@ -208,7 +208,7 @@ std::vector GetEntryInfo(StringRef prefix, unsigned int types) { return InstanceImpl::GetDefault()->storage.GetEntryInfo(0, prefix, types); } -std::vector GetEntryInfo(NT_Inst inst, StringRef prefix, +std::vector GetEntryInfo(NT_Inst inst, const Twine& prefix, unsigned int types) { int i = Handle{inst}.GetTypedInst(Handle::kInstance); auto ii = InstanceImpl::Get(i); @@ -250,7 +250,7 @@ NT_EntryListener AddEntryListener(StringRef prefix, } NT_EntryListener AddEntryListener( - NT_Inst inst, StringRef prefix, + NT_Inst inst, const Twine& prefix, std::function callback, unsigned int flags) { int i = Handle{inst}.GetTypedInst(Handle::kInstance); @@ -294,7 +294,8 @@ void DestroyEntryListenerPoller(NT_EntryListenerPoller poller) { } NT_EntryListener AddPolledEntryListener(NT_EntryListenerPoller poller, - StringRef prefix, unsigned int flags) { + const Twine& prefix, + unsigned int flags) { Handle handle{poller}; int id = handle.GetTypedIndex(Handle::kEntryListenerPoller); int i = handle.GetInst(); @@ -722,7 +723,7 @@ void SetNetworkIdentity(StringRef name) { InstanceImpl::GetDefault()->dispatcher.SetIdentity(name); } -void SetNetworkIdentity(NT_Inst inst, StringRef name) { +void SetNetworkIdentity(NT_Inst inst, const Twine& name) { auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance)); if (!ii) return; @@ -746,7 +747,7 @@ void StartServer(StringRef persist_filename, const char* listen_address, ii->dispatcher.StartServer(persist_filename, listen_address, port); } -void StartServer(NT_Inst inst, StringRef persist_filename, +void StartServer(NT_Inst inst, const Twine& persist_filename, const char* listen_address, unsigned int port) { auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance)); if (!ii) return; @@ -914,7 +915,7 @@ const char* SavePersistent(StringRef filename) { return InstanceImpl::GetDefault()->storage.SavePersistent(filename, false); } -const char* SavePersistent(NT_Inst inst, StringRef filename) { +const char* SavePersistent(NT_Inst inst, const Twine& filename) { auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance)); if (!ii) return "invalid instance handle"; @@ -928,7 +929,7 @@ const char* LoadPersistent( } const char* LoadPersistent( - NT_Inst inst, StringRef filename, + NT_Inst inst, const Twine& filename, std::function warn) { auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance)); if (!ii) return "invalid instance handle"; @@ -936,7 +937,8 @@ const char* LoadPersistent( return ii->storage.LoadPersistent(filename, warn); } -const char* SaveEntries(NT_Inst inst, StringRef filename, StringRef prefix) { +const char* SaveEntries(NT_Inst inst, const Twine& filename, + const Twine& prefix) { auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance)); if (!ii) return "invalid instance handle"; @@ -944,7 +946,7 @@ const char* SaveEntries(NT_Inst inst, StringRef filename, StringRef prefix) { } const char* LoadEntries( - NT_Inst inst, StringRef filename, StringRef prefix, + NT_Inst inst, const Twine& filename, const Twine& prefix, std::function warn) { auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance)); if (!ii) return "invalid instance handle"; diff --git a/src/main/native/include/networktables/NetworkTable.h b/src/main/native/include/networktables/NetworkTable.h index 2e23a2154d..d2b77adb3e 100644 --- a/src/main/native/include/networktables/NetworkTable.h +++ b/src/main/native/include/networktables/NetworkTable.h @@ -13,6 +13,7 @@ #include "llvm/ArrayRef.h" #include "llvm/StringMap.h" +#include "llvm/Twine.h" #include "networktables/NetworkTableEntry.h" #include "networktables/TableEntryListener.h" #include "networktables/TableListener.h" @@ -24,6 +25,7 @@ namespace nt { using llvm::ArrayRef; using llvm::StringRef; +using llvm::Twine; class NetworkTableInstance; @@ -79,9 +81,11 @@ class NetworkTable final : public ITable { * with a leading slash * @return normalized key */ - static std::string NormalizeKey(StringRef key, bool withLeadingSlash = true); + static std::string NormalizeKey(const Twine& key, + bool withLeadingSlash = true); - static StringRef NormalizeKey(StringRef key, llvm::SmallVectorImpl& buf, + static StringRef NormalizeKey(const Twine& key, + llvm::SmallVectorImpl& buf, bool withLeadingSlash = true); /** @@ -91,13 +95,13 @@ class NetworkTable final : public ITable { * @param key the key * @return List of super tables */ - static std::vector GetHierarchy(StringRef key); + static std::vector GetHierarchy(const Twine& key); /** * Constructor. Use NetworkTableInstance::GetTable() or GetSubTable() * instead. */ - NetworkTable(NT_Inst inst, StringRef path, const private_init&); + NetworkTable(NT_Inst inst, const Twine& path, const private_init&); virtual ~NetworkTable(); /** @@ -273,7 +277,7 @@ class NetworkTable final : public ITable { * @param key the key name * @return Network table entry. */ - NetworkTableEntry GetEntry(StringRef key) const; + NetworkTableEntry GetEntry(const Twine& key) const; /** * Listen to keys only within this table. @@ -291,7 +295,8 @@ class NetworkTable final : public ITable { * @param flags EntryListenerFlags bitmask * @return Listener handle */ - NT_EntryListener AddEntryListener(StringRef key, TableEntryListener listener, + NT_EntryListener AddEntryListener(const Twine& key, + TableEntryListener listener, unsigned int flags) const; /** @@ -356,7 +361,7 @@ class NetworkTable final : public ITable { * @param key the key name * @return the networktable to be returned */ - std::shared_ptr GetSubTable(StringRef key) const override; + std::shared_ptr GetSubTable(const Twine& key) const override; /** * Determines whether the given key is in this table. @@ -364,7 +369,7 @@ class NetworkTable final : public ITable { * @param key the key to search for * @return true if the table as a value assigned to the given key */ - bool ContainsKey(StringRef key) const override; + bool ContainsKey(const Twine& key) const override; /** * Determines whether there exists a non-empty subtable for this key @@ -374,7 +379,7 @@ class NetworkTable final : public ITable { * @return true if there is a subtable with the key which contains at least * one key/subtable of its own */ - bool ContainsSubTable(StringRef key) const override; + bool ContainsSubTable(const Twine& key) const override; /** * Gets all keys in the table (not including sub-tables). @@ -443,7 +448,7 @@ class NetworkTable final : public ITable { * * @param key the key name */ - void Delete(StringRef key) override; + void Delete(const Twine& key) override; /** * Put a number in the table @@ -665,7 +670,7 @@ class NetworkTable final : public ITable { * @param value the value that will be assigned * @return False if the table key already exists with a different type */ - bool PutValue(StringRef key, std::shared_ptr value) override; + bool PutValue(const Twine& key, std::shared_ptr value) override; /** * Gets the current value in the table, setting it if it does not exist. @@ -673,7 +678,7 @@ class NetworkTable final : public ITable { * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - bool SetDefaultValue(StringRef key, + bool SetDefaultValue(const Twine& key, std::shared_ptr defaultValue) override; /** @@ -683,7 +688,7 @@ class NetworkTable final : public ITable { * @return the value associated with the given key, or nullptr if the key * does not exist */ - std::shared_ptr GetValue(StringRef key) const override; + std::shared_ptr GetValue(const Twine& key) const override; /** * Gets the full path of this table. Does not include the trailing "/". @@ -697,7 +702,7 @@ class NetworkTable final : public ITable { * @param filename filename * @return error string, or nullptr if successful */ - const char* SaveEntries(StringRef filename) const; + const char* SaveEntries(const Twine& filename) const; /** * Load table values from a file. The file format used is identical to @@ -707,7 +712,7 @@ class NetworkTable final : public ITable { * @return error string, or nullptr if successful */ const char* LoadEntries( - StringRef filename, + const Twine& filename, std::function warn); }; diff --git a/src/main/native/include/networktables/NetworkTableEntry.h b/src/main/native/include/networktables/NetworkTableEntry.h index b3b7adbb1f..ef708a0415 100644 --- a/src/main/native/include/networktables/NetworkTableEntry.h +++ b/src/main/native/include/networktables/NetworkTableEntry.h @@ -12,6 +12,7 @@ #include #include "llvm/StringRef.h" +#include "llvm/Twine.h" #include "networktables/NetworkTableType.h" #include "networktables/NetworkTableValue.h" @@ -23,6 +24,7 @@ namespace nt { using llvm::ArrayRef; using llvm::StringRef; +using llvm::Twine; class NetworkTableInstance; @@ -206,7 +208,7 @@ class NetworkTableEntry final { * @param defaultValue the default value to set * @return False if the entry exists with a different type */ - bool SetDefaultString(StringRef defaultValue); + bool SetDefaultString(const Twine& defaultValue); /** * Sets the entry's value if it does not exist. @@ -262,7 +264,7 @@ class NetworkTableEntry final { * @param value the value to set * @return False if the entry exists with a different type */ - bool SetString(StringRef value); + bool SetString(const Twine& value); /** * Sets the entry's value. @@ -318,7 +320,7 @@ class NetworkTableEntry final { * changed to match the new value. * @param value the value to set */ - void ForceSetString(StringRef value); + void ForceSetString(const Twine& value); /** * Sets the entry's value. If the value is of different type, the type is diff --git a/src/main/native/include/networktables/NetworkTableEntry.inl b/src/main/native/include/networktables/NetworkTableEntry.inl index bd2ee70872..8f67e67e2d 100644 --- a/src/main/native/include/networktables/NetworkTableEntry.inl +++ b/src/main/native/include/networktables/NetworkTableEntry.inl @@ -102,7 +102,7 @@ inline bool NetworkTableEntry::SetDefaultDouble(double defaultValue) { return SetDefaultEntryValue(m_handle, Value::MakeDouble(defaultValue)); } -inline bool NetworkTableEntry::SetDefaultString(StringRef defaultValue) { +inline bool NetworkTableEntry::SetDefaultString(const Twine& defaultValue) { return SetDefaultEntryValue(m_handle, Value::MakeString(defaultValue)); } @@ -137,7 +137,7 @@ inline bool NetworkTableEntry::SetDouble(double value) { return SetEntryValue(m_handle, Value::MakeDouble(value)); } -inline bool NetworkTableEntry::SetString(StringRef value) { +inline bool NetworkTableEntry::SetString(const Twine& value) { return SetEntryValue(m_handle, Value::MakeString(value)); } @@ -169,7 +169,7 @@ inline void NetworkTableEntry::ForceSetDouble(double value) { SetEntryTypeValue(m_handle, Value::MakeDouble(value)); } -inline void NetworkTableEntry::ForceSetString(StringRef value) { +inline void NetworkTableEntry::ForceSetString(const Twine& value) { SetEntryTypeValue(m_handle, Value::MakeString(value)); } diff --git a/src/main/native/include/networktables/NetworkTableInstance.h b/src/main/native/include/networktables/NetworkTableInstance.h index 8d75d3e4ab..68f9e1f220 100644 --- a/src/main/native/include/networktables/NetworkTableInstance.h +++ b/src/main/native/include/networktables/NetworkTableInstance.h @@ -15,6 +15,7 @@ #include "llvm/ArrayRef.h" #include "llvm/StringRef.h" +#include "llvm/Twine.h" #include "networktables/NetworkTable.h" #include "networktables/NetworkTableEntry.h" @@ -37,6 +38,7 @@ namespace nt { using llvm::ArrayRef; using llvm::StringRef; +using llvm::Twine; /** NetworkTables Instance. * @@ -136,7 +138,7 @@ class NetworkTableInstance final { * @param name Key * @return Network table entry. */ - NetworkTableEntry GetEntry(StringRef name); + NetworkTableEntry GetEntry(const Twine& name); /** * Get entries starting with the given prefix. @@ -148,7 +150,7 @@ class NetworkTableInstance final { * @param types bitmask of types; 0 is treated as a "don't care" * @return Array of entries. */ - std::vector GetEntries(StringRef prefix, + std::vector GetEntries(const Twine& prefix, unsigned int types); /** @@ -161,7 +163,7 @@ class NetworkTableInstance final { * @param types bitmask of types; 0 is treated as a "don't care" * @return Array of entry information. */ - std::vector GetEntryInfo(StringRef prefix, + std::vector GetEntryInfo(const Twine& prefix, unsigned int types) const; /** @@ -170,7 +172,7 @@ class NetworkTableInstance final { * @param key the key name * @return The network table */ - std::shared_ptr GetTable(StringRef key) const; + std::shared_ptr GetTable(const Twine& key) const; /** * Deletes ALL keys in ALL subtables (except persistent values). @@ -192,7 +194,7 @@ class NetworkTableInstance final { * @return Listener handle */ NT_EntryListener AddEntryListener( - StringRef prefix, + const Twine& prefix, std::function callback, unsigned int flags) const; @@ -279,7 +281,7 @@ class NetworkTableInstance final { * visible through ConnectionInfo on the remote node. * @param name identity to advertise */ - void SetNetworkIdentity(StringRef name); + void SetNetworkIdentity(const Twine& name); /** * Get the current network mode. @@ -296,7 +298,7 @@ class NetworkTableInstance final { * address (UTF-8 string, null terminated) * @param port port to communicate over */ - void StartServer(StringRef persist_filename = "networktables.ini", + void StartServer(const Twine& persist_filename = "networktables.ini", const char* listen_address = "", unsigned int port = kDefaultPort); @@ -441,7 +443,7 @@ class NetworkTableInstance final { * @param filename filename * @return error string, or nullptr if successful */ - const char* SavePersistent(StringRef filename) const; + const char* SavePersistent(const Twine& filename) const; /** * Load persistent values from a file. The server automatically does this @@ -452,7 +454,7 @@ class NetworkTableInstance final { * @return error string, or nullptr if successful */ const char* LoadPersistent( - StringRef filename, + const Twine& filename, std::function warn); /** @@ -462,7 +464,7 @@ class NetworkTableInstance final { * @param prefix save only keys starting with this prefix * @return error string, or nullptr if successful */ - const char* SaveEntries(StringRef filename, StringRef prefix) const; + const char* SaveEntries(const Twine& filename, const Twine& prefix) const; /** * Load table values from a file. The file format used is identical to @@ -473,7 +475,7 @@ class NetworkTableInstance final { * @return error string, or nullptr if successful */ const char* LoadEntries( - StringRef filename, StringRef prefix, + const Twine& filename, const Twine& prefix, std::function warn); /** @} */ diff --git a/src/main/native/include/networktables/NetworkTableInstance.inl b/src/main/native/include/networktables/NetworkTableInstance.inl index 82403f11b8..23a6c2201f 100644 --- a/src/main/native/include/networktables/NetworkTableInstance.inl +++ b/src/main/native/include/networktables/NetworkTableInstance.inl @@ -29,12 +29,12 @@ inline void NetworkTableInstance::Destroy(NetworkTableInstance inst) { inline NT_Inst NetworkTableInstance::GetHandle() const { return m_handle; } -inline NetworkTableEntry NetworkTableInstance::GetEntry(StringRef name) { +inline NetworkTableEntry NetworkTableInstance::GetEntry(const Twine& name) { return NetworkTableEntry{::nt::GetEntry(m_handle, name)}; } inline std::vector NetworkTableInstance::GetEntries( - StringRef prefix, unsigned int types) { + const Twine& prefix, unsigned int types) { std::vector entries; for (auto entry : ::nt::GetEntries(m_handle, prefix, types)) entries.emplace_back(entry); @@ -42,7 +42,7 @@ inline std::vector NetworkTableInstance::GetEntries( } inline std::vector NetworkTableInstance::GetEntryInfo( - StringRef prefix, unsigned int types) const { + const Twine& prefix, unsigned int types) const { return ::nt::GetEntryInfo(m_handle, prefix, types); } @@ -73,7 +73,7 @@ inline bool NetworkTableInstance::WaitForRpcCallQueue(double timeout) { return ::nt::WaitForRpcCallQueue(m_handle, timeout); } -inline void NetworkTableInstance::SetNetworkIdentity(StringRef name) { +inline void NetworkTableInstance::SetNetworkIdentity(const Twine& name) { ::nt::SetNetworkIdentity(m_handle, name); } @@ -81,7 +81,7 @@ inline unsigned int NetworkTableInstance::GetNetworkMode() const { return ::nt::GetNetworkMode(m_handle); } -inline void NetworkTableInstance::StartServer(StringRef persist_filename, +inline void NetworkTableInstance::StartServer(const Twine& persist_filename, const char* listen_address, unsigned int port) { ::nt::StartServer(m_handle, persist_filename, listen_address, port); @@ -147,23 +147,23 @@ inline bool NetworkTableInstance::IsConnected() const { } inline const char* NetworkTableInstance::SavePersistent( - StringRef filename) const { + const Twine& filename) const { return ::nt::SavePersistent(m_handle, filename); } inline const char* NetworkTableInstance::LoadPersistent( - StringRef filename, + const Twine& filename, std::function warn) { return ::nt::LoadPersistent(m_handle, filename, warn); } inline const char* NetworkTableInstance::SaveEntries( - StringRef filename, StringRef prefix) const { + const Twine& filename, const Twine& prefix) const { return ::nt::SaveEntries(m_handle, filename, prefix); } inline const char* NetworkTableInstance::LoadEntries( - StringRef filename, StringRef prefix, + const Twine& filename, const Twine& prefix, std::function warn) { return ::nt::LoadEntries(m_handle, filename, prefix, warn); } diff --git a/src/main/native/include/networktables/NetworkTableValue.h b/src/main/native/include/networktables/NetworkTableValue.h index cff1a3431f..cacabe1cf1 100644 --- a/src/main/native/include/networktables/NetworkTableValue.h +++ b/src/main/native/include/networktables/NetworkTableValue.h @@ -16,6 +16,7 @@ #include "llvm/ArrayRef.h" #include "llvm/StringRef.h" +#include "llvm/Twine.h" #include "ntcore_c.h" @@ -23,6 +24,7 @@ namespace nt { using llvm::ArrayRef; using llvm::StringRef; +using llvm::Twine; /** * A network table entry value. @@ -241,10 +243,10 @@ class Value final { * time) * @return The entry value */ - static std::shared_ptr MakeString(StringRef value, + static std::shared_ptr MakeString(const Twine& value, unsigned long long time = 0) { auto val = std::make_shared(NT_STRING, time, private_init()); - val->m_string = value; + val->m_string = value.str(); val->m_val.data.v_string.str = const_cast(val->m_string.c_str()); val->m_val.data.v_string.len = val->m_string.size(); return val; diff --git a/src/main/native/include/ntcore_cpp.h b/src/main/native/include/ntcore_cpp.h index 1ac58d30b9..04bbaf24d3 100644 --- a/src/main/native/include/ntcore_cpp.h +++ b/src/main/native/include/ntcore_cpp.h @@ -17,6 +17,7 @@ #include "llvm/ArrayRef.h" #include "llvm/StringRef.h" +#include "llvm/Twine.h" #include "support/deprecated.h" #include "networktables/NetworkTableValue.h" @@ -25,6 +26,7 @@ namespace nt { using llvm::ArrayRef; using llvm::StringRef; +using llvm::Twine; /** NetworkTables Entry Information */ struct EntryInfo { @@ -166,7 +168,7 @@ class EntryNotification { public: EntryNotification() : listener(0), entry(0) {} EntryNotification(NT_EntryListener listener_, NT_Entry entry_, - llvm::StringRef name_, std::shared_ptr value_, + StringRef name_, std::shared_ptr value_, unsigned int flags_) : listener(listener_), entry(entry_), @@ -233,7 +235,7 @@ class LogMessage { public: LogMessage() : logger(0), level(0), filename(""), line(0) {} LogMessage(NT_Logger logger_, unsigned int level_, const char* filename_, - unsigned int line_, llvm::StringRef message_) + unsigned int line_, StringRef message_) : logger(logger_), level(level_), filename(filename_), @@ -310,7 +312,7 @@ NT_Inst GetInstanceFromHandle(NT_Handle handle); * @param name entry name (UTF-8 string) * @return entry handle */ -NT_Entry GetEntry(NT_Inst inst, StringRef name); +NT_Entry GetEntry(NT_Inst inst, const Twine& name); /** * Get Entry Handles. @@ -325,7 +327,7 @@ NT_Entry GetEntry(NT_Inst inst, StringRef name); * as a "don't care" * @return Array of entry handles. */ -std::vector GetEntries(NT_Inst inst, StringRef prefix, +std::vector GetEntries(NT_Inst inst, const Twine& prefix, unsigned int types); /** @@ -549,7 +551,7 @@ std::vector GetEntryInfo(StringRef prefix, unsigned int types); * * @param inst instance handle */ -std::vector GetEntryInfo(NT_Inst inst, StringRef prefix, +std::vector GetEntryInfo(NT_Inst inst, const Twine& prefix, unsigned int types); /** @@ -602,7 +604,7 @@ NT_EntryListener AddEntryListener(StringRef prefix, * @param inst instance handle */ NT_EntryListener AddEntryListener( - NT_Inst inst, StringRef prefix, + NT_Inst inst, const Twine& prefix, std::function callback, unsigned int flags); @@ -646,7 +648,8 @@ void DestroyEntryListenerPoller(NT_EntryListenerPoller poller); * @return Listener handle */ NT_EntryListener AddPolledEntryListener(NT_EntryListenerPoller poller, - StringRef prefix, unsigned int flags); + const Twine& prefix, + unsigned int flags); /** * Create a polled entry listener. @@ -1028,7 +1031,7 @@ void SetNetworkIdentity(StringRef name); * @copydoc SetNetworkIdentity(StringRef) * @param inst instance handle */ -void SetNetworkIdentity(NT_Inst inst, StringRef name); +void SetNetworkIdentity(NT_Inst inst, const Twine& name); /** * Get the current network mode. @@ -1061,7 +1064,7 @@ void StartServer(StringRef persist_filename, const char* listen_address, * @copydoc StartServer(StringRef, const char*, unsigned int) * @param inst instance handle */ -void StartServer(NT_Inst inst, StringRef persist_filename, +void StartServer(NT_Inst inst, const Twine& persist_filename, const char* listen_address, unsigned int port); /** @@ -1290,7 +1293,7 @@ const char* SavePersistent(StringRef filename); * @copydoc SavePersistent(StringRef) * @param inst instance handle */ -const char* SavePersistent(NT_Inst inst, StringRef filename); +const char* SavePersistent(NT_Inst inst, const Twine& filename); /** * Load persistent values from a file. The server automatically does this @@ -1310,7 +1313,7 @@ const char* LoadPersistent( * @param inst instance handle */ const char* LoadPersistent( - NT_Inst inst, StringRef filename, + NT_Inst inst, const Twine& filename, std::function warn); /** @@ -1321,7 +1324,8 @@ const char* LoadPersistent( * @param prefix save only keys starting with this prefix * @return error string, or nullptr if successful */ -const char* SaveEntries(NT_Inst inst, StringRef filename, StringRef prefix); +const char* SaveEntries(NT_Inst inst, const Twine& filename, + const Twine& prefix); /** * Load table values from a file. The file format used is identical to @@ -1332,7 +1336,8 @@ const char* SaveEntries(NT_Inst inst, StringRef filename, StringRef prefix); * @param warn callback function for warnings * @return error string, or nullptr if successful */ -const char* LoadEntries(NT_Inst inst, StringRef filename, StringRef prefix, +const char* LoadEntries(NT_Inst inst, const Twine& filename, + const Twine& prefix, std::function warn); /** @} */ diff --git a/src/main/native/include/tables/ITable.h b/src/main/native/include/tables/ITable.h index b9a79bdf19..9702592805 100644 --- a/src/main/native/include/tables/ITable.h +++ b/src/main/native/include/tables/ITable.h @@ -11,6 +11,7 @@ #include #include "llvm/StringRef.h" +#include "llvm/Twine.h" #include "networktables/NetworkTableValue.h" #include "support/deprecated.h" @@ -31,7 +32,7 @@ class WPI_DEPRECATED("Use NetworkTable directly") ITable { * @param key the key to search for * @return true if the table as a value assigned to the given key */ - virtual bool ContainsKey(llvm::StringRef key) const = 0; + virtual bool ContainsKey(const llvm::Twine& key) const = 0; /** * Determines whether there exists a non-empty subtable for this key @@ -41,7 +42,7 @@ class WPI_DEPRECATED("Use NetworkTable directly") ITable { * @return true if there is a subtable with the key which contains at least * one key/subtable of its own */ - virtual bool ContainsSubTable(llvm::StringRef key) const = 0; + virtual bool ContainsSubTable(const llvm::Twine& key) const = 0; /** * Gets the subtable in this table for the given name. @@ -49,7 +50,8 @@ class WPI_DEPRECATED("Use NetworkTable directly") ITable { * @param key the name of the table relative to this one * @return a sub table relative to this one */ - virtual std::shared_ptr GetSubTable(llvm::StringRef key) const = 0; + virtual std::shared_ptr GetSubTable( + const llvm::Twine& key) const = 0; /** * @param types bitmask of types; 0 is treated as a "don't care". @@ -116,7 +118,7 @@ class WPI_DEPRECATED("Use NetworkTable directly") ITable { * * @param key the key name */ - virtual void Delete(llvm::StringRef key) = 0; + virtual void Delete(const llvm::Twine& key) = 0; /** * Gets the value associated with a key as an object @@ -125,7 +127,7 @@ class WPI_DEPRECATED("Use NetworkTable directly") ITable { * @return the value associated with the given key, or nullptr if the key * does not exist */ - virtual std::shared_ptr GetValue(llvm::StringRef key) const = 0; + virtual std::shared_ptr GetValue(const llvm::Twine& key) const = 0; /** * Gets the current value in the table, setting it if it does not exist. @@ -133,7 +135,7 @@ class WPI_DEPRECATED("Use NetworkTable directly") ITable { * @param defaultValue the default value to set if key doesn't exist. * @returns False if the table key exists with a different type */ - virtual bool SetDefaultValue(llvm::StringRef key, + virtual bool SetDefaultValue(const llvm::Twine& key, std::shared_ptr defaultValue) = 0; /** @@ -143,7 +145,7 @@ class WPI_DEPRECATED("Use NetworkTable directly") ITable { * @param value the value that will be assigned * @return False if the table key already exists with a different type */ - virtual bool PutValue(llvm::StringRef key, + virtual bool PutValue(const llvm::Twine& key, std::shared_ptr value) = 0; /**