Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
Use Twine instead of StringRef where appropriate. (#259)
Browse files Browse the repository at this point in the history
Deprecated interfaces were not updated.
  • Loading branch information
PeterJohnson authored Nov 25, 2017
1 parent 0e4a1c5 commit aa2de65
Show file tree
Hide file tree
Showing 19 changed files with 282 additions and 243 deletions.
14 changes: 8 additions & 6 deletions src/main/native/cpp/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -115,19 +115,21 @@ DispatcherBase::~DispatcherBase() { Stop(); }
unsigned int DispatcherBase::GetNetworkMode() const { return m_networkMode; }

void DispatcherBase::StartServer(
StringRef persist_filename,
const Twine& persist_filename,
std::unique_ptr<wpi::NetworkAcceptor> acceptor) {
{
std::lock_guard<wpi::mutex> lock(m_user_mutex);
if (m_active) return;
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) {
Expand Down Expand Up @@ -198,9 +200,9 @@ void DispatcherBase::SetUpdateRate(double interval) {
m_update_rate = static_cast<unsigned int>(interval * 1000);
}

void DispatcherBase::SetIdentity(llvm::StringRef name) {
void DispatcherBase::SetIdentity(const Twine& name) {
std::lock_guard<wpi::mutex> lock(m_user_mutex);
m_identity = name;
m_identity = name.str();
}

void DispatcherBase::Flush() {
Expand Down
7 changes: 4 additions & 3 deletions src/main/native/cpp/Dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <vector>

#include "llvm/StringRef.h"
#include "llvm/Twine.h"
#include "support/condition_variable.h"
#include "support/mutex.h"

Expand Down Expand Up @@ -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<wpi::NetworkAcceptor> acceptor);
void StartClient();
void Stop();
void SetUpdateRate(double interval);
void SetIdentity(llvm::StringRef name);
void SetIdentity(const Twine& name);
void Flush();
std::vector<ConnectionInfo> GetConnections() const;
bool IsConnected() const;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/cpp/IStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <vector>

#include "llvm/ArrayRef.h"
#include "llvm/StringRef.h"
#include "llvm/Twine.h"

#include "Message.h"
#include "ntcore_cpp.h"
Expand Down Expand Up @@ -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<void(std::size_t line, const char* msg)> warn) = 0;
};

Expand Down
49 changes: 32 additions & 17 deletions src/main/native/cpp/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<wpi::mutex> lock(m_mutex);
return GetOrNew(name)->local_id;
}

std::vector<unsigned int> Storage::GetEntries(StringRef prefix,
std::vector<unsigned int> Storage::GetEntries(const Twine& prefix,
unsigned int types) {
llvm::SmallString<128> prefixBuf;
StringRef prefixStr = prefix.toStringRef(prefixBuf);
std::lock_guard<wpi::mutex> lock(m_mutex);
std::vector<unsigned int> 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);
}
Expand Down Expand Up @@ -829,14 +835,16 @@ unsigned long long Storage::GetEntryLastChange(unsigned int local_id) const {
return entry->value->last_change();
}

std::vector<EntryInfo> Storage::GetEntryInfo(int inst, StringRef prefix,
std::vector<EntryInfo> Storage::GetEntryInfo(int inst, const Twine& prefix,
unsigned int types) {
llvm::SmallString<128> prefixBuf;
StringRef prefixStr = prefix.toStringRef(prefixBuf);
std::lock_guard<wpi::mutex> lock(m_mutex);
std::vector<EntryInfo> 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);
Expand All @@ -850,16 +858,18 @@ std::vector<EntryInfo> Storage::GetEntryInfo(int inst, StringRef prefix,
}

unsigned int Storage::AddListener(
StringRef prefix,
const Twine& prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) const {
llvm::SmallString<128> prefixBuf;
StringRef prefixStr = prefix.toStringRef(prefixBuf);
std::lock_guard<wpi::mutex> 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);
}
Expand All @@ -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<wpi::mutex> 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,
Expand Down Expand Up @@ -949,17 +962,19 @@ bool Storage::GetPersistentEntries(
}

bool Storage::GetEntries(
StringRef prefix,
const Twine& prefix,
std::vector<std::pair<std::string, std::shared_ptr<Value>>>* 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<wpi::mutex> lock(m_mutex);
entries->reserve(m_entries.size());
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);
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/main/native/cpp/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,50 +97,51 @@ class Storage : public IStorage {

void DeleteAllEntries();

std::vector<EntryInfo> GetEntryInfo(int inst, StringRef prefix,
std::vector<EntryInfo> GetEntryInfo(int inst, const Twine& prefix,
unsigned int types);

unsigned int AddListener(
StringRef prefix,
const Twine& prefix,
std::function<void(const EntryNotification& event)> callback,
unsigned int flags) const;
unsigned int AddListener(
unsigned int local_id,
std::function<void(const EntryNotification& event)> 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<unsigned int> GetEntries(StringRef prefix, unsigned int types);
unsigned int GetEntry(const Twine& name);
std::vector<unsigned int> 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;
unsigned long long GetEntryLastChange(unsigned int local_id) const;

// 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<void(std::size_t line, const char* msg)> 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<void(std::size_t line, const char* msg)> 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<void(std::size_t line, const char* msg)> 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.
Expand Down Expand Up @@ -237,7 +238,7 @@ class Storage : public IStorage {
bool periodic,
std::vector<std::pair<std::string, std::shared_ptr<Value>>>* entries)
const;
bool GetEntries(StringRef prefix,
bool GetEntries(const Twine& prefix,
std::vector<std::pair<std::string, std::shared_ptr<Value>>>*
entries) const;
void SetEntryValueImpl(Entry* entry, std::shared_ptr<Value> value,
Expand All @@ -251,7 +252,7 @@ class Storage : public IStorage {
template <typename F>
void DeleteAllEntriesImpl(bool local, F should_delete);
void DeleteAllEntriesImpl(bool local);
Entry* GetOrNew(StringRef name);
Entry* GetOrNew(const Twine& name);
};

} // namespace nt
Expand Down
11 changes: 7 additions & 4 deletions src/main/native/cpp/Storage_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,16 @@ std::shared_ptr<Value> LoadPersistentImpl::ReadStringArrayValue() {
}

bool Storage::LoadEntries(
wpi::raw_istream& is, StringRef prefix, bool persistent,
wpi::raw_istream& is, const Twine& prefix, bool persistent,
std::function<void(std::size_t line, const char* msg)> warn) {
llvm::SmallString<128> prefixBuf;
StringRef prefixStr = prefix.toStringRef(prefixBuf);

// entries to add
std::vector<LoadPersistentImpl::Entry> 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<std::shared_ptr<Message>> msgs;
Expand Down Expand Up @@ -431,7 +434,7 @@ bool Storage::LoadEntries(
}

const char* Storage::LoadPersistent(
StringRef filename,
const Twine& filename,
std::function<void(std::size_t line, const char* msg)> warn) {
std::error_code ec;
wpi::raw_fd_istream is(filename, ec);
Expand All @@ -441,7 +444,7 @@ const char* Storage::LoadPersistent(
}

const char* Storage::LoadEntries(
StringRef filename, StringRef prefix,
const Twine& filename, const Twine& prefix,
std::function<void(std::size_t line, const char* msg)> warn) {
std::error_code ec;
wpi::raw_fd_istream is(filename, ec);
Expand Down
22 changes: 13 additions & 9 deletions src/main/native/cpp/Storage_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -225,17 +227,19 @@ const char* Storage::SavePersistent(StringRef filename, bool periodic) const {
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<SavePersistentImpl::Entry> 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
Expand Down
Loading

0 comments on commit aa2de65

Please sign in to comment.