Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 
     18 #ifndef CTSAUDIO_RWBUFFER_H
     19 #define CTSAUDIO_RWBUFFER_H
     20 
     21 #include <stdint.h>
     22 #include <utils/String8.h>
     23 #include "Log.h"
     24 
     25 /// utility for R/W buffer
     26 class RWBuffer {
     27 public:
     28     explicit RWBuffer(int capacity)
     29         : mCapacity(capacity),
     30           mWrPoint(0),
     31           mRdPoint(0) {
     32         mBuffer = new char[capacity];
     33     }
     34 
     35     ~RWBuffer() {
     36         delete[] mBuffer;
     37     }
     38 
     39     void reset() {
     40         mWrPoint = 0;
     41         mRdPoint = 0;
     42     }
     43 
     44     void resetWr() {
     45         mWrPoint = 0;
     46     }
     47 
     48     void resetRd() {
     49         mRdPoint = 0;
     50     }
     51 
     52     const char* getBuffer() {
     53         return mBuffer;
     54     }
     55     char* getUnwrittenBuffer() {
     56         return mBuffer + mWrPoint;
     57     }
     58 
     59     inline void assertWriteCapacity(int sizeToWrite) {
     60         ASSERT((mWrPoint + sizeToWrite) <= mCapacity);
     61     }
     62     void increaseWritten(int size) {
     63         assertWriteCapacity(0); // damage already done, but detect and panic if happened
     64         mWrPoint += size;
     65     }
     66 
     67     int getSizeWritten() {
     68         return mWrPoint;
     69     }
     70 
     71     int getSizeRead() {
     72         return mRdPoint;
     73     }
     74 
     75     template <typename T> void write(T v) {
     76         char* src = (char*)&v;
     77         assertWriteCapacity(sizeof(T));
     78         memcpy(mBuffer + mWrPoint, src, sizeof(T));
     79         mWrPoint += sizeof(T);
     80     }
     81     void writeStr(const android::String8& str) {
     82         size_t len = str.length();
     83         assertWriteCapacity(len);
     84         memcpy(mBuffer + mWrPoint, str.string(), len);
     85         mWrPoint += len;
     86     }
     87     template <typename T> T read() {
     88         T v;
     89         ASSERT((mRdPoint + sizeof(T)) <= mWrPoint);
     90         memcpy(&v, mBuffer + mRdPoint, sizeof(T));
     91         mRdPoint += sizeof(T);
     92     }
     93 
     94 private:
     95     int mCapacity;
     96     int mWrPoint;
     97     int mRdPoint;
     98     char* mBuffer;
     99 };
    100 
    101 
    102 #endif // CTSAUDIO_RWBUFFER_H
    103