Sequence.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_P_SEQUENCE_H
00014 #define _SUPPORT_P_SEQUENCE_H
00015 
00016 #include <stdint.h>
00017 #include <sys/types.h>
00018 
00019 #if _SUPPORTS_NAMESPACE
00020 namespace palmos {
00021 namespace support {
00022 #endif
00023 
00024 struct SSpan {
00025     inline SSpan(uint8_t const *d, uint32_t c) : data(d), count(c) { };
00026     uint8_t const   *data;
00027     uint32_t        count;
00028 };
00029 
00030 class SSequence {
00031 public:
00032     inline  SSequence(uint32_t item_size) : m_item_size(item_size), m_length(0) { };
00033     virtual ~SSequence();
00034 
00035     virtual status_t Insert(uint32_t position, uint8_t const *src, uint32_t count = 1) = 0;
00036     virtual status_t Delete(uint32_t position, uint32_t count = 1) = 0;
00037     virtual SSpan ItemsAt(uint32_t position, uint32_t count = 1) = 0;
00038 
00039     inline uint32_t Granularity(void) const { return m_item_size; };
00040     inline uint32_t Length(void) const { return m_length / m_item_size; };
00041 
00042 protected:
00043     uint32_t    m_item_size;
00044     uint32_t    m_length;
00045 private:
00046             SSequence(SSequence const &rhs);
00047 };
00048 
00049 class SGapBuffer : public SSequence {
00050 public:
00051             SGapBuffer(uint32_t items_per_page, uint32_t item_size);
00052     virtual ~SGapBuffer();
00053 
00054     virtual status_t Insert(uint32_t position, uint8_t const *src, uint32_t count = 1);
00055     virtual status_t Delete(uint32_t position, uint32_t count = 1);
00056     virtual SSpan ItemsAt(uint32_t position, uint32_t count = 1);
00057 
00058 private:
00059             SGapBuffer(SGapBuffer const &rhs);
00060 
00061     struct gap_buffer {
00062         gap_buffer  *next;
00063         uint32_t    start;
00064         uint32_t    end;
00065         uint8_t     data[4];
00066     };
00067     gap_buffer      *m_data;
00068     gap_buffer      *m_current;
00069     uint32_t        m_current_base;
00070     uint32_t        m_page_size;
00071     uint32_t        m_buf_size;
00072     
00073     gap_buffer *    NewBuffer(void);
00074     status_t        SplitBuffer(gap_buffer **buf, uint32_t pos);    // pos-th byte goes into the new buffer
00075     void            MoveGap(gap_buffer *buf, uint32_t pos);
00076     inline uint32_t BufferSize(gap_buffer *buf) const { return (uint32_t)(m_buf_size - (buf->end - buf->start)); };
00077     void            SeekTo(uint32_t);
00078 };
00079 
00080 class SPieceTable : public SSequence {
00081 public:
00082             SPieceTable(uint32_t item_size);
00083     virtual ~SPieceTable();
00084 
00085     virtual status_t Insert(uint32_t position, uint8_t const *src, uint32_t count = 1);
00086     virtual status_t Delete(uint32_t position, uint32_t count = 1);
00087     virtual SSpan ItemsAt(uint32_t position, uint32_t count = 1);
00088 
00089 private:
00090             SPieceTable(SSequence *pieces, uint32_t item_size);
00091             SPieceTable(SPieceTable const &rhs);
00092 
00093 #define ADS_SCOPE_BREAKAGE public:
00094 #define ADS_SCOPE_UNBREAKAGE private:
00095 ADS_SCOPE_BREAKAGE
00096 
00097     struct pt_buffer {
00098         pt_buffer   *next;
00099         uint32_t    length;
00100         uint32_t    used;
00101         uint8_t     data[4];
00102     };
00103 
00104 ADS_SCOPE_UNBREAKAGE
00105 #undef ADS_SCOPE_BREAKAGE
00106 #undef ADS_SCOPE_UNBREAKAGE
00107 
00108     struct pt_piece {
00109         pt_buffer   *buffer;
00110         uint32_t    offset;
00111         uint32_t    length;
00112     };
00113 
00114     SSequence   *m_pieces;
00115 
00116 };
00117 
00118 #if _SUPPORTS_NAMESPACE
00119 } } // namespace palmos::support
00120 #endif
00121 #endif