@ wrote... (8 years, 12 months ago)

I recently needed to have a pool of objects and it would make everyones life a lot easier if those objects would automatically return themselves to the pool when they were done.

Here's my take on a shared pointer pool.

more…

Category: tech, Tags: boost, cpp
Comments: 0
@ wrote... (10 years, 1 month ago)

Do you have a function called Enable() and another called Disable(). Make those two private and make a third:

void Enable(bool enable) {
  if ( enable )
    Enable();
  else
    Disable();
}

// now change all your calling code from
if( x == y )
  Enable();
else
  Disable();

// to
Enable( x == y ); // 4 lines are now 1

As per my example above, four lines got compressed into one line but nothing actually changed. The algorithm is identical but you now have fewer lines to think about and worry about. And all of this multiplies. If you make that change 4 times you've removed 12 lines of potential bugs and oversights.

Category: tech, Tags: cpp, programming, simple
Comments: 0
@ wrote... (10 years, 3 months ago)

I've finally gotten around to uploading a little project I did years ago. any_config is a template class that implements a somewhat fancy prototype pattern.

Read about at github.com.

Category: tech, Tags: boost, cpp, programming
Comments: 0
@ wrote... (10 years, 5 months ago)

Don't check a pointer before deleting it, just delete it.

// don't do this
if ( p ) delete p;

// just do this
delete p;

// ie: this doesn't crash or do anything bad
int* p = NULL;
delete p;

// but you probably need something like this
#define NUL_DEL( p ) { delete p; p = NULL; }
NUL_DEL(p)

But seriously, this isn't the dark ages, you really want to be doing this.

std::shared_ptr<int> p;
...
p.reset(); // no need to set to NULL (or rather null_ptr)
Category: tech, Tags: cpp, programming, stl
Comments: 0
@ wrote... (10 years, 6 months ago)

I'm not sure how else to name this entry, it's rather complicated. So what I was finally able to achieve is to pass a boost::bind to a boost::bind with placeholders, with templated callbacks. I'm not gonna lie, this took me awhile to figure out.

The rationale for all of this was that I had a parent class called group, and this group had the list of boost::asio::deadline_timer's and the boost::asio::io_service running in a thread. I wanted the ability for child objects to put work in that thread.

more…

Category: tech, Tags: boost, cpp, templates
Comments: 0
@ wrote... (10 years, 6 months ago)

Part of my Why is simple hard? series.

Do you even typedef bro?

more…

Category: tech, Tags: cpp, programming, simple
Comments: 0
@ wrote... (10 years, 7 months ago)

Part of my Why is simple hard? series.

Like most people with taste, I despise Hungarian notation. But, at the end of the day it really is only a style issue and easy enough to fix with a search & replace.

more…

Category: tech, Tags: cpp, programming, simple
Comments: 0
@ wrote... (11 years, 7 months ago)

Any Standard Template Library (STL) pro doesn't need to be told this but for somebody moving from MFC to the STL like me, the following would no doubt be handy.

The problem is that std::map creates a new object anytime you have map[key], even as an rvalue. So here is a super simple template function to quickly check if a map has a key.

template<typename T>
bool has_key( const T& map, typename const T::key_type& key )
{
    T::const_iterator iter = map.find( key );
    return iter != map.end();
}

// eg. has_key( my_map, "key" );

The tiny bit of magic here is the typename in typename const T::key_type&amp;. typename is required due to deep c++ voodoo that I really don't understand.

Or you could just…

map.count( key ) > 0;

Always learning.

Category: tech, Tags: cpp, stl
Comments: 0
@ wrote... (13 years, 2 months ago)

A static variable in a class method are shared between all instances of that class and not per class instance.

#include <stdio.h>

class Foo {
public:

    int foo() {
        static int i = 0;
        i++;
        return i;
    }
};

int main() {

    Foo a, b;

    printf( "a: %d\n", a.foo() );
    printf( "b: %d\n", b.foo() );
}
a: 1
b: 2
Category: tech, Tags: cpp, programming
Comments: 0
@ wrote... (13 years, 8 months ago)

I still don't know what eigenvalues are, what they're for, or how to use them, but I needed to calculate them for work and make sure they were positive.

more…

Category: tech, Tags: cpp, programming
Comments: 0