|
|
|
Back
Midas
Rome
Roody
Rootana
|
| Midas DAQ System |
Not logged in |
 |
|
16 Jul 2026, Konstantin Olchanski, Info, c++ exceptions, follow up
|
16 Jul 2026, Konstantin Olchanski, Info, c++ exceptions, follow up
|
16 Jul 2026, Yiwen Yang, Info, c++ exceptions, follow up
|
16 Jul 2026, Yiwen Yang, Info, c++ exceptions, follow up
|
20 Jul 2026, Konstantin Olchanski, Info, c++ exceptions, follow up
|
|
|
Message ID: 3248
Entry time: 16 Jul 2026
In reply to: 3247
Reply to this: 3253
|
| Author: |
Yiwen Yang |
| Topic: |
Info |
| Subject: |
c++ exceptions, follow up |
|
|
Looks like the code formatting got messed up by the elog... here it is in the attachment instead. |
|
#include <iostream>
#include <unistd.h>
void i2c_set_bit(int bit, int val) {
std::cout << "Bit " << bit << " set to " << val << "\n";
}
class i2c_temp_bit {
int bit; /* which bit */
int newval; /* value to set bit to */
int oldval; /* value to reset bit to */
public:
i2c_temp_bit(int b, int nv, int ov)
: bit{b}, newval{nv}, oldval{ov}
{
std::cout << "Bit " << bit << " set to " << newval << "\n";
}
~i2c_temp_bit()
{
std::cout << "Bit " << bit << " set to " << oldval << "\n";
}
i2c_temp_bit(const i2c_temp_bit&) = delete;
i2c_temp_bit(i2c_temp_bit&&) = delete;
i2c_temp_bit& operator=(const i2c_temp_bit&) = delete;
i2c_temp_bit& operator=(i2c_temp_bit&&) = delete;
};
int check_kettle_temp() {
static int callnum = 0;
if (++callnum > 5) {
return 100;
}
#ifdef BADNUM
else if (callnum == 4) { /* Don't like this number */
throw callnum;
}
#endif
else {
return 25;
}
}
void foo() {
std::cout << "Waiting for kettle to boil" << std::endl;
do { /* Check every 100 ms */
usleep(100000);
} while (check_kettle_temp() < 100);
std::cout << "Water is boiled!" << std::endl;
}
void boil_kettle()
{
#ifdef RAII
i2c_temp_bit temp(10, 1, 0);
foo();
#else
i2c_set_bit(10, 1); // bit 10 is stove heater control, turn it on
foo(); // wait for kettle to start boiling
i2c_set_bit(10, 0); // turn heater off
#endif
}
int main()
{
try {
boil_kettle();
} catch (...) {
std::cerr << "Abnormal termination\n";
}
}
|