bitworks/hwo.bitworks/MultipleBitsSource.cs

92 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using hwo.bitworks.identity;
namespace hwo.bitworks
{
public class MultipleBitsSource
{
static Dictionary<Type,List<MultipleBitsSource>> knownSources = new Dictionary<Type, List<MultipleBitsSource>>();
static protected void register(MultipleBitsSource s){
Type t = s.GetType();
if (!knownSources.ContainsKey(t)){
knownSources.Add(t,new List<MultipleBitsSource>());
}
knownSources[t].Add(s);
}
protected LogicBase[] sources;
public MultipleBitsSource()
{
register(this);
this.sources = null;
}
public MultipleBitsSource(LogicBase[] sources)
{
this.sources = sources;
}
public virtual int getBits(){
return sources.Length;
}
public virtual bool bitValue(int bit){
throw new NotImplementedException(String.Format("Class {0} doesn't implement needed method 'bool bitValue(int bit)'",this.GetType().FullName));
}
public LogicBase getLogicalBase(int bit){
return new BIT(this,bit);
}
public LogicBase[] getLogicalBases(){
return getLogicalBases(0,getBits());
}
public LogicBase[] getLogicalBases(int start,int len){
LogicBase[] lb = new LogicBase[len];
for (int n=0;n<len;n++){
lb[n] = getLogicalBase(n + start);
}
return lb;
}
public virtual BitIdentity getBitIdentity(int bit){
throw new NotImplementedException(String.Format("Class {0} doesn't implement needed method 'BitIdentity getBitIdentity(int bit)'",this.GetType().FullName));
}
public class BIT : LogicBase {
MultipleBitsSource source;
int bit;
public BIT(MultipleBitsSource source,int bit)
:base(new LogicBase[0])
{
this.source = source;
this.bit = bit;
}
public override bool value()
{
return source.bitValue(this.bit);
}
public override identity.BitIdentity identity()
{
return source.getBitIdentity(this.bit);
}
}
public string SourceName {
get {
int no = knownSources[GetType()].IndexOf(this);
return string.Format("{0}[{1}]",GetType().Name,no);
}
}
}
}