7/17/2013

Using Google C++ Style Guide


Probably, when many people write C++ code for their own project at home, they don't seem like to follow specific guide line. So they write code according to habit or experience. However, as the length of the code is getting longer, various styles are appeared.This is why many developers try to follow Google C++ Style Guide consiciously.

cpplint.py is a python file for identifying whether follow Google C++ Style Guide. For using, just install Python 2.7(2013-07-17 currently, it doesn't work in higher version.)

The direction is simple.

Also, you can use filter option for reducing unimportant style checking.

7/07/2013

Factory method pattern

This pattern is used with polymorphism and template which are representative feature of C++ language. The main advantage of factory method is that when making new object or extending previous object, define new sublayer class and then just overriding member function which is defined as a factory method. Therefore, we don't need to analyze existing source code detaily.

Here is a sample class diagram using Factory method pattern.

Implementation code is below.
sample.h
#include <map>

using namespace std;

typedef struct _controlInfo
{
    unsigned int ctrlId;
    struct _positionInfo
    {
        unsigned int x;
        unsigned int y;
    }Position;
    struct _sizeInfo
    {
        unsigned int width;
        unsigned int height;
    }Size;

}ControlInfo;

class CControlBase
{
public:
    virtual bool Create(const ControlInfo* info) = 0;
};

class CButton : public CControlBase
{
public :
    bool Create(const ControlInfo* info)
    {
        //Make Button
        //...
        //...

        return true;
    }
};

class ControlMaker
{
public :
    void Make(const ControlInfo* info)
    {
        CControlBase *base = CreateControl();
        base->Create(info);
        ctrlMap[info->ctrlId] = base;
    }

protected :
    virtual CControlBase* CreateControl() = 0;

private :
    map<unsigned int, CControlBase *>ctrlMap;
};

template <class ctrlType>
class ConcreteControlMaker : public ControlMaker
{
protected :
    CControlBase* CreateControl() { return new ctrlType; }
};
 

sample.cpp
//
#include "stdafx.h"
#include "Sample.h"

int _tmain(int argc, _TCHAR* argv[])
{  
    //get contrl info by using xml or ini
    ControlInfo* info = NULL;
    GetControlInfo(info);
   
    //if need to make a button
    ConcreteControlMaker<CButton> btn;
    btn.Make(info);
   
    return 0;
}
 
 
Because this pattern delegates sublayer classes to create object, therefore, factory method pattern has advantages when need to divide the responsibility of object creation.

7/06/2013

[VC++]How can I expand or collapse when click item in tree control?

The simplest way is to set 'single expand' style on tree ctrl. Without 'single expand', if you just use Expand() function, you will not receive notify such as TVN_ITEMEXPANDED, TVN_ITEMEXPANDING.

However, setting 'single expand' style has also a drawback. It is that expanded item once will be collapsed again when it is unselected. This behaviour is different with usual applications' behaviour. Therefore, if remaining unselected item, you should write code like below.

void CExpandTestDlg::OnItemexpandingTree(NMHDR* pNMHDR, LRESULT* pResult)
{
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    // TODO: Add your control notification handler code here

    //Getting a current item
    HTREEITEM hCurItem = m_Tree.GetSelectedItem();

    //Comparing to a received item and then,
    //if the received item is not same,
    //just return for protecting notify TVN_ITEMEXPANDED
    if(hCurItem != pNMTreeView->itemNew.hItem)
    {
        *pResult = 1;
        return;
    }

    *pResult = 0;
}
 

reference from MSDN


6/18/2013

Catch the boomerang(for attending 'Intel perceptual computing challenge')

Recently, I'm preparing to enter Intel perceptual computing challenge. One of my ideas is a simple gesture game. It's called 'Catch The Boomerang'. Some brief sketches are below.

Users can throw the boomerang by rotating their wrist. Distance of flying, speed and the count of caught boomerang are displayed on the game screen.

Of course, the thrown boomerang will be returned, so that users should catch the returning boomerang by grasping gesture.
I hope to get an opportunity which can attend next stage.

6/02/2013

When computer is locked in XP, proceeding redrawing.

During the time that computer is locked in XP, if application's size is altered or do other drawing work, these works will not reflect after the computer is unlocked. Although, it will be changed in Vista or Win 7.

In normal case, it will be solved by managing WM_ACTIVEAPP, but during the time, through executing other program + altering size + redrawing, if z-order is changed, unfortunately other program will be received WM_ACTIVEAPP.

Therefore, the most effective soluting is to manage WM_WCSSESSION_CHANGE.
After checking the WPARAM value of the message is WTS_SESSION_UNLOCK,  doing redraw will be solved this issue.

5/07/2013

Rule for using c++ 0x auto?

'auto' is one of popular keywords in c++ 0x. Now, one of envy lists, which we feel when we see c# or such as script languages, can be reduced. The most typical example of using is when declare 'iterator' in STL.
//when declared typical iter
for (map&ltstring, int>::const_iterator iter = MapScores.begin(); iter != MapScores.end(); ++iter)
{
    cout << "Name = " << iter->first << ", Score = " << iter->second << endl;
}
 
//when used auto
for (auto iter = MapScores.begin(); iter != MapScores.end(); ++iter)
{
    cout << "Name = " << iter->first << ", Score = " << iter->second << endl;
}
(In first one, instead of 'const_iterator', using of cbegin(), cend() is also fine for const appliance. cbegin(), cend() are also added containers of c++ 0x.)

The problem is that lots of c++ developers are already used for one rule, which type of variables always have to be declared. Therefore, inordinate using of 'auto' might bring about confusing and annoying. Furthermore, because of operation through misunderstanding, proportion of debugging time may be longer.

So, I make a rule for me.

  1. when immediately know 'return type' like above code and such as 'for', 'while' loop, when use local variable which available in short scope only.
  2. As much as possible, use 'const auto'. So that, if possible, it uses for 'read only'. I think it can aid to protect wrong operation by means of misunderstanding.

Below link is a stackoverflow thread which discussed for 'guideline using auto'.

http://stackoverflow.com/questions/6434971/how-much-is-too-much-with-c0x-auto-keyword
Personally, I believe that the use of at least is better way.

5/06/2013

The best way to appear dialog invisibly when dialog is created

For this, 'WM_WINDOWPOSCHANGING' is used.

If we use WM_CREATE or WM_INITDIALOG and then, call ShowWindow(SW_HIDE),
we will firstly see a dialog, after that, it will be disappear.

Below code shows how to use WM_WINDOWPOSCHANGING in WTL.

LRESULT CHideDialog::OnWindowPosChanging(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
 LPWINDOWPOS lpwndpos = (LPWINDOWPOS)lParam;
 if (FALSE == m_isVisible)
 {
  lpwndpos->flags &= ~SWP_SHOWWINDOW;
 }
 else
 {
  lpwndpos->flags |= SWP_SHOWWINDOW;
 }
 return 0;
}

As we see, the value of  m_isVisible decides on whether dialog is visible.
So, if we call below function, after setting the value of m_isVisible to TRUE,
the dialog will be appear again.

::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);