2012-04-30 08:40:55 +02:00
|
|
|
package main
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Size corresponds to an expression that represents the number of bytes
|
|
|
|
// in some *thing*. Generally, sizes are used to allocate buffers and to
|
|
|
|
// inform X how big requests are.
|
|
|
|
// Size is basically a thin layer over an Expression that yields easy methods
|
|
|
|
// for adding and multiplying sizes.
|
2012-04-30 08:40:55 +02:00
|
|
|
type Size struct {
|
|
|
|
Expression
|
2014-05-02 15:09:23 +02:00
|
|
|
exact bool
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// newFixedSize creates a new Size with some fixed and known value.
|
2014-05-02 15:09:23 +02:00
|
|
|
func newFixedSize(fixed uint, exact bool) Size {
|
|
|
|
return Size{&Value{v: int(fixed)}, exact}
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// newExpressionSize creates a new Size with some expression.
|
2014-05-02 15:09:23 +02:00
|
|
|
func newExpressionSize(variable Expression, exact bool) Size {
|
|
|
|
return Size{variable, exact}
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Add adds s1 and s2 and returns a new Size.
|
2012-04-30 08:40:55 +02:00
|
|
|
func (s1 Size) Add(s2 Size) Size {
|
2014-05-02 15:09:23 +02:00
|
|
|
return Size{newBinaryOp("+", s1, s2), s1.exact && s2.exact}
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Multiply mupltiplies s1 and s2 and returns a new Size.
|
2012-04-30 08:40:55 +02:00
|
|
|
func (s1 Size) Multiply(s2 Size) Size {
|
2014-05-02 15:09:23 +02:00
|
|
|
return Size{newBinaryOp("*", s1, s2), s1.exact && s2.exact}
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|