MoveComponentCommand.cc Source File

Back to the index.

MoveComponentCommand.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("move", "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 MoveComponentCommand::Execute(GXemul& gxemul, const vector<string>& arguments)
50 {
51  if (arguments.size() != 2) {
52  ShowMsg(gxemul, "syntax: move 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> whatToMove =
87  gxemul.GetRootComponent()->LookupPath(matchesFrom[0]);
88  if (whatToMove.IsNULL()) {
89  ShowMsg(gxemul, "Lookup of origin path " + pathFrom + " failed.\n");
90  return false;
91  }
92 
93  refcount_ptr<Component> parent = whatToMove->GetParent();
94  if (parent.IsNULL()) {
95  ShowMsg(gxemul, "Cannot find the component's parent.\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  parent->RemoveChild(whatToMove);
107  whereToAddIt->AddChild(whatToMove);
108 
109  return true;
110 }
111 
112 
114 {
115  return "Moves a component within the tree.";
116 }
117 
118 
120 {
121  return
122  "Moves a component (given a path) from one place in the configuration\n"
123  "tree to another. Example: Let's say a CPU component has by accident\n"
124  "been added outside of the machine's mainbus0 (where it is supposed to\n"
125  "be located). The move command fixes the problem:\n"
126  "\n"
127  "> add testmips\n"
128  "> add mips_cpu \n"
129  "> root\n"
130  " root\n"
131  " |-- machine0 [testmips]\n"
132  " | \\-- mainbus0\n"
133  " | |-- ram0 (32 MB at offset 0)\n"
134  " | |-- rom0 (16 MB at offset 0x1fc00000)\n"
135  " | \\-- cpu0 (MIPS, 100 MHz)\n"
136  " \\-- cpu0 (MIPS, 100 MHz)\n"
137  "> move root.cpu0 mainbus0 \n"
138  "> root\n"
139  " root\n"
140  " \\-- machine0 [testmips]\n"
141  " \\-- mainbus0\n"
142  " |-- ram0 (32 MB at offset 0)\n"
143  " |-- rom0 (16 MB at offset 0x1fc00000)\n"
144  " |-- cpu0 (MIPS, 100 MHz)\n"
145  " \\-- cpu1 (MIPS, 100 MHz)\n"
146  "\n"
147  "(Note that the moved cpu was automatically renamed to cpu1, to avoid a\n"
148  "collision.)\n"
149  "\n"
150  "See also: add (to add new components)\n"
151  " root (to inspect the current emulation setup)\n";
152 }
153 
154 
155 /*****************************************************************************/
156 
157 
158 #ifdef WITHUNITTESTS
159 
160 static void Test_MoveComponentCommand_Move()
161 {
162  GXemul gxemul;
163 
164  gxemul.GetCommandInterpreter().RunCommand("add testmips");
165  gxemul.GetCommandInterpreter().RunCommand("add ram");
166 
168  UnitTest::Assert("there should be 2 entries under root",
169  root->GetChildren().size(), 2);
170 
171  gxemul.GetCommandInterpreter().RunCommand("move root.ram mainbus");
172 
173  // The tree should now look like:
174  // root
175  // \-- machine0 [testmips]
176  // \-- mainbus0
177  // |-- ram0 (32 MB at offset 0)
178  // |-- rom0 (16 MB at offset 0x1fc00000)
179  // |-- fb_videoram0 (15 MB at offset 0x12000000)
180  // |-- cpu0 (MIPS, 100 MHz)
181  // \-- ram1 (0 bytes at offset 0)
182 
183  UnitTest::Assert("there should now be 1 entry (machine0) under root",
184  root->GetChildren().size(), 1);
185 
186  refcount_ptr<Component> machine0 = root->GetChildren()[0];
187 
188  UnitTest::Assert("there should be 1 entry (mainbus0) under machine0",
189  machine0->GetChildren().size(), 1);
190 
191  refcount_ptr<Component> mainbus0 = machine0->GetChildren()[0];
192 
193  UnitTest::Assert("there should be 5 components on mainbus0",
194  mainbus0->GetChildren().size(), 5);
195 }
196 
198 {
199  UNITTEST(Test_MoveComponentCommand_Move);
200 }
201 
202 #endif
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
Component * GetParent()
Gets this component&#39;s parent component, if any.
Definition: Component.cc:381
MoveComponentCommand()
Constructs a MoveComponentCommand.
bool RunCommand(const string &command, bool *pSuccess=NULL)
Runs a command, given as a string.
virtual bool Execute(GXemul &gxemul, const vector< string > &arguments)
Executes the command on a given GXemul instance.
virtual string GetLongDescription() const
Returns a long description/help message for the command.
Components & GetChildren()
Gets pointers to child components.
Definition: Component.cc:674
The main emulator class.
Definition: GXemul.h:54
#define UNITTESTS(class)
Helper for unit test case execution.
Definition: UnitTest.h:184
CommandInterpreter & GetCommandInterpreter()
Gets a reference to the CommandInterpreter.
Definition: GXemul.cc:631
A Command which moves a Component from one location to another in the component tree.
virtual string GetShortDescription() const
Returns a short (one-line) description of the command.
size_t RemoveChild(Component *childToRemove)
Removes a reference to a child component.
Definition: Component.cc:655
A Command is a named function, executed by the CommandInterpreter.
Definition: Command.h:48
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
Definition: UnitTest.cc:40
refcount_ptr< Component > GetRootComponent()
Gets a pointer to the root configuration component.
Definition: GXemul.cc:667
void AddChild(refcount_ptr< Component > childComponent, size_t insertPosition=(size_t) -1)
Adds a reference to a child component.
Definition: Component.cc:595
UI * GetUI()
Gets a pointer to the GXemul instance&#39; active UI.
Definition: GXemul.cc:661
const refcount_ptr< Component > LookupPath(string path) const
Looks up a path from this Component, and returns a pointer to the found Component, if any.
Definition: Component.cc:778
#define UNITTEST(functionname)
Helper for unit test case execution.
Definition: UnitTest.h:217
bool IsNULL() const
Checks whether or not an object is referenced by the reference counted pointer.
Definition: refcount_ptr.h:216

Generated on Sun Sep 30 2018 16:05:18 for GXemul by doxygen 1.8.13