VPTissue Reference Manual
CliWorkspaceManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "CliWorkspaceManager.h"
21 
22 #include "../../cpp_simshell/common/InstallDirs.h"
23 #include "../workspace/WorkspaceFactory.h"
24 #include <QDir>
25 #include <QFile>
26 #include <iostream>
27 
28 using namespace std;
29 using namespace SimShell;
30 using namespace SimPT_Shell;
31 using namespace SimPT_Shell::Ws;
32 
33 namespace SimPT_Shell {
34 
35 SimShell::Ws::IProject::FileIterator
36 CliWorkspaceManager::CreateLeaf(
37  const shared_ptr<SimShell::Ws::IProject>& project,
38  const string& ref_file_path, const string& file)
39 {
40  QFile::copy(QString::fromStdString(ref_file_path),
41  QString::fromStdString(project->GetPath() + '/' + file));
42  const auto it = project->Add(file);
43 
44  assert((it != project->end()) && "Iterator cannot be end().");
45  return it;
46 }
47 
48 SimShell::Ws::IWorkspace::ProjectIterator
49 CliWorkspaceManager::CreateProject(
50  const shared_ptr<SimShell::Ws::IWorkspace>& workspace, const string& name)
51 {
52  const auto it = workspace->New("project", name);
53 
54  assert( (it != workspace->end()) && "Project iterator cannot be end().");
55  return it;
56 }
57 
58 shared_ptr<Ws::CliWorkspace>
59 CliWorkspaceManager::CreateWorkspace(const string& base_path, const string& workspace)
60 {
61  const string workspace_path = (base_path == "") ? workspace : base_path + "/" + workspace;
62  shared_ptr<Ws::CliWorkspace> ws_ptr;
63 
64  if (!QDir(QString::fromStdString(workspace_path)).exists()) {
65  QDir(QString::fromStdString(base_path)).mkdir(QString::fromStdString(workspace));
66  }
67  Ws::CliWorkspace::Init(workspace_path);
68  ws_ptr = make_shared<Ws::CliWorkspace>(workspace_path);
69 
70  assert((ws_ptr != nullptr) && "Workspace pointer cannot be nullptr.");
71  return ws_ptr;
72 }
73 
74 SimShell::Ws::IProject::FileIterator
75 CliWorkspaceManager::OpenLeaf(
76  shared_ptr<SimShell::Ws::IProject> project, const string& file,
77  bool create_if_not_exists, std::shared_ptr<SimShell::Ws::IFile> ref_file)
78 {
79  assert((project != nullptr) && "Project cannot be nullptr.");
80  assert((ref_file != nullptr) && "reference file cannot be nullptr.");
81 
82  SimShell::Ws::IProject::FileIterator it = project->Find(file);
83 
84  if (it == project->end()) {
85  if (create_if_not_exists) {
86  it = CreateLeaf(project, ref_file->GetPath(), file);
87  }
88  }
89 
90  assert((it != project->end()) && "File iterator cannot be end().");
91  return it;
92 }
93 
94 SimShell::Ws::IProject::FileIterator
95 CliWorkspaceManager::OpenLeaf(
96  shared_ptr<SimShell::Ws::IProject> project, const string& file,
97  bool create_if_not_exists,
98  const string& ref_ws_path, const string& ref_project, const string& ref_file)
99 {
100  assert((project != nullptr) && "project cannot be nullptr.");
101 
102  SimShell::Ws::IProject::FileIterator it = project->Find(file);
103 
104  if (it == project->end()) {
105  if (create_if_not_exists) {
106  CliWorkspace ws_template(ref_ws_path);
107  it = CreateLeaf(project,
108  ws_template.Get(ref_project)->Get(ref_file)->GetPath(), file);
109  }
110  }
111 
112  assert((it != project->end()) && "Leaf iterator cannot be end().");
113  return it;
114 }
115 
116 SimShell::Ws::IWorkspace::ProjectIterator
117 CliWorkspaceManager::OpenProject(
118  shared_ptr<CliWorkspace> workspace, const string& project,
119  bool create_if_not_exists)
120 {
121  SimShell::Ws::IWorkspace::ProjectIterator it = workspace->Find(project);
122 
123  if (it == workspace->end()) {
124  if (create_if_not_exists) {
125  it = CreateProject(workspace, project);
126  }
127  }
128  assert((it != workspace->end()) && "Project iterator cannot be end().");
129  return it;
130 }
131 
132 shared_ptr<CliWorkspace>
133 CliWorkspaceManager::OpenWorkspace(
134  const string& base_path, const string& workspace,
135  bool create_if_not_exists)
136 {
137  const string workspace_path = (base_path == "") ? workspace : base_path + "/" + workspace;
138  shared_ptr<CliWorkspace> ws_ptr;
139 
140  if (!QDir(QString::fromStdString(workspace_path)).exists()
141  || !QFile::exists(QString::fromStdString(workspace_path+ "/" + WorkspaceFactory::g_workspace_index_file))) {
142  if (create_if_not_exists) {
143  ws_ptr = CreateWorkspace(base_path, workspace);
144  }
145  } else {
146  ws_ptr = make_shared<CliWorkspace>(workspace_path);
147  }
148 
149  assert((ws_ptr != nullptr) && "Workspace pointer cannot be nullptr.");
150  return ws_ptr;
151 }
152 
153 } // namespace
STL namespace.
Namespace for SimPT shell package.
Definition: Client.cpp:50
Cli Workspace for the command-line simulator.
Definition: CliWorkspace.h:30
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
Namespace for SimPT specific workspace classes.
Definition: Client.cpp:50
Interface for CliWorkspaceManager.