RootComponent.cc Source File

Back to the index.

RootComponent.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 "ComponentFactory.h"
30 #include "GXemul.h"
31 
32 
34  : Component("root", "root")
35  , m_gxemul(owner)
36  , m_accuracy("cycle")
37 {
38  SetVariableValue("name", "\"root\"");
39 
40  AddVariable("accuracy", &m_accuracy);
41 }
42 
43 
45 {
46  if (m_accuracy != "cycle" && m_accuracy != "sloppy") {
47  gxemul->GetUI()->ShowDebugMessage(this, "accuracy must be \"cycle\" or \"sloppy\".\n");
48  return false;
49  }
50 
51  return true;
52 }
53 
54 
56 {
57  m_gxemul = owner;
58 }
59 
60 
61 bool RootComponent::CheckVariableWrite(StateVariable& var, const string& oldValue)
62 {
63  UI* ui = GetUI();
64  const string& name = var.GetName();
65 
66  if (name == "accuracy") {
67  if (var.ToString() != "cycle" && var.ToString() != "sloppy") {
68  if (ui != NULL)
69  ui->ShowDebugMessage(this, "accuracy must be \"cycle\" or \"sloppy\".\n");
70 
71  return false;
72  }
73  }
74 
75  return Component::CheckVariableWrite(var, oldValue);
76 }
77 
78 
79 /*****************************************************************************/
80 
81 
82 #ifdef WITHUNITTESTS
83 
84 static void Test_RootComponent_CreateComponent()
85 {
86  refcount_ptr<Component> component;
87 
88  component = ComponentFactory::CreateComponent("root");
89  UnitTest::Assert("creating a root component with CreateComponent "
90  "should NOT be possible", component.IsNULL() == true);
91 }
92 
93 static void Test_RootComponent_InitialVariables()
94 {
95  refcount_ptr<Component> component = new RootComponent;
96 
97  StateVariable* name = component->GetVariable("name");
98  StateVariable* step = component->GetVariable("step");
99  StateVariable* accuracy = component->GetVariable("accuracy");
100 
101  UnitTest::Assert("name should be root", name->ToString(), "root");
102  UnitTest::Assert("step should be 0", step->ToInteger(), 0);
103  UnitTest::Assert("accuracy should be cycle", accuracy->ToString(), "cycle");
104 }
105 
106 static void Test_RootComponent_AccuracyValues()
107 {
108  GXemul dummyGXemul;
109 
110  refcount_ptr<Component> component = new RootComponent;
111  StateVariable* accuracy = component->GetVariable("accuracy");
112 
113  accuracy->SetValue("\"sloppy\"");
114  UnitTest::Assert("sloppy should be ok", component->PreRunCheck(&dummyGXemul));
115 
116  accuracy->SetValue("\"nonsense\"");
117  UnitTest::Assert("nonsense should not be ok", !component->PreRunCheck(&dummyGXemul));
118 
119  accuracy->SetValue("\"cycle\"");
120  UnitTest::Assert("cycle should be ok", component->PreRunCheck(&dummyGXemul));
121 }
122 
124 {
125  UNITTEST(Test_RootComponent_CreateComponent);
126  UNITTEST(Test_RootComponent_InitialVariables);
127  UNITTEST(Test_RootComponent_AccuracyValues);
128 
129  // TODO: Test owner
130 }
131 
132 #endif
133 
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
StateVariable * GetVariable(const string &name)
Gets a pointer to a state variable.
Definition: Component.cc:949
static refcount_ptr< Component > CreateComponent(const string &componentNameAndOptionalArgs, GXemul *gxemul=NULL)
Creates a component given a short component name.
const string & GetName() const
Gets the name of the variable.
bool AddVariable(const string &name, T *variablePointer)
Adds a state variable of type T to the Component.
Definition: Component.h:563
UI * GetUI()
Gets an UI reference for outputting debug messages during runtime.
Definition: Component.cc:583
A Component which is the default root node in the configuration.
Definition: RootComponent.h:57
The main emulator class.
Definition: GXemul.h:54
#define UNITTESTS(class)
Helper for unit test case execution.
Definition: UnitTest.h:184
bool SetValue(const string &expression)
Set the variable&#39;s value, using a string expression.
string ToString() const
Returns the variable as a readable string.
A Component is a node in the configuration tree that makes up an emulation setup. ...
Definition: Component.h:62
bool PreRunCheck(GXemul *gxemul)
Checks the state of this component and all its children, before starting execution.
Definition: Component.cc:298
RootComponent(GXemul *owner=NULL)
Constructs a RootComponent.
uint64_t ToInteger() const
Returns the variable as an unsignedinteger value.
StateVariables make up the persistent state of Component objects.
Definition: StateVariable.h:67
virtual bool CheckVariableWrite(StateVariable &var, const string &oldValue)
Checks whether a write to a variable is OK.
Definition: Component.cc:969
virtual bool CheckVariableWrite(StateVariable &var, const string &oldValue)
Checks whether a write to a variable is OK.
virtual bool PreRunCheckForComponent(GXemul *gxemul)
Checks the state of this component, before starting execution.
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
Definition: UnitTest.cc:40
bool SetVariableValue(const string &name, const string &expression)
Sets a variable to a new value.
Definition: Component.cc:1030
UI * GetUI()
Gets a pointer to the GXemul instance&#39; active UI.
Definition: GXemul.cc:661
void SetOwner(GXemul *owner)
Base class for a User Interface.
Definition: UI.h:40
#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