1 /******************************************************************************\ 2 * LuaUserData.cpp * 3 * A C++ equivalent of a Lua userdata. * 4 * * 5 * * 6 * Copyright (C) 2005-2013 by Leandro Motta Barros. * 7 * * 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 #include <Diluculum/LuaUserData.hpp> 28 #include <cstring> 29 30 31 namespace Diluculum 32 { 33 // - LuaUserData::LuaUserData ----------------------------------------------- LuaUserData(size_t size)34 LuaUserData::LuaUserData (size_t size) 35 : size_(size), data_ (new char[size_]) 36 { } 37 38 LuaUserData(const LuaUserData & other)39 LuaUserData::LuaUserData (const LuaUserData& other) 40 : size_(other.getSize()), data_ (new char[size_]) 41 { 42 memcpy (data_.get(), other.getData(), getSize()); 43 } 44 45 46 47 // - LuaUserData::operator= ------------------------------------------------- operator =(const LuaUserData & rhs)48 const LuaUserData& LuaUserData::operator= (const LuaUserData& rhs) 49 { 50 size_ = rhs.getSize(); 51 data_.reset (new char[getSize()]); 52 memcpy (getData(), rhs.getData(), getSize()); 53 return *this; 54 } 55 56 57 58 // - LuaUserData::operator> ------------------------------------------------- operator >(const LuaUserData & rhs) const59 bool LuaUserData::operator> (const LuaUserData& rhs) const 60 { 61 if (getSize() > rhs.getSize()) 62 return true; 63 else if (getSize() < rhs.getSize()) 64 return false; 65 else // getSize() == rhs.getSize() 66 return memcmp (getData(), rhs.getData(), getSize()) > 0; 67 } 68 69 70 71 // - LuaUserData::operator< ------------------------------------------------- operator <(const LuaUserData & rhs) const72 bool LuaUserData::operator< (const LuaUserData& rhs) const 73 { 74 if (getSize() < rhs.getSize()) 75 return true; 76 else if (getSize() > rhs.getSize()) 77 return false; 78 else // getSize() == rhs.getSize() 79 return memcmp (getData(), rhs.getData(), getSize()) < 0; 80 } 81 82 83 84 // - LuaUserData::operator== ------------------------------------------------ operator ==(const LuaUserData & rhs) const85 bool LuaUserData::operator== (const LuaUserData& rhs) const 86 { 87 return getSize() == rhs.getSize() 88 && memcmp (getData(), rhs.getData(), getSize()) == 0; 89 } 90 91 92 93 // - LuaUserData::operator!= ------------------------------------------------ operator !=(const LuaUserData & rhs) const94 bool LuaUserData::operator!= (const LuaUserData& rhs) const 95 { 96 return getSize() != rhs.getSize() 97 || memcmp (getData(), rhs.getData(), getSize()) != 0; 98 } 99 100 } // namespace Diluculum 101