| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef REMOTING_BASE_TYPED_BUFFER_H_ | 5 #ifndef REMOTING_BASE_TYPED_BUFFER_H_ | 
| 6 #define REMOTING_BASE_TYPED_BUFFER_H_ | 6 #define REMOTING_BASE_TYPED_BUFFER_H_ | 
| 7 | 7 | 
| 8 #include <assert.h> | 8 #include <assert.h> | 
|  | 9 #include <stdint.h> | 
| 9 | 10 | 
| 10 #include <algorithm> | 11 #include <algorithm> | 
| 11 | 12 | 
| 12 #include "base/basictypes.h" |  | 
| 13 #include "base/logging.h" | 13 #include "base/logging.h" | 
| 14 #include "base/move.h" | 14 #include "base/move.h" | 
| 15 | 15 | 
| 16 namespace remoting { | 16 namespace remoting { | 
| 17 | 17 | 
| 18 // A scoper for a variable-length structure such as SID, SECURITY_DESCRIPTOR and | 18 // A scoper for a variable-length structure such as SID, SECURITY_DESCRIPTOR and | 
| 19 // similar. These structures typically consist of a header followed by variable- | 19 // similar. These structures typically consist of a header followed by variable- | 
| 20 // length data, so the size may not match sizeof(T). The class supports | 20 // length data, so the size may not match sizeof(T). The class supports | 
| 21 // move-only semantics and typed buffer getters. | 21 // move-only semantics and typed buffer getters. | 
| 22 template <typename T> | 22 template <typename T> | 
| 23 class TypedBuffer { | 23 class TypedBuffer { | 
| 24   MOVE_ONLY_TYPE_FOR_CPP_03(TypedBuffer) | 24   MOVE_ONLY_TYPE_FOR_CPP_03(TypedBuffer) | 
| 25 | 25 | 
| 26  public: | 26  public: | 
| 27   TypedBuffer() : TypedBuffer(0) {} | 27   TypedBuffer() : TypedBuffer(0) {} | 
| 28 | 28 | 
| 29   // Creates an instance of the object allocating a buffer of the given size. | 29   // Creates an instance of the object allocating a buffer of the given size. | 
| 30   explicit TypedBuffer(uint32 length) : buffer_(NULL), length_(length) { | 30   explicit TypedBuffer(uint32_t length) : buffer_(NULL), length_(length) { | 
| 31     if (length_ > 0) | 31     if (length_ > 0) | 
| 32       buffer_ = reinterpret_cast<T*>(new uint8[length_]); | 32       buffer_ = reinterpret_cast<T*>(new uint8_t[length_]); | 
| 33   } | 33   } | 
| 34 | 34 | 
| 35   TypedBuffer(TypedBuffer&& rvalue) : TypedBuffer() { Swap(rvalue); } | 35   TypedBuffer(TypedBuffer&& rvalue) : TypedBuffer() { Swap(rvalue); } | 
| 36 | 36 | 
| 37   ~TypedBuffer() { | 37   ~TypedBuffer() { | 
| 38     if (buffer_) { | 38     if (buffer_) { | 
| 39       delete[] reinterpret_cast<uint8*>(buffer_); | 39       delete[] reinterpret_cast<uint8_t*>(buffer_); | 
| 40       buffer_ = NULL; | 40       buffer_ = NULL; | 
| 41     } | 41     } | 
| 42   } | 42   } | 
| 43 | 43 | 
| 44   TypedBuffer& operator=(TypedBuffer&& rvalue) { | 44   TypedBuffer& operator=(TypedBuffer&& rvalue) { | 
| 45     Swap(rvalue); | 45     Swap(rvalue); | 
| 46     return *this; | 46     return *this; | 
| 47   } | 47   } | 
| 48 | 48 | 
| 49   // Accessors to get the owned buffer. | 49   // Accessors to get the owned buffer. | 
| 50   // operator* and operator-> will assert() if there is no current buffer. | 50   // operator* and operator-> will assert() if there is no current buffer. | 
| 51   T& operator*() const { | 51   T& operator*() const { | 
| 52     assert(buffer_ != NULL); | 52     assert(buffer_ != NULL); | 
| 53     return *buffer_; | 53     return *buffer_; | 
| 54   } | 54   } | 
| 55   T* operator->() const  { | 55   T* operator->() const  { | 
| 56     assert(buffer_ != NULL); | 56     assert(buffer_ != NULL); | 
| 57     return buffer_; | 57     return buffer_; | 
| 58   } | 58   } | 
| 59   T* get() const { return buffer_; } | 59   T* get() const { return buffer_; } | 
| 60 | 60 | 
| 61   uint32 length() const { return length_; } | 61   uint32_t length() const { return length_; } | 
| 62 | 62 | 
| 63   // Helper returning a pointer to the structure starting at a specified byte | 63   // Helper returning a pointer to the structure starting at a specified byte | 
| 64   // offset. | 64   // offset. | 
| 65   T* GetAtOffset(uint32 offset) { | 65   T* GetAtOffset(uint32_t offset) { | 
| 66     return reinterpret_cast<T*>(reinterpret_cast<uint8*>(buffer_) + offset); | 66     return reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(buffer_) + offset); | 
| 67   } | 67   } | 
| 68 | 68 | 
| 69   // Allow TypedBuffer<T> to be used in boolean expressions, but not | 69   // Allow TypedBuffer<T> to be used in boolean expressions, but not | 
| 70   // implicitly convertible to a real bool (which is dangerous). | 70   // implicitly convertible to a real bool (which is dangerous). | 
| 71   typedef T* TypedBuffer::*Testable; | 71   typedef T* TypedBuffer::*Testable; | 
| 72   operator Testable() const { return buffer_ ? &TypedBuffer::buffer_ : NULL; } | 72   operator Testable() const { return buffer_ ? &TypedBuffer::buffer_ : NULL; } | 
| 73 | 73 | 
| 74   // Swap two buffers. | 74   // Swap two buffers. | 
| 75   void Swap(TypedBuffer& other) { | 75   void Swap(TypedBuffer& other) { | 
| 76     std::swap(buffer_, other.buffer_); | 76     std::swap(buffer_, other.buffer_); | 
| 77     std::swap(length_, other.length_); | 77     std::swap(length_, other.length_); | 
| 78   } | 78   } | 
| 79 | 79 | 
| 80  private: | 80  private: | 
| 81   // Points to the owned buffer. | 81   // Points to the owned buffer. | 
| 82   T* buffer_; | 82   T* buffer_; | 
| 83 | 83 | 
| 84   // Length of the owned buffer in bytes. | 84   // Length of the owned buffer in bytes. | 
| 85   uint32 length_; | 85   uint32_t length_; | 
| 86 }; | 86 }; | 
| 87 | 87 | 
| 88 }  // namespace remoting | 88 }  // namespace remoting | 
| 89 | 89 | 
| 90 #endif  // REMOTING_BASE_TYPED_BUFFER_H_ | 90 #endif  // REMOTING_BASE_TYPED_BUFFER_H_ | 
| OLD | NEW | 
|---|