-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
#include <pb/persistence/Persistence.h> | ||
|
||
namespace PB { | ||
class ProjectPersistenceListener { | ||
public: | ||
virtual ~ProjectPersistenceListener() = default; | ||
virtual void onMetadataUpdated() = 0; | ||
virtual void onProjectRead() = 0; | ||
virtual void onPersistenceError(PBDev::Error) = 0; | ||
}; | ||
|
||
class ProjectPersistence final : public PersistenceMetadataListener, | ||
public PersistenceProjectListener { | ||
public: | ||
ProjectPersistence() = default; | ||
~ProjectPersistence() = default; | ||
|
||
void configure(Path localStatePath); | ||
void configure(ProjectPersistenceListener *listener); | ||
|
||
std::shared_ptr<Project> currentProject(); | ||
|
||
void recallProject(boost::uuids::uuid const &uuid); | ||
|
||
void recallProject(std::string name); | ||
|
||
void recallMetadata(); | ||
|
||
boost::uuids::uuid uuid(std::string name); | ||
|
||
bool hasUUID(std::string name) const; | ||
|
||
std::vector<std::string> projectsNames() const; | ||
|
||
void onProjectRead(std::shared_ptr<Project> project) override; | ||
void onProjectPersistenceError(PBDev::Error) override; | ||
|
||
void onMetadataRead( | ||
boost::bimaps::bimap<boost::uuids::uuid, std::string> metadata) override; | ||
void onMetadataPersistenceError(PBDev::Error) override; | ||
|
||
private: | ||
std::string name(boost::uuids::uuid uuid); | ||
|
||
ProjectPersistenceListener *mListener = nullptr; | ||
Path mLocalStatePath; | ||
Persistence mPersistence; | ||
std::shared_ptr<Project> mProject = nullptr; | ||
|
||
boost::bimaps::bimap<boost::uuids::uuid, std::string> mMetadata; | ||
}; | ||
} // namespace PB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <pb/persistence/ProjectPersistence.h> | ||
|
||
namespace PB { | ||
void ProjectPersistence::configure(Path localStatePath) | ||
{ | ||
mLocalStatePath = localStatePath; | ||
} | ||
|
||
void ProjectPersistence::configure(ProjectPersistenceListener *listener) | ||
{ | ||
mListener = listener; | ||
} | ||
|
||
std::shared_ptr<Project> ProjectPersistence::currentProject() | ||
{ | ||
return mProject; | ||
} | ||
|
||
void ProjectPersistence::recallProject(boost::uuids::uuid const &uuid) | ||
{ | ||
mPersistence.recallProject(name(uuid)); | ||
} | ||
|
||
void ProjectPersistence::recallProject(std::string name) | ||
{ | ||
mPersistence.recallProject(name); | ||
} | ||
|
||
void ProjectPersistence::recallMetadata() { mPersistence.recallMetadata(); } | ||
|
||
boost::uuids::uuid ProjectPersistence::uuid(std::string name) | ||
{ | ||
return mMetadata.right.at(name); | ||
} | ||
|
||
bool ProjectPersistence::hasUUID(std::string name) const | ||
{ | ||
return mMetadata.right.find(name) != mMetadata.right.end(); | ||
} | ||
|
||
std::vector<std::string> ProjectPersistence::projectsNames() const | ||
{ | ||
std::vector<std::string> projects; | ||
for (auto &it : mMetadata) { | ||
projects.push_back(it.right); | ||
} | ||
return projects; | ||
} | ||
|
||
std::string ProjectPersistence::name(boost::uuids::uuid uuid) | ||
{ | ||
return mMetadata.left.at(uuid); | ||
} | ||
|
||
void ProjectPersistence::onProjectRead(std::shared_ptr<Project> project) | ||
{ | ||
mProject = project; | ||
mListener->onProjectRead(); | ||
} | ||
|
||
void ProjectPersistence::onProjectPersistenceError(PBDev::Error error) | ||
{ | ||
mListener->onPersistenceError(error); | ||
} | ||
|
||
void ProjectPersistence::onMetadataRead( | ||
boost::bimaps::bimap<boost::uuids::uuid, std::string> metadata) | ||
{ | ||
mMetadata = metadata; | ||
mListener->onMetadataUpdated(); | ||
} | ||
|
||
void ProjectPersistence::onMetadataPersistenceError(PBDev::Error error) | ||
{ | ||
mListener->onPersistenceError(error); | ||
} | ||
|
||
} // namespace PB |