How to delay a function call until a global variable gets set - c++

I'm trying to execute a function, only after my global bool flag gets set to true. I'm currently parsing a file with instructions. Two of the commands are attack and testIsZOmbie.
void NAttack::CodeGen(CodeContext& context)
{
if(flag == true)
{
context.m_Ops.push_back("attack ");
}
}
The problem is that the flag only gets set after my NisZombie::CodeGen function, which always gets called after NAttack....Is there any way to delay NAttack being called until NisZOmbie?
void NisZombie::CodeGen(CodeContext& context)
{
if (m_Dir->m_value == 1)
{
context.m_Ops.push_back("test_zombie,1");
//std::cout<<"HERE";
context.m_Ops.push_back("je, ");
flag = true;
}
}

Related

How do I check if the 3ds charger is plugged in?

I am writting a function to know wether or not the charger is plugged in my 3ds however my function keep returning false while the charger is plugged in.
I expect my function to return true if the charger is plugged in my 3ds.
Here is the function:
bool isChargerPluggedin() {
bool chargeState;
PTMU_GetAdapterState(&chargeState);
return chargeState == 1;
}
My first attempt to solve my problem was to allocate memory to the chargeState variable:
bool isChargerPluggedin() {
bool *chargeState = new bool;
PTMU_GetAdapterState(chargeState);
bool state = *chargeState == 1;
delete chargeState;
return state;
}
My second attempt was by using an global variable to chargeState instead of a local variable :
bool chargeState;
bool isChargerPluggedin() {
PTMU_GetAdapterState(&chargeState);
return chargeState == 1;
}
To fix this, you must initialize first the libraries in your main function using the function ptmuInit. You must deinitialize the library before your program exitHere is a example:
int main() {
ptmuInit();
// program loop
while (true) {
bool isPlugged = isChargerPluggedin();
...
}
ptmuExit();
}

Using gMock for function call within function

I am unit testing a bit of code for coverage and am curious how I should handle this given situation.
I can't write the exact code here, so i'm using some placeholders.
TEST_F(testing, testingFunctionCall)
{
bool result = FunctionCall();
EXPECT_EQ(true, result);
}
Now the function call is in another file, and it requires another function to return a number for FunctionCall() to return true.
// In a seperate file
bool FunctionCall()
{
int number = GettingNumberFromSystemState();
if(number > A_CERTAIN_VALUE)
{
return true;
}else{
return false;
}
}
}
The function GettingNumberFromSystemState() will always return a false number because the firmware is not running on the hardware, so how would I create a mock function for GettingNumberFromSystemState() to always return the number I want?

Arduino can't get my boolean to work

I cant get my boolean to work I don't know what I'm doing wrong with it. Could anyone take a look at the code and give me a hint on what is wrong with it? I have tested different ways to write it but without success. The only time the boolean worked was when I put the code under void loop. But I can't use it there.
#include <RemoteReceiver.h>
boolean statusLed1 = false;
void setup() {
Serial.begin(115200);
// Initialize receiver on interrupt 0 (= digital pin 2), calls the callback "showCode"
// after 3 identical codes have been received in a row. (thus, keep the button pressed
// for a moment)
//
// See the interrupt-parameter of attachInterrupt for possible values (and pins)
// to connect the receiver.
RemoteReceiver::init(0, 3, showCode);
}
void loop() {
}
// Callback function is called only when a valid code is received.
void showCode(unsigned long receivedCode, unsigned int period) {
// Note: interrupts are disabled. You can re-enable them if needed.
// Print the received code.
Serial.print("Code: ");
Serial.print(receivedCode);
Serial.print(", period duration: ");
Serial.print(period);
Serial.println("us.");
if (receivedCode == 353805)
{
statusLed1 = true;
}
if (receivedCode == 352829)
{
statusLed1 = false;
}
if (statusLed1 = true) {
Serial.print("on");
}
if (statusLed1 = false){
Serial.print("off");
}
}
if (statusLed1 = true) {
Oldest gotcha in the book. = is assignment, == is equality comparison.
Also, don't compare against a boolean like this regardless.
if (statusLed1) {
change
if (statusLed1 = true) {
Serial.print("on");
}
if (statusLed1 = false){
Serial.print("off");
}
}
to
if (statusLed1 == true) {
Serial.print("on");
}
if (statusLed1 == false){
Serial.print("off");
}
}
or
if (statusLed1) {
Serial.print("on");
}
if (!statusLed1){
Serial.print("off");
}
}

Execute a piece of code in a function from the second invocation onwards

If I desire to run a piece of code in a function, only from the second invocation of the function onwards,
Questions:
Is there something wrong to do that?
How can I possibly achieve this ? Is using a static variable to do this a good idea ?
There's two answers to this question, depending on whether you have to deal with multi-threaded serialization or not.
No threading:
void doSomething() {
static bool firstTime = true;
if (firstTime) {
// do code specific to first pass
firstTime = false;
} else {
// do code specific to 2nd+ pass
}
// do any code that is common
}
With threading:
I'll write the generic boilerplate, but this code is system specific (requiring some variant of an atomic compareAndSet).
void doSomethingThreadSafe() {
static volatile atomic<int> passState = 0;
do {
if ( passState == 2 ) {
//perform pass 2+ code
break;
} else
if ( passState.compareAndSet(0,1) ) { // if passState==0 set passState=1 return true else return false
//perform pass 1 initialization code
passState = 2;
break;
} else {
//loser in setup collision, delay (wait for init code to finish) then retry
sleep(1);
}
} while(1);
//perform code common to all passes
}
Multi-threading will be a problem. To prevent this, if required, you'll probably need something like a mutex.
Like this:
void someFunction()
{
static bool firstRun = true;
if (!firstRun)
{
// code to execute from the second time onwards
}
else
{
firstRun = false;
}
// other code
}
Add a global counter.
eg:-
static int counter = 0;
public void testFunc(){
if(counter==1){
........
<Execute the functionality>
........
}
counter++;
}

How to store the state of a deeply-nested loop?

I am trying to refactor the following code, as I don't think it is structured well.
Can you think of a more elegant way to do this?
Bar::Bar()
{
m_iter1 = 0;
m_iter2 = 0;
}
bool Bar::foo()
{
_reinitialize();
for (; m_iter1 < 2; m_iter1++, m_iter2 = 0) {
_log("TRYING METHOD: [%d]", m_iter1);
if (_something_wrong(m_iter1)) {
return false;
}
for (; m_iter2 < 6; m_iter2++) {
if (_try_with_these_params(m_iter1, m_iter2, ...)) {
m_status = success;
// store next iteration in case we need to retry.
m_iter2++;
return true;
}
}
}
return false;
}
bool try_foo(Bar& bar)
{
if (bar.foo()) {
if (meet_some_criteria) {
return true;
} else {
bar.invalidate();
// retry. the Bar object stores the state.
try_foo(bar);
}
} else {
return false;
}
}
int main()
{
Bar bar;
if (try_foo(bar)) {
_log("SUCCESS");
} else {
_log("FAILURE");
}
}
The code loops over different parameter sets and tries to perform some action with these parameters. If the action is successful, then external code may invalidate the action and attempt to retry. The object which performs the action stores the state, so that external code may retry and re-enter the parameter loop at the place it left off.
The output using one parameters affect others, so the calculations need to be accomplished locally within the Bar class.
I would like to extend this idea to more dimensions, but doing so with the current design is clumsy.
A lot here depends on how expensive the various actions are.
If initially generating a candidate parameter set is cheap (and the set isn't too large), then you might want to just generate all the candidate sets, then give that result to the external code and try each in turn until you find one that the external code will accept.