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