Annotation of parser3/src/include/pa_string.h, revision 1.2

1.1       paf         1: /*
1.2     ! paf         2:   $Id: pa_string.h,v 1.1 2001/01/26 15:43:11 paf Exp $
1.1       paf         3: */
                      4: 
                      5: /*
                      6: 
                      7:        String                          Chunk0
                      8:        ======                          ========
                      9:        head--------->[ptr, size]
                     10:        append_here-------->[ptr, size]
                     11:        link_row      ........
                     12:                        .                       .
                     13:                        .                       [ptr, size]
                     14:                        ...........>[link to the next chunk]
                     15: 
                     16: */
                     17: 
                     18: #ifndef PA_STRING_H
                     19: #define PA_STRING_H
                     20: 
                     21: #include <stddef.h>
                     22: 
                     23: class Pool;
                     24: 
                     25: class String {
                     26: public:
                     27:        enum {
                     28:                CR_PREALLOCATED_COUNT=5,
                     29:                CR_GROW_PERCENT=60
                     30:        };
                     31: 
                     32: private:
                     33:        friend Pool;
                     34: 
                     35:        // the pool I'm allocated on
                     36:        Pool *pool;
                     37: 
                     38:        // last chank allocated count cache
                     39:        int curr_chunk_rows;
                     40:        struct Chunk {
                     41:                // the number of rows per chunk
                     42:                int count;
                     43:                union Row {
                     44:                        // chunk item
                     45:                        struct {
                     46:                                char *ptr;  // pointer to the start of string fragment
                     47:                                size_t size;  // length of the fragment
                     48:                        } item;
                     49:                        Chunk *link;  // link to the next chunk in chain
1.2     ! paf        50:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf        51:                // next rows are here
                     52:                Chunk *preallocated_link;
                     53:        }
                     54:                head;  // the head chunk of the chunk chain
                     55: 
                     56:        // next append would write to this record
                     57:        Chunk::Row *append_here;
                     58:        
                     59:        // the address of place where lies address 
                     60:        // of the link to the next chunk to allocate
                     61:        Chunk::Row *link_row;
                     62: 
                     63:        // new&constructors made private to enforce factory manufacturing at pool
                     64:        void *operator new(size_t size, Pool *apool);
                     65: 
                     66:        void construct(Pool *apool);
1.2     ! paf        67:        String() { /* never */}
1.1       paf        68:        String(Pool *apool) { 
                     69:                construct(apool); 
                     70:        }
                     71:        String(Pool *apool, char *src) {
                     72:                construct(apool);
                     73:                *this+=src;
                     74:        }
                     75: 
                     76:        bool chunk_is_full() {
                     77:                return append_here == link_row;
                     78:        }
                     79:        void expand();
1.2     ! paf        80:        int used_rows();
1.1       paf        81: 
                     82: public:
                     83: 
1.2     ! paf        84:        String(String& src);
1.1       paf        85:        size_t size();
                     86:        char *c_str();
                     87:        String& operator += (char *src);
1.2     ! paf        88:        bool operator == (String& src);
        !            89: 
        !            90:        unsigned int hash_code();
1.1       paf        91: };
                     92: 
                     93: #endif

E-mail: