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
Vanadium
Jan 8, 2005

You could use OS-specific APIs, like epoll on Linux, or generally some variant of select.

Adbot
ADBOT LOVES YOU

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Vanadium posted:

You could use OS-specific APIs, like epoll on Linux, or generally some variant of select.

I read about "select" a bit, not sure if it's what I'm thinking of . . . I guess it isn't really a socket-related question on second thought, I just want something that can check for input without freezing up a loop.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

HondaCivet posted:

Is there a way to take in input without causing the program to hang? I'd like the program to check for input each time it runs through a loop but, of course, it waits for input instead of continuing through the loop. I'm using plain old cin (C++ here with Boost). What else could I do?
Since it looks like you're writing a socket program, you most certainly want to separate the network from your UI instead of, say, polling the iostream every time you go through the loop. The three canonical ways of handling multiple connections are:

1) fork() a process to handle the network connection, using signals handlers to inform the child network process to terminate and clean up
2) use a threading library like pthreads or whatever Boost gives you, where you call [fixed]pthread_exit[/fixed} or whatever.

(and the third, comedy option:

3) whatever crazy Boost poo poo there is (Is boost::asio still around? I still have nightmares from it; that poo poo was seriously my Vietnam.) )

I assume the problem you're trying to solve is "I'd like to include a little I/O on this thing so that transfer can be shut off with a text command." Any of those three solutions will work; beej has a pretty good Unix IPC guide if you want to go with 1).

Dijkstracula fucked around with this message at 23:14 on Aug 26, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Dijkstracula posted:

3) whatever crazy Boost poo poo there is (Is boost::asio still around? I still have nightmares from it; that poo poo was seriously my Vietnam.) )

HondaCivet is using Boost.Asio.

oldkike
Jan 10, 2003

hey

www.pleasegimmeadollar.com

HondaCivet posted:

I read about "select" a bit, not sure if it's what I'm thinking of . . . I guess it isn't really a socket-related question on second thought, I just want something that can check for input without freezing up a loop.

Seems to me that calling cin.peek() would work.

Vanadium
Jan 8, 2005

oldkike posted:

Seems to me that calling cin.peek() would work.

No

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

HondaCivet is using Boost.Asio.
Well, then, isn't this a solved problem then? :confused:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Dijkstracula posted:

Well, then, isn't this a solved problem then? :confused:

Well yes, if Honda were actually a programmer by trade, as opposed to an engineer. Luckily, we work a building apart from each other.

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Avenging Dentist posted:

Well yes, if Honda were actually a programmer by trade, as opposed to an engineer. Luckily, we work a building apart from each other.

