3/20/2014

Build yajl in windows

I've used yajl, which is very attractive library for json in C.
Although, in package including a doc, 'BUILDING.win32', when I followed the guide, I've got some error.

First of all, I have no experience about CMake, and 'CMakeLists.txt' for window build, seems to have a little mistake.

For solving, let's say the path of 'yajl' package is 'D:\yajl_2_0_4_test'.
(My environment is CMake 2.6 and VS2010 for visual studio command prompt.)
In 'D:\yajl_2_0_4_test\CMakeLists.txt', below code have to append on line 14,
cmake_minimum_required(VERSION 2.6)

SET (YAJL_MAJOR 2)
SET (YAJL_MINOR 0)
SET (YAJL_MICRO 4)
By adding above code, I could acquire such a nice build log!








3/14/2014

Build time speed-up in VC++ project

There are some ways to improve VC++ project compile time.

Firstly, through using #pragma once directive and #ifdef guard. Although, #pragma once is not standard, it can be recognized in MS VC++ compiler. Furthermore, it does not try to open itself, whereas #ifdef guard opens header file once, and then, if the symbol is defined, not load.
However, when I apply #pragma once in my project, I couldn't find distinct different result. Before applying #pragma once, I was using #ifdef guard only, the build time was approximately 3 minute more. After applying that, although the build time was shortened about several seconds, I'm not sure if there was any chance.

Second method is forwarding declaration. It means, if you just use pointer or reference of class as a class member in your header file, you don't need to include the header file which is declared the class.

And using 'impl' pattern is another well-known method, but if goal is just decreasing compile time, it may be cumbersome work in terms of additional writing code.

First of all, to turn of '/MP' switch is the most fastest and simplest method to improve build speed. '/MP' means 'build with multiple processes. In vs2010, the MP switch is turn off in default because it conflicts /Gm(Enable Minimum Rebuild).
When I turned on /MP switch, my project's compile time was shortened over 3 minutes to under one minute.

12/09/2013

Another method substitute for Initializer lists


Although, Initializer Lists was introduced in C++ 11, it was not applied to Visual Studio 2010.
So, I can't write below code, in VS2010.(and, may be same in VS2012)
vector<int> v = { 1, 2, 3, 4 };
So, I often used array rather than vector and non-member std::begin()/std::end(), since I prefer to use for_each, lambda, like below,
struct Actor
{
 string name;
 unsigned int age;
} actors[] = { {"joe", 19}, {"bach",24} };

for_each (std::begin(actors), std::end(actors), [](const Actor& a)
{
 //do something
 cout << ' ' << a.name;
 cout << ' ' << a.age;
});
Today, I read below article in StackOverFlow,
http://stackoverflow.com/questions/2236197/c-easiest-way-to-initialize-an-stl-vector-with-hardcoded-elements?rq=1

If I have to use only vector, this method will better solution, however, it will run for only primitive data type such as, int.
For user defined type, other method will be required.
Ref 1. http://en.cppreference.com/w/cpp/utility/initializer_list
Ref 2. http://oopscenities.net/2011/05/09/c0x-initializer-lists/


8/31/2013

Precautions in luainterface 2.0.3


For In visual studio 2010, below section should be added in app.config. Since luainterface was compiled as clr version 2.0, 'useLegacyV2RuntimeActivationPlicy' is must be set to true.
<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
</startup>


For reference a C# object in luainterface, the namespace of the reference object should be different with program main interface. For example, if I address the name of project as 'DrwaDesk', the main namespsce will be 'DrawDesk'. If I want to reference 'Book' class in luainterface, the 'Book' class should have different
namespace with 'DrawDesk'.
namespace DrawDesk
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
        }
    }
}
namespace ShredTypes
{
    class Book
    {
    }
}

Lastly, there is no sleep function in lua. So using os.time() or os.clock(), we normaly make sleep function. However, if the function call in several processes, it will throw exception. A possible solution for this, just wrap function which use the sleep function of C# and call this wrap function in Luainterface. 
 
 
 
 

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