Select Git revision
ParallelComputer.html
mem_persistence.cpp 2.04 KiB
#include <string>
#include <map>
#include <vector>
#include "mqtt/client.h"
// Connection persistence class.
class mem_persistence : virtual public mqtt::iclient_persistence {
// Whether the store is open
bool open_;
// Use an STL map to store shared persistence pointers
// against string keys.
std::map<std::string, std::string> store_;
public:
mem_persistence() : open_(false) {}
// "Open" the store
void open(const std::string &clientId, const std::string &serverURI) override {
open_ = true;
}
// Close the persistent store that was previously opened.
void close() override {
open_ = false;
}
// Clears persistence, so that it no longer contains any persisted data.
void clear() override {
store_.clear();
}
// Returns whether or not data is persisted using the specified key.
bool contains_key(const std::string &key) override {
return store_.find(key) != store_.end();
}
// Returns the keys in this persistent data store.
const mqtt::string_collection &keys() const override {
static mqtt::string_collection ks;
ks.clear();
for (const auto &k : store_)
ks.push_back(k.first);
return ks;
}
// Puts the specified data into the persistent store.
void put(const std::string &key, const std::vector<mqtt::string_view> &bufs) override {
std::string str;
for (const auto &b : bufs)
str += b.str();
store_[key] = std::move(str);
}
// Gets the specified data out of the persistent store.
mqtt::string_view get(const std::string &key) const override {
auto p = store_.find(key);
if (p == store_.end())
throw mqtt::persistence_exception();
return mqtt::string_view(p->second);
}
// Remove the data for the specified key.
void remove(const std::string &key) override {
auto p = store_.find(key);
if (p == store_.end())
throw mqtt::persistence_exception();
store_.erase(p);;
}
};