1 //===--- CharUnits.h - Character units for sizes and offsets ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CharUnits class
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_CHARUNITS_H
15 #define LLVM_CLANG_AST_CHARUNITS_H
17 #include "llvm/System/DataTypes.h"
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.
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.
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.
39 typedef int64_t QuantityType;
42 QuantityType Quantity;
44 explicit CharUnits(QuantityType C) : Quantity(C) {}
48 /// CharUnits - A default constructor.
49 CharUnits() : Quantity(0) {}
51 /// Zero - Construct a CharUnits quantity of zero.
52 static CharUnits Zero() {
56 /// One - Construct a CharUnits quantity of one.
57 static CharUnits One() {
61 /// fromQuantity - Construct a CharUnits quantity from a raw integer type.
62 static CharUnits fromQuantity(QuantityType Quantity) {
63 return CharUnits(Quantity);
66 // Compound assignment.
67 CharUnits& operator+= (const CharUnits &Other) {
68 Quantity += Other.Quantity;
71 CharUnits& operator-= (const CharUnits &Other) {
72 Quantity -= Other.Quantity;
76 // Comparison operators.
77 bool operator== (const CharUnits &Other) const {
78 return Quantity == Other.Quantity;
80 bool operator!= (const CharUnits &Other) const {
81 return Quantity != Other.Quantity;
84 // Relational operators.
85 bool operator< (const CharUnits &Other) const {
86 return Quantity < Other.Quantity;
88 bool operator<= (const CharUnits &Other) const {
89 return Quantity <= Other.Quantity;
91 bool operator> (const CharUnits &Other) const {
92 return Quantity > Other.Quantity;
94 bool operator>= (const CharUnits &Other) const {
95 return Quantity >= Other.Quantity;
100 /// isZero - Test whether the quantity equals zero.
101 bool isZero() const { return Quantity == 0; }
103 /// isOne - Test whether the quantity equals one.
104 bool isOne() const { return Quantity == 1; }
106 /// isPositive - Test whether the quantity is greater than zero.
107 bool isPositive() const { return Quantity > 0; }
109 /// isNegative - Test whether the quantity is less than zero.
110 bool isNegative() const { return Quantity < 0; }
112 // Arithmetic operators.
113 CharUnits operator* (QuantityType N) const {
114 return CharUnits(Quantity * N);
116 CharUnits operator/ (QuantityType N) const {
117 return CharUnits(Quantity / N);
119 QuantityType operator/ (const CharUnits &Other) const {
120 return Quantity / Other.Quantity;
122 CharUnits operator% (QuantityType N) const {
123 return CharUnits(Quantity % N);
125 QuantityType operator% (const CharUnits &Other) const {
126 return Quantity % Other.Quantity;
128 CharUnits operator+ (const CharUnits &Other) const {
129 return CharUnits(Quantity + Other.Quantity);
131 CharUnits operator- (const CharUnits &Other) const {
132 return CharUnits(Quantity - Other.Quantity);
137 /// getQuantity - Get the raw integer representation of this quantity.
138 QuantityType getQuantity() const { return Quantity; }
144 inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale,
145 const clang::CharUnits &CU) {
149 #endif // LLVM_CLANG_AST_CHARUNITS_H