Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Volguus
Mar 3, 2009

Ciaphas posted:

I'm trying to use xerces-c and XQilla to parse some documents. Unfortunately these documents have problems at the source like double-declared or undeclared namespaces, and it chokes trying to parse these documents until I manually fix the declaration (a no go in production). I don't actually care about the name spaces at all, is it possible to just programmatically ignore them?

Would this help you: https://xerces.apache.org/xerces-c/apiDocs-3/classDOMConfiguration.html#details , more specifically the namespaces paragraph:

quote:

"namespaces"

true
[required] (default)
Perform the namespace processing as defined in
[XML Namespaces].

false
[optional]
Do not perform the namespace processing.

Adbot
ADBOT LOVES YOU

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Yeah, I tried that one already, though using the xercesc::XMLUni::fgDOMNamespaces constant instead of "namespaces". I assume they're one and the same from docs, but I'll go back upstairs and try that.

Another link also shows "namespace-declarations" as a config option (constant fgDOMNamespaceDeclarations) and says not supported, but gently caress it at this stage I'ma try that too. I'll report back.

Ciaphas fucked around with this message at 01:36 on Jul 29, 2016

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


No joy, "namespaces" is indeed the value of the constant I was using, and trying "namespace-declarations" raised a SIGABRT.

No matter what parameters I twiddle before setting off the parser, it just comes back with a DOMDocument containing the <?xml ... ?> stuff and nothing else, like it outright gave up on the <kml xmlns:atom="blah" xmlns:atom="blah2">...</kml> (the k is not a typo, it's Google Earth data I guess??)

A pox on customers who provide invalid data that isn't even consistent in how it's invalid.

Ciaphas fucked around with this message at 02:27 on Jul 29, 2016

Volguus
Mar 3, 2009
Maybe try the SAX Parser then? https://xerces.apache.org/xerces-c/program-sax2-3.html

Or other parsing libraries (if possible)? Libxml2 or pugixml? Libxml is quite heavy , but so is xerces. Pugixml I used in the past and so far it served me well (http://pugixml.org/).

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


xerces is too thoroughly embedded in this thing, near as I can tell. Not my code. I'll look up the other parsing libraries, though, if I get some spare time, so thanks for that.

Got it to kind of sort of work, anyway--one of the config parameters is to force it to continue parsing (with the expected caveat emptor of course), and the result of that got me most of what I wanted. Should hold up until we get better versions of this data, at least.

mekkanare
Sep 12, 2008
We have detected you are using ad blocking software.

Please add us to your whitelist to view this content.
I have a question about formatting try/catch blocks. I've created a paste here.

To be more specific all it will do is try to read and open a file, and if there's any errors it'll return false.
I try to minimize the amount of returns in a function as a personal taste, but I'd like feedback on what others
might think. This is a personal project, so there is no official code formatting to follow.

raminasi
Jan 25, 2005

a last drink with no ice
I personally find 3 the best, 1 reasonable, and 2 ridiculous.

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

If you want to minimize returns, you can always create a variable and just have one:
code:
bool butt4() {
  bool result = false;
  try {
    buttstuff
    result = true;
  } catch (fart) {
    //fartz
  } catch (poop) {
    //poopz
  } catch (...) {
    //...
  }
  return result;
}

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

if you want to minimize returns you're in the wrong thread, try this one instead

mekkanare
Sep 12, 2008
We have detected you are using ad blocking software.

Please add us to your whitelist to view this content.
Thanks raminasi and peepsalot for your input.
I am trying to use try/catches more and worry about readability.

That Turkey Story
Mar 30, 2003

Bonfire Lit posted:

if you want to minimize returns you're in the wrong thread, try this one instead

Disappointed that wasn't a link to a bitcoin thread or something.

Combat Pretzel
Jun 23, 2004

No, seriously... what kurds?!
So, regarding micro-optimization of checking whether a float is non-zero or not...

When I set a float specifically to zero, are all 32 bits zero?

If so, when I want to check whether it was not specifically set to zero, can I do magic like this?

code:
if(*((int*)&some_float)) { ... }

OddObserver
Apr 3, 2009
That's an aliasing rules violation.

VikingofRock
Aug 24, 2008




Combat Pretzel posted:

So, regarding micro-optimization of checking whether a float is non-zero or not...

When I set a float specifically to zero, are all 32 bits zero?


Yes, when you set a float to zero it gets set to positive zero, which has all bits equal zero.

quote:


If so, when I want to check whether it was not specifically set to zero, can I do magic like this?

code:
if(*((int*)&some_float)) { ... }

That's undefined behavior because it violates strict aliasing.

Ralith
Jan 12, 2011

I see a ship in the harbor
I can and shall obey
But if it wasn't for your misfortune
I'd be a heavenly person today

Combat Pretzel posted:

So, regarding micro-optimization of checking whether a float is non-zero or not...

When I set a float specifically to zero, are all 32 bits zero?

If so, when I want to check whether it was not specifically set to zero, can I do magic like this?

code:
if(*((int*)&some_float)) { ... }

What gives you the impression that "some_float == 0" is not already as fast as it gets? If you need more speed, look into hand-rolled SIMD or throw hardware at it or something.

Sex Bumbo
Aug 14, 2004
Negative zero is not all zero bits, so your code won't work on -0.0f. Shouldn't just casting it to a bool work? It seems like that would be the most obvious hint to the compiler about what you're trying to do.

Sex Bumbo fucked around with this message at 04:26 on Aug 8, 2016

Ralith
Jan 12, 2011

I see a ship in the harbor
I can and shall obey
But if it wasn't for your misfortune
I'd be a heavenly person today

Sex Bumbo posted:

Negative zero is not all zero bits, so your code won't work on -0.0f. Shouldn't just casting it to a bool work? It seems like that would be the most obvious hint to the compiler about what you're trying to do.
:psyduck: The most obvious hint to the compiler that you want to compare a float to 0 is comparing a float to 0.

You don't get bonus points for using obscure synonyms.

Doc Block
Apr 15, 2003
Fun Shoe
I saw some JavaScript (I think?) code where the programmer had done the equivalent of something like:
code:
if(!thing == false)
because the ! would essentially be converting it to a bool, that way the equality test would be a True Boolean Comparison :rolleyes: and then I discovered that this is apparently a thing with web developers :stonk:

I recall also seeing stuff where they would do if(!(!thing)) for the same dumb reason.

The moral of the story: just compare against what you're trying to compare against. So if you want to test if a float is nonzero, just do if(f != 0.0f) or if(f) or whatever.

Doc Block fucked around with this message at 08:48 on Aug 8, 2016

b0lt
Apr 29, 2005

VikingofRock posted:

Yes, when you set a float to zero it gets set to positive zero, which has all bits equal zero.

Strictly speaking, neither the C nor the C++ standard specifies the bit pattern of floats, although in practice, unless you're on something extremely weird, it's going to be IEEE754.

Doc Block posted:

I saw some JavaScript (I think?) code where the programmer had done the equivalent of something like:
code:
if(!thing == false)
because the ! would essentially be converting it to a bool, that way the equality test would be a True Boolean Comparison :rolleyes: and then I discovered that this is apparently a thing with web developers :stonk:

I recall also seeing stuff where they would do if(!(!thing)) for the same dumb reason.

!!x is a somewhat common idiom in C to coerce to 0 or 1.

Xerophyte
Mar 17, 2008

This space intentionally left blank
"!!thing" is a pretty common idiom, like it or not. Mostly in C as short for thing == 0 ? 0 : 1, but I've seen it often enough in C++ to cast to bool. You'll come across it in perfectly sane code.

E:F,b.

Doc Block
Apr 15, 2003
Fun Shoe
Yeah, I can understand doing !!integer as a short way to force an integer value to be 0 or 1, or if for some weird reason you really needed the comparison to be against 0 or 1 instead of zero/nonzero, but I was more poking fun at cargo cult web developers doing it needlessly in comparisons, like if(!thing == false) instead of just if(thing) because somehow doing a "true boolean comparison" (a thing a web developer actually told me) was somehow better or faster.

Guess it was only tangential to the point, which was that the OP should just write if(!myFloat) or if(myFloat == 0.0f) and be done with it, as opposed to trying to micro-optimize it or casting it to a bool first as a hint to the compiler etc. etc. etc.

Doc Block fucked around with this message at 08:56 on Aug 8, 2016

raminasi
Jan 25, 2005

a last drink with no ice

Doc Block posted:

somehow doing a "true boolean comparison" (a thing a web developer actually told me) was somehow better or faster.

I assumed it was some hack around idiotic truthiness testing, but I don't know JavaScript.

Doc Block
Apr 15, 2003
Fun Shoe
When it comes to JavaScript, who knows? v:shobon:v

But :doh: at True Boolean ComparisonTM. Even if that really is a faster/better way of doing it in JavaScript, I got the distinct impression he was just cargo culting.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Doc Block posted:

Even if that really is a faster/better way of doing it in JavaScript

It's not.

Volguus
Mar 3, 2009

Combat Pretzel posted:

So, regarding micro-optimization of checking whether a float is non-zero or not...

When I set a float specifically to zero, are all 32 bits zero?

If so, when I want to check whether it was not specifically set to zero, can I do magic like this?

code:
if(*((int*)&some_float)) { ... }

Anytime you're thinking about optimizing code, there is only one thing that matters: measurements. Write both test cases, verify validity of results and measure performance. Then pick the most readable option that is not "too" slow (where "too" is specific to your project).

Spatial
Nov 15, 2007

Combat Pretzel posted:

So, regarding micro-optimization of checking whether a float is non-zero or not...

When I set a float specifically to zero, are all 32 bits zero?

If so, when I want to check whether it was not specifically set to zero, can I do magic like this?

code:
if(*((int*)&some_float)) { ... }
Unfortunately appropriate avatar there. :)

You're barking wayyyyy up the wrong tree with that kind of thinking. My advice, try to get a feel for where the compiler needs help and where it doesn't before you go off on an optimisation adventure. Cases like this aren't the kind it needs help with, that code could even be slower than normal depending on a few factors.

You were posting about making a synth before so I'm assuming you're optimising that. Probably the biggest gains will be cache aware datastructure layout and memory access patterns, use of vector instructions (SSE2 can safely be assumed on any PC), and the usual DSP tricks like factoring out trigonometry ops.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Making xerces-c and xqilla work correctly on both solaris and linux is driving me bugfuck insane and i'm giving up as soon as I pin down an alternative set of libraries. Anyone have any recommendations for xml parsing and XPath 1.0 support? I've been pointed at libxml2 and pugixml but don't really have any specific recommendations yet. Prefer something simple enough that it'll have a reasonable shot at compiling on Linux/Intel and Solaris/SPARC.

Preferably something that can capably either handle or completely ignore xml namespaces, too, so I can use XPath queries like /kml//Placemark/*:Track or //gx:Track/when or whatever without it flipping its goddamned lid on one operating system or the other but never both at the same time loving argh

Ciaphas fucked around with this message at 22:20 on Aug 8, 2016

csammis
Aug 26, 2003

Mental Institution

Ciaphas posted:

I've been pointed at libxml2 and pugixml but don't really have any specific recommendations yet. Prefer something simple enough that it'll have a reasonable shot at compiling on Linux/Intel and Solaris/SPARC.

Have you tried either of them on your dataset? Since the documents are malformed to start with I doubt you're going to get any useful off-the-cuff suggestions.

Here's an example of a useless suggestion: I've used xerces on Windows/linux/AIX and pugi on Windows and they're both fine, though pugi seems a little more user-friendly.

and our XML documents are well-formed and don't use namespacing

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


In this particular case, xqilla on Linux fails to work on even well-formed (but still namespaced) documents, which is why I was getting so frustrated.

Guess I'll just pick one and try it and hope

Sex Bumbo
Aug 14, 2004
Maybe everyone knew this but I found out you can put lambdas into constructor initializers and call them and I'm all ready to write incredibly stupid code now.

https://ideone.com/YvSAcX

raminasi
Jan 25, 2005

a last drink with no ice
Yeah that's pretty stupid, because if the lambda is simple you can just use an expression directly, and if it's complicated you should probably factor it into a named function.

Sex Bumbo
Aug 14, 2004
I was specifically thinking of making a COM object which is created by an output parameter and storing it in a smart pointer object making it impossible to do in the initializer list without a wrapping function (I think? )

raminasi
Jan 25, 2005

a last drink with no ice
Yes, but your wrapping function can be a named function. No need for a lambda.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

raminasi posted:

Yes, but your wrapping function can be a named function. No need for a lambda.

On the other hand, is there a need to name the function if you're only using it there?

Ralith
Jan 12, 2011

I see a ship in the harbor
I can and shall obey
But if it wasn't for your misfortune
I'd be a heavenly person today

leper khan posted:

On the other hand, is there a need to name the function if you're only using it there?
That depends. Is there a need for your code not to be an awkward mess?

Beef
Jul 26, 2004
Remember, this is still C++ you're talking about.

Cory Parsnipson
Nov 15, 2015
I had do this this ugly thing where I turned a "regular" class (class Butt) into a Singleton, but it calls methods from another singleton class (class Poop). In the constructor of Butt, I call a method in Poop to initialize an entry in it's std::map member. Unfortunately, it looks like the map is empty when I try and access it later.

For example:

code:
Butt::Butt() {
	Poop::load("fart", 10); // assume Poop has an std::map<std::string, int> member
}

int Butt::execute() {
	Poop::get("fart"); // gdb shows in this function that std::map<std::string, int> has 0 entries
}
Butt is sort of like this:

code:
class Butt {
public:
	static Butt* inst();

private:
	static Butt* instance_;

	// constructors etc etc
};
My Butt singleton does lazy init, where the instance accessor calls the constructor the first time it's called.

code:
// in Butt.cpp

Butt* Butt::instance_ = nullptr;

Butt* Butt::inst() {
	if (!Butt::instance_) {
		Butt::instance_ = new Butt();
	}
	return Butt::instance_;
}
But my Poop singleton does not.

code:
// in Poop.cpp

Poop* Poop::instance_ = new Poop();

Poop* Poop::inst() {
	return Poop::instance_;
}
Am I relying on ordered initialization of static objects? I have a feeling something like that is happening, but I can't exactly pinpoint how that applies to this specific code. When I go over the code in my head, it doesn't seem to match up with the behavior I'm seeing. I figure that the static instance of Poop is initialized before Butt::execute() is called, but since Poop's map doesn't have any entries, I'm not sure what's going on...

My main function is like this:

code:
int main() {
	Butt::inst()->execute();
}
But if I change it to this, everything works as expected:

code:
int main() {
	Butt::inst();
	Butt::inst()->execute();	// Poop's map now has the expected entry
}
And yes, I know this is really ugly, unsafe code. I'm planning on refactoring it later, but I just want to understand why my mental model doesn't match gcc's output...

Thanks!

Beef
Jul 26, 2004
Whatever the answer is, you will probably be able to post it in coding horrors. Avoid singletons in C++, but I'm sure someone forced you.

Put a break at the Poop::load line and see if it is actually executed. execute() does not depend on any state from Butt, so perhaps there is some optimization going on where gcc sees a static dispatch on a static instance and just calls get() without going by the load() in ctor.
I was not able to reproduce it though.

raminasi
Jan 25, 2005

a last drink with no ice

leper khan posted:

On the other hand, is there a need to name the function if you're only using it there?

Yes, to give the future maintenance programmer some idea of what the hell you were doing. Like, there's definitely some daylight between "a single expression" and "a named function" - like, maybe, two expressions - but in most cases putting a lambda in an initializer list is going to be pretty obfuscatory.

Adbot
ADBOT LOVES YOU

taqueso
Mar 8, 2004


:911:
:wookie: :thermidor: :wookie:
:dehumanize:

:pirate::hf::tinfoil:

Naming (any) functions just gets in the way of letting the code speak for itself. It's like the language is trying to force you to spoil the code with pseudo-comments.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply