memory.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2006 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_MEMORY_H
00021 #define ARCHON_UTILITIES_MEMORY_H
00022 
00023 #include <algorithm>
00024 #include <limits>
00025 #include <vector>
00026 
00027 namespace Archon
00028 {
00029   namespace Utilities
00030   {
00031     using namespace std;
00032 
00045     inline int findLeastSignificantBit(unsigned v)
00046     {
00047       if(!v) return -1;
00048       int i = numeric_limits<unsigned char>::digits * sizeof(v) - 1;
00049       v <<= 1;
00050       while(v) { v <<= 1; --i; }
00051       return i;
00052     }
00053 
00054 
00070     inline int findMostSignificantBit(unsigned v)
00071     {
00072       if(!v) return -1;
00073       int i = 0;
00074       v >>= 1; // Does not introduce new bits since v is unsigned.
00075       while(v) { v >>= 1; ++i; }
00076       return i;
00077     }
00078 
00079 
00108     vector<bool> detectNativeEndianness();
00109 
00110 
00124     bool compareEndianness(const vector<bool> &a,
00125                            const vector<bool> &b = vector<bool>(),
00126                            int levels = 0);
00127 
00128 
00137     vector<int> computeBytePermutation(const vector<bool> &endianness, int levels);
00138 
00139 
00152     template<typename T>
00153     inline T readWithBytePermutation(const char *data,
00154                                      const vector<int> &bytePermutation)
00155     {
00156       T v;
00157       char *p = reinterpret_cast<char *>(&v);
00158       for(int i=0; i<static_cast<int>(sizeof(T)); ++i)
00159         p[bytePermutation[i]] = data[i];
00160       return v;
00161     }
00162 
00163 
00176     template<typename T>
00177     inline T readWithSpecificEndianness(const char *data,
00178                                         const vector<bool> &endianness)
00179     {
00180       vector<int> bytePermutation =
00181         computeBytePermutation(endianness, findMostSignificantBit(sizeof(T)));
00182       return readWithBytePermutation<T>(data, bytePermutation);
00183     }
00184 
00185 
00194     template<typename T>
00195     struct Array
00196     {
00201       Array(T *p = 0): p(p) {}
00202 
00211       Array(size_t n): p(new T[n]) {}
00212 
00220       Array(size_t n, T v): p(new T[n])
00221       {
00222         fill(p, p+n, v);
00223       }
00224 
00225       ~Array() { delete[] p; }
00226 
00236       const T *get() const { return p; }
00237 
00241       T *get() { return p; }
00242 
00251       T *out() { T *q=p; p=0; return q; }
00252 
00258       void reset(T *q = 0)
00259       {
00260         if(p == q) return;
00261         delete[] p;
00262         p = q;
00263       }
00264 
00271       void reset(size_t n)
00272       {
00273         reset(new T[n]);
00274       }
00275 
00281       void reset(size_t n, T v)
00282       {
00283         reset(new T[n]);
00284         fill(p, p+n, v);
00285       }
00286 
00290       const T &operator[](int i) const { return p[i]; }
00291 
00295       T &operator[](int i) { return p[i]; }
00296 
00303       operator bool() const { return p; }
00304 
00305     private:
00306       T *p;
00307     };
00308 
00309     typedef Array<char> MemoryBuffer;
00310   }
00311 }
00312 
00313 #endif // ARCHON_UTILITIES_MEMORY_H

Generated on Sun Jul 30 22:55:44 2006 for Archon by  doxygen 1.4.4