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;
    }
}

2010年5月12日 星期三

Create General Object From DLL.

// the GUI is a GUI.DLL

// GUI.h


typedef IuGUI* (*LPFN_CREATEGUI)(void);

GUI_API IuGUI* CreateObject(void);
GUI_API IuGUI* CreateObject(void)
{
    return new CuGUI;
}

// CuGUI

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

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

    virtual void OnDraw(HDC hDC);
};

// CGUILoadTest

void CGUILoadTest::testDllCreateObject(void)
{

    LPFN_CREATEGUI lpfnCreateGUI = NULL;

    CuDllManager dllGUI;
    BOOL bResult = dllGUI.LoadLibrary(TEXT("GUI.dll"));
    CPPUNIT_ASSERT( bResult != NULL );

    lpfnCreateGUI = (LPFN_CREATEGUI)dllGUI.GetProcAddress(("CreateObject"));
    CPPUNIT_ASSERT( lpfnCreateGUI != NULL );

    IuGUI *pIuGUI = NULL;

    if( lpfnCreateGUI != NULL )
        pIuGUI = lpfnCreateGUI();

    CPPUNIT_ASSERT( pIuGUI != NULL );

    if( pIuGUI != NULL )
        pIuGUI->Release();

}

2010年5月2日 星期日

filedrive - file upload download

class filedrive extends controller{

    function filedrive()
    {
        parent::controller();
        $this->load->model('filedrive_model','FileDrive',true);
        $this->load->library("cudownload");
        $this->load->library("cuupload");
        $this->culogin->onlogin();
    }

    function index($id="")
    {
        $data["downloadlist"] = "";
        if (empty($id))
                $query = $this->FileDrive->file_query();
        else
                $query = $this->FileDrive->tags_like($id);

        foreach($query->result() as $row){
                $filelist["row"] = $row;
                $data["downloadlist"] .= $this->load->view("downloadlist",$filelist,true);
        }
        $dnTable = $this->load->view("downloadtable",$data,true);
        $viewData["body"] = $dnTable;
        $this->_MainFrame($viewData);
    }

    function upload_form(){
        $tags = $this->FileDrive->get_tags();
        $upForm = array("error"=>"");
        $upForm["active_url"] = "filedrive/upload";
        $upLoadform = $this->cuciload->view("upload_form",$upForm,true);
        $tags = $this->cuciload->view("tags",array("tags"=>$tags,),true);
        $backpage = $this->cuciload->view("backpage",array("goback" => "filedrive"),true);
        $viewData["body"] = $backpage.$upLoadform.$tags;
        $this->_MainFrame($viewData);
    }

    function update_form($id){
        $data["row"] = $this->FileDrive->select_file($id);
        $viewData["body"] = $this->load->view("update_form",$data,true).$this->file_tags();
        $this->_MainFrame($viewData);
    }

    function delete_form($id){
        $data["row"] = $this->FileDrive->select_file($id);
        $viewData["body"] = $this->load->view("delete_form",$data,true);
        $this->_MainFrame($viewData);
    }
    
    function file_tags(){
        $tags = $this->FileDrive->get_tags();
        return $this->cuciload->view("tags",array("tags"=>$tags,),true);
    }

    function update($id){
        $this->FileDrive->update($id);
        redirect("filedrive","location");
    }

    function delete($id){
        $row = $this->FileDrive->select_file($id);
        $this->FileDrive->delete_file($id);
        unlink("./uploads/".$row->file_name.$row->file_ext);
        redirect("filedrive","location");
    }


    function IsUpload($result){
        $data = $this->upload->data();
        $data["true_name"] = $result["true_name"];
        $data["file_date"] = date("Y-m-d");
        $data["file_time"] = date("H:i:s");
        $data["file_tag"] = $this->FileDrive->write_sql($data);
        $data["is_img"]= lang("NO")."</li>";
        if($data["is_image"])
        {
            $data["is_img"] = $this->load->view("is_image",$data,true);
        }

        return $data;
    }

    function Upload()
    {
        if ( empty($_FILES["userfile"]["name"]) )
                redirect("filedrive/upload_form", "location");
        $result = $this->FileDrive->filename();

        $this->cuupload->InitUploadLib_($result["file_name"]);

        if ( ! $this->upload->do_upload())
        {
            $data["body"] = $this->upload->display_errors()."<br>".anchor("filedrive","return file upload!");
        }
        else
        {
            $susess_data = $this->IsUpload($result);
            redirect("filedrive","location");
        }
        $this->_MainFrame($data);
    }

    function _MainFrame($frame)
    {
        $data = $this->_Tag_List();
        $frame["leftmenu"] = $this->load->view("leftmenu",$data,true);
        $frame["submenu"] = $this->load->view("submenu","",true);
        $this->csmainframe->view($frame);
    }

    function _Tag_List(){
        $tags = array();
        $query =$this->FileDrive->get_tags();

        foreach($query->result() as $row){
                $number = $this->FileDrive->count_tags_like($row->id);
                $tags[] = anchor("filedrive/index/".$row->id, $row->tags . " ( ".$number ." ) ");
        }

        $query_all = $this->FileDrive->tags_like("");
        $data["all_num"] = $query_all->num_rows();

        $notags_num = $this->FileDrive->tags_like("null");
        $data["notags_num"] = $notags_num->num_rows();
        $data["tag"] = $tags;
        return $data;
    }

    function download($id){
        $result = $this->FileDrive->download($id);
        $this->cudownload->DownLoadEx($result, "./uploads/");
    }

}