summaryrefslogtreecommitdiffstats
path: root/dom/xslt/base/txList.h
blob: eeafd12dda8cb04f520d64389f46ae120cd8cbb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef TRANSFRMX_LIST_H
#define TRANSFRMX_LIST_H

#include "txCore.h"

class txListIterator;

/**
 * Represents an ordered list of Object pointers. Modeled after a Java 2 List.
 **/
class txList : public txObject {
  friend class txListIterator;

 public:
  /**
   * Creates an empty txList
   **/
  txList();

  /**
   * txList destructor, object references will not be deleted.
   **/
  ~txList();

  /**
   * Returns the number of items in this txList
   **/
  int32_t getLength();

  /**
   * Returns true if there are no items in this txList
   */
  inline bool isEmpty() { return itemCount == 0; }

  /**
   * Adds the given Object to the list
   **/
  void add(void* objPtr);

  /*
   * Removes all the objects from the list
   */
  void clear();

 protected:
  struct ListItem {
    ListItem* nextItem;
    ListItem* prevItem;
    void* objPtr;
  };

  /**
   * Removes the given ListItem pointer from the list
   **/
  ListItem* remove(ListItem* sItem);

 private:
  txList(const txList& aOther);  // not implemented

  ListItem* firstItem;
  ListItem* lastItem;
  int32_t itemCount;

  void insertAfter(void* objPtr, ListItem* sItem);
  void insertBefore(void* objPtr, ListItem* sItem);
};

/**
 * An Iterator for the txList Class
 **/
class txListIterator {
 public:
  /**
   * Creates a new txListIterator for the given txList
   * @param list, the txList to create an Iterator for
   **/
  explicit txListIterator(txList* list);

  /**
   * Adds the Object pointer to the txList pointed to by this txListIterator.
   * The Object pointer is inserted as the next item in the txList
   * based on the current position within the txList
   * @param objPtr the Object pointer to add to the list
   **/
  void addAfter(void* objPtr);

  /**
   * Adds the Object pointer to the txList pointed to by this txListIterator.
   * The Object pointer is inserted as the previous item in the txList
   * based on the current position within the txList
   * @param objPtr the Object pointer to add to the list
   **/
  void addBefore(void* objPtr);

  /**
   * Returns true if a successful call to the next() method can be made
   * @return true if a successful call to the next() method can be made,
   * otherwise false
   **/
  bool hasNext();

  /**
   * Returns the next Object pointer from the list
   **/
  void* next();

  /**
   * Returns the previous Object pointer from the list
   **/
  void* previous();

  /**
   * Returns the current Object
   **/
  void* current();

  /**
   * Removes the Object last returned by the next() or previous() methods;
   * @return the removed Object pointer
   **/
  void* remove();

  /**
   * Resets the current location within the txList to the beginning of the
   * txList
   **/
  void reset();

  /**
   * Resets the current location within the txList to the end of the txList
   **/
  void resetToEnd();

 private:
  //-- points to the current list item
  txList::ListItem* currentItem;

  //-- points to the list to iterator over
  txList* list;

  //-- we've moved off the end of the list
  bool atEndOfList;
};

using List = txList;

#endif