firemail
标题: nsTArray [打印本页]
作者: hechengjin 时间: 2015-10-12 18:33
标题: nsTArray
本帖最后由 hechengjin 于 2015-10-13 19:03 编辑
nsTArray<uint32_t> mIndicesToNoteChange;
nsMsgViewIndexArray selection;
GetSelectedIndices(selection);
nsMsgViewIndex *indices = selection.Elements();
int32_t numIndices = selection.Length();
nsMsgDBView::CopyMessages(nsIMsgWindow *window, nsMsgViewIndex *indices, int32_t numIndices, bool isMove, nsIMsgFolder *destFolder){
mIndicesToNoteChange.AppendElements(indices, numIndices); //把另一个数组追加到此数组之后
}
2864 nsCOMPtr<nsIMutableArray> messageArray(do_CreateInstance(NS_ARRAY_CONTRACTID, &rv));2865 NS_ENSURE_SUCCESS(rv, rv);2866 rv = GetHeadersFromSelection(indices, numIndices, messageArray);
return copyService->CopyMessages(m_folder /* source folder */, messageArray, destFolder, isMove, nullptr /* listener */, window, true /*allowUndo*/);
877 template<class Item>878 elem_type *AppendElements(const Item* array, size_type arrayLen) {879 if (!this->EnsureCapacity(Length() + arrayLen, sizeof(elem_type)))880 return nullptr;881 index_type len = Length();882 AssignRange(len, arrayLen, array);883 this->IncrementLength(arrayLen);884 return Elements() + len;885 }
- nsTArray<T> - a C++ class which provides a typesafe container for objects or primitive types (pointers, integers, etc). The objects must define a default constructor and a copy constructor. To use IndexOf without providing a comparator, they must also define anoperator==. For sorting without providing a comparator they must define an operator<. Note that this class differs from the other array types by using unsigned indices.
Class | Data Type | Scriptable? | Typesafe? | Can be modified? | Built in buffer? | Ownership |
|
|
|
|
|
|
|
nsTArray<T> | Any that has a default constructor and copy constructor | No | Yes | Yes* | No | Can hold objects directly, in which case it owns them. When holding pointers, doesn't own the pointer. |
Will your array store non-refcounted objects and need automatic resizing? If so, use nsTArray<T>.
nsTArray<T>nsTArray<T> is a typesafe array for holding various objects. It can be used to hold objects directly, not just pointers to objects.
UsageIt is most often used as a member of a C++ class to store a list of well-typed objects. It is also usually declared as an inline member rather than a pointer. As a class member, nsTArray<T> is preferred over nsVoidArray.
For example, here is its use in a class:
class MediaList {public: void AddMedium(const nsString& aMedium); private: nsTArray<nsString> mMedia;};// typesafety of mMedia ensures that we only append an nsStringvoid NodeContainer::AddMedium(const nsString& aMedium) { mMedia.AppendElement(aMedium);}
nsTArray<T> can also be declared on the stack to collect a temporary list of objects and manipulate them. When the object goes out of scope, all its members have their destructors called. Note that if the nsTArray<T> holds pointers to objects, the objects will not be deleted (and hence not have their destructors called).
void ProcessVisibleItems(){ // temporary stack-based nsTArray nsTArray<FooStruct> fooItems; GetCompleteList(fooItems); // now filter out non visible objects // doing this backwards PRUint32 i = fooItems.Length(); while (i > 0) { --i; PRBool isVisible; fooItems->GetIsVisible(&isVisible); if (!isVisible) { fooItems.RemoveElementAt(i); } } // now deal with the processed list ProcessList(fooItems); // fooItems will call the destructors of all the FooStruct objects // when it goes out of scope}
Access to elementsnsTArray<T> is a concrete C++ class, and so the [] operator is used to access its members.
For example, this code calls the same method on each member:
void NotifyObservers(const nsTArray<ObserverClass*>& observers) { for (PRUint32 i = observers.Length(); i > 0 ; ) { --i; observers->Observe(); }}
Passing as a parameterWhen passing nsTArray<T> among functions, the convention is to pass by reference. Also be sure to use const if you want to enforce that the array is read-only.
Here is an example with a read-only and a writable array:
// array is read-only because of constvoid PrintSize(const nsTArray<nsElement>& elements) { printf("There are %d elements.\n", elements.Length());}// no const, so we can modify the arrayvoid TweakArray(nsTArray<nsElement>& elements, const nsElement& newElement) { elements.RemoveElementAt(0); elements.AppendElement(newElement);}
In-place enumerationThere are no enumerator objects that work on an nsTArray<T>.
欢迎光临 firemail (http://firemail.wang:8088/) |
Powered by Discuz! X3 |