|
It counts the initial characters in the string until the pointer is aligned on a longword boundary. It then uses some clever bit manipulations to count the rest of the characters four at a time. If you run that code in the debugger you will be able to see exactly what happens.
|
|
|
|
|
Presumably already clear what the 4/8 block is used in the first place.
Googling with following provides some answers.
explain strlen himagic lomagic
Following line is the key
if (((longword - lomagic) & ~longword & himagic) != 0)
I tried eye balling that and using examples in my head but still not clear. I believe it is relying on arithmetic overflow.
If I wanted to understand that I would write up some test code with examples characters (4 blocks) with zero at the end (position 4, 3, 2, 1). Then have it print the results of each clause in the above if using binary representation (1 and 0) to see how the bits look for each different example and for each part of the clause.
Perhaps as a learning experience also copy that code into your own space and then write a test jig to time results via that and using the more straightforward (just by char) code. You should start with a large number of runs like probably at least 100,000.
|
|
|
|
|
More update
Found the crasher - wrong "exit" from "for" loop looking for main menu.
Having issues (expected ) using other than first main menu...
UPDATE
I am posting this to show SOME progress.
I did pepper the original with handful of "debug"...
( I need to add comments about what the code does...)
So far it works with main menu = 0 and all its sub menus - SUCCESS.
I got the sub menu index - so part of my goal is done.
Now I need to find the "main menu" index and then find the "crasher"..
BTW the author deserves LARGE credit for the code..
int MainWindow_Bluetooth::setupLambda ( void )
{
#ifdef LAMBDA
text = "START TASK DEBUG lambda ... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
QObject obj;
qDebug() << " index_sub " << index_sub ; obj.connect(subMenu[index_sub],&QMenu::triggered,subMenu[index_sub ],[](QAction* action)
{
QString path=action->text();
#ifdef LAMBDA
qDebug() << "TEST need index ?? " << path;
qDebug()<<"path (sub menu trigger ) "<< path ;
#endif
QWidget* parent=action->parentWidget();
path = QString("(%1)").arg(parent->actions().indexOf(action));
#ifdef LAMBDA
qDebug()<<"path (sub menu ) with index TOK "<< path ;
#endif
while(parent)
{
QMenu* menu=qobject_cast<QMenu*>(parent);
QString title=menu->title();
#ifdef LAMBDA
qDebug()<<"title (main menu ) "<< title ;
#endif
path+="->"+title;
qDebug()<<"path (title) "<< path ;
parent=parent->parentWidget();
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
if(parent)
{
QMenu* menu=qobject_cast<QMenu*>(parent);
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
int index=0;
const QList<QAction*> actions=menu->actions();
for(const QAction *act : actions)
{
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
if(act->text()==title)
{
path+=QString("(%1)").arg(index);
#ifdef LAMBDA
qDebug()<<"path (main menu -> sub menu) "<< path ;
#endif
break;
}
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
index++;
}
}
}
qDebug()<<" FINAL (??) "<< path;
} );
#ifdef LAMBDA
text = "END TASK DEBUG lambda... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
return 0;
}
OUTPUT
11:57:03: Starting /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi...
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
QObject::connect(QAction, Unknown): invalid nullptr parameter
"START TASK DEBUG lambda ... int MainWindow_Bluetooth::setupLambda()89"
index_sub 0
"END TASK DEBUG lambda... int MainWindow_Bluetooth::setupLambda()167"
TEST need index ?? "SubWindow LOCAL terminal all options #0"
path (sub menu trigger ) "SubWindow LOCAL terminal all options #0"
path (sub menu ) with index TOK "(0)"
title (main menu ) "terminal # 0"
path (title) "(0)->terminal # 0"
TRACE 128
TRACE 134
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
path (main menu -> sub menu) "(0)->terminal # 0(5)"
title (main menu ) "Window control"
path (title) "(0)->terminal # 0(5)->Window control"
TRACE 128
TRACE 134
11:57:22: /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi crashed.
I do not get what is missing - the error "points" at "action".
Code snippet :
QObject::connect(&MainWindow_Bluetooth::subMenu,&QMenu::triggered,&MainWindow_Bluetooth::subMenu,[](QAction* action)
{ QString path=action->text();
QWidget* parent=action->parentWidget();
path+=QString("(%1)").arg(parent->actions().indexOf(action));
while(parent)
{
QMenu* menu=qobject_cast<QMenu*>(parent);
QString title=menu->title();
path+="->"+title;
parent=parent->parentWidget();
if(parent)
ERROR :
/mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth_copy.cpp:42: error: C++ requires a type specifier for all declarations
mainwindow_Bluetooth_copy.cpp:42:10: error: C++ requires a type specifier for all declarations
QObject::connect(&MainWindow_Bluetooth::subMenu,&QMenu::triggered,&MainWindow_Bluetooth::subMenu,[](QAction* action)
^
modified 3-Feb-24 16:02pm.
|
|
|
|
|
No, the error is on QObject::connect(... . You cannot call a member function of a class(*). You have to insantiate an object of the class and call the member function on that object. Something like:
QObject obj();
obj.connect();
[*] Unless the member function is a static member function. I doubt that is the case here.
Mircea
|
|
|
|
|
Certainly looks to me that at a minimum you are missing a paren in the first line/statement.
|
|
|
|
|
That's what I thought too, but then I saw the comment //lambda just after the opening brace. I think that the following code is all a lambda expression as a parameter to the QObject::connect() call. I think the OP could have cleaned up the submission with something like
QObject::connect(&MainWindow_Bluetooth::subMenu,&QMenu::triggered,&MainWindow_Bluetooth::subMenu,[](QAction* action){ });
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
I suggest looking through all your code (there really is no "points" when something comes to an "action" especially when ALL of the code is not available) for "typedef". Then, assuming these errors are sallying forth from the mingw/gnu/etc compiler you're using, and have no code associated with them to differentiate one from the other, change any typedefs of the form:
(this only an example, not the code you have)
std::vector<int>::size_type
To
std::vector<int>::size_type size
See if any errors go away.
modified 2-Feb-24 14:34pm.
|
|
|
|
|
Thanks, the code is an example I found, so I am just starting to modify it for my use.
I stopped when I received the error.
I actually got more , but I just do not see how to fix the "action".
The "problem" is - the original error "pointed " on "connect" and this error "points " on "action"
Let me work on this...maybe I can actually clean up the whole "lambda".
|
|
|
|
|
Here is modified , incomplete , code .
int MainWindow_Bluetooth::setupLambda ( void )
{
#ifdef LAMBDA
text = "START TASK DEBUG LAMBDA... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
QObject obj();
obj.connect(&subMenu[index],&QMenu::triggered,&subMenu[index],[](QAction* action)
{
}
);
#ifdef LAMBDA
text = "END TASK DEBUG LAMBDA... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
return 0;
}
Now I am getting this error
/mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth_copy.cpp:92: error: base of member reference is a function; perhaps you meant to call it with no arguments?
I am still lost...
(I do not like lambda - too cryptic )
|
|
|
|
|
You are getting somewhere. Try this:
QObject obj;
Salvatore Terress wrote: (I do not like lambda - too cryptic ) Tough!
Mircea
|
|
|
|
|
Salvatore Terress wrote: (I do not like lambda - too cryptic )
There's nothing that says you have to use a lambda. Sometimes, the capture variables are very useful, but if you'd rather write a normal function, the go ahead and do so.
Sometimes a function object works best: Function Objects in the C++ Standard Library | Microsoft Learn
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
I am using this code example to get more familiar with "connect".
I am at 3rd attempt to write easy to understand way to process menu / submenu code.
I had it working at one time, for one menu. Now I am adding more menu and now my submenus are "multiple selection" instead of single submenu.
This code example seems to have that "fixed" , but now it is using lambda...
|
|
|
|
|
hi,
VS=2015
in my mfc dialog class that is inherited from CDialogEx, i can find PreTranslateMessage method?.
I select the myDlg class, and there is no such method. Need it for some char exclusions inside edit box.
There is no Onchar method for edit box either.
|
|
|
|
|
Open ClassWizard and and add virtual method PreTranslateMessage.
Then select the message WM_CHAR to handle it in OnChar
|
|
|
|
|
How to? I know the basic printing functions and using the CPrintDialog plus PRINTDLG pd,
a little sample that only prints a statement but shows what I am using:
CPrintDialog dlg(FALSE, pd.Flags);
if (dlg.DoModal() != IDOK)
{
AfxMessageBox("Abort or Unknown Printer or Printer device error");
return;
}
pd.hDC = dlg.CreatePrinterDC();
ASSERT(pd.hDC !=0);
CDC * dc = new CDC;
dc = CDC::FromHandle(pd.hDC);
int cxPage = ::GetDeviceCaps (pd.hDC, HORZRES) ;
int cyPage = ::GetDeviceCaps (pd.hDC, VERTRES) ;
sizePrn.cx = cxPage;
sizePrn.cy = cyPage;
memset(&lf, 0, sizeof(lf));
lf.lfHeight = -MulDiv(12, dc->GetDeviceCaps(LOGPIXELSY), 72);
lf.lfWeight = FW_BOLD;
lf.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
lf.lfQuality = PROOF_QUALITY;
lstrcpy(lf.lfFaceName, "Times New Roman");
VERIFY(font.CreateFontIndirect(&lf));
dc->SetMapMode (MM_ISOTROPIC) ;
dc->SetWindowExt ( 1000, 1000) ;
dc->SetViewportExt (cxPage / 2, -cyPage / 2) ;
dc->SetViewportOrg (cxPage / 2, cyPage / 2) ;
dc->SetTextAlign(TA_BASELINE | TA_CENTER);
dc->SetBkMode(OPAQUE);
dc->SetMapMode(MM_TEXT);
dc->PatBlt(0, 0, sizePrn.cx, sizePrn.cy, WHITENESS);
dc->LPtoDP(&sizePrn);
dc->SelectObject(&font) ;
CSize extentChar = dc ->GetTextExtent("M",1);
int nCharHeight = extentChar.cy+4;
int nCharWidth = extentChar.cx+10;
BeginWaitCursor();
CString printDate = GetMyCurDateTime();
DOCINFO docinfo;
memset(&docinfo, 0, sizeof(docinfo));
docinfo.cbSize = sizeof(docinfo);
docinfo.lpszDocName = _T("Calif Lottery Winnings");
docinfo.fwType = 0;
rc = dc->StartDocA(&docinfo);
if (rc < 0)
{
sprintf(temp, "Unable to Begin printing - Error[%d]", rc);
MessageBox(temp, NULL, MB_OK);
dc->ReleaseAttribDC();
dc->ReleaseOutputDC();
dc->DeleteTempMap();
EndWaitCursor();
DeleteDC(pd.hDC);
if(pd.hDevMode != NULL)
GlobalFree(pd.hDevMode);
if(pd.hDevNames != NULL)
GlobalFree(pd.hDevNames);
dc = 0;
return;
}
szTitle=CString("California Lottery System Printout ") + printDate;
dc -> StartPage();
dc -> SetTextAlign(TA_LEFT | TA_TOP);
dc -> TextOut(0, 0, szTitle, szTitle.GetLength() );
dc -> MoveTo( 0, nCharHeight );
dc -> LineTo(dc -> GetTextExtent(szTitle, szTitle.GetLength()).cx, nCharHeight);
nStart =1;
And goes on to print 1 page.
What I want to do is to place a non-character on a ticket but not like an 'X' but a block like a pencil fill-in mark at a specific place on the paper. I need to supply the coordinates for the mark and do that for the full page. Has anybody done something like this before? if so, give me some hints.
Thanks
Craig
|
|
|
|
|
When things get too complicate (re: printing), I create "forms" / pages / windows that match the required output; then "capture" the visual and print that in one go (as an "image" or whatever). Total WYSIWYG. Users can even "update" the "report" (if you let them).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
That is a good idea, will look into it.
|
|
|
|
|
I am debugging / modifying my working loop code and like to insert test code and "skip " the rest of large loop.
What would be a cool way to accomplish that...?
I have been "commenting out " temporary unwanted code , but that is pretty clumsy when large code blocks are commented out.
for (index = 0; index < list.size(); ++index)
{
... working code
{
...insert test block
}
... skip this working code
} loop end
|
|
|
|
|
If I find I need to do something like this I do something like
#ifdef DEBUG
void foo( ... )
{
}
#endif
In debug mode, then I can call foo(arg, arg, arg, ... ) when I need to. Usually this would do something like print all the elements of a collection, for example.
As far as I know, you can't skip over code. You can set a break point at the top of the loop, and continue to that point. If you really want to omit portions of the loop in your debug testing the best I could suggest would be to wrap that in an #ifdef DEBUG/#endif. Alternatively, you might
#if DEBUG == 2
...
#endif Of course, you could use #if DEBUG <= 2 , etc to get different debug levels of code when you compile.
If you go that route, check with your IDE. It probably does not add a -DDEBUG to the compile command line in debug mode, so you'd have to do that yourself. If you want to have different debug levels you would pass -DDEBUG=2 (e.g.) (Linux - its probably different, but similar for windows)
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
modified 28-Jan-24 15:44pm.
|
|
|
|
|
Do you mean this (skip to next iteration)
for (int n=0; n<10; ++n)
{
cout << "working code A\n";
{
cout << "debug code, iteration = " << n << "\n";
continue;
}
cout << "working code B\n";
}
Or, possibly, this (skip the loop)
for (int n=0; n<10; ++n)
{
cout << "working code A\n";
{
cout << "debug code, iteration = " << n << "\n";
n=10; continue;
}
cout << "working code B\n";
}
?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
In C++
Macros
- As noted you can remove the code entirely
- You can also use a macro that resolves to a different value
For code
- Add a flag.
- Add a class that manages flags
Larger apps would use the second.
Your code then tests the flag. There are various ways the flags can be set: command line, config file, database. There are various ways to manage the class itself. Could be static or use a builder pattern.
Example of flag usage
if (myFlagClass.IsSet("TestFlag131")) ....
For systems (not just an app) can use special values in an API. The code of the target service looks for those special values and returns a fixed, or even variable, result. This is somewhat useful in verifying end to end enterprise functionality.
|
|
|
|
|
Voluntarily removed - wrong forum.
modified 27-Jan-24 16:00pm.
|
|
|
|
|
Salvatore Terress wrote: To make sure for people who "do not get it" - if you are not wiling to help,
please do not respond..
Kindly stop giving orders. As you have been repeatedly told, just post the question. If the answer is not to your exacting requirements, then you have two choices:
1. Politely ask for clarification.
2. Ignore it.
|
|
|
|
|
I tried placing the UpdateGame() function inside the window message processing loop but the execution never reaches that spot
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
UpdateGame();
}
}
I`m trying to mix code from two different tutorials which might be the cause of the problem. Bellow is what the initial message processing loop looked like.
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
|
|
|
|
|
At a guess PeekMessage() is always returning true. If you want UpdateGame() to always be called then remove it from the else block:
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UpdateGame();
}
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|