-
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.
Merge pull request #12 from j0tunn/feat/prj.05
ДЗ-05. Editor
- Loading branch information
Showing
10 changed files
with
400 additions
and
2 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
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,12 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../common/CMakeLists.txt) | ||
|
||
project(editor VERSION ${PROJECT_VERSION}) | ||
|
||
add_executable(editor | ||
main.cpp | ||
) | ||
|
||
# Package | ||
setupCPack(editor) |
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 <memory> | ||
#include <iostream> | ||
#include "model.h" | ||
#include "view.h" | ||
|
||
class Controller { | ||
public: | ||
Controller() | ||
: pModel_(nullptr) | ||
, pView_(nullptr) | ||
{} | ||
|
||
~Controller() = default; | ||
|
||
void NewDocument() { | ||
pModel_ = std::make_unique<Model::Document>(); | ||
pView_ = std::make_unique<View::Document>(*pModel_); | ||
} | ||
|
||
void AddSquare(const unsigned int side) { | ||
pModel_->AddObject<Model::Square>(side); | ||
} | ||
|
||
void AddCircle(const unsigned int radius) { | ||
pModel_->AddObject<Model::Circle>(radius); | ||
} | ||
|
||
void DeleteLastObject() { | ||
pModel_->RmLastObject(); | ||
} | ||
|
||
void ExportDocument(const std::string& file) const { | ||
const std::string documentContent = pModel_->Serialize(); | ||
|
||
std::cout << "Exporting document content '" << documentContent << "' into file '" << file << "'" << std::endl; | ||
} | ||
|
||
void ImportDocument(const std::string& file) { | ||
std::cout << "Importing document content from file '" << file << "'" << std::endl; | ||
|
||
// TODO: read file content | ||
const std::string fileContent = ""; | ||
|
||
pModel_ = std::unique_ptr<Model::Document>(Model::Document::Deserialize(fileContent)); | ||
pView_ = std::make_unique<View::Document>(*pModel_); | ||
} | ||
|
||
private: | ||
std::unique_ptr<Model::Document> pModel_; | ||
std::unique_ptr<View::Document> pView_; | ||
}; |
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,28 @@ | ||
#include <iostream> | ||
#include <exception> | ||
#include "controller.h" | ||
|
||
using namespace std; | ||
|
||
int main(int, char **) { | ||
Controller app; | ||
|
||
try { | ||
app.NewDocument(); | ||
|
||
app.AddSquare(2); | ||
app.AddCircle(3); | ||
app.AddSquare(4); | ||
|
||
app.DeleteLastObject(); | ||
|
||
app.ExportDocument("foo/bar.baz"); | ||
|
||
app.ImportDocument("foo/bar.baz"); | ||
} catch(exception& e) { | ||
cout << e.what() << endl; | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
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,85 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <list> | ||
#include <numeric> | ||
#include "observer.h" | ||
|
||
namespace Model { | ||
|
||
/// | ||
class IObject { | ||
public: | ||
virtual ~IObject() = default; | ||
virtual std::string Serialize() const = 0; | ||
}; | ||
|
||
/// | ||
class Square : public IObject { | ||
public: | ||
Square(const unsigned int side) : side_(side) {} | ||
|
||
std::string Serialize() const override { | ||
return "square:" + std::to_string(side_); | ||
} | ||
|
||
unsigned int GetSide() const { | ||
return side_; | ||
} | ||
|
||
private: | ||
unsigned int side_; | ||
}; | ||
|
||
/// | ||
class Circle : public IObject { | ||
public: | ||
Circle(const unsigned int radius) : radius_(radius) {} | ||
|
||
std::string Serialize() const override { | ||
return "circle:" + std::to_string(radius_); | ||
} | ||
|
||
unsigned int GetRadius() const { | ||
return radius_; | ||
} | ||
|
||
private: | ||
unsigned int radius_; | ||
}; | ||
|
||
/// | ||
class Document : public Observable { | ||
public: | ||
static std::unique_ptr<Document> Deserialize(const std::string& str) { | ||
std::unique_ptr<Document> pDoc(new Document()); | ||
|
||
// TODO: parse string, for each obj call pDoc->AddObject() | ||
|
||
return pDoc; | ||
} | ||
|
||
template <typename T, typename... Args> | ||
void AddObject(Args... args) { | ||
auto pObj = new T(args...); | ||
objects_.push_back(std::unique_ptr<IObject>(pObj)); | ||
|
||
NotifyNewObject(*pObj); | ||
} | ||
|
||
void RmLastObject() { | ||
objects_.pop_back(); | ||
|
||
NotifyRmLastObject(); | ||
} | ||
|
||
std::string Serialize() const { | ||
return std::accumulate(objects_.begin(), objects_.end(), std::string(""), [](auto res, const auto& o) { return res + o->Serialize() + ";"; }); | ||
} | ||
|
||
private: | ||
std::list<std::unique_ptr<IObject> > objects_; | ||
}; | ||
|
||
} |
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,46 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <stdexcept> | ||
|
||
namespace Model { | ||
|
||
class Square; | ||
class Circle; | ||
|
||
/// | ||
class IObserver { | ||
public: | ||
virtual ~IObserver() = default; | ||
|
||
virtual void OnNewObject(const Square&) = 0; | ||
virtual void OnNewObject(const Circle&) = 0; | ||
|
||
virtual void OnRmLastObject() = 0; | ||
}; | ||
|
||
/// | ||
class Observable { | ||
public: | ||
void AddObserver(IObserver* observer) { | ||
observers_.push_back(observer); | ||
} | ||
|
||
void NotifyRmLastObject() { | ||
for (auto observer : observers_) { | ||
observer->OnRmLastObject(); | ||
} | ||
} | ||
|
||
template <typename T> | ||
void NotifyNewObject(const T& obj) { | ||
for (auto observer : observers_) { | ||
observer->OnNewObject(obj); | ||
} | ||
} | ||
|
||
private: | ||
std::vector<IObserver*> observers_; | ||
}; | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,92 @@ | ||
@startuml | ||
|
||
class Controller { | ||
-pModel_: Model::Document | ||
-pView_: View::Document | ||
+NewDocument() | ||
+AddSquare(number) | ||
+AddCircle(number) | ||
+DeleteLastObject() | ||
+ExportDocument(string) | ||
+ImportDocument(string) | ||
} | ||
|
||
Controller o-- Model.Document | ||
Controller o-- View.Document | ||
|
||
namespace Model { | ||
interface IObject { | ||
+Serialize(): string | ||
} | ||
class Square { | ||
-side_: number | ||
+Square(number) | ||
+Serialize() | ||
+GetSide(): number | ||
} | ||
class Circle { | ||
-radius_: number | ||
+Circle(number) | ||
+Serialize() | ||
+GetRadius(): number | ||
} | ||
class Document { | ||
-objects_: List<IObject> | ||
+{static} Deserialize(string): Document | ||
+AddObject<T, Args...>(Args...) | ||
+RmLastObject() | ||
+Serialize(): string | ||
} | ||
interface IObserver { | ||
+OnNewObject(Square) | ||
+OnNewObject(Circle) | ||
+OnRmLastObject() | ||
} | ||
class Observable { | ||
-observers_: List<IObserver> | ||
+AddObserver(IObserver) | ||
+NotifyNewObject<T>(T) | ||
+NotifyRmLastObject() | ||
} | ||
|
||
IObject <|-- Square | ||
IObject <|-- Circle | ||
Observable <|-- Document | ||
IObserver <.. Observable | ||
Document o-- IObject | ||
|
||
Square <.. .View.Square | ||
Circle <.. .View.Circle | ||
IObserver <|-- .View.Document | ||
} | ||
|
||
namespace View { | ||
interface IDrawable { | ||
+Draw() | ||
} | ||
class Square { | ||
-model_: Model::Square | ||
+Square(Model::Square) | ||
+Draw() | ||
} | ||
class Circle { | ||
-model_: Model::Circle | ||
+Circle(Model::Circle) | ||
+Draw() | ||
} | ||
class Document { | ||
-subViews_: List<IDrawable> | ||
+Document(Model::Document) | ||
+OnRmLastObject() | ||
+OnNewObject(Model::Square) | ||
+OnNewObject(Model::Circle) | ||
-AddSubView_<T, Model::U>(Model::U) | ||
-Draw() | ||
} | ||
|
||
IDrawable <|-- Square | ||
IDrawable <|-- Circle | ||
Document o-- IDrawable | ||
} | ||
|
||
@enduml |
Oops, something went wrong.