A Developer's Diary

Aug 31, 2010

zthreads - building zthread dll using visual studio 2008

ZThreads is a cross platform object oriented library written in C++ which provides a clean and simple interface to both POSIX threads and Windows API threads.

Building ZThread shared library using Visual Studio 2008

1. Download ZThread source tar ball and extract it.
2. Create a new Win32Project using the Visual Studio installed templates
3. Go to Application Settings of Win32 Application Wizard
4. Select dll as the Application type and Empty project in the Additional options.
5. Add all the cxx files inside ZThread src folder into your project Source Files excluding the sub directories
6. Go to C/C++ tab in the Project Properties. Specify ZThread include directory in the Additional Include Directories
7. Change the default Character Set used by your Visual Studio project from Use Unicode Character Set to Not Set
8. Build the project

Read more ...

Aug 29, 2010

Mercurial - A short tutorial

Why Mercurial?

Mercurial can boast of really unique set of properties that make it a particular good choice as a distributed revision control system.
1. Easy to learn and safe to use
2. Lightweight
3. Delightful extensions and customization
4. Scalable
Download and install the Windows shell extension for Mercurial (TortoiseHg) from here

Creating a Repository

hg init main
where
main is the repository name

Mercurial reads configuration data from several files, if they exist. On Windows, it normally looks for %USERPROFILE%/mercurial.ini file or %USERPROFILE%/.hgrc file and in Unix based machines it looks for $HOME/.hgrc file. For detailed information type hg help config in the command line window or the terminal
Configuring Mercurial

Open %USERPROFILE%/mercurial.ini or %USERPROFILE%/.hgrc file and add following lines to it.

[ui]
username = Firstname Lastname <firstname.lastname@example.net>

This sets the username for the mercurial and this information is for reading by other users

Mercurial has inbuilt lightweight HTTP server that can be used to share the repository.
Serving the Repository

hg serve -p 80 -d
where
-p is used to specify a different port (default port 8000)
-d is used to run the server in the background

Creating a local copy of the Repository

hg clone http://host-name/
This creates a clone of the repository on the local machine

Read more ...

Jul 19, 2010

C++ Idioms - Non Copyable Objects

The Non Copyable idiom in C++ allows us to create classes whose objects cannot be constructed using a copy constructor or assigned to another. Some of the classes using this idiom provide mutually exclusive access to the resources e.g. Mutex, Semaphores, Document, Memory, File etc

Key Idea
To prevent objects of a class from being copy constructed or assigned to each other, declare the copy constructor and the assignment operator as private and do not provide implementations
//File: NonCopyable.h
#include <iostream>

class NonCopyable
{
protected:
    NonCopyable(){}
    ~NonCopyable(){}

private:
    //Restrict the copy constructor
    NonCopyable(const NonCopyable&);
    //Restrict the assignment operator
    NonCopyable& operator=(const NonCopyable&);
};

Read more ...

Jul 18, 2010

Static Member Functions

A static member function can be accessed using an instance variable or using the class name. It is better to use class name to access a static method as it is more readable and speaks by itself that the method being called is a static member

//File: static_example.h
#include <iostream>
using namespace std;

class StaticExample
{
    public:
        StaticExample();
       ~StaticExample();

        //static member function
        static int StaticMethod();

    private:
        StaticExample(const StaticExample&);
        StaticExample& operator=(const StaticExample&);

        std::string m_str;
};

//File: static_example.cpp
#include "static_example.h"

StaticExample::StaticExample()
{
}

StaticExample::~StaticExample()
{
}

int StaticExample::StaticMethod()
{
    cout << "static method called";
    cout << endl;
    return 0;
}

Read more ...

Jul 17, 2010

Conditional execution in ant

An ant target can be configured to execute conditionally using if and unless attributes available.
A simple ant target without any attributes

<project name="examples" default="simple_target_example">

  <target name="simple_target_example">
    <echo message="a simple target example"/>
  </target>

</project>
Output
$ ant
Buildfile: build.xml

simple_target_example:
     [echo] a simple target example

BUILD SUCCESSFUL
Total time: 0 seconds

Ant target with an if attribute
<project name="examples" default="example_if_target">

  <target name="property_target">
    <property name="greetings" value="1"/>
  </target>

  <target name="example_if_target" depends="property_target" if="greetings">
    <echo message="Greetings"/>
  </target>

</project>
Output
$ ant
Buildfile: build.xml

property_target:

example_if_target:
[echo] Greetings

BUILD SUCCESSFUL
Total time: 0 seconds

The if="greetings" attribute causes the target to be executed if greetings property is set and the unless="greetings" attribute causes the target to be executed only if the property greetings is not set

Ant target with an unless attribute
<project name="examples" default="example_unless_target">

  <target name="example_unless_target" unless="greetings">
    <echo message="Greetings"/>
  </target>

</project>
Output
$ ant
Buildfile: build.xml

example_unless_target:
[echo] Greetings

BUILD SUCCESSFUL
Total time: 0 seconds


//setting the greetings property
Output
$ ant -Dgreetings=1
Buildfile: build.xml
 

example_unless_target:
BUILD SUCCESSFUL
Total time: 0 seconds

Read more ...