github.com/BurntSushi/xgb@v0.0.0-20210121224620-deaf085860bc/xgbgen/size.go (about) 1 package main 2 3 // Size corresponds to an expression that represents the number of bytes 4 // in some *thing*. Generally, sizes are used to allocate buffers and to 5 // inform X how big requests are. 6 // Size is basically a thin layer over an Expression that yields easy methods 7 // for adding and multiplying sizes. 8 type Size struct { 9 Expression 10 exact bool 11 } 12 13 // newFixedSize creates a new Size with some fixed and known value. 14 func newFixedSize(fixed uint, exact bool) Size { 15 return Size{&Value{v: int(fixed)}, exact} 16 } 17 18 // newExpressionSize creates a new Size with some expression. 19 func newExpressionSize(variable Expression, exact bool) Size { 20 return Size{variable, exact} 21 } 22 23 // Add adds s1 and s2 and returns a new Size. 24 func (s1 Size) Add(s2 Size) Size { 25 return Size{newBinaryOp("+", s1, s2), s1.exact && s2.exact} 26 } 27 28 // Multiply mupltiplies s1 and s2 and returns a new Size. 29 func (s1 Size) Multiply(s2 Size) Size { 30 return Size{newBinaryOp("*", s1, s2), s1.exact && s2.exact} 31 }