AddComponentCommand.cc Source File

Back to the index.

AddComponentCommand.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-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 "ComponentFactory.h"
30 #include "GXemul.h"
31 
32 
34  : Command("add", "component-name [path]")
35 {
36 }
37 
38 
40 {
41 }
42 
43 
44 static void ShowMsg(GXemul& gxemul, const string& msg)
45 {
46  gxemul.GetUI()->ShowDebugMessage(msg);
47 }
48 
49 
50 bool AddComponentCommand::Execute(GXemul& gxemul, const vector<string>& arguments)
51 {
52  if (arguments.size() < 1) {
53  ShowMsg(gxemul, "No component-name given.\n");
54  return false;
55  }
56 
57  if (arguments.size() > 2) {
58  ShowMsg(gxemul, "Too many arguments.\n");
59  return false;
60  }
61 
62  string componentName = arguments[0];
63 
64  refcount_ptr<Component> componentToAdd =
65  ComponentFactory::CreateComponent(componentName, &gxemul);
66 
67  if (componentToAdd.IsNULL()) {
68  ShowMsg(gxemul, componentName + ": unknown component,"
69  " or invalid arguments given.\n");
70  return false;
71  }
72 
73  string path = "root";
74  if (arguments.size() == 2)
75  path = arguments[1];
76 
77  vector<string> matches = gxemul.GetRootComponent()->
78  FindPathByPartialMatch(path);
79  if (matches.size() == 0) {
80  ShowMsg(gxemul, path +
81  ": not a path to a known component.\n");
82  return false;
83  }
84  if (matches.size() > 1) {
85  ShowMsg(gxemul, path + " matches multiple components:\n");
86  for (size_t i=0; i<matches.size(); i++)
87  ShowMsg(gxemul, " " + matches[i] + "\n");
88  return false;
89  }
90 
91  refcount_ptr<Component> whereToAddIt =
92  gxemul.GetRootComponent()->LookupPath(matches[0]);
93  if (whereToAddIt.IsNULL()) {
94  ShowMsg(gxemul, path + ": lookup of path failed.\n");
95  return false;
96  }
97 
98  whereToAddIt->AddChild(componentToAdd);
99 
100  return true;
101 }
102 
103 
105 {
106  return "Adds a component to the emulation.";
107 }
108 
109 
111 {
112  return
113  "Adds a component (given by the component-name) to the current emulation\n"
114  "setup. If path is omitted, the component is added at the root of the\n"
115  "component tree. For example:\n"
116  "\n"
117  "> add testmips <-- this adds machine0\n"
118  "> root\n"
119  " root\n"
120  " \\-- machine0 [testmips]\n"
121  " \\-- mainbus0\n"
122  " |-- ram0 (32 MB at offset 0)\n"
123  " |-- rom0 (16 MB at offset 0x1fc00000)\n"
124  " \\-- cpu0 (5KE, 100 MHz)\n"
125  "...\n"
126  "> add mips_cpu(model=R4400) mainbus0 <-- note that arguments\n"
127  "> root can be given during\n"
128  " root component creation\n"
129  " \\-- machine0 [testmips]\n"
130  " \\-- mainbus0\n"
131  " |-- ram0 (32 MB at offset 0)\n"
132  " |-- rom0 (16 MB at offset 0x1fc00000)\n"
133  " |-- cpu0 (5KE, 100 MHz)\n"
134  " \\-- cpu1 (R4400, 100 MHz)\n"
135  "\n"
136  "See also: copy (to copy/clone a component in the tree)\n"
137  " list-components (to get a list of available component types)\n"
138  " remove (to remove a component from the emulation)\n"
139  " root (to inspect the current emulation setup)\n";
140 }
141 
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
static refcount_ptr< Component > CreateComponent(const string &componentNameAndOptionalArgs, GXemul *gxemul=NULL)
Creates a component given a short component name.
virtual string GetShortDescription() const
Returns a short (one-line) description of the command.
The main emulator class.
Definition: GXemul.h:54
AddComponentCommand()
Constructs an AddComponentCommand.
A Command is a named function, executed by the CommandInterpreter.
Definition: Command.h:48
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
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.
bool IsNULL() const
Checks whether or not an object is referenced by the reference counted pointer.
Definition: refcount_ptr.h:216

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13