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