2 registered members (Akow, tomaslolo),
1,536
guests, and 12
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
What's (currently) your favourite snippet of code?
#444492
08/12/14 19:17
08/12/14 19:17
|
Joined: Nov 2008
Posts: 946
the_clown
OP
User
|
OP
User
Joined: Nov 2008
Posts: 946
|
Given that Acknex is often called an engine for programmers, I guess a lot of you here actually are programmers, so quick and dirty, what's currently your favourite snippet of code that does something cool with only a few lines? May be any language, any context, as long as the length/effect ratio is cool in any way. Oh and if you feel like it, do NOT tell us what it actually does, instead let us have a guess. If it's not too hard. I'll start off with my current favourite in C++ - this should be rather easy to guess:
template <class T>
static T& Instance(){
static T t;
return t;
}
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: the_clown]
#444493
08/12/14 19:23
08/12/14 19:23
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Singleton. Breaks horribly when part of a library and is used in an compilation unit that generates code linked into another binary/library. Source: Raynes singleton system breaking horribly on Windows. My favourite. Should be easy to guess, but can someone point out the beauty of this snippet?
_titleLabel->SetFrame([&]() -> RN::Rect {
RN::Rect rect = _titleLabel->GetFrame();
rect.x = 5.0f;
rect.y = 5.0f;
return rect;
}());
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: WretchedSid]
#444495
08/12/14 19:43
08/12/14 19:43
|
Joined: Nov 2008
Posts: 946
the_clown
OP
User
|
OP
User
Joined: Nov 2008
Posts: 946
|
Singleton. Breaks horribly when part of a library and is used in an compilation unit that generates code linked into another binary/library. Source: Raynes singleton system breaking horribly on Windows.
Oh it does? Good to know, how exactly does it break? Your snippet sets the size of a label, or the label's frame, to 5x5 I guess, by basically making a copy of the existing frame, modifying the properties of that copy and then setting it as new frame for the label?
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: the_clown]
#444496
08/12/14 20:18
08/12/14 20:18
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Oh it does? Good to know, how exactly does it break? Assuming you have a library and a normal application, and both use this to create a singleton of type "Foo". The library and application will then both have their very own instance that is not shared. Normally the linker is responsible for getting rid of the multiple template instantiations and to make them into one, however, the linker doesn't work cross libraries, so the application and library both have their very own instances of the code and there of "Foo". Your snippet sets the size of a label, or the label's frame, to 5x5 I guess, by basically making a copy of the existing frame, modifying the properties of that copy and then setting it as new frame for the label? Well, kind of. It only sets the origin and doesn't touch the size, but that's not the beauty of the snippet. Want to take another guess or should I say it?
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: the_clown]
#444498
08/12/14 20:42
08/12/14 20:42
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Ah, so the problem here is the fact that it's a templated method? Interesting pitfall. Kind of, but it wouldn't work without templates either. If you declare it static, you would get an instantiation per compilation unit that makes use of it. Without static, your linker would complain about duplicated symbols. Because it's a template, the linker will collapse the multiple instantiations down to one. Hmm I could imagine it's the fact you're doing the whole thing with a single function call using C++'s Lambda feature? At least I think that's quite a nice thing. Getting close. The real nice thing is that it doesn't pollute the outer scope with "rect" and makes the whole thing look nicer (imho anyways). Here is the context: https://github.com/uberpixel/Downpour/bl...w.cpp#L169-L175It really is just a much nicer way of writing this:
RN::Rect rect = _titleLabel->GetFrame();
rect.x = 5.0f;
rect.y = 5.0f;
_titleLabel->SetFrame(rect);
// Why does rect have to still be visible here?!
I sue this kind of sparingly and only when it makes sense to limit the scope and visibility of variables. Edit: Are we allowed multiple posts? I know one can only have _one_ favourite, but what about other really neat snippets?
Last edited by WretchedSid; 08/12/14 20:52.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: WretchedSid]
#444499
08/12/14 21:04
08/12/14 21:04
|
Joined: Nov 2008
Posts: 946
the_clown
OP
User
|
OP
User
Joined: Nov 2008
Posts: 946
|
Kind of, but it wouldn't work without templates either. If you declare it static, you would get an instantiation per compilation unit that makes use of it. Without static, your linker would complain about duplicated symbols. Because it's a template, the linker will collapse the multiple instantiations down to one.
Hmm I'm trying to make sure I get you right here (even if that collapses the thread miles into off-topic, but as it's my thread I guess I can do that), you say if I made this a non-templated method and did something like this
static Singleton& Instance(){
static Singleton s;
return s;
}
that would break when used in a library, too? EDIT: And you're welcome to post more than one snippet.
Last edited by the_clown; 08/12/14 21:05.
|
|
|
Re: What's (currently) your favourite snippet of code?
[Re: the_clown]
#444502
08/12/14 22:07
08/12/14 22:07
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
that would break when used in a library, too? It would just break. Static functions are only visible to the current compilation unit and aren't shared. You can test this yourself by returning the pointer to the static variable and printing that in the callee, then do this from two compilation units and link it all together. Here is my second snippet. It also uses static variables, and it's a tad longer.
class Initializer
{
public:
typedef void (*Callback)();
Initializer(Callback ctor, Callback dtor) :
_dtor(dtor)
{
if(ctor) ctor();
}
~Initializer()
{
if(_dtor) _dtor();
}
private:
Callback _dtor;
};
#define RN_REGISTER_INITIALIZER(name, body) \
namespace { \
static void __RNGlobalInit##name##Callback() { body; } \
static RN::Initializer __RNGlobalInit##name (__RNGlobalInit##name##Callback, nullptr); \
}
#define RN_REGISTER_DESTRUCTOR(name, body) \
namespace { \
static void __RNGlobalDestructor##name##Callback() { body; } \
static RN::Initializer __RNGlobalDestructor##name (nullptr, __RNGlobalDestructor##name##Callback); \
}
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
|