Home | History | Annotate | Download | only in cpp
      1 /**
      2  * Copied from node_buffer.h
      3  * see http://www.nodejs.org/
      4  *
      5  * Node's license follows:
      6  *
      7  * Copyright 2009, 2010 Ryan Lienhart Dahl. All rights reserved.
      8  * Permission is hereby granted, free of charge, to any person obtaining a copy
      9  * of this software and associated documentation files (the "Software"), to
     10  * deal in the Software without restriction, including without limitation the
     11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     12  * sell copies of the Software, and to permit persons to whom the Software is
     13  * furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in
     16  * all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     24  * IN THE SOFTWARE.
     25  */
     26 
     27 #ifndef MOCK_RIL_NODE_BUFFER_H_
     28 #define MOCK_RIL_NODE_BUFFER_H_
     29 
     30 #include <v8.h>
     31 #include "node_object_wrap.h"
     32 
     33 /* A buffer is a chunk of memory stored outside the V8 heap, mirrored by an
     34  * object in javascript. The object is not totally opaque, one can access
     35  * individual bytes with [] and slice it into substrings or sub-buffers
     36  * without copying memory.
     37  *
     38  * // return an ascii encoded string - no memory iscopied
     39  * buffer.asciiSlide(0, 3)
     40  *
     41  * // returns another buffer - no memory is copied
     42  * buffer.slice(0, 3)
     43  *
     44  * Interally, each javascript buffer object is backed by a "struct buffer"
     45  * object.  These "struct buffer" objects are either a root buffer (in the
     46  * case that buffer->root == NULL) or slice objects (in which case
     47  * buffer->root != NULL).  A root buffer is only GCed once all its slices
     48  * are GCed.
     49  */
     50 
     51 
     52 struct Blob_;
     53 
     54 class Buffer : public ObjectWrap {
     55  public:
     56   ~Buffer();
     57 
     58   static void Initialize(v8::Handle<v8::Object> target);
     59   static void InitializeObjectTemplate(v8::Handle<v8::ObjectTemplate> target);
     60   static Buffer* New(size_t length); // public constructor
     61   static inline bool HasInstance(v8::Handle<v8::Value> val) {
     62     if (!val->IsObject()) return false;
     63     v8::Local<v8::Object> obj = val->ToObject();
     64     return constructor_template->HasInstance(obj);
     65   }
     66 
     67   char* data();
     68   size_t length() const { return length_; }
     69   struct Blob_* blob() const { return blob_; }
     70   void   NewBlob(size_t length);
     71 
     72   int AsciiWrite(char *string, int offset, int length);
     73   int Utf8Write(char *string, int offset, int length);
     74 
     75  private:
     76   static v8::Persistent<v8::FunctionTemplate> constructor_template;
     77 
     78   static v8::Handle<v8::Value> New(const v8::Arguments &args);
     79   static v8::Handle<v8::Value> Slice(const v8::Arguments &args);
     80   static v8::Handle<v8::Value> BinarySlice(const v8::Arguments &args);
     81   static v8::Handle<v8::Value> AsciiSlice(const v8::Arguments &args);
     82   static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
     83   static v8::Handle<v8::Value> BinaryWrite(const v8::Arguments &args);
     84   static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args);
     85   static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args);
     86   static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args);
     87   static v8::Handle<v8::Value> Unpack(const v8::Arguments &args);
     88   static v8::Handle<v8::Value> Copy(const v8::Arguments &args);
     89 
     90   Buffer(size_t length);
     91   Buffer(Buffer *parent, size_t start, size_t end);
     92 
     93   size_t off_; // offset inside blob_
     94   size_t length_; // length inside blob_
     95   struct Blob_ *blob_;
     96 };
     97 
     98 #endif  // MOCK_RIL_NODE_BUFFER_H_
     99