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
Max Facetime
Apr 18, 2009

Battle Bott posted:

http://www.urubatan.info/2008/11/commenting-source-code-is-only-for-the-weak/

The first example looks perfectly fine to me, and the refactored version is more confusing.

There's three big problems in that example. The signature implies that the method writes the contents to a file or is at least ambiguous. Then there's the confusing use of Strings, StringBuilders and StringBuffers. Then it returns the results as a return value and in the passed container, possibly killing performance.

That being said, I don't agree with the refactored version. I would write it like this to keep the spirit of the original:

code:
public void appendFileContents(String filename, StringBuilder builder) 
  throws IOException {

  BufferedReader reader = 
    new BufferedReader(new FileReader(new File(filename)));
  String line;
  while((line = reader.readLine()) != null) {
    builder.append(line).append('\n');
  }
  reader.close();
}

Adbot
ADBOT LOVES YOU

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.

Avenging Dentist posted:

If you write a function that is only called once (and you never intend to use it other places), you are an idiot.
int main()

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Mustach posted:

int main()

Wait, people write nonrecursive main functions? How do you write your event loop?

Contero
Mar 28, 2004

I'm poking around boost for the first time and the first area I'm trying out are the functional libraries.

I'm trying to figure out if there is a way to write a function that takes some function pointer or functor and returns an anonymous function that say, does that function twice. The signatures can be fixed, but if you can do it for any function I'd be pretty impressed.

I've checked out boost::function, lambda and phoenix and I haven't managed to get anything working that does this exactly (without making the parameter of twice an actual function pointer).

I know I could do this by making a class for this specific purpose; I just want to know if it's possible to wrap all this functionality into a single one-line function. My goal is flexibility and minimum LOC. Also no 0x lambdas.

So:

code:
// I guess boost::function types

function<int (int)> twice (function<int (int)> f)
{
   return // something like: lambda(x)(f(f(x));
}

int square (int i)
{
   return i*i;
}

...
   cout << twice(twice(square))(2) << endl; // 65536

Vanadium
Jan 8, 2005

return boost::bind(f, boost::bind(f, _1)); seems to do.

http://www.boost.org/doc/libs/1_39_0/libs/bind/bind.html#nested_binds

HondaCivet
Oct 16, 2005

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


So at my job I've been tasked with making a program that can send a stream of data to another program. This should be really simple but I am almost completely a programming newbie so I didn't really know where to start. A friend suggested using the Asio library in Boost so I am giving that a shot after getting frustrated with conventional Windows sockets. I installed Boost 1.39 and have plugged an example from Boost's tutorials (http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/tutorial/tutdaytime1/src.html) into Visual Studio 2008. I got past a couple of errors but I can't figure this one out:

code:
LINK : fatal error LNK1104: cannot open file 'libboost_system-vc90-mt-gd-1_39.lib'
I searched Windows for it and it doesn't seem to exist so no wonder it can't open it I guess. Google has been zero help. Could someone suggest either a fix for this or an even easier way to send a stream of data between two programs?

amotea
Mar 23, 2008
Grimey Drawer
You need to compile the Boost library for it to work (parts of it anyway), the Asio library is not a header only library. It's probably easier to just use the binaries installer http://www.boostpro.com/download.

You then need to tell Visual Studio to search in the boost /lib directory for additional linker dependencies.

Avenging Dentist
Oct 1, 2005

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

amotea posted:

You then need to tell Visual Studio to search in the boost /lib directory for additional linker dependencies.

I'm pretty sure the installer does that for you, but then I haven't used it in a long time since I build Boost from source.

floWenoL
Oct 23, 2002

Avenging Dentist posted:

I'm pretty sure the installer does that for you, but then I haven't used it in a long time since I build Boost from source.

Shouldn't there be some #pragmas in the boost headers that add the boost lib directories to the right place?

ctz
Feb 6, 2003

floWenoL posted:

Shouldn't there be some #pragmas in the boost headers that add the boost lib directories to the right place?

No, those pragmas only name the libraries which need to be linked in. This doesn't help with library search.

'HondaCivet' -- if you did build boost you didn't build it for the right flavour. 'mt-gd' denotes a debug build against the multi-threaded debug DLL CRT. See: http://www.boost.org/doc/libs/1_39_0/more/getting_started/windows.html#library-naming

HondaCivet
Oct 16, 2005

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


ctz posted:

No, those pragmas only name the libraries which need to be linked in. This doesn't help with library search.

