#ifndef _WIRES_H #define _WIRES_H #include #include using namespace std; #include "tvector.h" // A wire has current flowing through it. // When the current changes, a wire notifies // all the gates listening on the wire that the current // has changed. The gates act accordingly (see gates.h). // // Some of the "gates" are really connectors to other wires. // A connector allows gates to be strung together, but connectors // are more like solder than real gates. As such, connectors aren't // part of a CompositeGate. To facilitate finding a wire's // connectors, a ConnectorIterator can be used to access all a wire's // connectors. class Gate; class Connector; class Wire { public: Wire(const string& name=""); virtual ~Wire(); virtual bool GetSignal() const; // true/false, on/off virtual string tostring() const; // for I/O virtual void SetSignal(bool signal); // set signal, propagate virtual void AddGate(Gate * g); // g monitors this wire virtual void RemoveGate(Gate * g); // g stops monitoring virtual int Number() const; // which wire is this? friend class ConnectorIterator; // access myGates private: tvector myGates; bool mySignal; string myName; int myNumber; static int ourCount; // class wide, keeps count }; ostream& operator << (ostream& out, const Wire& w); // A WireFactory is used to encapsulate wire creation. // If wires are "ordered" from the factory, the factory takes // care of cleaning up the wires when the factory ceases to exist. // This is a rudimentary factory, there's no facility for clients // to recycle wires and the factory doesn't clean up the gates // attached to the wires it destroys. // // MakeWire -- creates a new wire // GetWire -- retrieves an already created wire by the wire's number class WireFactory { public: WireFactory(); virtual ~WireFactory(); virtual Wire * MakeWire(const string& name="wire"); // create anew virtual Wire * GetWire(int num) const; // get by number private: tvector myWires; }; // standard tapestry iterator for iterating over all // connectors attached to a wire class ConnectorIterator { public: ConnectorIterator(Wire* w); void Init(); bool HasMore(); void Next(); Connector * Current(); private: Wire * myWire; Connector * myConnector; int myIndex; }; #endif