Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose simulate_until to C API #325

Merged
merged 10 commits into from
Sep 4, 2019
22 changes: 22 additions & 0 deletions include/cse.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
extern "C" {
#endif

/// boolean false
const int CSE_FALSE = 0;

/// boolean true
const int CSE_TRUE = 1;

/// The type used to specify (simulation) time points. The time unit is nanoseconds.
typedef int64_t cse_time_point;
Expand Down Expand Up @@ -49,6 +54,9 @@ typedef enum
/// Invalid function argument.
CSE_ERRC_INVALID_ARGUMENT,

/// Function may not be called while in this state.
CSE_ERRC_ILLEGAL_STATE,

/// Index out of range.
CSE_ERRC_OUT_OF_RANGE,

Expand Down Expand Up @@ -216,6 +224,20 @@ cse_slave_index cse_execution_add_slave(
*/
int cse_execution_step(cse_execution* execution, size_t numSteps);

/**
* Advances an execution to a specific point in time.
*
* \param [in] execution
* The execution to be stepped.
* \param [in] targetTime
* The point in time, which to advance the simulation execution.
*
* \returns
* -1 on error, 0 if the simulation was stopped prior to reaching the specified targetTime
* and 1 if the simulation was successfully advanced to the specified targetTime.
*/
int cse_execution_simulate_until(cse_execution* execution, cse_time_point targetTime);
markaren marked this conversation as resolved.
Show resolved Hide resolved


/**
* Starts an execution.
Expand Down
19 changes: 19 additions & 0 deletions src/c/cse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ int cse_execution_step(cse_execution* execution, size_t numSteps)
}
}

int cse_execution_simulate_until(cse_execution* execution, cse_time_point targetTime)
{
if (execution->cpp_execution->is_running()) {
set_last_error(CSE_ERRC_ILLEGAL_STATE, "Function 'cse_execution_simulate_until' may not be called while simulation is running!");
return failure;
markaren marked this conversation as resolved.
Show resolved Hide resolved
} else {
execution->state = CSE_EXECUTION_RUNNING;
try {
const bool notStopped = execution->cpp_execution->simulate_until(to_time_point(targetTime)).get();
execution->state = CSE_EXECUTION_STOPPED;
return notStopped ? CSE_TRUE : CSE_FALSE;
} catch (...) {
handle_current_exception();
execution->state = CSE_EXECUTION_ERROR;
return failure;
}
}
}

int cse_execution_start(cse_execution* execution)
{
if (execution->t.joinable()) {
Expand Down