CopyComponentCommand.cc Source File

Back to the index.

CopyComponentCommand.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009-2010 Anders Gavare. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
29 #include "GXemul.h"
30 
31 
33  : Command("copy", "path-from path-to")
34 {
35 }
36 
37 
39 {
40 }
41 
42 
43 static void ShowMsg(GXemul& gxemul, const string& msg)
44 {
45  gxemul.GetUI()->ShowDebugMessage(msg);
46 }
47 
48 
49 bool CopyComponentCommand::Execute(GXemul& gxemul, const vector<string>& arguments)
50 {
51  if (arguments.size() != 2) {
52  ShowMsg(gxemul, "syntax: copy path-from path-to\n");
53  return false;
54  }
55 
56  string pathFrom = arguments[0];
57  string pathTo = arguments[1];
58 
59  vector<string> matchesFrom = gxemul.GetRootComponent()->
60  FindPathByPartialMatch(pathFrom);
61  if (matchesFrom.size() == 0) {
62  ShowMsg(gxemul, pathFrom + " is not a path to a known component.\n");
63  return false;
64  }
65  if (matchesFrom.size() > 1) {
66  ShowMsg(gxemul, pathFrom + " matches multiple components:\n");
67  for (size_t i=0; i<matchesFrom.size(); i++)
68  ShowMsg(gxemul, " " + matchesFrom[i] + "\n");
69  return false;
70  }
71 
72  vector<string> matchesTo = gxemul.GetRootComponent()->
73  FindPathByPartialMatch(pathTo);
74  if (matchesTo.size() == 0) {
75  ShowMsg(gxemul, pathTo + " is not a path to a known component.\n");
76  return false;
77  }
78  if (matchesTo.size() > 1) {
79  ShowMsg(gxemul, pathTo + " matches multiple components:\n");
80  for (size_t i=0; i<matchesTo.size(); i++)
81  ShowMsg(gxemul, " " + matchesTo[i] + "\n");
82  return false;
83  }
84 
85 
86  refcount_ptr<Component> whatToCopy =
87  gxemul.GetRootComponent()->LookupPath(matchesFrom[0]);
88  if (whatToCopy.IsNULL()) {
89  ShowMsg(gxemul, "Lookup of origin path " + pathFrom + " failed.\n");
90  return false;
91  }
92 
93  refcount_ptr<Component> clone = whatToCopy->Clone();
94  if (clone.IsNULL()) {
95  ShowMsg(gxemul, "Failed to clone " + pathFrom + ".\n");
96  return false;
97  }
98 
99  refcount_ptr<Component> whereToAddIt =
100  gxemul.GetRootComponent()->LookupPath(matchesTo[0]);
101  if (whereToAddIt.IsNULL()) {
102  ShowMsg(gxemul, "Lookup of destination path " + pathTo + " failed.\n");
103  return false;
104  }
105 
106  whereToAddIt->AddChild(clone);
107 
108  return true;
109 }
110 
111 
113 {
114  return "Copies (clones) a component.";
115 }
116 
117 
119 {
120  return
121  "Copies a component (given a path) from one place in the configuration\n"
122  "tree to another. The following example duplicates a CPU component:\n"
123  "\n"
124  "> root\n"
125  " root\n"
126  " \\-- machine0 [testmips]\n"
127  " \\-- mainbus0\n"
128  " |-- ram0 (32 MB at offset 0)\n"
129  " |-- rom0 (16 MB at offset 0x1fc00000)\n"
130  " \\-- cpu0 (MIPS, 100 MHz)\n"
131  "> copy cpu0 mainbus\n"
132  "> root\n"
133  " root\n"
134  " \\-- machine0 [testmips]\n"
135  " \\-- mainbus0\n"
136  " |-- ram0 (32 MB at offset 0)\n"
137  " |-- rom0 (16 MB at offset 0x1fc00000)\n"
138  " |-- cpu0 (MIPS, 100 MHz)\n"
139  " \\-- cpu1 (MIPS, 100 MHz)\n"
140  "\n"
141  "(Note that the cloned CPU was automatically renamed to cpu1, to avoid a\n"
142  "collision.)\n"
143  "\n"
144  "See also: add (to add new components)\n"
145  " move (to move components within the tree)\n"
146  " remove (to remove components)\n"
147  " root (to inspect the current emulation setup)\n";
148 }
149 
150 
151 /*****************************************************************************/
152 
153 
154 #ifdef WITHUNITTESTS
155 
156 static void Test_CopyComponentCommand_Copy()
157 {
158  GXemul gxemul;
159 
160  gxemul.GetCommandInterpreter().RunCommand("add testmips");
161 
163  UnitTest::Assert("there should initially be 1 entry under root",
164  root->GetChildren().size(), 1);
165 
166  gxemul.GetCommandInterpreter().RunCommand("copy machine0 root");
167 
168  // The tree should now look like:
169  // root
170  // |-- machine0 [testmips]
171  // | \-- mainbus0
172  // | |-- ram0 (32 MB at offset 0)
173  // | |-- rom0 (16 MB at offset 0x1fc00000)
174  // | \-- cpu0 (MIPS, 100 MHz)
175  // \-- machine1 [testmips]
176  // \-- mainbus0
177  // |-- ram0 (32 MB at offset 0)
178  // |-- rom0 (16 MB at offset 0x1fc00000)
179  // \-- cpu0 (MIPS, 100 MHz)
180 
181  UnitTest::Assert("there should now be 2 entries (machine0 and machine1) under root",
182  root->GetChildren().size(), 2);
183 
184  refcount_ptr<Component> machine0 = root->GetChildren()[0];
185  refcount_ptr<Component> machine1 = root->GetChildren()[1];
186 
187  UnitTest::Assert("name 0 mismatch", machine0->GetVariable("name")->ToString(), "machine0");
188  UnitTest::Assert("name 1 mismatch", machine1->GetVariable("name")->ToString(), "machine1");
189 }
190 
192 {
193  UNITTEST(Test_CopyComponentCommand_Copy);
194 }
195 
196 #endif
refcount_ptr::IsNULL
bool IsNULL() const
Checks whether or not an object is referenced by the reference counted pointer.
Definition: refcount_ptr.h:216
CopyComponentCommand::Execute
virtual bool Execute(GXemul &gxemul, const vector< string > &arguments)
Executes the command on a given GXemul instance.
Definition: CopyComponentCommand.cc:49
CopyComponentCommand::GetLongDescription
virtual string GetLongDescription() const
Returns a long description/help message for the command.
Definition: CopyComponentCommand.cc:118
CopyComponentCommand
A Command which copies (clones) a Component from one location to another in the component tree.
Definition: CopyComponentCommand.h:41
Component::AddChild
void AddChild(refcount_ptr< Component > childComponent, size_t insertPosition=(size_t) -1)
Adds a reference to a child component.
Definition: Component.cc:595
GXemul
The main emulator class.
Definition: GXemul.h:54
GXemul::GetCommandInterpreter
CommandInterpreter & GetCommandInterpreter()
Gets a reference to the CommandInterpreter.
Definition: GXemul.cc:631
CopyComponentCommand::GetShortDescription
virtual string GetShortDescription() const
Returns a short (one-line) description of the command.
Definition: CopyComponentCommand.cc:112
refcount_ptr< Component >
UNITTESTS
#define UNITTESTS(class)
Helper for unit test case execution.
Definition: UnitTest.h:184
Command
A Command is a named function, executed by the CommandInterpreter.
Definition: Command.h:48
UNITTEST
#define UNITTEST(functionname)
Helper for unit test case execution.
Definition: UnitTest.h:217
UnitTest::Assert
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
Definition: UnitTest.cc:40
Component::GetChildren
Components & GetChildren()
Gets pointers to child components.
Definition: Component.cc:674
CopyComponentCommand::CopyComponentCommand
CopyComponentCommand()
Constructs a CopyComponentCommand.
Definition: CopyComponentCommand.cc:32
GXemul::GetRootComponent
refcount_ptr< Component > GetRootComponent()
Gets a pointer to the root configuration component.
Definition: GXemul.cc:667
UI::ShowDebugMessage
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
Component::LookupPath
const refcount_ptr< Component > LookupPath(string path) const
Looks up a path from this Component, and returns a pointer to the found Component,...
Definition: Component.cc:778
CopyComponentCommand::~CopyComponentCommand
virtual ~CopyComponentCommand()
Definition: CopyComponentCommand.cc:38
StateVariable::ToString
string ToString() const
Returns the variable as a readable string.
Definition: StateVariable.cc:229
CommandInterpreter::RunCommand
bool RunCommand(const string &command, bool *pSuccess=NULL)
Runs a command, given as a string.
Definition: CommandInterpreter.cc:957
GXemul::GetUI
UI * GetUI()
Gets a pointer to the GXemul instance' active UI.
Definition: GXemul.cc:661
CopyComponentCommand.h
Component::GetVariable
StateVariable * GetVariable(const string &name)
Gets a pointer to a state variable.
Definition: Component.cc:949
Component::Clone
refcount_ptr< Component > Clone() const
Clones the component and all its children.
Definition: Component.cc:76
GXemul.h

Generated on Tue Mar 24 2020 14:04:48 for GXemul by doxygen 1.8.17