im sorry hopefully they wont let me program anymore :(

Paniolo
Oct 9, 2007

Heads will roll.
I'm experimenting with a handle-style class with a reference counted implementation shared among all copies of the same handle. Basically it looks like this:

code:
struct handle {
   handle();
   handle(const handle&);
   handle& operator=(const handle&);
   ~handle();

   // this is a function which modifies the actual object
   void foo();

   // this clones the object and returns a handle to the clone
   handle clone();

private:
   boost::intrusive_ptr<impl> obj;
}
This code works, but there's a problem with it: you can copy a const handle to get a non-const handle, allowing you to modify the underlying object. I can fix this problem by changing the signature of the copy constructor and assignment operators so they only accept non-const references, but this creates a new problem:

code:
handle a;
handle b = a.clone(); // ERROR - no valid constructor
I don't want to implement copy-on-write because I want multiple owners of a handle to be able to modify the same object (and these are non-trivial objects representing system resources.) Is there a way to do what I'm trying to do, or do I need to give up the idea of maintaining const correctness for the underlying object?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Just change the semantics of the clone operation to do something other than return a temporary; temporaries can't be bound to non-const (lvalue) references. How exactly you choose to do that is up to you.

plushpuffin
Jan 10, 2003

Fratercula arctica

Nap Ghost
Is there ever a reason to pass a "const long &" to a function? I keep seeing this in my company's code and I think it's fracking retarded.

One of the other programmers said that it might be more efficient to pass a reference to the long instead of passing the long itself, because that way you don't have to copy the value of the long.

The explanation actually makes it seem even more stupid, because regardless of what you pass, you're still putting a 32-bit value onto the stack and it must be copied either from the value of the long or from the address of the long. Passing it by reference probably hurts performance, actually, because your function's variables are no longer likely to be close together in memory. The only possible situation I can see in which this could even so much as break even is if the long is stored in a register and the compiler/processor combined are smart enough not to clear that register when entering the function (during this process it is supposed to store the registers of the calling function, is it not?).

edit: I just thought of something. Is this done in order to convince the compiler to inline certain small functions?

plushpuffin fucked around with this message at 01:42 on Aug 29, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

plushpuffin posted:

Is there ever a reason to pass a "const long &" to a function? I keep seeing this in my company's code and I think it's fracking retarded.

One of the other programmers said that it might be more efficient to pass a reference to the long instead of passing the long itself, because that way you don't have to copy the value of the long.

Given that on every architecture I can think of, sizeof(long) <= sizeof(void*), this is pointless.

plushpuffin posted:

The only possible situation I can see in which this could even so much as break even is if the long is stored in a register and the compiler/processor combined are smart enough not to clear that register when entering the function (during this process it is supposed to store the registers of the calling function, is it not?).

Actually it would never* break even with a non-inline function and always break even with an inline function. While the calling conventions of functions can vary, they almost universally expect arguments to be on the stack, and to allow some stuff on the stack and others in registers would require self-modifying code (or multiple versions of each function).

The only primitives that are ever worth passing as a const-ref are double and long double (ok, and maybe some weird types too, like vector types).

Also, here's what you do to convince a compiler to inline a function: inline. (Or a compiler extension if you want to get pushy.)

* Probably

Avenging Dentist fucked around with this message at 01:44 on Aug 29, 2009

plushpuffin
Jan 10, 2003

Fratercula arctica

Nap Ghost

Avenging Dentist posted:

Also, here's what you do to convince a compiler to inline a function: inline. (Or a compiler extension if you want to get pushy.)

Given that the inline keyword is actually just a hint that can be ignored, what I meant by "convince" is that it makes the compiler's job easier when deciding which functions it should automatically inline and which functions it won't.

Thanks for the explanation!

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

plushpuffin posted:

Given that the inline keyword is actually just a hint that can be ignored, what I meant by "convince" is that it makes the compiler's job easier when deciding which functions it should automatically inline and which functions it won't.

Giving it an obtuse hint in addition to an obvious hint isn't really going to help any sane compiler. If you want to do more than hint, you should use things like __forceinline in MSVC and __attribute__(always_inline) in GCC.

ehnus
Apr 16, 2003

Now you're thinking with portals!

Avenging Dentist posted:

The only primitives that are ever worth passing as a const-ref are double and long double (ok, and maybe some weird types too, like vector types).


Even still some well known compilers for some platforms will do the retarded thing and pass it (the double/vector type) in memory instead of in register even if the ABI allows for it.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

ehnus posted:

Even still some well known compilers for some platforms will do the retarded thing and pass it (the double/vector type) in memory instead of in register even if the ABI allows for it.

Well registers technically have no address.

king_kilr
May 25, 2007

Avenging Dentist posted:

Giving it an obtuse hint in addition to an obvious hint isn't really going to help any sane compiler. If you want to do more than hint, you should use things like __forceinline in MSVC and __attribute__(always_inline) in GCC.

The __attribute__(always_inline) is also used in Clang :eng101:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

king_kilr posted:

The __attribute__(always_inline) is also used in Clang :eng101:

Yes but clang isn't a C++ compiler. :colbert:

ehnus
Apr 16, 2003

Now you're thinking with portals!

Avenging Dentist posted:

Well registers technically have no address.

Well, yeah, but I meant that there would be no performance advantage of passing a const reference over passing one of those primitive types by value because of the forced memory access

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

ehnus posted:

Well, yeah, but I meant that there would be no performance advantage of passing a const reference over passing one of those primitive types by value because of the forced memory access

What if you need to save 4 bytes of memory? What then, mister?

ehnus
Apr 16, 2003

Now you're thinking with portals!

Avenging Dentist posted:

What if you need to save 4 bytes of memory? What then, mister?

I'd shorten my variable names.

That Turkey Story
Mar 30, 2003

plushpuffin posted:

Is there ever a reason to pass a "const long &" to a function? I keep seeing this in my company's code and I think it's fracking retarded.

You might want to take the address of the long and have it be the address of the passed-in variable.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Avenging Dentist posted:

Yes but clang isn't a C++ compiler. :colbert:

Only because you are too lazy :whip:

Jaded Burnout
Jul 10, 2004


I'm trying to make sense of some source code for a splay tree as I'm going to have to modify it slightly for use in a Ruby library.

I'm pretty comfortable with the concept of splay trees especially using bottom-up splays (i.e. BST style search, then splay), but this code uses a top-down approach.

Firstly, is a top-down approach (splaying from the root rather than running to the bottom first) actually worth while in terms of performance gains over the easier to understand bottom-up approach?

Secondly, the code. This section I am particularly confused about :

code:
Tree N, *l, *r, *y;
if (t == NULL) return t;
N.left = N.right = NULL;
l = r = &N;

[.. the main splaying loop ..]

l->right = t->left;
r->left = t->right;
t->left = N.right;
t->right = N.left;
This last part is in the context of this function and is adorned only with the comment "assemble". My pointer fu is clearly not up to scratch here to actually get the point.

Maybe someone who's worked with this technique before can give me an idea?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
There are a lot of undocumented invariants in that function; an important method for puzzling them out is to look at final code like that and see how it pieces things back together. Doing so tells you that:
  • t is the current candidate for the root,
  • N's left and right children are candidates for the right and left subtrees of t, respectively, and
  • l (r) has no relevant right (left) child, and setting that child to t->left (t->right) doesn't break the tree's structural invariant.

The crucial invariants are:
  • Everything in N.right is to the left of everything in t in the tree's order.
  • Everything in N.left is to the right of everything in t in the tree's order.
  • l is either N or is the rightmost node in N.right. It is N if and only if N.right is null.
  • r is either N or is the leftmost node in N.left. It is N if and only if N.left is null.

If you look over the entire top-down splay with those invariants in mind, everything should come together.

rjmccall fucked around with this message at 18:39 on Aug 31, 2009

newsomnuke
Feb 25, 2007

code:
struct Variable
{
	int type;
	union
	{
		int i;
		float f;
		void* p;
	};
	float v[2];
};

void setVector3(Variable* dst, int index, float f)
{
	*(&dst->f + index) = f;
}
Is this safe? I can't think why it wouldn't be but C++ is always surprising me.

Chuu
Sep 11, 2004

Grimey Drawer
Is there a C++ library out there that can help with "graphical" console apps? Basically I just need to output to the console, clear my output and replace it with new output, and change colors -- nothing too fancy.

I took a quick look at ncurses and it seems a bit of a bear. Is there a better alternative?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

ultra-inquisitor posted:

Is this safe? I can't think why it wouldn't be but C++ is always surprising me.

Well, it is obviously broken when sizeof(float) < sizeof(void*) (which is true on any 64-bit platform that I know of), and C++ doesn't guarantee the absence of padding between adjacent struct members — but if you're specifically targeting a platform with an ABI that lays out that struct like you want, then it's certainly safe.

That said, you should (1) probably guard it in platform-specific #ifs or, better yet, (2) just make it float f[3], since presumably you never want two floats and a pointer. Or just heap-allocate the vector case, which is presumably much less common than the other two.

newsomnuke
Feb 25, 2007

rjmccall posted:

C++ doesn't guarantee the absence of padding between adjacent struct members
Can you use a #pragma (I forget whether pack or align=packed is more appropriate)?

quote:

That said, you should (1) probably guard it in platform-specific #ifs or, better yet, (2) just make it float f[3], since presumably you never want two floats and a pointer. Or just heap-allocate the vector case, which is presumably much less common than the other two.
The issue is that I want to keep the structure at 16 bytes, while allowing it to hold ints/floats/pointers/vector3s. I've been playing around with a scripting language which allocated vectors on the heap and this was actually one of its slow areas because the vectors would then be subject to garbage collection among other things, and this is my attempt at fixing that. Otherwise I'd use a float[3] in an instant.

Ultimately it's all a speed issue and only profiling will tell for sure what the best method is, but I'm just putting my thoughts out in case I'm doing something obviously stupid.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

ultra-inquisitor posted:

The issue is that I want to keep the structure at 16 bytes, while allowing it to hold ints/floats/pointers/vector3s.

Ummmmm....

Chuu
Sep 11, 2004

Grimey Drawer

Dijkstracula posted:

3) whatever crazy Boost poo poo there is (Is boost::asio still around? I still have nightmares from it; that poo poo was seriously my Vietnam.) )

