Boost statechart Assertion `reactionResult_' error - boost

I have the following boost::statechart state:
struct PendingWFInfo : bsc::state<PendingWFInfo, PseudoIdle> {
bool success;
PendingWFInfo(my_context ctx) : my_base(ctx) {
std::cout << "[+] PendingWFInfo" << std::endl;
success = context<RH_StateMachine>().startReplenishment();
if (success) {
std::cout
<< ".....................Start replenishment successfully called\n";
} else {
std::cout << ".....................Failed to call Start replenishment "
"service\n";
}
}
~PendingWFInfo() { std::cout << "[-] leaving PendingWFInfo state... \n"; }
typedef boost::mpl::list<bsc::custom_reaction<InboundInfoReceived>,
bsc::transition<Error, CommunicationError>
> reactions;
bsc::result react(const InboundInfoReceived &event) {
if (context<RH_StateMachine>().getWireframeCapacity() > 0) {
transit<UserConfirmationPending>();
}
else{
transit<InboundImpossible>();
}
}
};
When i am in the current state and a InboundInfoReceived event is emitted, i get the following error
/usr/include/boost/statechart/result.hpp:58: boost::statechart::detail::safe_reaction_result::~safe_reaction_result(): Assertion `reactionResult_ == consumed' failed.
The if statement is true but the transition gives me an assertion error.

Related

vulkan unable to find validation layers

I'm following a vulkan tutorial and when i'm trying to check for validation layer support.
the function always returns false when it is supposed to return true.
this is the code:
bool app::checkValidationLayersSupport() {
uint32_t layersCount = 0;
vkEnumerateInstanceLayerProperties(&layersCount, nullptr);
std::vector<VkLayerProperties> availableLayers(layersCount);
vkEnumerateInstanceLayerProperties(&layersCount, availableLayers.data());
std::cout << layersCount << "\n";
for (const char* layerName : validationLayers) {
std::cout << layerName << "\n";
for (const auto& layerProperties : availableLayers) {
if (strcmp(layerName, layerProperties.layerName) == 0) {
std::cout << layerProperties.layerName << std::endl;
return true;
}
}
}
return false;}
validationLayers value is { "VK_LAYER_KHRONOS_validation" } (std::vector<const char*>)
after vkEnumerateInstanceLayerProperties LayersCount value is 5 so i think the problem is that the second vkEnumerateInstanceLayerProperties can't change the value of availableLayers so it remains empty
edit:
I checked it and the second vkEnumerateInstanceLayerProperties is initializing the vector but the needed validation layer is not in it.
fix: i reinstalled the sdk

C++ try and catch block waring: function reaches end of non void function

I have the following code and I am getting this Error which looks strange to me, as I have catch(...) which is equivalent to default. Am I missing anything?
Please, let me know.
Basically, I have int run function what should be the return placed to resolve the error??
class AgentLauncher : FSWatcher::Watcher {
public:
AgentLauncher(bool watch_, std::vector<string>& configFiles_)
: watch(watch_), configFiles(configFiles_),
stopped(false), need_reload(false) {}
int run() {
try {
FSWatcher configWatcher;
addWatches(configWatcher);
configWatcher.setInitialScan(false);
configWatcher.start();
while (true) {
std::unique_lock<std::mutex> lock(mutex);
opflex::ofcore::OFFramework framework;
Agent agent(framework);
configure(agent);
agent.start();
cond.wait(lock, [this]{ return stopped || need_reload; });
if (!stopped && need_reload) {
LOG(INFO) << "Reloading agent because of " <<
"configuration update";
}
agent.stop();
if (stopped) {
return 0;
}
need_reload = false;
}
configWatcher.stop();
} catch (pt::json_parser_error& e) {
return 4;
} catch (const std::exception& e) {
LOG(ERROR) << "Fatal error: " << e.what();
return 2;
} catch (...) {
LOG(ERROR) << "Unknown fatal error";
return 3;
}
}
/* some more function */
};
I am getting this Error which looks strange to me, as I have catch(...) which is equivalent to default. Am I missing anything?
That's true only when an exception gets thrown. When no exceptions are thrown, the function continues execution after all the catch statements. There is no return statement after them. That's a problem.
Add a return statement just before the end of the function.
int run()
{
try
{
...
}
catch (pt::json_parser_error& e)
{
return 4;
}
catch (const std::exception& e)
{
LOG(ERROR) << "Fatal error: " << e.what();
return 2;
}
catch (...)
{
LOG(ERROR) << "Unknown fatal error";
return 3;
}
// Add this.
return <something_appropriate>;
}

