/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ using System; using System.Collections.Generic; using System.Text; namespace OpenTraffic.Model { [Serializable] public class ClassField { public int Field { get; private set; } public ClassField(string str) { Field = 0; if (str != null) { for (int i = str.Length - 1; i >= 0; i--) { char c = str[i]; Field = Field << 1; if (c != '0') Field += 1; } } } public ClassField(int i) { this.Field = i; } public ClassField(bool b) { Reset(b); } public void Reset(bool b) { Field = b ? 0xffffff : 0; } public bool IsSet(VehicleClass c) { if (c != null) { return (((1 << (c.IdRegister - 1)) & Field) != 0); } return false; } public void Set(VehicleClass c, bool value) { if (c != null) { if (IsSet(c) == value) return; Field = ((1 << (c.IdRegister - 1)) ^ Field); } } public static ClassField operator |(ClassField cf1, ClassField cf2) { return BitwiseOr(cf1, cf2); } public static ClassField BitwiseOr(ClassField cf1, ClassField cf2) { if (cf1 != null && cf2 != null) { return new ClassField(cf1.Field | cf2.Field); } return null; } public static ClassField operator &(ClassField cf1, ClassField cf2) { return BitwiseAnd(cf1, cf2); } public static ClassField BitwiseAnd(ClassField cf1, ClassField cf2) { if (cf1 != null && cf2 != null) { return new ClassField(cf1.Field & cf2.Field); } return null; } } }