Advertisement
Guest User

paste

a guest
Nov 5th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #pragma once
  2.  
  3.  
  4. #include "File.h"
  5. #include "FileSystemObject.h"
  6. #include "FileSystemObjectsContainer.h"
  7. #include "Directory.h"
  8. #include "ByteContainer.h"
  9. #include "Shortcuts.h"
  10.  
  11. #include <string>
  12. #include <vector>
  13. #include <sstream>
  14. #include <stack>
  15.  
  16. class Explorer
  17. {
  18. private:
  19.     std::shared_ptr<FileSystemObjectsContainer> currLocation;
  20.     std::vector<std::shared_ptr<FileSystemObject>>& objects;
  21.     std::stack<std::shared_ptr<FileSystemObject>> clipboard;
  22.  
  23.  
  24.     void moveFileAtCurrentLocation(const std::shared_ptr<FileSystemObject>& file)
  25.     {
  26.         std::shared_ptr<FileSystemObjectsContainer> fileParent = std::dynamic_pointer_cast<FileSystemObjectsContainer>(file->getParent().lock());
  27.         std::weak_ptr<FileSystemObject> newParent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
  28.         file->setParent(newParent);
  29.  
  30.  
  31.         std::vector<std::shared_ptr<FileSystemObject>> kidsOfNewParent(currLocation->begin(), currLocation->end());
  32.         kidsOfNewParent.push_back(file);
  33.  
  34.         if(fileParent != nullptr)
  35.         {
  36.             std::vector<std::shared_ptr<FileSystemObject>> kidsOfOldParent(fileParent->begin(),fileParent->end());
  37.             kidsOfOldParent.erase(std::find(kidsOfOldParent.begin(), kidsOfOldParent.end(), file));
  38.         }else
  39.         {
  40.             objects.erase(std::find(objects.begin(), objects.end(), file));
  41.         }
  42.     }
  43.  
  44. public:
  45.     Explorer(std::vector<std::shared_ptr<FileSystemObject>>& rootObjects) : objects(rootObjects), currLocation(nullptr) {}
  46.  
  47.     void cut(const std::string& name)
  48.     {
  49.         std::shared_ptr<FileSystemObject> file = this->getFile(name);
  50.         this->clipboard.push(file);
  51.     }
  52.  
  53.  
  54.     void paste()
  55.     {
  56.         if(!this->clipboard.empty())
  57.         {
  58.             std::shared_ptr<FileSystemObject> file = clipboard.top();
  59.             moveFileAtCurrentLocation(file);
  60.             clipboard.pop();
  61.         }
  62.     }
  63.  
  64. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement