Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1822|回复: 0
打印 上一主题 下一主题

nsTArray

[复制链接]

388

主题

602

帖子

2218

积分

金牌会员

Rank: 6Rank: 6

积分
2218
跳转到指定楼层
楼主
发表于 2015-10-12 18:33:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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.
ClassData TypeScriptable?Typesafe?Can be modified?Built in buffer?Ownership







nsTArray<T>Any that has a default constructor and copy constructorNoYesYes*NoCan 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.
Usage
It 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 elements
nsTArray<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 parameter
When 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 enumeration
There are no enumerator objects that work on an nsTArray<T>.




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-22 06:45 , Processed in 0.065896 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表