WPILibC++  trunk
Subsystem.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2011. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #include "Commands/Subsystem.h"
8 
9 #include "Commands/Command.h"
10 #include "Commands/Scheduler.h"
11 #include "WPIErrors.h"
12 
17 Subsystem::Subsystem(const char *name) :
18  m_currentCommand(NULL),
19  m_defaultCommand(NULL),
20  m_initializedDefaultCommand(false)
21 {
22  m_name = name;
24  m_table = NULL;
25  m_currentCommandChanged = true;
26 }
36 
37 }
38 
49 {
50  if (command == NULL)
51  {
52  m_defaultCommand = NULL;
53  }
54  else
55  {
56  bool found = false;
57  Command::SubsystemSet requirements = command->GetRequirements();
58  Command::SubsystemSet::iterator iter = requirements.begin();
59  for (; iter != requirements.end(); iter++)
60  {
61  if (*iter == this)
62  {
63  found = true;
64  break;
65  }
66  }
67 
68  if (!found)
69  {
70  wpi_setWPIErrorWithContext(CommandIllegalUse, "A default command must require the subsystem");
71  return;
72  }
73 
74  m_defaultCommand = command;
75  }
76  if (m_table != NULL)
77  {
78  if (m_defaultCommand != NULL)
79  {
80  m_table->PutBoolean("hasDefault", true);
81  m_table->PutString("default", m_defaultCommand->GetName());
82  }
83  else
84  {
85  m_table->PutBoolean("hasDefault", false);
86  }
87  }
88 }
89 
95 {
96  if (!m_initializedDefaultCommand) {
97  m_initializedDefaultCommand = true;
99  }
100  return m_defaultCommand;
101 }
102 
108 {
109  m_currentCommand = command;
110  m_currentCommandChanged = true;
111 }
112 
118 {
119  return m_currentCommand;
120 }
121 
127 void Subsystem::ConfirmCommand()
128 {
129  if (m_currentCommandChanged) {
130  if (m_table != NULL)
131  {
132  if (m_currentCommand != NULL)
133  {
134  m_table->PutBoolean("hasCommand", true);
135  m_table->PutString("command", m_currentCommand->GetName());
136  }
137  else
138  {
139  m_table->PutBoolean("hasCommand", false);
140  }
141  }
142  m_currentCommandChanged = false;
143  }
144 }
145 
146 
147 
148 std::string Subsystem::GetName()
149 {
150  return m_name;
151 }
152 
154 {
155  return "Subsystem";
156 }
157 
159 {
160  m_table = table;
161  if(m_table!=NULL){
162  if (m_defaultCommand != NULL) {
163  m_table->PutBoolean("hasDefault", true);
164  m_table->PutString("default", m_defaultCommand->GetName());
165  } else {
166  m_table->PutBoolean("hasDefault", false);
167  }
168  if (m_currentCommand != NULL) {
169  m_table->PutBoolean("hasCommand", true);
170  m_table->PutString("command", m_currentCommand->GetName());
171  } else {
172  m_table->PutBoolean("hasCommand", false);
173  }
174  }
175 }
176 
178  return m_table;
179 }
Subsystem(const char *name)
Definition: Subsystem.cpp:17
Definition: ITable.h:26
Command * GetCurrentCommand()
Definition: Subsystem.cpp:117
virtual ITable * GetTable()
Definition: Subsystem.cpp:177
virtual std::string GetName()
Definition: Subsystem.cpp:148
virtual std::string GetName()
Definition: Command.cpp:438
virtual void PutBoolean(std::string key, bool value)=0
virtual std::string GetSmartDashboardType()
Definition: Subsystem.cpp:153
Command * GetDefaultCommand()
Definition: Subsystem.cpp:94
void RegisterSubsystem(Subsystem *subsystem)
Definition: Scheduler.cpp:185
virtual void InitTable(ITable *table)
Definition: Subsystem.cpp:158
void SetCurrentCommand(Command *command)
Definition: Subsystem.cpp:107
virtual void InitDefaultCommand()
Definition: Subsystem.cpp:35
void SetDefaultCommand(Command *command)
Definition: Subsystem.cpp:48
static Scheduler * GetInstance()
Definition: Scheduler.cpp:47
virtual void PutString(std::string key, std::string value)=0
SubsystemSet GetRequirements()
Definition: Command.cpp:260