'HondaCivet' -- if you did build boost you didn't build it for the right flavour. 'mt-gd' denotes a debug build against the multi-threaded debug DLL CRT. See: http://www.boost.org/doc/libs/1_39_0/more/getting_started/windows.html#library-naming

Thanks but could you elaborate on this? It doesn't really provide any direction or instruction or tell me what's going on.

Vayra
Aug 3, 2007
I wanted a big red title but I'm getting a small white one instead.
Hello, I hope you guys can help me. I'm learning C# as my first programming language (I have a friend who is helping me a bit, thus my choice) and I'm looking for a good book to get to help me learn it from scratch, especially regarding game programming, as I feel I've about reached the bounds of what I can teach myself without direction.

If this should be in the .Net thread I'll post it over there but it's not clear to me which thread I should be asking in. Thanks.

BattleMaster
Aug 14, 2000

The Good Professor posted:

Hello, I hope you guys can help me. I'm learning C# as my first programming language (I have a friend who is helping me a bit, thus my choice) and I'm looking for a good book to get to help me learn it from scratch, especially regarding game programming, as I feel I've about reached the bounds of what I can teach myself without direction.

If this should be in the .Net thread I'll post it over there but it's not clear to me which thread I should be asking in. Thanks.

While it is great that you want to learn, this is the C/C++ thread not a C# thread. You probably want the .net one unless there actually is a C# specific one that I haven't seen.

Avenging Dentist
Oct 1, 2005

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

HondaCivet posted:

Thanks but could you elaborate on this? It doesn't really provide any direction or instruction or tell me what's going on.

Just download the installer from BoostPro Computing and install the first two library types (I think).

Dijkstracula
Mar 18, 2003

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

The Good Professor posted:

Hello, I hope you guys can help me. I'm learning C# as my first programming language (I have a friend who is helping me a bit, thus my choice) and I'm looking for a good book to get to help me learn it from scratch, especially regarding game programming, as I feel I've about reached the bounds of what I can teach myself without direction.

If this should be in the .Net thread I'll post it over there but it's not clear to me which thread I should be asking in. Thanks.
Yes, the .NET thread is where you should be. That said, whenever people ask this question I automatically refer them to O'Reilly's Learning C# by Liberty.

Jo
Jan 24, 2005

:allears:
Soiled Meat
Got it working! Yaaay!

Jo fucked around with this message at 22:36 on Jul 31, 2009

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?
I've seen code by a few people that passes built-in types as const references, e.g.
code:
void f(const int& arg1, const double& arg2);
Is there any reason not to do pass-by-value with built-ins? I know why pass-by-const-reference is a good idea for more complex types, but it seems redundant here.

HondaCivet
Oct 16, 2005

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


Avenging Dentist posted:

Just download the installer from BoostPro Computing and install the first two library types (I think).

Argh you are right I just didn't install it right. :(

Anyway, so the client and [url = "http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime2.html]server[/url] parts seem to run without error but I still think they aren't working right. It looks and sounds like the server is supposed to pass a time stamp to the client who prints it up on the screen. When I run it, the server just puts up a blank command prompt screen that just sits there and the client pops one up for a split second and then it disappears. I never see any writing on either but for the client, even if there was any, it'd be pretty hard to read. What am I doing wrong? I haven't changed anything in the code given so it seems funny that I would've broken it already. I am trying to test them on a single machine, is that not possible?

Adhemar
Jan 21, 2004

Kellner, da ist ein scheussliches Biest in meiner Suppe.

DoctorTristan posted:

I've seen code by a few people that passes built-in types as const references, e.g.
code:
void f(const int& arg1, const double& arg2);
Is there any reason not to do pass-by-value with built-ins? I know why pass-by-const-reference is a good idea for more complex types, but it seems redundant here.

A double is usually 8 bytes, whereas a reference (essentially a pointer under the hood) would be only 4 bytes on a 32 bit machine, so it can reduce the amount of data that needs to be copied around. For int it doesn't really make sense, because it's only 4 bytes, while a pointer or reference is the same size, or even bigger (on a 64 bit machine).

Avenging Dentist
Oct 1, 2005

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

HondaCivet posted:

When I run it, the server just puts up a blank command prompt screen that just sits there and the client pops one up for a split second and then it disappears.

The client is probably exiting once it receives and prints the data. Since it's a command-line thing in Windows it'll close the command prompt almost immediately. The solution is to either 1) run it from Visual Studio (non-debug mode), since Visual Studio will insert a pause before it closes the prompt, 2) run it from Visual Studio in debug mode and set a breakpoint just before you return from main, or 3) open up a command prompt manually and execute the client program from there.

