D Module Name Being Printed by Module Destructor - d

I've recently started learning D version 1, using the Tango library. I decided to write a small class Dout that wraps tango.io.Stdout, except it overrides opShl to better match C++'s << style output. My implementation is like so:
// dout.d
module do.Dout;
import tango.io.Stdout;
class Dout
{
public static Dout opShl(T) (T arg)
{
stdout(arg);
return new Dout;
}
public static Dout newline()
{
stdout.newline;
return new Dout;
}
}
And in main, I make a simple call to Dout.opShl(), like so.
// main.d
import do.Dout;
import tango.io.Console;
int main(char[][] argv)
{
Dout << "Hello" << " world!" << Dout.newline;
Cin.get();
return 0;
}
This works, but after pressing enter and exiting main, the text "do.Dout.Dout" is printed. After stepping through the code, I found that this text is printed at the assembly instruction:
00406B5C call __moduleDtor (40626Ch)
In which do.Dout's destructor is being called.
My question is, why is the module name being printed upon exiting main, and what can I do to stop this behaviour?

the reason "do.Dout.Dout" is printed is because Dout << Dout.newline; prints a new line (in the newline property call) and then attempts to print a human readable string of a Dout object (after it is passed to opShl!Dout())
and you only see it during destruction because then the output is flushed ;)
you should have done
__gshared Doutclass Dout = new Doutclass;
class Doutclass
{
public Dout opShl(T) (T arg)
{
static if(T == NLine){
stdout.newline;//if nl is passed do newline
}else{
stdout(arg);
}
return this;
}
struct NLine{}//this might need a dummy field to stop compiler complaints
public static NLine newline()
{
return NLine();
}
}
which is closer to the C style (Dout is an global object and doesn't get recreated each call, newline is a special struct that flushed the output besides adding a newline to it)

Related

C++ : how to change a member function at runtime (inside a shared library)?

I have a class DoorTerminal with a member function getInput() as shown below.
DoorTerminal::DoorTerminal(DialogueBox *dialogue_, Door* door_): DialogueInterfacing(dialogue_){
emptyFor.push_back(string("Player"));
type.push_back(string("DoorTerminal"));
type.push_back(string("DialogueInterfacing"));
imgpath = "/vagrant_data/Skeleton-game/src/sprites/terminal.png";
typestring = "DoorTerminal";
door = door_;
}
void DoorTerminal::getInput(){
string input = dialogue->getInput();
if(input == "1234"){
door->unlock();
}
}
I'm trying to create an instance of this class inside a function defined in a .so shared library and change the getInput function pointer to instead point to a different function at runtime, like so
void getInput2(){
string input = "EE";
if(input == "1234"){
cout << "hello" << endl;
}
}
DoorTerminal* doorTerminal = new DoorTerminal(Door* door);
doorTerminal->getInput = getInput2;
which results in an "invalid use of member function" error.
I've also experimented with std::function for the same purpose but so far with no luck.
Is there a simple way to do this?

log4cplus does not show all messages

We are building a module that is part of a full embedded Linux operating systems. We are using log4cplus for diagnostics.
log.properties contains:
log4cplus.logger.CompanyGlue=TRACE, console
cm_bind_helper.h contains much more complicated than this. All of the DBG_XXX messages appear on the console.
#include "qobject.h"
#include "log4cplus/dbglog.h"
class bind_helper : public QObject
{
protected:
const char *debugModuleName = "CompanyGlue";
log4cplus::Logger debugModule { Logger::getInstance(debugModuleName) };
bool set_value(CMPath path, PType &input)
{
auto old_value = CMRoot()->getValue(path);
if (input != old_value) {
DBG_DEBUG("Changing " << path << " to " << input);
}
}
}
cm_bridge.cpp contains much more code than this. However, none of the DBG_XXX messages appear on the console.
#include "cm_bridge.h"
#include "cm_bind_helpers.h"
struct CompanyContentModelBridge::detail_ : public bind_helper
{
std::shared_ptr<company::device_base> base;
detail_(QObject *parent)
: bind_helper(parent)
{
configure_system_advanced_control();
}
void configure_system_advanced_control();
std::string get_exported_file_size();
}
using detail_ = CompanyContentModelBridge::detail_;
void detail_::configure_system_advanced_control()
{
base->add_get_only_entry<std::string>("SPDSZ", get_exported_file_size());
}
std::string detail_::get_exported_file_size()
{
DBG_INFO("get exported file size");
//diag("exported file size");
}
I have tried various ways to configure log4cplus in cm_bridge.cpp, including the same two lines in cm_bind_helpers.h and as follows. I also removed them assuming that inheritance would handle it. Nothing made the DBG_XXX messages appear.
DBG_IMPL_DEBUG_MODULE(CompanyGlue);
Eventually, I added this function to cm_bind_helpers.h. It worked when called from its own class but failed when called from get_exported_file_size() (with comment removed).
void diag(std::string_view what) {
DBG_DEBUG("bind helper diag " << what);
}
I'm stumped. I do not know why messages are being suppressed from one file but not the other.

boost::stacktrace::frame::name() hangs

I am trying to retrieve a stack trace in my program, and store it for later use (debugging purposes). But the call to boost::stacktrace::frame::name() never returns, and I have no clue why. When I use this exact code in a simple project, it runs nicely. Any ideas?
boost::stacktrace::stacktrace stacktrace;
stringstream stacktraceText;
for (const auto& entry : stacktrace)
{
if (entry.empty() == false)
{
auto name = entry.name();
stacktraceText << name << "\n";
}
else
{
stacktraceText += L"<missing symbol info>\n";
}
}

QLibrary functions work slow on first call

I'm using QLibrary to load functions from one .dll file.
I succesfully load it, succesfully resolve functions.
But when i use some function from that .dll for the first time, this function works very slow(even if it is very simple one). Next time i use it again - and the speed is just fine (immediately, as it should be).
What is the reason for such behaviour? I suspect some caсhing somewhere.
Edit 1: Code:
typedef int(*my_type)(char *t_id);
QLibrary my_lib("Path_to_lib.dll");
my_lib.load();
if(my_lib.isLoaded){
my_type func = (my_type)my_lib.resolve("_func_from_dll");
if(func){
char buf[50] = {0};
char buf2[50] = {0};
//Next line works slow
qint32 resultSlow = func(buf);
//Next line works fast
qint32 resultFast = func(buf2);
}
}
I wouldn't blame QLibrary: func simply takes long the first time it's invoked. I bet that you'll have identical results if you resolve its address using platform-specific code, e.g. dlopen and dlsym on Linux. QLibrary doesn't really do much besides wrapping the platform API. There's nothing specific to it that would make the first call slow.
There is some code smell of doing file I/O in constructors of presumably generic classes: do the users of the class know that the constructor may block on disk I/O and thus ideally shouldn't be invoked from the GUI thread? Qt makes the doing this task asynchronously fairly easy, so I'd at least try to be nice that way:
class MyClass {
QLibrary m_lib;
enum { my_func = 0, other_func = 1 };
QFuture<QVector<FunctionPointer>> m_functions;
my_type my_func() {
static my_type value;
if (Q_UNLIKELY(!value) && m_functions.size() > my_func)
value = reinterpret_cast<my_type>(m_functions.result().at(my_func));
return value;
}
public:
MyClass() {
m_lib.setFileName("Path_to_lib.dll");
m_functions = QtConcurrent::run{
m_lib.load();
if (m_lib.isLoaded()) {
QVector<QFunctionPointer> funs;
funs.push_back(m_lib.resolve("_func_from_dll"));
funs.push_back(m_lib.resolve("_func2_from_dll"));
return funs;
}
return QVector<QFunctionPointer>();
}
}
void use() {
if (my_func()) {
char buf1[50] = {0}, buf2[50] = {0};
QElapsedTimer timer;
timer.start();
auto result1 = my_func()(buf1);
qDebug() << "first call took" << timer.restart() << "ms";
auto result2 = my_func()(buf2);
qDebug() << "second call took" << timer.elapsed() << "ms";
}
}
};

Coding State Machine in C++

I'm attempting to code a state machine based on a gumball machine. I have a interface class of a basic state, while I have specific states that use this interface. I have four states, no_quarter, has_quarter, sold, and sold_out states. I also have a gumball machine class that handles these states and depending on which state my machine is in, it will go that class and do the needed action. Here is my code that is having the problem, I'll post my functions also.
Gumball_Machine.h
class Gumball_Machine
{
private:
int gumball_count;
State *current_state;
No_Quarter_State *nqs;
Has_Quarter_State *hqs;
Sold_State *ss;
Sold_Out_State *sos;
public:
Gumball_Machine(int inventory)
{
gumball_count = inventory;
nqs = new No_Quarter_State(this);
hqs = new Has_Quarter_State(this);
ss = new Sold_State(this);
sos = new Sold_Out_State(this);
if (gumball_count == 0)
set_state(sos);
else
set_state(nqs);
}
void insert_quarter()
{
current_state->insert_quarter();
}
void eject_quarter()
{
current_state->eject_quarter();
}
void turn_crank()
{
current_state->turn_crank();
}
void dispense()
{
current_state->dispense();
}
void set_state(State *new_state)
{
current_state = new_state;
}
State *get_no_quarter_state()
{
return nqs;
}
State *get_has_quarter_state()
{
return hqs;
}
State *get_sold_state()
{
return ss;
}
State *get_sold_out_state()
{
return sos;
}
No_Quarter_State.h
#ifndef NO_QUARTER_STATE_H_INCLUDED
#define NO_QUARTER_STATE_H_INCLUDED
#include "State.h"
class No_Quarter_State: public State
{
public:
No_Quarter_State(Gumball_Machine *gbm);
void insert_quarter();
void eject_quarter();
void turn_crank();
void dispense();
};
#endif // NO_QUARTER_STATE_H_INCLUDED
No_Quarter_State.cpp
#include "No_Quarter_State.h"
#include "Gumball_Machine.h"
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
void No_Quarter_State::insert_quarter()
{
cout << "You inserted a quarter.\n";
machine->set_state(machine->get_has_quarter_state());
}
void No_Quarter_State::eject_quarter()
{
cout << "You must insert a quarter before you can eject one.\n";
}
void No_Quarter_State::turn_crank()
{
cout << "You must insert a quarter before you can crank the handle.\n";
}
void No_Quarter_State::dispense()
{
cout << "You need to pay first before you can get a gumball.\n";
}
The line I'm having an issue is in the No_Quarter_State.cpp
machine->set_state(machine->get_has_quarter_state());
This is giving me a run-time error. I've seen examples like this but I'm not completely sure if this is legal in C++. I'm attempting to switch the state of my gumball machine object.
The error I get is a generic not responding error: "test.ext has stopped working". I'm using CodeBlocks to code this.
In the constructor, the presumed member variable machine is hidden by the parameter.
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
You can fix this by using initializer list syntax instead:Thanks Sneftel and NathanOliver
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
: machine(machine)
{
}
However, in regular method functions, you would have to use this-> if you named the method parameter the same as the member variable. A typical style used to avoid that issue is to prepend m_ or append _ to member names.