I assume the problem you're trying to solve is "I'd like to include a little I/O on this thing so that transfer can be shut off with a text command." Any of those three solutions will work; beej has a pretty good Unix IPC guide if you want to go with 1).

Don't let people scare you off from boost::asio, it's actually really easy to use and has dozens of example programs that you can probably template your needs around. You might want to look at this specific one which use posix::stream_descriptor's to read from stdin. I'm using this right now at work to aggregate and sort through a ton of real time logging data from files.

The only real gotcha to boost::asio I found when dealing with formatted strings is the read methods are sort of misleadingly named. "read_until" methods that take a regex only guarantee that the returned buffer would returned "true" if checked for the regular expression. So if you "read until" newline, you'll get a newline in there somewhere. There is no guarantee that you'll end with a newline, or have only a single newline in your buffer.

newsomnuke
Feb 25, 2007

Avenging Dentist posted:

Ummmmm....
Oh hahaha :doh:

Fecotourist
Nov 1, 2008
Does clang have a hope of making template errors more readable? Or is that problem basically intractable.
Boost.fusion reminds me of that line from The Usual Suspects. "How do you shoot the devil in the back? What if you miss?" When you get it right it's miraculous, but one little mistake and gcc is all
code:
instantiated from here
instantiated from here
instantiated from here
instantiated from here
instantiated from here
edit:
So given a transform view, what's the slick way to put its contents into a boost.array? The adaptation from array into fusion is great, but the other direction isn't jumping out at me. Could I make a no-op view of the destination array and then do a view-to-view assignment? But there must be something cleaner.

