Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "File.h"
- #include "FileSystemObject.h"
- #include "FileSystemObjectsContainer.h"
- #include "Directory.h"
- #include "ByteContainer.h"
- #include "Shortcuts.h"
- #include <string>
- #include <vector>
- #include <sstream>
- #include <stack>
- class Explorer
- {
- private:
- std::shared_ptr<FileSystemObjectsContainer> currLocation;
- std::vector<std::shared_ptr<FileSystemObject>>& objects;
- std::stack<std::shared_ptr<FileSystemObject>> clipboard;
- void moveFileAtCurrentLocation(const std::shared_ptr<FileSystemObject>& file)
- {
- std::shared_ptr<FileSystemObjectsContainer> fileParent = std::dynamic_pointer_cast<FileSystemObjectsContainer>(file->getParent().lock());
- std::weak_ptr<FileSystemObject> newParent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
- file->setParent(newParent);
- std::vector<std::shared_ptr<FileSystemObject>> kidsOfNewParent(currLocation->begin(), currLocation->end());
- kidsOfNewParent.push_back(file);
- if(fileParent != nullptr)
- {
- std::vector<std::shared_ptr<FileSystemObject>> kidsOfOldParent(fileParent->begin(),fileParent->end());
- kidsOfOldParent.erase(std::find(kidsOfOldParent.begin(), kidsOfOldParent.end(), file));
- }else
- {
- objects.erase(std::find(objects.begin(), objects.end(), file));
- }
- }
- public:
- Explorer(std::vector<std::shared_ptr<FileSystemObject>>& rootObjects) : objects(rootObjects), currLocation(nullptr) {}
- void cut(const std::string& name)
- {
- std::shared_ptr<FileSystemObject> file = this->getFile(name);
- this->clipboard.push(file);
- }
- void paste()
- {
- if(!this->clipboard.empty())
- {
- std::shared_ptr<FileSystemObject> file = clipboard.top();
- moveFileAtCurrentLocation(file);
- clipboard.pop();
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement