File: Ap4List.h

package info (click to toggle)
kodi-inputstream-adaptive 2.6.14%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,036 kB
  • sloc: cpp: 53,019; ansic: 492; makefile: 10
file content (516 lines) | stat: -rw-r--r-- 13,145 bytes parent folder | download
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*****************************************************************
|
|    AP4 - Lists
|
|    Copyright 2002-2008 Axiomatic Systems, LLC
|
|
|    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
|    Unless you have obtained Bento4 under a difference license,
|    this version of Bento4 is Bento4|GPL.
|    Bento4|GPL is free software; you can redistribute it and/or modify
|    it under the terms of the GNU General Public License as published by
|    the Free Software Foundation; either version 2, or (at your option)
|    any later version.
|
|    Bento4|GPL is distributed in the hope that it will be useful,
|    but WITHOUT ANY WARRANTY; without even the implied warranty of
|    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|    GNU General Public License for more details.
|
|    You should have received a copy of the GNU General Public License
|    along with Bento4|GPL; see the file COPYING.  If not, write to the
|    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|    02111-1307, USA.
|
 ****************************************************************/

#ifndef _AP4_LIST_H_
#define _AP4_LIST_H_

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "Ap4Types.h"
#include "Ap4Results.h"

/*----------------------------------------------------------------------
|   forward references
+---------------------------------------------------------------------*/
template <typename T> class AP4_List;

/*----------------------------------------------------------------------
|   AP4_List
+---------------------------------------------------------------------*/
template <typename T> 
class AP4_List 
{
public:
    // types
    class Item 
    {
    public:
        // types
        class Operator 
        {
        public:
            // methods
            virtual ~Operator() {}
            virtual AP4_Result Action(T* data) const = 0;
        };

        class Finder 
        {
        public:
            // methods
            virtual ~Finder() {}
            virtual AP4_Result Test(T* data) const = 0;
        };

        // methods
        Item(T* data) : m_Data(data), m_Next(0), m_Prev(0) {}
       ~Item() {}
        Item* GetNext() { return m_Next; }
        Item* GetPrev() { return m_Prev; }
        T*    GetData() { return m_Data; }

    private:
        // members
        T*    m_Data;
        Item* m_Next;
        Item* m_Prev;

        // friends
        friend class AP4_List;
    };

    // methods
                 AP4_List<T>(): m_ItemCount(0), m_Head(0), m_Tail(0) {}
    virtual     ~AP4_List<T>();
    AP4_Result   Clear();
    AP4_Result   Add(T* data);
	AP4_Result   Add(AP4_List<T> &data);
    AP4_Result   Add(Item* item);
    AP4_Result   Remove(Item* item);
    AP4_Result   Remove(T* data);
    AP4_Result   Insert(Item* where, T* data);
    AP4_Result   Get(AP4_Ordinal idx, T*& data) const;
    AP4_Result   PopHead(T*& data);
    AP4_Result   Apply(const typename Item::Operator& op) const;
    AP4_Result   ApplyUntilFailure(const typename Item::Operator& op) const;
    AP4_Result   ApplyUntilSuccess(const typename Item::Operator& op) const ;
    AP4_Result   ReverseApply(const typename Item::Operator& op) const;
    AP4_Result   Find(const typename Item::Finder& finder, T*& data) const;
    AP4_Result   ReverseFind(const typename Item::Finder& finder, T*& data) const;
    AP4_Result   DeleteReferences();
    AP4_Cardinal ItemCount() const { return m_ItemCount; }
    Item*        FirstItem() const { return m_Head; }
    Item*        LastItem()  const { return m_Tail; }
 
protected:
    // members
    AP4_Cardinal m_ItemCount;
    Item*        m_Head;
    Item*        m_Tail;
    
private:
	// these cannot be used
    AP4_List<T>(const AP4_List<T>&);
	AP4_List<T>& operator=(const AP4_List<T>&);
};

