* Merged in jbm's changes for Linux compatibility, fixing Mac compatibility in the process :)
* Fixed two regressions having to do with the _previousTrailerPosition in VersionDictionary.cpp.
* Made sure RTTI is enabled in the OttomanTest target because gtest requires it.
5 * Created by Jens Alfke on 8/18/09.
6 * Copyright 2009 Jens Alfke. All rights reserved.
7 * BSD-Licensed: See the file "LICENSE.txt" for details.
10 #ifndef _MOOSEYARD_BASE_
11 #define _MOOSEYARD_BASE_
16 #ifdef __MACH__ /* OS X specific bits */
17 #include <CoreFoundation/CFByteOrder.h>
22 /** A 32-bit file position/offset. Supports files of up to 4GB. */
23 typedef uint32_t FilePosition;
24 const FilePosition kNoFilePosition = (FilePosition)-1L;
27 /** A simple structure representing a range of memory (a pointer plus a length).
28 It doesn't do any memory management; it just points. */
33 Blob() :bytes(NULL), length(0) { }
34 Blob (const void *b, size_t len) :bytes(b), length(len) { }
35 Blob (const char *str);
36 const void* end() const {return (const uint8_t*)bytes+length;}
38 operator bool() const {return bytes!=NULL;}
39 bool operator== (const Blob &b) {return bytes==b.bytes && length==b.length;}
40 bool equals (const Blob &b) const {return length==b.length && memcmp(bytes,b.bytes,length)==0;}
41 void clear() {bytes=NULL; length=0;}
45 // Utility to offset a pointer by some number of bytes.
46 inline const void* offsetBy(const void *src, size_t off) {return (const uint8_t*)src + off;}
47 inline void* offsetBy(void *src, size_t off) {return (uint8_t*)src + off;}
50 #define UNCOPYABLE(CLASS) private: CLASS(const CLASS&); CLASS& operator=(const CLASS&)
54 #pragma mark LITTLE-ENDIAN:
56 /** A template for numeric types that are always stored in little-endian form.
57 Useful for structures stored in files that need to be useable on all architectures. */
58 template <typename INT, typename REP=INT>
61 inline LittleEndian ();
62 inline LittleEndian (INT);
63 inline operator INT() const;
64 inline LittleEndian& operator= (INT);
65 inline LittleEndian& operator= (const LittleEndian&);
66 inline LittleEndian& operator++();
67 inline LittleEndian& operator--();
69 static inline REP makeLittle (INT);
70 static inline INT makeNative (REP);
74 typedef LittleEndian<double,CFSwappedFloat64> LittleEndianDouble;
77 // Implementation gunk:
78 template <typename INT, typename REP>
79 inline LittleEndian<INT,REP>::LittleEndian () :_value() { }
80 template <typename INT, typename REP>
81 inline LittleEndian<INT,REP>::LittleEndian (INT i) :_value(makeLittle(i)) { }
82 template <typename INT, typename REP>
83 inline LittleEndian<INT,REP>::operator INT() const {return makeNative(_value);}
84 template <typename INT, typename REP>
85 inline LittleEndian<INT,REP>& LittleEndian<INT,REP>::operator++() {return *this = (INT)*this + 1;}
86 template <typename INT, typename REP>
87 inline LittleEndian<INT,REP>& LittleEndian<INT,REP>::operator--() {return *this = (INT)*this - 1;}
89 template <typename INT, typename REP>
90 inline LittleEndian<INT,REP>& LittleEndian<INT,REP>::operator= (INT i) {
91 _value = makeLittle(i);
94 template <typename INT, typename REP>
95 inline LittleEndian<INT,REP>& LittleEndian<INT,REP>::operator= (const LittleEndian<INT,REP> &p) {
100 #ifdef __COREFOUNDATION_CFBYTEORDER__
101 template <> inline uint32_t LittleEndian<uint32_t>::makeLittle (uint32_t i)
102 {return OSSwapHostToLittleInt32(i);}
103 template <> inline uint32_t LittleEndian<uint32_t>::makeNative (uint32_t i)
104 {return OSSwapLittleToHostInt32(i);}
105 template <> inline uint16_t LittleEndian<uint16_t>::makeLittle (uint16_t i)
106 {return OSSwapHostToLittleInt16(i);}
107 template <> inline uint16_t LittleEndian<uint16_t>::makeNative (uint16_t i)
108 {return OSSwapLittleToHostInt16(i);}
109 template <> inline CFSwappedFloat64 LittleEndian<double,CFSwappedFloat64>::makeLittle (double d)
110 {return CFConvertDoubleHostToSwapped(d);}
111 template <> inline double LittleEndian<double,CFSwappedFloat64>::makeNative (CFSwappedFloat64 d)
112 {return CFConvertDoubleSwappedToHost(d);}
114 //FIXME: Not implemented yet for non-Mac platforms
115 template <> inline uint32_t LittleEndian<uint32_t>::makeLittle (uint32_t i)
117 template <> inline uint32_t LittleEndian<uint32_t>::makeNative (uint32_t i)
119 template <> inline uint16_t LittleEndian<uint16_t>::makeLittle (uint16_t i)
121 template <> inline uint16_t LittleEndian<uint16_t>::makeNative (uint16_t i)
123 template <> inline CFSwappedFloat64 LittleEndian<double,CFSwappedFloat64>::makeLittle (double d)
125 template <> inline double LittleEndian<double,CFSwappedFloat64>::makeNative (CFSwappedFloat64 d)
130 #endif /* _MOOSEYARD_BASE_ */