Annotation of nono/lib/memorystream.h, revision 1.1.1.7

1.1       root        1: //
                      2: // nono
1.1.1.2   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.4   root        7: //
                      8: // メモリストリーム
                      9: //
                     10: 
1.1       root       11: #pragma once
                     12: 
                     13: #include "header.h"
                     14: 
                     15: // ホストのメモリ領域に対するストリームクラスみたいなもの。
1.1.1.6   root       16: class MemoryStream
1.1       root       17: {
                     18:  public:
1.1.1.7 ! root       19:        explicit MemoryStream(void *start_) {
1.1.1.4   root       20:                Init(start_);
1.1       root       21:        }
                     22: 
1.1.1.4   root       23:        // 開始ポインタを設定する
                     24:        // 現在のポインタ位置は開始ポインタ位置になる
                     25:        void Init(void *start_) {
                     26:                start = (uint8 *)start_;
                     27:                ptr = start;
                     28:        }
                     29: 
                     30:        // 開始ポインタを取得する
                     31:        void *GetStartPtr() const
                     32:        {
                     33:                return (void *)start;
                     34:        }
                     35: 
                     36:        // バイトオフセットを設定する
                     37:        void SetOffset(uint32 ofs)
                     38:        {
                     39:                ptr = start + ofs;
1.1       root       40:        }
                     41: 
1.1.1.4   root       42:        // 現在のバイトオフセットを取得する
                     43:        uint32 GetOffset() const
                     44:        {
                     45:                return (uint32)(ptr - start);
1.1       root       46:        }
                     47: 
                     48:        // 1バイト読み込んでポインタを進める
1.1.1.7 ! root       49:        uint32 Read1() {
1.1.1.3   root       50:                uint32 data = *ptr++;
1.1       root       51:                return data;
                     52:        }
1.1.1.6   root       53: 
                     54:        // 1バイト書き込んでポインタを進める
1.1.1.7 ! root       55:        void Write1(uint32 data) {
1.1.1.6   root       56:                *ptr++ = data;
                     57:        }
                     58: 
                     59:        // 文字列を書き込んでポインタを進める。
                     60:        // 書き込む文字列も終端の '\0' まで書く。
                     61:        void WriteString(const char *p) {
                     62:                while (*p) {
1.1.1.7 ! root       63:                        Write1(*p++);
1.1.1.6   root       64:                }
1.1.1.7 ! root       65:                Write1('\0');
1.1.1.6   root       66:        }
                     67: 
                     68:  protected:
                     69:        uint8 *ptr {};
                     70:        uint8 *start {};
                     71: };
                     72: 
                     73: // データの受け渡しはホストエンディアンで、
                     74: // アクセスはビッグエンディアンになる。境界への整列は不要。
                     75: class MemoryStreamBE : public MemoryStream
                     76: {
                     77:        using inherited = MemoryStream;
                     78:  public:
1.1.1.7 ! root       79:        explicit MemoryStreamBE(void *start_)
1.1.1.6   root       80:                : inherited(start_)
                     81:        {
                     82:        }
                     83: 
1.1       root       84:        // BE で2バイト読み込んでポインタを進める
1.1.1.7 ! root       85:        uint32 Read2() {
1.1.1.3   root       86:                uint32 data;
1.1.1.7 ! root       87:                data  = Read1() << 8;
        !            88:                data |= Read1();
1.1.1.3   root       89:                return data;
1.1       root       90:        }
                     91:        // BE で4バイト読み込んでポインタを進める
1.1.1.7 ! root       92:        uint32 Read4() {
1.1.1.3   root       93:                uint32 data;
1.1.1.7 ! root       94:                data  = Read2() << 16;
        !            95:                data |= Read2();
1.1.1.3   root       96:                return data;
1.1       root       97:        }
                     98: 
                     99:        // BE で2バイト書き込んでポインタを進める
1.1.1.7 ! root      100:        void Write2(uint32 data) {
        !           101:                Write1(data >> 8);
        !           102:                Write1(data & 0xff);
1.1       root      103:        }
                    104:        // BE で4バイト書き込んでポインタを進める
1.1.1.7 ! root      105:        void Write4(uint32 data) {
        !           106:                Write2(data >> 16);
        !           107:                Write2(data & 0xffff);
1.1       root      108:        }
1.1.1.6   root      109: };
1.1       root      110: 
1.1.1.6   root      111: // データの受け渡しはホストエンディアンで、
                    112: // アクセスはリトルエンディアンになる。境界への整列は不要。
                    113: class MemoryStreamLE : public MemoryStream
                    114: {
                    115:        using inherited = MemoryStream;
                    116:  public:
1.1.1.7 ! root      117:        explicit MemoryStreamLE(void *start_)
1.1.1.6   root      118:                : inherited(start_)
                    119:        {
1.1.1.5   root      120:        }
                    121: 
1.1.1.6   root      122:        // LE で2バイト読み込んでポインタを進める
1.1.1.7 ! root      123:        uint32 Read2() {
1.1.1.6   root      124:                uint32 data;
1.1.1.7 ! root      125:                data  = Read1();
        !           126:                data |= Read1() << 8;
1.1.1.6   root      127:                return data;
                    128:        }
                    129:        // LE で4バイト読み込んでポインタを進める
1.1.1.7 ! root      130:        uint32 Read4() {
1.1.1.6   root      131:                uint32 data;
1.1.1.7 ! root      132:                data  = Read2();
        !           133:                data |= Read2() << 16;
1.1.1.6   root      134:                return data;
1.1.1.5   root      135:        }
                    136: 
1.1.1.6   root      137:        // LE で2バイト書き込んでポインタを進める。
1.1.1.7 ! root      138:        void Write2(uint32 data) {
        !           139:                Write1(data & 0xff);
        !           140:                Write1(data >> 8);
1.1.1.6   root      141:        }
                    142:        // LE で4バイト書き込んでポインタを進める。
1.1.1.7 ! root      143:        void Write4(uint32 data) {
        !           144:                Write2(data & 0xffff);
        !           145:                Write2(data >> 16);
1.1.1.6   root      146:        }
                    147:        // LE で8バイト書き込んでポインタを進める。
1.1.1.7 ! root      148:        void Write8(uint64 data) {
        !           149:                Write4((uint32)data);
        !           150:                Write4(data >> 32);
1.1.1.6   root      151:        }
1.1       root      152: };

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.