Fecotourist fucked around with this message at 06:42 on Sep 2, 2009

king_kilr
May 25, 2007
I don't know how Clang does on template error messages, but one of the things I've heard, both from Brett Cannon on compiling CPython and from the Ars Technica review of Clang integration in XCode is that the errors are lightyears better.

haveblue
Aug 15, 2005



Toilet Rascal

king_kilr posted:

I don't know how Clang does on template error messages, but one of the things I've heard, both from Brett Cannon on compiling CPython and from the Ars Technica review of Clang integration in XCode is that the errors are lightyears better.

The examples in the Ars review were brilliant. Highlight the exact token it choked on in the editor view? Yes, please.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Well, for one thing, clang tries to use the shorthand you (the programmer) used for your templates. So you'll see std::string (std::basic_string<char>) if you gently caress something up with a string.

Fecotourist
Nov 1, 2008

Avenging Dentist posted:

Now you've gone and done it. I'm just going to have to recommend Boost.Parameter. :colbert:

I came across another approach that is a little less intimidating than boost.parameter and probably fits most realistic use cases:
http://www.nabble.com/-fusion--parameter--a-solution-for-named-parameters---parameter-packs-using-fusion-maps-td18963630.html

Is David Abrahams always such a bitch? Here's more from him:

http://lists.boost.org/boost-users/2007/06/28487.php

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
How is he being a bitch in either of those?

Adbot
ADBOT LOVES YOU

Marsol0
Jun 6, 2004
No avatar. I just saved you some load time. You're welcome.

Chuu posted:

Is there a C++ library out there that can help with "graphical" console apps? Basically I just need to output to the console, clear my output and replace it with new output, and change colors -- nothing too fancy.

I took a quick look at ncurses and it seems a bit of a bear. Is there a better alternative?

NCurses is pretty easy to use.

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