LogCabin
Core/Buffer.cc
Go to the documentation of this file.
00001 /* Copyright (c) 2012 Stanford University
00002  *
00003  * Permission to use, copy, modify, and distribute this software for any
00004  * purpose with or without fee is hereby granted, provided that the above
00005  * copyright notice and this permission notice appear in all copies.
00006  *
00007  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00008  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00009  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00010  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00011  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00012  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00013  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00014  */
00015 
00016 #include "Core/Buffer.h"
00017 
00018 namespace LogCabin {
00019 namespace Core {
00020 
00021 Buffer::Buffer()
00022     : data(NULL)
00023     , length(0)
00024     , deleter(NULL)
00025 {
00026 }
00027 
00028 Buffer::Buffer(void* data, uint64_t length, Deleter deleter)
00029     : data(data)
00030     , length(length)
00031     , deleter(deleter)
00032 {
00033 }
00034 
00035 Buffer::Buffer(Buffer&& other)
00036     : data(other.data)
00037     , length(other.length)
00038     , deleter(other.deleter)
00039 {
00040     other.data = NULL; // Not strictly necessary, but may help
00041     other.length = 0;  // catch bugs in caller code.
00042     other.deleter = NULL;
00043 }
00044 
00045 Buffer::~Buffer()
00046 {
00047     if (deleter != NULL)
00048         (*deleter)(data);
00049 }
00050 
00051 Buffer&
00052 Buffer::operator=(Buffer&& other)
00053 {
00054     if (deleter != NULL)
00055         (*deleter)(data);
00056     data = other.data;
00057     length = other.length;
00058     deleter = other.deleter;
00059     other.data = NULL; // Not strictly necessary, but may help
00060     other.length = 0;  // catch bugs in caller code.
00061     other.deleter = NULL;
00062     return *this;
00063 }
00064 
00065 void
00066 Buffer::setData(void* data, uint64_t length, Deleter deleter)
00067 {
00068     if (this->deleter != NULL)
00069         (*this->deleter)(this->data);
00070     this->data = data;
00071     this->length = length;
00072     this->deleter = deleter;
00073 }
00074 
00075 void
00076 Buffer::reset()
00077 {
00078     if (deleter != NULL)
00079         (*deleter)(data);
00080     data = NULL;
00081     length = 0;
00082     deleter = NULL;
00083 }
00084 
00085 } // namespace LogCabin::Core
00086 } // namespace LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines