CharUnits.h revision c3c90b25cf321d851314f0f19f67e9a00df0da0d
1//===--- CharUnits.h - Character units for sizes and offsets ----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the CharUnits class 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_CHARUNITS_H 15#define LLVM_CLANG_AST_CHARUNITS_H 16 17#include "llvm/System/DataTypes.h" 18 19#include <string> 20 21namespace clang { 22 23 /// CharUnits - This is an opaque type for sizes expressed in character units. 24 /// Instances of this type represent a quantity as a multiple of the size 25 /// of the standard C type, char, on the target architecture. As an opaque 26 /// type, CharUnits protects you from accidentally combining operations on 27 /// quantities in bit units and character units. 28 /// 29 /// It should be noted that characters and bytes are distinct concepts. Bytes 30 /// refer to addressable units of data storage on the target machine, and 31 /// characters are members of a set of elements used for the organization, 32 /// control, or representation of data. According to C99, bytes are allowed 33 /// to exceed characters in size, although currently, clang only supports 34 /// architectures where the two are the same size. 35 /// 36 /// For portability, never assume that a target character is 8 bits wide. Use 37 /// CharUnit values whereever you calculate sizes, offsets, or alignments 38 /// in character units. 39 class CharUnits { 40 public: 41 typedef int64_t RawType; 42 43 private: 44 RawType Quantity; 45 46 explicit CharUnits(RawType C) : Quantity(C) {} 47 48 public: 49 50 /// CharUnits - A default constructor. 51 CharUnits() : Quantity(0) {} 52 53 /// Zero - Construct a CharUnits quantity of zero. 54 static CharUnits Zero() { 55 return CharUnits(0); 56 } 57 58 /// One - Construct a CharUnits quantity of one. 59 static CharUnits One() { 60 return CharUnits(1); 61 } 62 63 /// fromRaw - Construct a CharUnits quantity from a raw integer type. 64 static CharUnits fromRaw(RawType Quantity) { 65 return CharUnits(Quantity); 66 } 67 68 // Compound assignment. 69 CharUnits& operator+= (const CharUnits &Other) { 70 Quantity += Other.Quantity; 71 return *this; 72 } 73 CharUnits& operator-= (const CharUnits &Other) { 74 Quantity -= Other.Quantity; 75 return *this; 76 } 77 78 // Comparison operators. 79 bool operator== (const CharUnits &Other) const { 80 return Quantity == Other.Quantity; 81 } 82 bool operator!= (const CharUnits &Other) const { 83 return Quantity != Other.Quantity; 84 } 85 86 // Relational operators. 87 bool operator< (const CharUnits &Other) const { 88 return Quantity < Other.Quantity; 89 } 90 bool operator<= (const CharUnits &Other) const { 91 return Quantity <= Other.Quantity; 92 } 93 bool operator> (const CharUnits &Other) const { 94 return Quantity > Other.Quantity; 95 } 96 bool operator>= (const CharUnits &Other) const { 97 return Quantity >= Other.Quantity; 98 } 99 100 // Other predicates. 101 102 /// isZero - Test whether the quantity equals zero. 103 bool isZero() const { return Quantity == 0; } 104 105 /// isOne - Test whether the quantity equals one. 106 bool isOne() const { return Quantity == 1; } 107 108 /// isPositive - Test whether the quanity is greater than zero. 109 bool isPositive() const { return Quantity > 0; } 110 111 /// isNegative - Test whether the quantity is less than zero. 112 bool isNegative() const { return Quantity < 0; } 113 114 // Arithmetic operators. 115 CharUnits operator* (RawType N) const { 116 return CharUnits(Quantity * N); 117 } 118 CharUnits operator/ (RawType N) const { 119 return CharUnits(Quantity / N); 120 } 121 RawType operator/ (const CharUnits &Other) const { 122 return Quantity / Other.Quantity; 123 } 124 CharUnits operator% (RawType N) const { 125 return CharUnits(Quantity % N); 126 } 127 RawType operator% (const CharUnits &Other) const { 128 return Quantity % Other.Quantity; 129 } 130 CharUnits operator+ (const CharUnits &Other) const { 131 return CharUnits(Quantity + Other.Quantity); 132 } 133 CharUnits operator- (const CharUnits &Other) const { 134 return CharUnits(Quantity - Other.Quantity); 135 } 136 137 // Conversions. 138 139 /// toString - Convert to a string. 140 std::string toString() const; 141 142 /// getRaw - Get the raw integer representation of this quantity. 143 RawType getRaw() const { return Quantity; } 144 145 146 }; // class CharUnit 147} // namespace clang 148 149inline clang::CharUnits operator* (clang::CharUnits::RawType Scale, 150 const clang::CharUnits &CU) { 151 return CU * Scale; 152} 153 154#endif // LLVM_CLANG_AST_CHARUNITS_H 155