Can I do this without a macro (in C++ 11)?

I have code like this:
void function()
{
auto isOk=task(1);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(2);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(3);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(4);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(5);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(6);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(7);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(8);
if(!isOk)
{
return;
}
// more code here
auto isOk=task(9);
if(!isOk)
{
return;
}
}
It should be noted that I can not put them in a loop (My code is similar to this but not exactly this code)
The if block is very ugly and I may be bale to write it as follow:
#define TASK(x) {if(!task(x)) return;}
void function()
{
TASK(1);
// more code here
TASK(2);
// more code here
TASK(3);
// more code here
TASK(4);
// more code here
TASK(5);
// more code here
TASK(6);
// more code here
TASK(7);
// more code here
TASK(8);
// more code here
TASK(9);
}
My question is:
Is there any better way to do this when I am using C++11?
The problem with this code is:
I can not debug it easily.
The macro is not inside a namespace and maybe conflict with other macros.
Update 1
As most of the answer here tries to solve the problem in the specific code, when I am looking for the general solution, I am asking specifc questions related to this code:
1- Can I use lambda to mimic the macro?
2- Can I use a constexpr to mimic a macro?
3- Any other way to mimic a MACRO in a compiler friendly way (with the same result as a macro) so I can easily debug them?
void function() {
if (!task(1)) return;
// code here
if (!task(2)) return;
// more code here
if (!task(3)) return;
// more code here
}
This is small and tight and no ugly bulky blocks.
If task(1) is much larger, you can put return; on the next line indented.
Instead of using a plain return, you could choose to use exceptions instead, which not only leave the current function, but all functions until they find a catch block.
Something like this:
void tryTask(int i){
auto isOk=task(i);
if(!isOk)
{
throw std::runtime_error("Task failed: Nr. "+to_string(i));
}
}
function()
{
tryTask(1);
// more code here
tryTask(2);
// more code here
tryTask(3);
...
}
This however lets your function throw an exception instead of just returning if one of the tasks failed. If this is not what you want, surround it either inside the function with a try-catch block or call it from a second function like this:
void callfunction(){
try{
function();
} catch (std::exception& e) {
//do whatever happens if the function failed, or nothing
}
}
If you have control about the task() function, you might also decide to throw the exception directly inside this function instead of returning a bool.
If you want to make sure you only catch your own exceptions, write a small class for this taking only the information you need for handling the exception (if you don't need any, an empty class will do the job) and throw/catch an instance of your class instead.
Here's a quick and dirty approach with lambdas.
Assuming this is your task function:
#include <iostream>
/** Returns 0 on success; any other returned value is a failure */
int task(int arg)
{
std::cout << "Called task " << arg << std::endl;
return arg < 3 ? 0 : 1;
}
Invoke the tasks in a chain as follows:
#include <iostream>
int main()
{
int result = Chain::start()
.and_then([]() -> int {return task(1);})
.and_then([]() -> int {return task(2);})
.and_then([]() -> int {return task(3);})
.and_then([]() -> int {return task(4);})
.and_then([]() -> int {return task(5);})
.and_then([]() -> int {return task(6);})
.and_then([]() -> int {return task(7);})
.and_then([]() -> int {return task(8);})
.and_then([]() -> int {return task(9);})
.result();
std::cout << "Chain result: " << result << std::endl;
return result;
}
Because the task returns success only when called with an argument value less than 3, the invocation chain stops as expected after the 3rd step:
$ ./monad
Called task 1
Called task 2
Called task 3
Chain result: 1
This is the implementation of the Chain class:
class Chain
{
public:
const int kSuccess = 0;
Chain() {_result = kSuccess;}
static Chain start() { return Chain(); }
Chain& and_then(std::function<int()> nextfn) {
if(_result == 0) {
_result = nextfn();
}
return *this;
}
int result() { return _result; }
private:
int _result;
};
I know, it looks ugly and it's non-generic. But if this is the general direction you were thinking of, let me know and we can evolve it.
I would put code to execute btw calling task into a vector and then run a loop:
const size_t steps = 9;
using ops = std::function<void()>;
std::vector<ops> vops(steps);
steps[0] = [] { /* some code here to execute after task 0 */ };
...
for( size_t i = 0; i < steps; ++i ) {
if( !task(i) ) return;
if( vops[i] ) (vops[i])();
}
You can use an integer sequence.
// No task to call without an integer.
bool function(std::index_sequence<>) { return true; }
template<std::size_t I, std::size_t... S>
bool function(std::index_sequence<I, S...>) {
return [](){
auto isOk = task(I)
if (!isOk) return false;
// some code
return true;
// it will call function with the rest of the sequence only if the lambda return true.
}() && function(std::index_sequence<S...>{});
}
void function() {
// this call with a integer sequence from 0 to 9
function(std::make_index_sequence<10>{});
}
This code will expand just as if you'd write it by hands.
If the code between calls of task is different for each step, you can use a tuple.
auto afterTask = std::make_tuple(
[](){ std::cout << "after task 0" << std::endl; },
[](){ std::cout << "after task 1" << std::endl; },
[](){ std::cout << "after task 2" << std::endl; },
[](){ std::cout << "after task 3" << std::endl; },
[](){ std::cout << "after task 4" << std::endl; },
[](){ std::cout << "after task 5" << std::endl; },
[](){ std::cout << "after task 6" << std::endl; },
[](){ std::cout << "after task 7" << std::endl; },
[](){ std::cout << "after task 8" << std::endl; },
[](){ std::cout << "after task 9" << std::endl; }
);
And then change the definition of function with:
template<std::size_t I, std::size_t... S>
bool function(std::index_sequence<I, S...>) {
return task(I) &&
(static_cast<void>(std::get<I>(afterTask)()), true) &&
function(std::index_sequence<S...>{});
}

How to get error object? when use member function in deadline_timer

I use boost::asio::deadline_timer using a member function as a handler (callback function).
If I cancel a timer, how to get error object in print() member function?
class printer
{
public:
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print()
{
if (count_ < 5)
{
std::cout << count_ << "\n";
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
io.run();
return 0;
}
I try to set error object using bind in async_wait(), but it's compile error
timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error));
As long as your method signature matches, it should be no problem:
void print(boost::system::error_code const ec)
// and
boost::bind(&printer::print, this, boost::asio::placeholders::error)
See it Live On Coliru:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
class printer
{
public:
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print(boost::system::error_code const ec)
{
if (ec)
std::cout << "Error: " << ec.message() << "\n";
if (count_ < 5)
{
std::cout << count_ << "\n";
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
io.run();
}

boost.asio error on read from socket

The following code of the client:
typedef boost::array<char, 10> header_packet;
header_packet header;
boost::system::error_code error;
...
/** send header */
boost::asio::write(
_socket,
boost::asio::buffer(header, header.size()),
boost::asio::transfer_all(),
error
);
/** send body */
boost::asio::write(
_socket,
boost::asio::buffer(buffer, buffer.length()),
boost::asio::transfer_all(),
error
);
of the server:
struct header {
boost::uint32_t header_length;
boost::uint32_t id;
boost::uint32_t body_length;
};
static header unpack_header(const header_packet& data) {
header hdr;
sscanf(data.data(), "%02d%04d%04d", &hdr.header_length, &hdr.id, &hdr.body_length);
return hdr;
}
void connection::start() {
boost::asio::async_read(
_socket,
boost::asio::buffer(_header, _header.size()),
boost::bind(
&connection::read_header_handler,
shared_from_this(),
boost::asio::placeholders::error
)
);
}
/***************************************************************************/
void connection::read_header_handler(const boost::system::error_code& e) {
if ( !e ) {
std::cout << "readed header: " << _header.c_array() << std::endl;
std::cout << constants::unpack_header(_header);
boost::asio::async_read(
_socket,
boost::asio::buffer(_body, constants::unpack_header(_header).body_length),
boost::bind(
&connection::read_body_handler,
shared_from_this(),
boost::asio::placeholders::error
)
);
} else {
/** report error */
std::cout << "read header finished with error: " << e.message() << std::endl;
}
}
/***************************************************************************/
void connection::read_body_handler(const boost::system::error_code& e) {
if ( !e ) {
std::cout << "readed body: " << _body.c_array() << std::endl;
start();
} else {
/** report error */
std::cout << "read body finished with error: " << e.message() << std::endl;
}
}
On the server side the method read_header_handler() is called, but the method read_body_handler() is never called. Though the client has written down the data in a socket.
The header is readed and decoded successfully.
What's the error?
problem is solved.
error was in my code when sending the result of serialization in asio::write(). so the server could not read anything.

Resources