|
AFAIK there is no reflection in C++. You cannot have something like:
struct S{
int value;
double number;
}
S to_serialize{1, 3.14};
std::string tag;
stream strm;
for (auto& m: members_of(S))
{
strm << name_of(m) << ': ';
strm << to_serialize.m << std::endl;
} In a language with reflection this would produce an output like:
value: 1
number: 3.14
Mircea
|
|
|
|
|
Mircea Neacsu wrote: AFAIK there is no reflection in C++.
Ok? That of course makes sense given that the link I posted is a about a new feature that will be added to the next standard.
Kind of silly to add it if it already existed.
|
|
|
|
|
RTTI is the only metadata functionality I'm aware of in C++.
There are hacks to sort of divine some reflective type information at compile time like SFINAE but not at runtime excepting RTTI i think.
But then the only thing I really have worked with is C++17 and prior, so if they have introduced something besides since I wouldn't know.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
honey the codewitch wrote: RTTI
Yeah that is the one that I was remembering.
So then that is not sufficient perhaps? Limited in some way?
In Java/C# one can get to things like class name, method names (public and private), method parameters (types), access types, class attributes. All of those can be listed and manipulated.
So for example if a class attribute is private and modifying that is going to help with better unit testing one can write reflection code to modify it. That is the most common use I have used for accessing private attributes. I don't use it that much and seems like bit of a wash versus adding a setter/getter.
|
|
|
|
|
RTTI is extremely limited last time I checked. Given I've maybe used it once to play around with it years ago and that's it, so don't quote me. It's nothing like what .NET offers, for example.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
Ok. But a brief reading of the link I posted suggests that will changing.
|
|
|
|
|
Personally, I'd probably still avoid it unless it solved a very significant problem for me because I like being able to make 17KB dlls that are actually useful.
Metadata ain't free.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
honey the codewitch wrote: I'd probably still avoid it unless it solved a very significant problem
Specific times I have used it in other languages.
- Dynamic loading of classes. Especially when parameter lists are dynamic.
- Messing with private data of a class. Most the time for unit tests, but I think one time it was in the production code. In C++ there is a way to do that anyways but it always risky and just as hacky.
honey the codewitch wrote: Metadata ain't free.
Which is why it has taken so long I suspect.
|
|
|
|
|
I am asking a question about regular expression.
I am NOT looking for references to AI regular expression generators.
Here in my text to find matches of ALL "hci" in it:
"Devices:\n\thci1\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
Here is my attempt to accomplish that task
QRegularExpression re("(hci[0-9]+)");
It matches only first occurrence of hci1" and twice
Please follow the established CodeProject instruction and read the post.
PLEASE no references to AI regular expression generators.
I need to learn TO BUILD regular repression - specially how to write it so it will attempt to match multiple items.
PS I did try to use "global match" , no go.
|
|
|
|
|
Salvatore Terress wrote: Please follow the established CodeProject instruction and read the post.
PLEASE no references to AI regular expression generators.
Please stop giving us orders. You have already been kicked off this forum once for your bad attitude. Everyone who tries to answer questions here does it in their own time and at no cost to you. If the answer is not what you were hoping for then feel free to ignore it.
|
|
|
|
|
Why didn't you post the code?
I mean, you commented
But only the regular expression constructor is shown.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Salvatore Terress wrote: It matches only first occurrence of hci1" and twice
Did you mean something like 'it only matches once but there are two'?
Salvatore Terress wrote: I need to learn TO BUILD regular repression
Your regular expression is correct.
So that means your usage of the regular expression engine/library is incorrect. Nothing to do with the regular expression itself
Typically an engine/library will have an iteration idiom where each loop matches the next one.
I didn't look at all but the following google seems to return results that would be relevant.
{code}
"QRegularExpression" iterate through matches
{code}
|
|
|
|
|
Since my last post is nowhere to be found here...
I do appreciate all help resolving the issue.
And since posting code was requested, here it is.
It generally works retrieving multiple matches...
such as "hci1 hci0 "
what is missing is to retrieve PAIR of matches
such as
"hci1 00:xx:yy...
in other words I can retrieve name or address but not BOTH.
I would be grateful if somebody can give me the actual code and explanation how to match PAIR of values.
As can be seen - I did try different placement of parentheses
" (...)" but it did not work.
{
text = " START MATCH NAME AND ADDRESS CODE BLOCK ";
textEditPtr_DEBUG->append(text);
#ifdef TASK
DELETED
#endif
QString word;
QStringList words;
QString pattern = "Devices:\n\thci1\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n";
QRegularExpression re("(([0-F]{2}[:]){5}[0-F]{2})");
QRegularExpressionMatchIterator i = re.globalMatch(pattern);
text = " HAS MATCH MULTI";
qDebug()<< text;
textEditPtr_DEBUG->append(text);
#ifdef BYPASS no go
foreach(auto item,i)
{
text = " TEST foreach ";
text += item;
qDebug()<< text;
textEditPtr_DEBUG->append(text);
}
#endif
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
word = match.captured(1);
words << word;
qDebug()<< word;
text = " Full match name and address... ";
text += word;
qDebug()<< text;
textEditPtr_DEBUG->append(text);
}
#ifdef BYPASS
else
{
text = " NO MATCH MULTI";
}
#endif
qDebug()<< text;
text = " END MATCH ADDRESS CODE BLOCK ";
textEditPtr_DEBUG->append(text);
}
|
|
|
|
|
Salvatore Terress wrote: Here in my text to find matches of ALL "hci" in it:
So based on your sub - reply to me
"Devices:\n\thci1\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
What you actually want is the following
hci 00:50:B6:80:4D:5D
hci 00:15:83:15:A2:CB
Your text sample represents a multi-line input presumably with tab separators.
From that sample it is actually pointless to use a regex since normal parsing (or csv parser would work.)
But in terms of Regex you have several problems
1. Dealing with multiple lines
2. Correctly matching the values.
3. Dealing with special characters (including perhaps #1 above.)
4. Dealing with a list of results - as I stated in my other reply.
So your regex is not even close to correct for matching the address.
Looking at 2 only because that is the part most likely to be usable as a regex the equivalent regex for Perl would look like the following. Since you are not using Perl yours might vary but I suspect not.
(hci\w+([a-fA-F0-9][a-fA-F0-9]:)+[a-fA-F0-9][a-fA-F0-9])
There are variations on the above
- Making the tab explicit
- Restricting the id to the exact length
- variation on the hex digit but I prefer the above form
|
|
|
|
|
Thanks, but it still does not do what I want, as you expected it may. .
Would it be OK to actually discuss and analyze this ?
I am asking I am NOT telling the group shoud do that.
QRegularExpression re("(hci\w+([a-fA-F0-9][a-fA-F0-9] +[a-fA-F0-9][a-fA-F0-9])");
There are , imho , important parts (to reg exp ) and they are ok when used alone.
1. If I opt to use QRegularExpressionMatchIterator i = re.globalMatch(pattern);
There should be at lest three "code groups " delimited by "(,,,code group... )"
2. there are TWO base "reg expressions " one working OK as is
"hci[0-9]" - function as "match hcix where x = [0-9] - some doc call [0-9] range
or
hci\w should perform SAME task
3. the analysis of "xx:" using [a-fA-F0-9] is simply "too cute " and [0-F] with "how many times do the previous - {2} works as well.
I have no need to check for lower case "hexadecimal " since that actually does not exist anyway...
So - I will , perhaps stubbornly, try to continue usage of at least ONE of the QT classes and maybe will
find correct modification of the above.
If i end up with plain "reg_exp" so be it...
Thanks
|
|
|
|
|
When you post code use code blocks.
Salvatore Terress wrote: If I opt to use QRegularExpressionMatchIterator i = re.globalMatch(pattern);
Not sure how to emphasize what I have already said several times.
That question is NOT about regular expressions.
It is about how to use that specific library.
I suggest you start with a less difficult example to figure out how iteration works. And google for examples as I documented in the other reply.
Salvatore Terress wrote: There should be at lest three "code groups " delimited by "(,,,code group... )"
You mean the parens. Parens are used for 'capturing' and for 'grouping'.
For the case I gave the inner ones was to group the outer for capturing.
And yes that is a problem when you iterate. The library you are using might have a way to specify that the parens are not 'capturing'.
In Perl it looks like the following. There would be only one capturing group which would match either 'abcxyz' or 'defxyz'.
((?:abc|def)xyz)
I will note that I use that feature so rarely that I had to look it up.
Salvatore Terress wrote: the analysis of "xx:"
As I mentioned there are variations to what I suggested. Myself I don't care for using '{2}' in a case like this. Just a preference.
(Had to edit the above because I messed up the code block)
modified 16-Nov-23 10:40am.
|
|
|
|
|
SOLVED and CLOSED
as initially asked for , by using named subgroup option.
QRegularExpression re("(((?<one>hci[0-9])(.*))?(?<two>(([0-F]{2}[:]){5}[0-F]{2}))(.*))");
Thanks very much for all support given.
|
|
|
|
|
int AnInt=6;
void EditInt(int * IntPnt)
{
*IntPnt=7;
}
EditInt(&AnInt);
Is this how you should proceed?
|
|
|
|
|
That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's
void EditInt(int& IntRef)
{
IntRef=7;
}
int AnInt=6;
EditInt(AnInt);
|
|
|
|
|
Greg Utas wrote: That's passing as a pointer, not as a reference. Which is just the same, apart from semantics.
|
|
|
|
|
Very true. A reference can even be to nullptr , and you need to check for this if writing bulletproof code:
void function(type& arg)
{
if(&arg == nullptr)
throw std::invalid_argument("function received null argument");
}
type* nasty = nullptr;
function(*nasty); Without the check, function crashes when it gets around to using arg .
|
|
|
|
|
And the same with a pointer, since there is no material difference between the two.
|
|
|
|
|
I would assert() something like that rather than throw .
It strikes me as an error in the code itself, rather than something that should have filtered its way into runtime code, so I think assert is more appropriate.
As I understand it, best practices indicate that a reference should never be deliberately null. Could be wrong here?
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
What to do depends on the environment you're running in. I've never used assert , so I had to look up the documentation, which says that it calls abort . Most application code won't catch an invalid_argument exception, so the effect will be similar.
In my open-source software, I neither throw nor assert in this situation. Instead, I generate a debug log with a traceback and return whatever signifies failure if the function has a result. That's appropriate if avoiding crashes is paramount, which is what my software aims for.
You're right that a reference shouldn't be null. For many functions, a pointer argument shouldn't be null either. But applications misbehave, so you have to decide whether your function will check for bad arguments or just crash.
|
|
|
|
|