C++ - Prevent an object by being copied with boost::noncopyable

22. August 2013 07:00

 

Sometimes in C++ it can be an advantage for making a class noncopyable for various reasons. This can be acomplished by inherting a class from the boost libraries. Which is called boost::noncopyable. This will force the classes copy constructors to be private.

 

 

class MyClass : private boost::noncopyable
{
	public:
		MyClass() { }
};

 

By doing this. It will prevent the following from being able to compile because a copy is passed to the function. A reference should be passed instead. This will also prevent operations such as "a = b".

 

 

void func(MyClass a)
{

}

int main(int argc, char **argv)
{
	MyClass a, b;
	
	func(a);

	return 0;
}

 

 

Some advantages of using this include.

 

  • Boost::noncopyable prevents the classes methods from accidentally using the private copy constructor
  • It is more explicit and descriptive in the intent. Using private copy functions is an idiom that takes longer to spot than noncopyable.
  • It is less code / less typing / less clutter / less room for error (the easiest would be accidentally providing an implementation).
  • You don't have to write your own private copy constructor.
  • Attempting to copy will catch issues sooner. eg at compile time.

 

One disadvantage is that there is a small overheat of the class inheirtance.

E-mail Kick it! DZone it! del.icio.us Permalink


Bash - Upgrading lots of git repos.

16. August 2013 19:33

 

Here is a nice example of how to execute a git command on multiple git repositories one after another. Assuming that they are all in the same directory.

 

#!/bin/bash

for i in * ; do
	if [ -d $i ] ; then
		if [ -d $i/.git ] ; then
			echo Updating $i ....
			(cd $i ; git pull)
			echo
		fi
	fi
done

E-mail Kick it! DZone it! del.icio.us Permalink


Pyton - How to find out if a key exists in a dictionary

9. April 2013 08:00

 

Another simple python example which is how to find out if a key exists in python

 

Just use "if" and "in" like in the following example

 

 

>>> x['item1'] = 0
>>> if 'item1' in x:
...  print "True"
...
True

 

 

Some of the mistakes that I have seen is this will "appear" to work correctly but in fact it does not. Have a look at these examples. You can see here that it will fail because it is testing the value rather than the key.

 

 

>>> x['item1'] = 1
>>> if x['item1']:
...  print "True"
...
True
>>>

>>> x['item1'] = 0
>>> if x['item1']:
...  print "True"
...
>>>
E-mail Kick it! DZone it! del.icio.us Permalink


Python - json encode / decode

4. April 2013 08:00

 

This is a short example to be able to encode / decode json inside python. Its actually really easy to do.

You will first need to import the json library using

import json

 

Encoding to json

 

>>> x = {}
>>> x['1'] = 0
>>> x['2'] = 0
>>> x['3'] = 0
>>> x['4'] = 0
>>> json.dumps(x)

 

Deocoding from json

 

>>> x = json.loads('{"1": 0, "2": 0, "3": 0, "4": 0}')
>>> print x
{u'1': 0, u'3': 0, u'2': 0, u'4': 0}
>>>

 

It really is as simple as that!

E-mail Kick it! DZone it! del.icio.us Permalink


C++ - std::vector add, insert, remove, process, erase, swap, sort, clear

2. April 2013 08:00

 

Here is some examples of how to use std::vector with string for most of the common operations that can be performed on std::vector.

 

The following example assume the following has been declared

 

 

std::vector<std::string> vec;

 

 

Adding Items

vec.push_back("Item 1");
vec.push_back("Item 2");
vec.push_back("Item 3");
vec.push_back("Item 4");
vec.push_back("Item 5");

 

Adding Items to front

vec.insert(vec.begin(), "Front 2");
vec.insert(vec.begin(), "Front 1");

 

Remove Last Item

 

vec.pop_back();

 

 

Remove Last Item

 

vec.erase(vec.end());

 

 

Remove First Item

 

vec.erase(vec.begin());

 

 

Process All Items

for(std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); it++)
{
  printf("%s\n", it->c_str());
}

 

Process All Items

for(size_t i = 0; i < vec.size(); i++)
{
  printf("%s\n", vec[i].c_str());
}

 

Reverse The Order - Note you can also just run the above backwards instead.

for(size_t i = 0; i < vec.size() / 2; i++)
{
  std::string tmp = vec[i];
  vec[i] = vec[vec.size() - i - 1];
  vec[vec.size() - i - 1] = tmp;
}

 

Sorting

std::sort(vec.begin(), vec.end());

 

Find and Remove an item

for(std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); it++)
{
  if (*it == "Item 2")
  {
    vec.erase(it);
    break;	//it is now invalud must break!
  }
}

 

Clear All Items

vec.clear();
E-mail Kick it! DZone it! del.icio.us Permalink