cppapp.cpp
资源名称:cppapp.zip [点击查看]
上传用户:smh666999
上传日期:2007-01-14
资源大小:553k
文件大小:12k
源码类别:
BREW编程
开发平台:
Visual C++
- #include "cppapp.h"
- #include "cppapp.bid"
- #include <stdio.h>
- #include <stdlib.h>
- //-------------------------------------------------------------------------
- // This application serves as a sample C++ application. The C++ capabilities
- // being demonstrated are inheritance, operator overloading, function overloading,
- // new/delete overloading and polymorphism.
- // CPPApp is the applet class that derives from AEEApplet. This class uses
- // two helper classes, Writer and SmartWriter to display messages on device
- // screen.
- // Writer class writes messages on device screen using ITextCtl. SmartWriter
- // extends Writer class and writes messages in bold using IStatic. Writer
- // class provides WriteLine() method for writiting any message to device display.
- // This method is specialised by derived class SmartWriter to write message
- // in bold. Writer class has overloaded DisplayOutput() methods to support
- // WriteLine() methods of base as well as derived class.
- // Both of the classes have overloaded new, delete and stream out operators.
- //-------------------------------------------------------------------------
- // App specific constants
- #define TEST1 100
- #define TEST2 101
- #define TEST3 102
- #define TEST4 103
- #define TEST5 104
- #define TEST6 105
- #define TEST7 106
- #define APP_RES_FILE "cppapp.bar"
- //--------------------------------------------------------------------------
- // All static functions of CPPApp class.
- //--------------------------------------------------------------------------
- extern "C"
- int AEEClsCreateInstance(AEECLSID clsID, IShell* pIShell, IModule* pIModule, void **ppobj)
- {
- if(clsID == AEECLSID_CPPAPP)
- {
- // We want to load this App. So, invoke AEEApplet_New().To it, pass the
- // address of the app-specific handle event function.
- if(!AEEApplet_New(sizeof(CPPApp), clsID, pIShell, pIModule, (IApplet**)ppobj,(AEEHANDLER)CPPApp::HandleEvent,
- (PFNFREEAPPDATA)CPPApp::freeAppData))
- return EFAILED;
- // Allocate and initialize app specific data
- if (!CPPApp::InitAppData((IApplet *)*ppobj))
- return EFAILED;
- return SUCCESS;
- }
- return EFAILED;
- }
- boolean CPPApp::InitAppData(IApplet *pIApplet)
- {
- CPPApp* pCPPApp = (CPPApp*)pIApplet;
- return pCPPApp->OnAppInitData();
- }
- void CPPApp::freeAppData(CPPApp *pCPPApp)
- {
- pCPPApp->OnAppfreeData();
- }
- boolean CPPApp::HandleEvent(CPPApp *pCPPApp, AEEEvent eCode, uint16 wParam, uint32 dwParam)
- {
- return pCPPApp->OnEvent(eCode, wParam, dwParam);
- }
- //--------------------------------------------------------------------------
- // All member functions of CPPApp class.
- //--------------------------------------------------------------------------
- boolean CPPApp::OnAppInitData()
- {
- m_pIMenuCtl = NULL;
- rr_ = new ResourceRegistry(3);
- return TRUE;
- }
- void CPPApp::OnAppfreeData()
- {
- if (m_pIMenuCtl)
- IMENUCTL_Release(m_pIMenuCtl);
- delete rr_;
- }
- boolean CPPApp::OnEvent(AEEEvent eCode, uint16 wParam, uint32 dwParam)
- {
- switch (eCode)
- {
- case EVT_APP_START:
- // When this applet is started, try to create the main menu
- // that allows the user to select a usage example to execute.
- // If we cannot create an instance of the menu class, exit and
- // indicate that we have handled the event by returning TRUE
- if(ISHELL_CreateInstance(m_pIShell, AEECLSID_MENUCTL,
- (void **)&m_pIMenuCtl) != SUCCESS)
- return TRUE;
- // Succeeded in obtaining a menu instance pointer, so
- // construct the menu and display it on the screen
- BuildMainMenu();
- return(TRUE);
- case EVT_KEY:
- IMENUCTL_SetActive(m_pIMenuCtl, TRUE);
- if (wParam == AVK_CLR)
- return TRUE;
- else
- return IMENUCTL_HandleEvent(m_pIMenuCtl, EVT_KEY, wParam, dwParam);
- case EVT_COMMAND:
- switch(wParam)
- {
- // The following commands are generated by user selections
- // from the main usage app menu.
- case TEST1:
- case TEST2:
- case TEST3:
- case TEST4:
- case TEST5:
- case TEST6:
- case TEST7:
- IMENUCTL_SetActive(m_pIMenuCtl, FALSE);
- // Execute the usage example the user has selected
- CPPUsage (wParam);
- return TRUE;
- default:
- return FALSE;
- }
- case EVT_APP_STOP:
- return(TRUE);
- default:
- return(FALSE);
- }
- }
- bool CPPApp::CPPUsage(uint16 wParam)
- {
- switch (wParam)
- {
- case TEST1:
- {
- test1();
- break;
- }
- case TEST2:
- {
- test2();
- break;
- }
- case TEST3:
- {
- test3();
- break;
- }
- case TEST4:
- {
- test4();
- break;
- }
- default:
- return FALSE;
- }
- return TRUE;
- }
- /*===========================================================================
- FUNCTION: BuildMainMenu
- DESCRIPTION
- This function constructs the menu control that allows the user to
- select a usage example to execute. The command ID for each menu item
- is passed to the usage example function above when the user selects the
- menu item for that example.
- PROTOTYPE:
- void CPPApp::BuildMainMenu()
- PARAMETERS:
- None
- DEPENDENCIES
- None
- RETURN VALUE
- None
- SIDE EFFECTS
- None
- ===========================================================================*/
- void CPPApp::BuildMainMenu()
- {
- AEERect qrc;
- AEEDeviceInfo di;
- AECHAR* szBuf;
- char* charBuf;
- // Make sure the pointers we'll be using are valid
- if (m_pIShell == NULL || m_pIMenuCtl == NULL)
- return;
- if ((szBuf = (AECHAR *) MALLOC(TEXT_BUFFER_SIZE)) == NULL)
- return;
- if ((charBuf = (char *) MALLOC(TEXT_BUFFER_SIZE/sizeof(AECHAR))) == NULL)
- {
- FREE(szBuf);
- return;
- }
- // Set Title
- STRCPY(charBuf, "C++ Examples");
- STR_TO_WSTR(charBuf, szBuf, TEXT_BUFFER_SIZE);
- IMENUCTL_SetTitle(m_pIMenuCtl, NULL, 0, szBuf);
- //Set the Rectangle for the Menu
- ISHELL_GetDeviceInfo(m_pIShell,&di);
- qrc.x = 0;
- qrc.y = 0;
- qrc.dx = di.cxScreen;
- qrc.dy = di.cyScreen;
- IMENUCTL_SetRect(m_pIMenuCtl, &qrc);
- //Add individual entries to the Menu
- // Add Base Class Usage to menu
- STRCPY(charBuf, "1. Http Test");
- STR_TO_WSTR(charBuf, szBuf, TEXT_BUFFER_SIZE);
- IMENUCTL_AddItem(m_pIMenuCtl, 0, 0, TEST1, szBuf, 0);
- STRCPY(charBuf, "2. File Test");
- STR_TO_WSTR(charBuf, szBuf, TEXT_BUFFER_SIZE);
- IMENUCTL_AddItem(m_pIMenuCtl, 0, 0, TEST2, szBuf, 0);
- STRCPY(charBuf, "3. File Test");
- STR_TO_WSTR(charBuf, szBuf, TEXT_BUFFER_SIZE);
- IMENUCTL_AddItem(m_pIMenuCtl, 0, 0, TEST3, szBuf, 0);
- STRCPY(charBuf, "4. Generic Resource");
- STR_TO_WSTR(charBuf, szBuf, TEXT_BUFFER_SIZE);
- IMENUCTL_AddItem(m_pIMenuCtl, 0, 0, TEST4, szBuf, 0);
- // Activate menu
- IMENUCTL_SetActive(m_pIMenuCtl,TRUE);
- FREE(szBuf);
- FREE(charBuf);
- }
- template <class Y, class S>
- struct pkAEECBK_Reg : public pkAEECBK<Y>, IRelease
- {
- pkAEECBK_Reg( )
- {}
- virtual void releaseResource( )
- {
- delete this;
- }
- virtual int getUID( )
- {
- return 200+ S::getID();
- }
- static int getID()
- {
- return 200 + S::getID();
- }
- private:
- pkAEECBK_Reg( const pkAEECBK_Reg &Value );
- const pkAEECBK_Reg &operator = ( const pkAEECBK_Reg &Rhs );
- };
- void CPPApp::test1()
- {
- String s2("Weendedupwithasimpleplug-inFSMcomponentthatisportableacrossmanyplatformsandisoptimizedforexecutionspeed.Gettingridofuglylookingnestedswitchstatementsgreatlyimprovesthedesignofstate-machinebasedprograms.Thebasiccomponentreliesonathird-partylistclasstoimplementtheeventqueue.YoucanuseanycommerciallistimplementationortheSTLqueuetemplateWeendedupwithasimpleplug-inFSMcompon
- entthatisportableacrossmanyplatformsandisoptimizedforexecutionspeed.Gettingridofuglylookingnestedswitchstatementsgreatlyimprovesthedesignofstate-machinebasedprograms.Thebas
- iccomponentreliesonathird-partylistclasstoimplementtheeventqueue.YoucanuseanycommerciallistimplementationortheSTLqueuetemplateWeendedupwithasimpleplug-inFSMcomponentthatisportableacrossmanyplatformsandisoptimizedforexecutionspeed.Gettingridofuglylookingnestedswitchstatementsgreatlyimprovesthedesignofstate-machinebasedprograms.Theb
- asiccomponentreliesonathird-partylistclasstoimplementtheeventqueue.YoucanuseanycommerciallistimplementationortheSTLqueuetemplateWeendedupwithasimpleplug-inFSMcomponentthatisportableacrossmanyplatformsandisoptimizedforexecutionspeed.Gettingridofuglylookingnestedswitchstatementsgreatlyimprovesthedesignofstate-machinebasedprograms.Thebasiccomponentreliesonathird-partylistclasstoimplementtheeventqueue.Yo
- ucanuseanycommerciallistimplementationortheSTLqueuetemplateWeendedupwithasimpleplug-inFSMcomponentthatisportableacrossmanyplatformsandisoptimizedforexecutionspeed.Gettingridofuglylookingnestedswitchstatementsgreatlyimprovesthedesignofstate-machinebasedprograms.Thebasiccomponentreliesonathird-partylistclasstoimplementtheeventq
- ueue.YoucanuseanycommerciallistimplementationortheSTLqueuetemplateWeendedupwithasimpleplug-inFSMcompon
- entthatisportableacrossmanyplatformsandisoptimizedforexecutionspeed.Gettingridofuglylookingnestedswitchstatements
- greatlyimprov");
- String s;
- s.ensureCapacity(5 * s2.length() + 12);
- s+="/a.htm?a=";
- for (int i = 0; i <5; i++)
- {
- s += s2;
- }
- initIO(&CPPApp::onWriteCbk,"bubumic", String("abcdefgh"), Type2Type<FileStream>() , Type2Type<FDS>());
- initIO(&CPPApp::onReadHttpCbk, "192.168.10.100", String("/a.htm"), Type2Type<HTTPpipe>() , Type2Type<HDS>());
- }
- void CPPApp::test2()
- {
- initIO(&CPPApp::onWriteCbk,"bubu", String("1234567890"), Type2Type<FileStream>() , Type2Type<FDS>());
- }
- void CPPApp::test3()
- {
- initIO(&CPPApp::onWriteCbk,"bubumic", String("WWWW"), Type2Type<FileStream>() , Type2Type<FDS>());
- }
- void CPPApp::test4()
- {
- initIO(&CPPApp::onWriteCbk,"San Diego", String("Qualcomm"), Type2Type<GResource>() , Type2Type<GDS>());
- }
- void CPPApp::onWriteCbk()
- {
- FDS* ds = static_cast<FDS*>(rr_->getRegistered(FDS::getID()));
- if (ds->error == SUCCESS)
- {
- initIO(&CPPApp::onReadFSCbk, ds->address, String(""), Type2Type<FileStream>() , Type2Type<FDS>());
- initIO(&CPPApp::onReadHttpCbk, "192.168.10.100", String("/a.htm"), Type2Type<HTTPpipe>() , Type2Type<HDS>());
- }
- else
- {
- Writer writer(m_pIShell);
- String e((long)ds->error);
- String s("ERROR: ", 7, e, e.length());
- writer.WriteLine(s.toCharArray());
- }
- }
- void CPPApp::onReadHttpCbk()
- {
- onReadCbk(Type2Type<HDS>());
- }
- void CPPApp::onReadFSCbk()
- {
- onReadCbk(Type2Type<FDS>());
- }
- void CPPApp::onReadGSCbk()
- {
- onReadCbk(Type2Type<GDS>());
- }
- template <class S>
- void CPPApp::onReadCbk(Type2Type<S>)
- {
- Writer writer(m_pIShell);
- S* ds = static_cast<S*>(rr_->getRegistered(S::getID()));
- if (ds->error == SUCCESS)
- {
- const char* s = ds->data.toCharArray();
- if (s)
- writer.WriteLine(s);
- }
- else
- {
- String e((long)ds->error);
- String s("ERROR: ", 7, e, e.length());
- writer.WriteLine(s.toCharArray());
- }
- }
- template <class R, class S>
- void CPPApp::initIO(FNC f, char* address,const String& param, Type2Type<R> , Type2Type<S> )
- {
- typedef pkAEECBK_Reg<CPPApp,S> PR;
- PR& pr = PR::initCbk(rr_,this,f, m_pIShell, Type2Type<PR>());
- S* ds = S::initDataStruct(rr_,address, param);
- CbkDispatcher<PR, ConnectionPolicy, R, S>::getDispatcher(m_pIShell, rr_, pr, ds);
- }