2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2011. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Commands/Command.h"
|
|
|
|
|
#include "Commands/CommandGroup.h"
|
|
|
|
|
#include "Commands/Scheduler.h"
|
2014-08-08 17:05:49 -04:00
|
|
|
#include "RobotState.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Timer.h"
|
|
|
|
|
#include "WPIErrors.h"
|
2014-09-21 16:54:39 -04:00
|
|
|
#include <typeinfo>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
static const char *kName = "name";
|
|
|
|
|
static const char *kRunning = "running";
|
|
|
|
|
static const char *kIsParented = "isParented";
|
|
|
|
|
|
|
|
|
|
int Command::m_commandCounter = 0;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::InitCommand(const char *name, double timeout) {
|
|
|
|
|
m_timeout = timeout;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_name = name == nullptr ? std::string() : name;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new command.
|
|
|
|
|
* The name of this command will be default.
|
|
|
|
|
*/
|
2015-06-23 04:49:51 -07:00
|
|
|
Command::Command() { InitCommand(nullptr, -1.0); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new command with the given name and no timeout.
|
|
|
|
|
* @param name the name for this command
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Command::Command(const char *name) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (name == nullptr) wpi_setWPIErrorWithContext(NullParameter, "name");
|
2015-06-25 15:07:55 -04:00
|
|
|
InitCommand(name, -1.0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new command with the given timeout and a default name.
|
|
|
|
|
* @param timeout the time (in seconds) before this command "times out"
|
|
|
|
|
* @see Command#isTimedOut() isTimedOut()
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Command::Command(double timeout) {
|
|
|
|
|
if (timeout < 0.0)
|
|
|
|
|
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
|
2015-06-23 04:49:51 -07:00
|
|
|
InitCommand(nullptr, timeout);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new command with the given name and timeout.
|
|
|
|
|
* @param name the name of the command
|
|
|
|
|
* @param timeout the time (in seconds) before this command "times out"
|
|
|
|
|
* @see Command#isTimedOut() isTimedOut()
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Command::Command(const char *name, double timeout) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (name == nullptr) wpi_setWPIErrorWithContext(NullParameter, "name");
|
2015-06-25 15:07:55 -04:00
|
|
|
if (timeout < 0.0)
|
|
|
|
|
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
|
|
|
|
|
InitCommand(name, timeout);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Command::~Command() { // TODO deal with cleaning up all listeners
|
2015-06-23 04:49:51 -07:00
|
|
|
/*if (m_table != nullptr){
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->RemoveChangeListener(kRunning, this);
|
|
|
|
|
}*/
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the ID (sequence number) for this command
|
|
|
|
|
* The ID is a unique sequence number that is incremented for each command.
|
|
|
|
|
* @return the ID of this command
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int Command::GetID() const { return m_commandID; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the timeout of this command.
|
|
|
|
|
* @param timeout the timeout (in seconds)
|
|
|
|
|
* @see Command#isTimedOut() isTimedOut()
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::SetTimeout(double timeout) {
|
|
|
|
|
if (timeout < 0.0)
|
|
|
|
|
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
|
|
|
|
|
else
|
|
|
|
|
m_timeout = timeout;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the time since this command was initialized (in seconds).
|
|
|
|
|
* This function will work even if there is no specified timeout.
|
|
|
|
|
* @return the time since this command was initialized (in seconds).
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Command::TimeSinceInitialized() const {
|
|
|
|
|
if (m_startTime < 0.0)
|
|
|
|
|
return 0.0;
|
|
|
|
|
else
|
|
|
|
|
return Timer::GetFPGATimestamp() - m_startTime;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* This method specifies that the given {@link Subsystem} is used by this
|
|
|
|
|
* command.
|
2013-12-15 18:30:16 -05:00
|
|
|
* This method is crucial to the functioning of the Command System in general.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* <p>Note that the recommended way to call this method is in the
|
|
|
|
|
* constructor.</p>
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
|
|
|
|
* @param subsystem the {@link Subsystem} required
|
|
|
|
|
* @see Subsystem
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::Requires(Subsystem *subsystem) {
|
|
|
|
|
if (!AssertUnlocked("Can not add new requirement to command")) return;
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
if (subsystem != nullptr)
|
2015-06-25 15:07:55 -04:00
|
|
|
m_requirements.insert(subsystem);
|
|
|
|
|
else
|
|
|
|
|
wpi_setWPIErrorWithContext(NullParameter, "subsystem");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called when the command has been removed.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This will call {@link Command#interrupted() interrupted()} or {@link
|
|
|
|
|
* Command#end() end()}.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::Removed() {
|
|
|
|
|
if (m_initialized) {
|
|
|
|
|
if (IsCanceled()) {
|
|
|
|
|
Interrupted();
|
|
|
|
|
_Interrupted();
|
|
|
|
|
} else {
|
|
|
|
|
End();
|
|
|
|
|
_End();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_initialized = false;
|
|
|
|
|
m_canceled = false;
|
|
|
|
|
m_running = false;
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) m_table->PutBoolean(kRunning, false);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts up the command. Gets the command ready to start.
|
2015-06-25 15:07:55 -04:00
|
|
|
* <p>Note that the command will eventually start, however it will not
|
|
|
|
|
* necessarily
|
|
|
|
|
* do so immediately, and may in fact be canceled before initialize is even
|
|
|
|
|
* called.</p>
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::Start() {
|
|
|
|
|
LockChanges();
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_parent != nullptr)
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
CommandIllegalUse,
|
|
|
|
|
"Can not start a command that is part of a command group");
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Scheduler::GetInstance()->AddCommand(this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The run method is used internally to actually run the commands.
|
|
|
|
|
* @return whether or not the command should stay within the {@link Scheduler}.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::Run() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (!m_runWhenDisabled && m_parent == nullptr && RobotState::IsDisabled())
|
2015-06-25 15:07:55 -04:00
|
|
|
Cancel();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (IsCanceled()) return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!m_initialized) {
|
|
|
|
|
m_initialized = true;
|
|
|
|
|
StartTiming();
|
|
|
|
|
_Initialize();
|
|
|
|
|
Initialize();
|
|
|
|
|
}
|
|
|
|
|
_Execute();
|
|
|
|
|
Execute();
|
|
|
|
|
return !IsFinished();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::_Initialize() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::_Interrupted() {}
|
|
|
|
|
|
|
|
|
|
void Command::_Execute() {}
|
|
|
|
|
|
|
|
|
|
void Command::_End() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called to indicate that the timer should start.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is called right before {@link Command#initialize() initialize()} is,
|
|
|
|
|
* inside the
|
2013-12-15 18:30:16 -05:00
|
|
|
* {@link Command#run() run()} method.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::StartTiming() { m_startTime = Timer::GetFPGATimestamp(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns whether or not the {@link Command#timeSinceInitialized()
|
|
|
|
|
* timeSinceInitialized()}
|
|
|
|
|
* method returns a number which is greater than or equal to the timeout for the
|
|
|
|
|
* command.
|
2013-12-15 18:30:16 -05:00
|
|
|
* If there is no timeout, this will always return false.
|
|
|
|
|
* @return whether the time has expired
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::IsTimedOut() const {
|
|
|
|
|
return m_timeout != -1 && TimeSinceInitialized() >= m_timeout;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns the requirements (as an std::set of {@link Subsystem Subsystems}
|
|
|
|
|
* pointers) of this command
|
|
|
|
|
* @return the requirements (as an std::set of {@link Subsystem Subsystems}
|
|
|
|
|
* pointers) of this command
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Command::SubsystemSet Command::GetRequirements() const {
|
|
|
|
|
return m_requirements;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prevents further changes from being made
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::LockChanges() { m_locked = true; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If changes are locked, then this will generate a CommandIllegalUse error.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param message the message to report on error (it is appended by a default
|
|
|
|
|
* message)
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return true if assert passed, false if assert failed
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::AssertUnlocked(const char *message) {
|
|
|
|
|
if (m_locked) {
|
|
|
|
|
char buf[128];
|
|
|
|
|
snprintf(buf, 128,
|
|
|
|
|
"%s after being started or being added to a command group",
|
|
|
|
|
message);
|
|
|
|
|
wpi_setWPIErrorWithContext(CommandIllegalUse, buf);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the parent of this command. No actual change is made to the group.
|
|
|
|
|
* @param parent the parent
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::SetParent(CommandGroup *parent) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (parent == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(NullParameter, "parent");
|
2015-06-23 04:49:51 -07:00
|
|
|
} else if (m_parent != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(CommandIllegalUse,
|
|
|
|
|
"Can not give command to a command group after "
|
|
|
|
|
"already being put in a command group");
|
|
|
|
|
} else {
|
|
|
|
|
LockChanges();
|
|
|
|
|
m_parent = parent;
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutBoolean(kIsParented, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is used internally to mark that the command has been started.
|
|
|
|
|
* The lifecycle of a command is:
|
|
|
|
|
*
|
|
|
|
|
* startRunning() is called.
|
|
|
|
|
* run() is called (multiple times potentially)
|
|
|
|
|
* removed() is called
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* It is very important that startRunning and removed be called in order or some
|
|
|
|
|
* assumptions
|
2013-12-15 18:30:16 -05:00
|
|
|
* of the code will be broken.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::StartRunning() {
|
|
|
|
|
m_running = true;
|
|
|
|
|
m_startTime = -1;
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) m_table->PutBoolean(kRunning, true);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether or not the command is running.
|
|
|
|
|
* This may return true even if the command has just been canceled, as it may
|
|
|
|
|
* not have yet called {@link Command#interrupted()}.
|
|
|
|
|
* @return whether or not the command is running
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::IsRunning() const { return m_running; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This will cancel the current command.
|
2015-06-25 15:07:55 -04:00
|
|
|
* <p>This will cancel the current command eventually. It can be called
|
|
|
|
|
* multiple times.
|
|
|
|
|
* And it can be called when the command is not running. If the command is
|
|
|
|
|
* running though,
|
2013-12-15 18:30:16 -05:00
|
|
|
* then the command will be marked as canceled and eventually removed.</p>
|
|
|
|
|
* <p>A command can not be canceled
|
2015-06-25 15:07:55 -04:00
|
|
|
* if it is a part of a command group, you must cancel the command group
|
|
|
|
|
* instead.</p>
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::Cancel() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_parent != nullptr)
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
CommandIllegalUse,
|
|
|
|
|
"Can not cancel a command that is part of a command group");
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
_Cancel();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* This works like cancel(), except that it doesn't throw an exception if it is
|
|
|
|
|
* a part
|
2013-12-15 18:30:16 -05:00
|
|
|
* of a command group. Should only be called by the parent command group.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::_Cancel() {
|
|
|
|
|
if (IsRunning()) m_canceled = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether or not this has been canceled.
|
|
|
|
|
* @return whether or not this has been canceled
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::IsCanceled() const { return m_canceled; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether or not this command can be interrupted.
|
|
|
|
|
* @return whether or not this command can be interrupted
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::IsInterruptible() const { return m_interruptible; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets whether or not this command can be interrupted.
|
|
|
|
|
* @param interruptible whether or not this command can be interrupted
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::SetInterruptible(bool interruptible) {
|
|
|
|
|
m_interruptible = interruptible;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the command requires the given {@link Subsystem}.
|
|
|
|
|
* @param system the system
|
2015-06-23 04:49:51 -07:00
|
|
|
* @return whether or not the subsystem is required (false if given nullptr)
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::DoesRequire(Subsystem *system) const {
|
|
|
|
|
return m_requirements.count(system) > 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the {@link CommandGroup} that this command is a part of.
|
|
|
|
|
* Will return null if this {@link Command} is not in a group.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return the {@link CommandGroup} that this command is a part of (or null if
|
|
|
|
|
* not in group)
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
CommandGroup *Command::GetGroup() const { return m_parent; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Sets whether or not this {@link Command} should run when the robot is
|
|
|
|
|
* disabled.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* <p>By default a command will not run when the robot is disabled, and will in
|
|
|
|
|
* fact be canceled.</p>
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param run whether or not this command should run when the robot is disabled
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::SetRunWhenDisabled(bool run) { m_runWhenDisabled = run; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns whether or not this {@link Command} will run when the robot is
|
|
|
|
|
* disabled, or if it will cancel itself.
|
|
|
|
|
* @return whether or not this {@link Command} will run when the robot is
|
|
|
|
|
* disabled, or if it will cancel itself
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::WillRunWhenDisabled() const { return m_runWhenDisabled; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Command::GetName() {
|
|
|
|
|
if (m_name.length() == 0) {
|
|
|
|
|
m_name = std::string("Command_") + std::string(typeid(*this).name());
|
|
|
|
|
}
|
|
|
|
|
return m_name;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Command::GetSmartDashboardType() const { return "Command"; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::InitTable(ITable *table) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) m_table->RemoveTableListener(this);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = table;
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutString(kName, GetName());
|
|
|
|
|
m_table->PutBoolean(kRunning, IsRunning());
|
2015-06-23 04:49:51 -07:00
|
|
|
m_table->PutBoolean(kIsParented, m_parent != nullptr);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->AddTableListener(kRunning, this, false);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable *Command::GetTable() const { return m_table; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::ValueChanged(ITable *source, const std::string &key,
|
|
|
|
|
EntryValue value, bool isNew) {
|
|
|
|
|
if (value.b) {
|
|
|
|
|
if (!IsRunning()) Start();
|
|
|
|
|
} else {
|
|
|
|
|
if (IsRunning()) Cancel();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|