2015-06-21 23:42:29 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifndef NT_STORAGE_H_
|
|
|
|
|
#define NT_STORAGE_H_
|
|
|
|
|
|
2015-06-27 10:22:59 -07:00
|
|
|
#include <cstddef>
|
2015-07-14 23:15:08 -07:00
|
|
|
#include <iosfwd>
|
|
|
|
|
#include <memory>
|
2015-06-27 10:22:59 -07:00
|
|
|
|
2015-06-21 23:42:29 -07:00
|
|
|
#include "ntcore.h"
|
|
|
|
|
|
2015-06-28 21:52:06 -07:00
|
|
|
#include "Value.h"
|
2015-06-21 23:42:29 -07:00
|
|
|
#include "llvm/StringMap.h"
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
namespace ntimpl {
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
class StorageEntry {
|
|
|
|
|
public:
|
2015-06-28 21:52:06 -07:00
|
|
|
StorageEntry() { m_flags = 0; }
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-06-28 21:52:06 -07:00
|
|
|
Value& value() { return m_value; }
|
|
|
|
|
const Value& value() const { return m_value; }
|
|
|
|
|
|
|
|
|
|
unsigned int flags() const { return m_flags; }
|
|
|
|
|
void set_flags(unsigned int flags) { m_flags = flags; }
|
|
|
|
|
bool IsPersistent() const { return (m_flags & NT_PERSISTENT) != 0; }
|
2015-06-25 22:57:43 -07:00
|
|
|
|
|
|
|
|
StorageEntry(const StorageEntry&) = delete;
|
|
|
|
|
StorageEntry& operator=(const StorageEntry&) = delete;
|
2015-06-28 21:52:06 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Value m_value;
|
|
|
|
|
unsigned int m_flags;
|
2015-06-21 23:42:29 -07:00
|
|
|
};
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
class Storage {
|
|
|
|
|
public:
|
|
|
|
|
static Storage& GetInstance() {
|
2015-07-14 23:15:08 -07:00
|
|
|
if (!m_instance) m_instance.reset(new Storage);
|
2015-06-25 22:57:43 -07:00
|
|
|
return *m_instance;
|
|
|
|
|
}
|
2015-07-14 23:15:08 -07:00
|
|
|
~Storage();
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
typedef llvm::StringMap<StorageEntry> EntriesMap;
|
2015-06-27 10:02:20 -07:00
|
|
|
|
|
|
|
|
EntriesMap& entries() { return m_entries; }
|
|
|
|
|
const EntriesMap& entries() const { return m_entries; }
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-06-27 10:22:59 -07:00
|
|
|
void SavePersistent(std::ostream& os) const;
|
2015-06-28 21:52:06 -07:00
|
|
|
bool LoadPersistent(std::istream& is,
|
2015-06-27 10:22:59 -07:00
|
|
|
void (*warn)(std::size_t line, const char* msg));
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
private:
|
|
|
|
|
Storage();
|
|
|
|
|
Storage(const Storage&) = delete;
|
|
|
|
|
Storage& operator=(const Storage&) = delete;
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-06-27 10:02:20 -07:00
|
|
|
EntriesMap m_entries;
|
|
|
|
|
|
2015-07-14 23:15:08 -07:00
|
|
|
static std::unique_ptr<Storage> m_instance;
|
2015-06-21 23:42:29 -07:00
|
|
|
};
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
} // namespace ntimpl
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
#endif // NT_STORAGE_H_
|