quick_recipes (779892), страница 11
Текст из файла (страница 11)
The newer array classes are R classes, and may thus beinstantiated on the heap or the stack, but are usually stack-based. As Rclasses, they hold a handle to a block of memory used to store the arraybuffer, which in both cases is a resizable flat buffer rather than a doublylinked list of segments.For performance reasons, when CArrayFixFlat is the type ofarray needed, we recommend that you actually use RArray, andwhen CArrayPtrFlat is a good choice, we recommend that youuse RPointerArray instead.56SYMBIAN OS DEVELOPMENT BASICSTable 3.3 Array Combinations Defined in the SDKClass nameElements and memory layoutElement cleanupCarrayFixFlatFixed size elements contained in thearray.Occupies a single memory buffer that isresized as necessary.Fixed size elements contained in thearray.Occupies segments of memory.Elements are pointers to variable sizeobjects contained outside the array.Occupies a single memory buffer that isresized as necessary.Elements are pointers to variable sizeobjects contained outside the array.Occupies segments of memory.Elements are pointers to CBase-derivedobjects stored outside the array.Occupies a single memory buffer that isresized as necessary.Elements are pointers to CBase-derivedobjects stored outside the array.Occupies segments of memory.Variable size elements occupy thearray, each preceded by its length.Occupies a single memory buffer that isresized as necessary.Destroyed automaticallyby the array.CArrayFixSegCarrayVarFlatCArrayVarSegCarrayPtrFlatCArrayPtrSegCarrayPakFlatDestroyed automaticallyby the array.Destroyed automaticallyby the array.Destroyed automaticallyby the array.CArrayPtrFlat::ResetAndDestroy().CArrayPtrFlat::ResetAndDestroy().Destroyed automaticallyby the array.3.9.3 Cleanup of the RArray ClassesRArray<class T> is an array of elements of the same size which arestored within the array buffer.
The Close() or Reset() functions mustbe called to clean up the array by freeing the memory allocated for theelements.• RArray::Close() frees the memory used to store the array andcloses the array object.• RArray::Reset() frees the memory used to store the array butsimply resets its state so it can be reused.RPointerArray<class T> is an array of pointer elements addressing objects stored elsewhere. The ownership of these objects must beaddressed separately when the array is destroyed.
If pointers to theobjects are held elsewhere (by other components), then calling Close()or Reset() to clean up the memory for the array of pointers is sufficient.ARRAYS ON SYMBIAN OS57However, if the array has ownership of the objects its elements point to,it must take responsibility for cleanup. ResetAndDestroy() must becalled to delete the object associated with each pointer element in thearray, and then destroy the array itself.3.9.4 When Should You Use CArrayX Arrays?The CArrayX classes have been retained for legacy code, and becausethey provide a segmented-memory implementation which may be moreappropriate for arrays that are frequently resized. Thus, CArrayFixSegand CArrayPtrSeg are useful alternatives to RArray and RPointerArray, respectively, when segmented memory is required, but RArrayshould always be preferred over CArrayFixFlat, and RPointerArray always preferred over CArrayPtrFlat.The only drawback of the RArray classes is the requirement for allelements to be 4-byte-aligned in memory.
Check the classes you developfor this or you risk unhandled exceptions when running your applicationson phones.3.9.5 Sorting and SearchingSorting is based on TKeyArrayXXX classes for CArrayX arrays and theTLinearOrder class for RArrays. The same is true for insert and findmethods, which perform ‘in order’ operations.The _FOFF macro is used to find the offset of a member variable inthe binary representation of an object in memory. For example:class TElementSort{public :TElementSort(){}public :TBuf<4> iData;};...CArrayFixFlat<TElement>* fixflat =new (ELeave) CArrayFixFlat<TElement>(3);// populate fixflat hereTKeyArrayFix key(_FOFF(TElementSort, iData), ECmpNormal);TInt res = fixflat->Sort(key);// the elements inside fixflat have been ordered...delete fixflat;To insert elements in the array so that it remains ordered throughout,use the InsertIsqL() method with such a sort key.58SYMBIAN OS DEVELOPMENT BASICSTLinearOrder is used differently and requires the class of theelement objects to define its own ordering:class CPerson : public CBase{public:static CPerson* NewLC(const TDesC& aFirstName,const TDesC& aLastName);static TInt CompareByLastName(const CPerson& person1,const CPerson& person2);protected:CPerson(const TDesC& aFirstName, const TDesC& aLastName);private:TBuf<64> iFirstName;TBuf<64> iLastName;};TInt CPerson::CompareByLastName(const CPerson& person1,const CPerson& person2){return person1.iLastName().CompareC(person2.LastName());}RPointerArray<CPerson> people;CleanupClosePushL(people);CPerson* tyler = CPerson::NewLC(_L("Sam"), _L("Tyler"));CPerson* hunt = CPerson::NewLC(_L("Gene"), _L("Hunt"));people.AppendL(hunt);CleanupStack::Pop(hunt); // Now owned by people arraypeople.AppendL(tyler);CleanupStack::Pop(tyler); // Now owned by people arrayTLinearOrder<CPerson> byLastName(CPerson::CompareByLastName);people.Sort(byLastName);CleanupStack::Pop(people);people.ResetAndDestroy();people.Close();After the elements in the array are sorted, the FindInOrder()functions can perform a binary search using the same ordering.3.10Executable FilesThere are two common executable files on Symbian OS: EXE and DLL.You have already seen an example of a Carbide.c++ project, whichcreates HelloWorld.exe, in Chapter 2.
You can also use Carbide.c++ tocreate a Symbian OS DLL file. If you do so, it is worth examining thedifferences between the MMP file for an EXE and a DLL file. In particular,you will notice a difference between the TARGETTYPE and UID valuesspecified in the MMP file.EXECUTABLE FILES59TARGETTYPE indicates the type of file to be built, for example an EXE,in the case of an executable application, or a DLL. The most commonlyused Symbian OS target types are DLL, EXE and PLUGIN (ECOM plug-in).Other supported types include PDD and LDD (physical device driver andlogical device driver, respectively), LIB (a static library whose binary codeis included directly in any component that links against it – as comparedto a shared library, which is released as a separate binary), and EXEXP(an executable which exports functions, so they can be used by otherapplications or DLLs).UID specifies the final two of the target’s three unique identifiers (UIDs)that are used to uniquely identify the binary.
On Symbian OS, each UIDis a 32-bit value. The three UIDs are as follows.3.10.1 UID1This is the system-level identifier that distinguishes between EXEs andDLLs. For example, the TARGETTYPE specified for a shared library isDLL. This means that UID1 is set by the build tools to be KDynamicLibraryUid (0x10000079). An application has UID1 set to beKExecutableImageUid (0x1000007a) by specifying a TARGETTYPEEXE.The value of UID1 is built into the binary by the build tools dependingon the keyword used with the TARGETTYPE identifier in the MMP file,and the value of UID1 does not need to be specified in an MMP filebecause it is automatically applied.3.10.2 UID2For DLLs, this UID is used to differentiate between shared library andpolymorphic interface DLLs.
For shared libraries, UID2 is KSharedLibraryUid (0x1000008d) but the value for polymorphic DLLs variesdepending on their plug-in type (for example, the socket server protocolmodule UID2 value is 0x1000004A). You can find out more aboutthe difference between the DLL types in the Symbian Developer Librarydocumentation in your SDK.UID2 is not relevant for TARGETTYPE EXE and can simply be set to 0or not specified at all in the MMP file.3.10.3 UID3The third UID value identifies a file uniquely. No two executables mayhave the same UID3 value, and values must be requested from Symbian, which allocates them from a central database to ensure thateach binary has a different value.
You must register with SymbianSigned (www.symbiansigned.com) to request values for use as UID360SYMBIAN OS DEVELOPMENT BASICSvalues in production code, but you can assign yourself a value from thedevelopment range (0xE1000000-0xEFFFFFFF) for test code.3.11Platform Security: CapabilitiesPlatform security on Symbian OS v9 prevents applications from havingunauthorized access to hardware, software and system or user data.The intention is to prevent malware, or even just badly written code,from compromising the operation of the phone, corrupting or stealingconfidential user data, or adversely affecting the phone network.Every Symbian OS process is assigned a level of privilege through aset of capabilities which are like tokens.
A capability grants a processthe trust that it will not abuse the services associated with the associatedprivilege. The Symbian OS kernel holds a list of capabilities for everyrunning process and checks it before allowing a process to access aprotected service.The Symbian OS software installer verifies the digital signature ofsoftware at install time to ensure that it is authorized to use the capabilitiesit was built with, and the digital signature can only come from a trustedauthority. This prevents applications from arbitrarily assigning themselvesthe capabilities they want prior to installation.There are 20 capabilities in Symbian OS (see Table 3.4).
You cancheck the capabilities needed for a certain API from the SDK Help. Anapplication must define the capabilities it is going to use by includingthem in its MMP file. For example:CAPABILITY UserEnvironment NetworkServicesThis statement means that the application uses two capabilities,UserEnvironment and NetworkServices.There are four different types of platform security capability, whendigital signing is considered. The differences arise because of the sensitivity of the data or system resources the capabilities protect, andthe requirements that are placed on the developer before they aregiven permission to use them. Please refer to Chapter 6 for moreinformation about Symbian Signed, the scheme that performs digitalsigning on commercial software, to grant platform security capabilities todevelopers.The capabilities of a process cannot be changed at runtime. TheSymbian OS loader starts a process by reading the executable andchecking the capabilities it has been assigned.