OpenMW
components/misc/slice_array.hpp
Go to the documentation of this file.
00001 /*
00002   OpenMW - The completely unofficial reimplementation of Morrowind
00003   Copyright (C) 2008-2010  Nicolay Korslund
00004   Email: < korslund@gmail.com >
00005   WWW: http://openmw.sourceforge.net/
00006 
00007   This file (slice_array.h) is part of the OpenMW package.
00008 
00009   OpenMW is distributed as free software: you can redistribute it
00010   and/or modify it under the terms of the GNU General Public License
00011   version 3, as published by the Free Software Foundation.
00012 
00013   This program is distributed in the hope that it will be useful, but
00014   WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License
00019   version 3 along with this program. If not, see
00020   http://www.gnu.org/licenses/ .
00021 
00022  */
00023 
00024 #ifndef MISC_SLICE_ARRAY_H
00025 #define MISC_SLICE_ARRAY_H
00026 
00027 // A simple array implementation containing a pointer and a
00028 // length. Used for holding slices into a data buffer.
00029 #include <string.h>
00030 #include <string>
00031 
00032 namespace Misc
00033 {
00034 
00035 template <class T>
00036 struct SliceArray
00037 {
00038   const T* ptr;
00039   size_t length;
00040 
00042   SliceArray() : ptr(0), length(0) {}
00043 
00045   SliceArray(const T* _ptr, size_t _length)
00046     : ptr(_ptr), length(_length) {}
00047 
00049   SliceArray(const char* str)
00050   {
00051     ptr = str;
00052     length = strlen(str);
00053   }
00054 
00055   bool operator==(SliceArray &t)
00056   {
00057     return
00058       length == t.length &&
00059       (memcmp(ptr,t.ptr, length*sizeof(T)) == 0);
00060   }
00061 
00063   bool operator==(const char* str)
00064   {
00065     return
00066       str[length] == 0 &&
00067       (strncmp(ptr, str, length) == 0);
00068   }
00069 
00072   std::string toString()
00073   { return std::string(ptr,length); }
00074 };
00075 
00076 typedef SliceArray<char> SString;
00077 typedef SliceArray<int> IntArray;
00078 typedef SliceArray<float> FloatArray;
00079 
00080 }
00081 
00082 #endif