Ignore:
Timestamp:
Nov 9, 2005 3:37:19 PM (18 years ago)
Author:
bennylp
Message:

Rework pjlib++

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/main/pjlib/include/pj++/list.hpp

    r29 r36  
    11/* $Id$ 
    2  * 
    32 */ 
    43#ifndef __PJPP_LIST_H__ 
     
    65 
    76#include <pj/list.h> 
    8  
    9 template <typename T> 
    10 struct PJ_List_Node 
    11 { 
    12     PJ_DECL_LIST_MEMBER(T) 
    13 }; 
    14  
    15  
    16 template <class Node> 
    17 class PJ_List 
     7#include <pj++/pool.hpp> 
     8 
     9 
     10// 
     11// Linked-list. 
     12// 
     13// Note: 
     14//  List_Node must have public member next and prev. Normally 
     15//  it will be declared like: 
     16// 
     17//  struct my_node 
     18//  { 
     19//      PJ_DECL_LIST_MEMBER(struct my_node); 
     20//      .. 
     21//  }; 
     22// 
     23// 
     24template <class List_Node> 
     25class Pj_List : public Pj_Object 
    1826{ 
    1927public: 
    20     PJ_List() { pj_list_init(&root_); if (0) compiletest(); } 
    21     ~PJ_List() {} 
    22  
     28    // 
     29    // List const_iterator. 
     30    // 
    2331    class const_iterator 
    2432    { 
    2533    public: 
    26         const_iterator() : node_(NULL) {} 
    27         const_iterator(const Node *nd) : node_((Node*)nd) {} 
    28         const Node * operator *() { return node_; } 
    29         const Node * operator -> () { return node_; } 
    30         const_iterator operator++() { return const_iterator(node_->next); } 
    31         bool operator==(const const_iterator &rhs) { return node_ == rhs.node_; } 
    32         bool operator!=(const const_iterator &rhs) { return node_ != rhs.node_; } 
     34        const_iterator()  
     35            : node_(NULL)  
     36        {} 
     37        const_iterator(const List_Node *nd)  
     38            : node_((List_Node*)nd)  
     39        {} 
     40        const List_Node * operator *()  
     41        {  
     42            return node_;  
     43        } 
     44        const List_Node * operator -> ()  
     45        {  
     46            return node_;  
     47        } 
     48        const_iterator operator++()  
     49        {  
     50            return const_iterator(node_->next);  
     51        } 
     52        bool operator==(const const_iterator &rhs)  
     53        {  
     54            return node_ == rhs.node_;  
     55        } 
     56        bool operator!=(const const_iterator &rhs)  
     57        {  
     58            return node_ != rhs.node_;  
     59        } 
    3360 
    3461    protected: 
    35         Node *node_; 
     62        List_Node *node_; 
    3663    }; 
    3764 
     65    // 
     66    // List iterator. 
     67    // 
    3868    class iterator : public const_iterator 
    3969    { 
    4070    public: 
    41         iterator() {} 
    42         iterator(Node *nd) : const_iterator(nd) {} 
    43         Node * operator *() { return node_; } 
    44         Node * operator -> () { return node_; } 
    45         iterator operator++() { return iterator(node_->next); } 
    46         bool operator==(const iterator &rhs) { return node_ == rhs.node_; } 
    47         bool operator!=(const iterator &rhs) { return node_ != rhs.node_; } 
     71        iterator()  
     72        {} 
     73        iterator(List_Node *nd)  
     74            : const_iterator(nd)  
     75        {} 
     76        List_Node * operator *()  
     77        {  
     78            return node_;  
     79        } 
     80        List_Node * operator -> ()  
     81        {  
     82            return node_;  
     83        } 
     84        iterator operator++()  
     85        {  
     86            return iterator(node_->next);  
     87        } 
     88        bool operator==(const iterator &rhs)  
     89        {  
     90            return node_ == rhs.node_;  
     91        } 
     92        bool operator!=(const iterator &rhs)  
     93        {  
     94            return node_ != rhs.node_;  
     95        } 
    4896    }; 
    4997 
     98    // 
     99    // Default constructor. 
     100    // 
     101    Pj_List()  
     102    {  
     103        pj_list_init(&root_);  
     104        if (0) compiletest();  
     105    } 
     106 
     107    // 
     108    // Check if list is empty. 
     109    //  
    50110    bool empty() const 
    51111    { 
     
    53113    } 
    54114 
     115    // 
     116    // Get first element. 
     117    // 
    55118    iterator begin() 
    56119    { 
     
    58121    } 
    59122 
     123    // 
     124    // Get first element. 
     125    // 
    60126    const_iterator begin() const 
    61127    { 
     
    63129    } 
    64130 
     131    // 
     132    // Get end-of-element 
     133    // 
    65134    const_iterator end() const 
    66135    { 
    67         return const_iterator((Node*)&root_); 
    68     } 
    69  
     136        return const_iterator((List_Node*)&root_); 
     137    } 
     138 
     139    // 
     140    // Get end-of-element 
     141    // 
    70142    iterator end() 
    71143    { 
    72         return iterator((Node*)&root_); 
    73     } 
    74  
    75     void insert_before (iterator &pos, Node *node) 
     144        return iterator((List_Node*)&root_); 
     145    } 
     146 
     147    // 
     148    // Insert node. 
     149    // 
     150    void insert_before (iterator &pos, List_Node *node) 
    76151    { 
    77152        pj_list_insert_before( *pos, node ); 
    78153    } 
    79154 
    80     void insert_after(iterator &pos, Node *node) 
     155    // 
     156    // Insert node. 
     157    // 
     158    void insert_after(iterator &pos, List_Node *node) 
    81159    { 
    82160        pj_list_insert_after(*pos, node); 
    83161    } 
    84162 
    85     void merge_first(Node *list2) 
     163    // 
     164    // Merge list. 
     165    // 
     166    void merge_first(List_Node *list2) 
    86167    { 
    87168        pj_list_merge_first(&root_, list2); 
    88169    } 
    89170 
    90     void merge_last(PJ_List *list) 
     171    // 
     172    // Merge list. 
     173    // 
     174    void merge_last(Pj_List *list) 
    91175    { 
    92176        pj_list_merge_last(&root_, &list->root_); 
    93177    } 
    94178 
    95     void insert_nodes_before(iterator &pos, PJ_List *list2) 
     179    // 
     180    // Insert list. 
     181    // 
     182    void insert_nodes_before(iterator &pos, Pj_List *list2) 
    96183    { 
    97184        pj_list_insert_nodes_before(*pos, &list2->root_); 
    98185    } 
    99186 
    100     void insert_nodes_after(iterator &pos, PJ_List *list2) 
     187    // 
     188    // Insert list. 
     189    // 
     190    void insert_nodes_after(iterator &pos, Pj_List *list2) 
    101191    { 
    102192        pj_list_insert_nodes_after(*pos, &list2->root_); 
    103193    } 
    104194 
     195    // 
     196    // Erase an element. 
     197    // 
    105198    void erase(iterator &it) 
    106199    { 
     
    108201    } 
    109202 
    110     Node *front() 
     203    // 
     204    // Get first element. 
     205    // 
     206    List_Node *front() 
    111207    { 
    112208        return root_.next; 
    113209    } 
    114210 
    115     const Node *front() const 
     211    // 
     212    // Get first element. 
     213    // 
     214    const List_Node *front() const 
    116215    { 
    117216        return root_.next; 
    118217    } 
    119218 
     219    // 
     220    // Remove first element. 
     221    // 
    120222    void pop_front() 
    121223    { 
     
    123225    } 
    124226 
    125     Node *back() 
     227    // 
     228    // Get last element. 
     229    // 
     230    List_Node *back() 
    126231    { 
    127232        return root_.prev; 
    128233    } 
    129234 
    130     const Node *back() const 
     235    // 
     236    // Get last element. 
     237    // 
     238    const List_Node *back() const 
    131239    { 
    132240        return root_.prev; 
    133241    } 
    134242 
     243    // 
     244    // Remove last element. 
     245    // 
    135246    void pop_back() 
    136247    { 
     
    138249    } 
    139250 
    140     iterator find(Node *node) 
    141     { 
    142         Node *n = pj_list_find_node(&root_, node); 
     251    // 
     252    // Find a node. 
     253    // 
     254    iterator find(List_Node *node) 
     255    { 
     256        List_Node *n = pj_list_find_node(&root_, node); 
    143257        return n ? iterator(n) : end(); 
    144258    } 
    145259 
    146     const_iterator find(Node *node) const 
    147     { 
    148         Node *n = pj_list_find_node(&root_, node); 
     260    // 
     261    // Find a node. 
     262    // 
     263    const_iterator find(List_Node *node) const 
     264    { 
     265        List_Node *n = pj_list_find_node(&root_, node); 
    149266        return n ? const_iterator(n) : end(); 
    150267    } 
    151268 
    152     void push_back(Node *node) 
     269    // 
     270    // Insert a node in the back. 
     271    // 
     272    void push_back(List_Node *node) 
    153273    { 
    154274        pj_list_insert_after(root_.prev, node); 
    155275    } 
    156276 
    157     void push_front(Node *node) 
     277    // 
     278    // Insert a node in the front. 
     279    // 
     280    void push_front(List_Node *node) 
    158281    { 
    159282        pj_list_insert_before(root_.next, node); 
    160283    } 
    161284 
     285    // 
     286    // Remove all elements. 
     287    // 
    162288    void clear() 
    163289    { 
     
    169295    struct RootNode 
    170296    { 
    171         PJ_DECL_LIST_MEMBER(Node) 
     297        PJ_DECL_LIST_MEMBER(List_Node); 
    172298    } root_; 
    173299 
     
    175301    { 
    176302        // If you see error in this line,  
    177         // it's because Node is not derived from PJ_List_Node. 
    178         Node *n = (Node*)0; 
     303        // it's because List_Node is not derived from Pj_List_Node. 
     304        List_Node *n = (List_Node*)0; 
    179305        n = n->next; n = n->prev; 
    180306    } 
Note: See TracChangeset for help on using the changeset viewer.