Note that in cases 1 and 2, you will need to specify a command-line argument (the name of the server, "localhost" in your case) in the Visual Studio project settings. However, I don't remember how to do this. It's somewhere in the Alt+F7 menu I think.

EDIT: Here it is (probably a little different since this is VS 2003 apparently, and it looks like a .NET project)

Avenging Dentist fucked around with this message at 20:33 on Jul 30, 2009

HondaCivet
Oct 16, 2005

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


Avenging Dentist posted:

The client is probably exiting once it receives and prints the data. Since it's a command-line thing in Windows it'll close the command prompt almost immediately. The solution is to either 1) run it from Visual Studio (non-debug mode), since Visual Studio will insert a pause before it closes the prompt, 2) run it from Visual Studio in debug mode and set a breakpoint just before you return from main, or 3) open up a command prompt manually and execute the client program from there.

Note that in cases 1 and 2, you will need to specify a command-line argument (the name of the server, "localhost" in your case) in the Visual Studio project settings. However, I don't remember how to do this. It's somewhere in the Alt+F7 menu I think.

EDIT: Here it is (probably a little different since this is VS 2003 apparently, and it looks like a .NET project)


OK sweet, I think I found where to write it in VS 2008 but how do you write the command to set the server? Google isn't much help for some reason.

Avenging Dentist
Oct 1, 2005

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

HondaCivet posted:

OK sweet, I think I found where to write it in VS 2008 but how do you write the command to set the server? Google isn't much help for some reason.

If you mean the value to fill in for the client's command-line argument, you literally just type localhost in that field. It'll make the client behave like you typed client.exe localhost at a command prompt, which is what the test app expects.

The server app should run fine without any arguments passed in.

HondaCivet
Oct 16, 2005

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


Avenging Dentist posted:

If you mean the value to fill in for the client's command-line argument, you literally just type localhost in that field. It'll make the client behave like you typed client.exe localhost at a command prompt, which is what the test app expects.

The server app should run fine without any arguments passed in.

Hooray it works now, thanks! :D

So is this the right type of server/client pair to use for streaming data or is it going to explode if I try it?

Avenging Dentist
Oct 1, 2005

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

HondaCivet posted:

So is this the right type of server/client pair to use for streaming data or is it going to explode if I try it?

If the server will only ever have one client, that basic method should be sufficient. Otherwise, you'd want to look at the asynchronous version.

HondaCivet
Oct 16, 2005

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


Avenging Dentist posted:

If the server will only ever have one client, that basic method should be sufficient. Otherwise, you'd want to look at the asynchronous version.

What's the difference? It sounds like synchronous doesn't do anything until it gets a request but the asynchronous does?

Also, what about connecting between computers? Is that another thing that has to be done in the command line?

Avenging Dentist
Oct 1, 2005

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

HondaCivet posted:

What's the difference? It sounds like synchronous doesn't do anything until it gets a request but the asynchronous does?

Synchronous communication is just the process of sending data immediately and not doing anything else while that's happening. Asynchronous communication sends stuff in the background, making it easier to handle multiple connections or to do other stuff while the transfer is happening.

HondaCivet posted:

Also, what about connecting between computers? Is that another thing that has to be done in the command line?

With the sample code from Boost, yes. However, as long as you let Boost.Asio know what server you're connecting to, you can do it however you want. (Hard-coding, config file, GUI, etc).

ehnus
Apr 16, 2003

Now you're thinking with portals!

Adhemar posted:

A double is usually 8 bytes, whereas a reference (essentially a pointer under the hood) would be only 4 bytes on a 32 bit machine, so it can reduce the amount of data that needs to be copied around. For int it doesn't really make sense, because it's only 4 bytes, while a pointer or reference is the same size, or even bigger (on a 64 bit machine).

It's not the best of ideas though. Some platforms (like PowerPC or x86-64) will pass parameters in registers when possible but if you're passing a const reference to a primitive type some compilers will force it to the stack and pass the address which isn't anywhere near an improvement in performance.

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?

Adhemar and ehnus posted:

answers

Thanks guys. On similar lines, I've seen other code where class methods obtain members as (*this).membername or this->membername. Is this done to protect against potential naming conflicts? So that you always get the member membername and not something in a more local scope with the same name?

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


DoctorTristan posted:

Thanks guys. On similar lines, I've seen other code where class methods obtain members as (*this).membername or this->membername. Is this done to protect against potential naming conflicts? So that you always get the member membername and not something in a more local scope with the same name?

Yes, that or they want to be pedanticexplicit for the sake of being mildly annoyingclarity.

Avenging Dentist
Oct 1, 2005

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

Ledneh posted:

Yes, that or they want to be pedanticexplicit for the sake of being mildly annoyingclarity.

More than likely they've become crippled by code completion and can't remember the names of member variables on their own.

That Turkey Story
Mar 30, 2003

DoctorTristan posted:

Thanks guys. On similar lines, I've seen other code where class methods obtain members as (*this).membername or this->membername. Is this done to protect against potential naming conflicts? So that you always get the member membername and not something in a more local scope with the same name?

If you're in a class template member function definition and you need to access a member of a base whose type is dependent on a template argument you have to access that member explicitly otherwise your compiler will attempt to find the name during the first phase of name lookup and you will get a compile-time error, or worse, refer to the wrong thing. The same goes for accessing members of the base class from the child template definition (you need to qualify the name).

Avenging Dentist
Oct 1, 2005

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

That Turkey Story posted:

(you need to qualify the name).

Of course, it also works if you qualify it with a nested-name-specifier, but that tends to get verbose.

floWenoL
Oct 23, 2002

What does everyone think about http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/ ? It seems useful for writing range-checking code more tersely, as in the given example. I checked the standard and throw-expression has type "void", although it seems to have additional magic properties. For example:

code:
int x = y > 0 ? y : (void)0;
gives the error (in gcc):

code:
error: '0' has type 'void' and is not a throw-expression
I didn't find anything in the standard that talks about this (although I only looked at section 15).

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.
Interesting:
1. http://codepad.org/Uih70A6G
2. http://codepad.org/09VrvGRb
3. http://codepad.org/sI4jbaAY

Avenging Dentist
Oct 1, 2005

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

floWenoL posted:

I didn't find anything in the standard that talks about this (although I only looked at section 15).

See 5.16p2 and 5.17p1.

That Turkey Story
Mar 30, 2003

floWenoL posted:

What does everyone think about http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/ ? It seems useful for writing range-checking code more tersely, as in the given example. I checked the standard and throw-expression has type "void", although it seems to have additional magic properties.

I'm pretty sure the behavior is described as a part of the ternary operator.

Coincidentally, at Boostcon Doug Gregor and Sebastian Reidl briefly walked through some clang code for dealing with the ternary operator, including the throw expression behavior, to show off the elegance of the code-base. I think Sebastian had just recently written it at the time.

The Red Baron
Jan 10, 2005

Speaking of Clang, my current personal project is to teach myself LLVM and spicing up things through Boost.Proto to make the whole JIT experience more straight forward. The thought is that it should be trivial to compose and combine expression snippets easily in order to produce the end result lazily. Silly example:
code:
if_(_1 < 5)
[
  return_(2.5 * _1)
]
.else_
[
  return_(10.0)
]
code:
entry:
        %1 = icmp slt i32 %0, 5         ; <i1> [#uses=1]
        br i1 %1, label %then, label %else

then:           ; preds = %entry
        %2 = sitofp i32 %0 to double            ; <double> [#uses=1]
        %3 = fmul double 2.500000e+000, %2              ; <double> [#uses=1]
        ret double %3

else:           ; preds = %entry
        ret double 1.000000e+001
Currently features some implicit integer promotion and floating-point conversion. Tune in again in a couple of weeks when I've no doubt become bored, left the code in a perpetual prototype state and picked up another useless obsession instead :woop: ooh something shiny~

oldkike
Jan 10, 2003

hey

www.pleasegimmeadollar.com
I felt like I knew most of C++, until I discovered this perfectly valid C++ the other day (related to the throw stuff above).

code:
struct A {
A(int f) try : mF(f > 0 ? f : throw exception()) {} catch (...) { throw; }
};

litghost
May 26, 2004
Builder

oldkike posted:

I felt like I knew most of C++, until I discovered this perfectly valid C++ the other day (related to the throw stuff above).

code:
struct A {
A(int f) try : mF(f > 0 ? f : throw exception()) {} catch (...) { throw; }
};

What does it do?

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

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

litghost posted:

What does it do?

Exactly what it looks like.

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