/*----------------------------------------------------------------------
|   AP4_List<T>::~AP4_List<T>
+---------------------------------------------------------------------*/
template <typename T>
AP4_List<T>::~AP4_List()
{
    Clear();
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Clear
+---------------------------------------------------------------------*/
template <typename T>
inline
AP4_Result
AP4_List<T>::Clear()
{
    Item* item = m_Head;
 
    while (item) {
        Item* next = item->m_Next;
        delete item;
        item = next;
    }
    m_ItemCount = 0;
    m_Head = m_Tail = NULL;
    
    return AP4_SUCCESS;
}
 
/*----------------------------------------------------------------------
|   AP4_List<T>::Add
+---------------------------------------------------------------------*/
template <typename T>
inline
AP4_Result
AP4_List<T>::Add(T* data)
{
    return Add(new Item(data));
}

template <typename T>
inline
AP4_Result
AP4_List<T>::Add(AP4_List<T> &data)
{
	Item* item = data.m_Head;
	while (item) {
		Add(new Item(item->GetData()));
		item->m_Data = NULL;
		item = item->m_Next;
	}
	return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Add
+---------------------------------------------------------------------*/
template <typename T>
AP4_Result
AP4_List<T>::Add(Item* item)
{
    // add element at the tail
    if (m_Tail) {
        item->m_Prev = m_Tail;
        item->m_Next = NULL;
        m_Tail->m_Next = item;
        m_Tail = item;
    } else {
        m_Head = item;
        m_Tail = item;
        item->m_Next = NULL;
        item->m_Prev = NULL;
    }

    // one more item in the list now
    m_ItemCount++;
 
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Remove
+---------------------------------------------------------------------*/
template <typename T>
AP4_Result
AP4_List<T>::Remove(Item* item)
{
    if (item->m_Prev) {
        // item is not the head
        if (item->m_Next) {
            // item is not the tail
            item->m_Next->m_Prev = item->m_Prev;
            item->m_Prev->m_Next = item->m_Next;
        } else {
            // item is the tail
            m_Tail = item->m_Prev;
            m_Tail->m_Next = NULL;
        }
    } else {
        // item is the head
        m_Head = item->m_Next;
        if (m_Head) {
            // item is not the tail
            m_Head->m_Prev = NULL;
        } else {
            // item is also the tail
            m_Tail = NULL;
        }
    }

    // delete the item
    delete item;

    // one less item in the list now
    m_ItemCount--;

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Remove
+---------------------------------------------------------------------*/
template <typename T>
AP4_Result
AP4_List<T>::Remove(T* data)
{
    Item* item = m_Head;

    while (item) {
        if (item->m_Data == data) {
            // delete item
            return Remove(item);
        }
        item = item->m_Next;
    }
 
    return AP4_ERROR_NO_SUCH_ITEM;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Insert
+---------------------------------------------------------------------*/
template <typename T>
AP4_Result
AP4_List<T>::Insert(Item* where, T* data)
{
    Item* item = new Item(data);

    if (where == NULL) {
        // insert as the head
        if (m_Head) {
            // replace the current head
            item->m_Prev = NULL;
            item->m_Next = m_Head;
            m_Head->m_Prev = item;
            m_Head = item;
        } else {
            // this item becomes the head and tail
            m_Head = item;
            m_Tail = item;
            item->m_Next = NULL;
            item->m_Prev = NULL;
        }
    } else {
        // insert after the 'where' item
        if (where == m_Tail) {
            // add the item at the end
            return Add(item);
        } else {
            // update the links
            item->m_Prev = where;
            item->m_Next = where->m_Next;
            where->m_Next->m_Prev = item;
            where->m_Next = item;
        }
    }

    // one more item in the list now
    ++m_ItemCount;

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Get
+---------------------------------------------------------------------*/
template <typename T>
AP4_Result
AP4_List<T>::Get(AP4_Ordinal idx, T*& data) const
{
    Item* item = m_Head;

    if (idx < m_ItemCount) {
        while (idx--) item = item->m_Next;
        data = item->m_Data;
        return AP4_SUCCESS;
    } else {
        data = NULL;
        return AP4_ERROR_NO_SUCH_ITEM;
    }
}

/*----------------------------------------------------------------------
|   AP4_List<T>::PopHead
+---------------------------------------------------------------------*/
template <typename T>
AP4_Result
AP4_List<T>::PopHead(T*& data)
{
    // check that we have at least one item
    if (m_Head == NULL) {
        return AP4_ERROR_LIST_EMPTY;
    }

    // remove the item and return it
    data = m_Head->m_Data;
    Item* head = m_Head;
    m_Head = m_Head->m_Next;
    if (m_Head) {
        m_Head->m_Prev = NULL;
    } else {
        m_Tail = NULL;
    }

    // delete item
    delete head;

    // one less item in the list now
    m_ItemCount--;
 
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Apply
+---------------------------------------------------------------------*/
template <typename T>
inline 
AP4_Result
AP4_List<T>::Apply(const typename Item::Operator& op) const
{
    Item* item = m_Head;
 
    while (item) {
        op.Action(item->m_Data);
        item = item->m_Next;
    }

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::ApplyUntilFailure
+---------------------------------------------------------------------*/
template <typename T>
inline 
AP4_Result
AP4_List<T>::ApplyUntilFailure(const typename Item::Operator& op) const
{
    Item* item = m_Head;
 
    while (item) {
        AP4_Result result;
        result = op.Action(item->m_Data);
        if (result != AP4_SUCCESS) return result;
        item = item->m_Next;
    }

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::ApplyUntilSuccess
+---------------------------------------------------------------------*/
template <typename T>
inline 
AP4_Result
AP4_List<T>::ApplyUntilSuccess(const typename Item::Operator& op) const
{
    Item* item = m_Head;
 
    while (item) {
        AP4_Result result;
        result = op.Action(item->m_Data);
        if (result == AP4_SUCCESS) return AP4_SUCCESS;
        item = item->m_Next;
    }

    return AP4_FAILURE;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::ReverseApply
+---------------------------------------------------------------------*/
template <typename T>
inline 
AP4_Result
AP4_List<T>::ReverseApply(const typename Item::Operator& op) const
{
    Item* item = m_Tail;
 
    while (item) {
        if (op.Action(item->m_Data) != AP4_SUCCESS) {
            return AP4_ERROR_LIST_OPERATION_ABORTED;
        }
        item = item->m_Prev;
    }

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::Find
+---------------------------------------------------------------------*/
template <typename T>
inline 
AP4_Result
AP4_List<T>::Find(const typename Item::Finder& finder, T*& data) const
{
    Item* item = m_Head;
 
    while (item) {
        if (finder.Test(item->m_Data) == AP4_SUCCESS) {
            data = item->m_Data;
            return AP4_SUCCESS;
        }
        item = item->m_Next;
    }

    data = NULL;
    return AP4_ERROR_NO_SUCH_ITEM;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::ReverseFind
+---------------------------------------------------------------------*/
template <typename T>
inline 
AP4_Result
AP4_List<T>::ReverseFind(const typename Item::Finder& finder, T*& data) const
{
    Item* item = m_Tail;
 
    while (item) {
        if (finder.Test(item->m_Data) == AP4_SUCCESS) {
            data = item->m_Data;
            return AP4_SUCCESS;
        }
        item = item->m_Prev;
    }

    data = NULL;
    return AP4_ERROR_NO_SUCH_ITEM;
}

/*----------------------------------------------------------------------
|   AP4_List<T>::DeleteReferences
+---------------------------------------------------------------------*/
template <typename T>
inline 
AP4_Result
AP4_List<T>::DeleteReferences()
{
    Item* item = m_Head;
 
    while (item) {
        Item* next = item->m_Next;
        delete item->m_Data;
        delete item;
        item = next;
    }

    // no more items
    m_Head = m_Tail = NULL;
    m_ItemCount = 0;

    return AP4_SUCCESS;
}

#endif // _AP4_LIST_H_