2011年5月19日 星期四

cweb_grid

class cweb_grid {

    var $column_obj; // 欄位
    var $item_arr; // 項目列
    var $param; // 參數列

    function cweb_grid()
    {

        $this->column_obj = new empty_object;
        $this->item_arr = array();
        $this->param = array();
        
    }


    function _pre_html()
    {
        $param_str = "";
        foreach($this->param as $key => $value )
        {
            $param_str .= " {$key}=\"{$value}\" ";
        }

        $result = "<table " . $param_str . " >";

        return $result;

    }

    function _after_html()
    {
        return "</table>";
    }

    function _arr2obj($row)
    {
        return $row;
    }

    function _proc_row($row)
    {
        $result = "";
        foreach($this->column_obj as $key => $value)
        {
            if( !isset($row->$key) )
                $result .= "<td ></td>";
            else
                $result .= "<td >{$row->$key}</td>";
        }

        $result = "<tr> {$result} </tr>";

        return $result;

    }

    function _proc_column()
    {
        return $this->_proc_row($this->column_obj);

    }

    function _proc_item()
    {

        $result = "";
        foreach($this->item_arr as $item)
        {
            $result .= $this->_proc_row($item);
        }

        return $result;
    }

    function insert_column($key, $text)
    {
        $this->column_obj->$key = $text;
    }

    function insert_item($row)
    {

        $obj = $this->_arr2obj($row);
        $this->item_arr[] = $obj;

    }

    function add_param($key, $value)
    {
        $this->param[$key] = $value;
    }

    function outHtml()
    {
        $result = "";
        
        $result .= $this->_pre_html();

        $result .= $this->_proc_column();

        $result .= $this->_proc_item();

        $result .= $this->_after_html();

        return $result;
    }

}

2011年5月18日 星期三

cweb_link

class cweb_link
{
    var $id;
    var $url;
    var $text;
    var $display;
    function cweb_link($id = "")
    {
        $this->id = $id;
        $this->url = null;
        $this->text = null;
        $this->display = null;
    }

    function insert($url=null,$text=null,$display=true)
    {
        if($url == null || $text == null)
            return;
        $this->url = $url;
        $this->text = $text;
        $this->display = $display;
    }

    function outHtml()
    {
        if($this->display==false)
            return "";

        $html = "<a id='{$this->id}' href='{$this->url}'>{$this->text}</a>";
        if($this->id == "")
            $html = "<a href='{$this->url}'>{$this->text}</a>";
        
        return $html;
    }
}

2011年5月17日 星期二

cweb_select

class cweb_select {
    //put your code here

    var $name;
    var $option;
    var $selected;

    function cweb_select($name = "")
    {
        $this->option = array();
        $this->selected = null;
        $this->name = $name;
    }

    function insert($row /*array or object*/ )
    {
        foreach ($row as $key => $value )
        {
            $this->insertItem($key, $value);
        }
    }

    function insert_only_value($row /*array or object*/ )
    {
        foreach ($row as $value )
        {
            $this->insertItem($value, $value);
        }
    }

    function insertItem($value , $text)
    {
        $this->option[$value] = $text;
    }

    function selected($value)
    {
        $this->selected = $value;
    }

    function getText($value)
    {
        return $this->option[$value];
    }

    function outHtml()
    {
        $result = "";


        foreach( $this->option as $value => $text )
        {
            $selected = "";
            if( $this->selected == $value)
                $selected = " selected";

            $result .= "<option value='{$value}'{$selected}>{$text}</option>";
        }

        $result = "<select name='{$this->name}' >{$result}</select>";

        return $result;
    }
}


2010年5月31日 星期一

Non-Virtual Interface (NVI)

class IuGUI : public IuUnknown
{

public:
    void OnDraw(HDC hDC)
    {
        this->OnOwnerDraw(hDC);
    }

private:
    virtual void OnOwnerDraw(HDC hDC) = 0;

};

class CuGUI :
    public IuGUI
{
public:
    CuGUI(void);
    ~CuGUI(void);

    virtual ULONG Release() 
    { 
        delete this;
        return 0;
    }

private:
    virtual void OnOwnerDraw(HDC hDC);
};

2010年5月24日 星期一

how to using CriticalSection

struct CParam
{
    BOOL bActive;
    int nCount;
    CCriticalSection CriticalSection;

    CParam();
};

CParam::CParam()
:bActive(TRUE)
,nCount(0)
{

}

DWORD WINAPI MyThread(LPVOID lpParameter)
{
    CParam *pParam;
    pParam = (CParam*)lpParameter;
    pParam->bActive = TRUE;

    while(1)
    {
        pParam->CriticalSection.Enter();
        ++pParam->nCount;
        pParam->CriticalSection.Leave();
        Sleep(1);
    }

    pParam->bActive = FALSE;

    ExitThread(0);
}



void InitThread()
{
    HANDLE hThread;
    DWORD ThreadId;

    CParam param;
    hThread = CreateThread(NULL, 0, 
        (LPTHREAD_START_ROUTINE)MyThread, (void*)¶m, 0, &ThreadId);

    while(param.bActive == TRUE)
    {
        param.CriticalSection.Enter();
        Sleep(100);
        cout << param.nCount << "\n";
        param.CriticalSection.Leave();
        
    }

    CloseHandle(hThread);
}


int _tmain(int argc, _TCHAR* argv[])
{
    InitThread();
    return 0;
}

define SaveToSQL interface

class ISaveToSQL {
    function GetField()
    {
        echo "hi... go back to study PHP 3 year!";
    }
}
class cucustom extends ISaveToSQL{
    ....

    function GetField()
    {
        $this->field["name"] = $this->name;
        $this->field["tel"] = $this->tel;
        $this->field["address"] = $this->address;

        return $this->field;
    }
}
class CuModelSave extends model
{
    function insertSaveToSQL(/*ISaveToSQL*/ $ISaveToSQL)
    {
        $this->db->insert($this->table_name, $ISaveToSQL->GetField());
    }
}
class custom_sql extends CuModelSave{
    ...
}

2010年5月16日 星期日

Prototype Pattern -- Load User Define Library

function testUnit_CreateCustom()
    {
        $Custom = $this->cucustom->MyClone();
        echo $this->unit->run($Custom != NULL, "is_TRUE", "Create Custom");
        
        if( $Custom == NULL )
            return;
        
        $Custom->Init("Eric", "1234567", "高興路");
        echo $this->unit->run($Custom->name, "Eric", "name");
        echo $this->unit->run($Custom->tel, "1234567", "tel");
        echo $this->unit->run($Custom->address, "高興路", "address");
        
    }

class cucustom {

    var $name;
    var $tel;
    var $address;

    function cucustom($name = "", $tel = "", $address = "")
    {
        $this->Init($name, $tel, $address);
    }

    function MyClone()
    {
        return new cucustom;
    }

    function Init($name = "", $tel = "", $address = "")
    {
        $this->name = $name;
        $this->tel = $tel;
        $this->address = $address;
    }
}