#pragma once #include "cmpnnt.h" #include "slot.h" #include "memstrm.h" class PlugStream; class Simulation; class Entity; //########################################################################## //########################### AttributeWatcher ####################### //########################################################################## class AttributeWatcher: public Component { public: // //----------------------------------------------------------------------- // Destructor //----------------------------------------------------------------------- // ~AttributeWatcher(); Logical TestInstance() const; // //----------------------------------------------------------------------- // DidAttributeChange // // Returns True if attribute has changed since the last time // DidAttributeChange was called. //----------------------------------------------------------------------- // Logical DidAttributeChange(); protected: // //----------------------------------------------------------------------- // Constructor //----------------------------------------------------------------------- // AttributeWatcher( PlugStream *stream, Entity *entity ); // //----------------------------------------------------------------------- // SendNotificationOfChange //----------------------------------------------------------------------- // virtual void SendNotificationOfChange(); // //----------------------------------------------------------------------- // BuildFromPage //----------------------------------------------------------------------- // static void BuildFromPage( PlugStream *stream, NameList *name_list, ClassID class_ID, ObjectID object_ID ); // //----------------------------------------------------------------------- // Protected data //----------------------------------------------------------------------- // Simulation* simulation; void* attributePointer; Logical attributeChanged; Logical sendNotificationOnChange; }; //########################################################################## //########################### AttributeWatcherOf ##################### //########################################################################## template class AttributeWatcherOf: public AttributeWatcher { public: ~AttributeWatcherOf(); // //----------------------------------------------------------------------- // Accessors //----------------------------------------------------------------------- // T GetCurrentValue() {return currentValue;} T* GetCurrentPointer() {return ¤tValue;} // //----------------------------------------------------------------------- // Execute //----------------------------------------------------------------------- // void Execute(); // //----------------------------------------------------------------------- // PrimeWatcher //----------------------------------------------------------------------- // void PrimeWatcher(); protected: // //----------------------------------------------------------------------- // Constructor //----------------------------------------------------------------------- // AttributeWatcherOf( PlugStream *stream, Entity *entity ); // //----------------------------------------------------------------------- // BuildFromPage //----------------------------------------------------------------------- // static void BuildFromPage( PlugStream *stream, NameList *name_list, ClassID class_ID, ObjectID object_ID ); private: // //----------------------------------------------------------------------- // InitializeCurrentValue //----------------------------------------------------------------------- // void InitializeCurrentValue(); // //----------------------------------------------------------------------- // GrabCurrentValue //----------------------------------------------------------------------- // void GrabCurrentValue(); // //----------------------------------------------------------------------- // DumpValue //----------------------------------------------------------------------- // virtual void DumpValue(const T *attribute_ptr); // //----------------------------------------------------------------------- // Private data //----------------------------------------------------------------------- // T currentValue; #if DEBUG_LEVEL>0 Logical dumpValue; #endif }; //~~~~~~~~~~~~~~~~~~~~~~~~ AttributeWatcherOf templates ~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template AttributeWatcherOf::~AttributeWatcherOf() { } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template AttributeWatcherOf::AttributeWatcherOf( PlugStream *stream, Entity *entity ): AttributeWatcher(stream, entity) { Check_Pointer(this); Logical dump_value; MemoryStream_Read(stream, &dump_value); #if DEBUG_LEVEL>0 dumpValue = dump_value; if (dumpValue) { Tell( "AttributeWatcherOf::AttributeWatcherOf - " << *(T*)attributePointer << "\n" ); } #endif InitializeCurrentValue(); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template void AttributeWatcherOf::BuildFromPage( PlugStream *stream, NameList *name_list, ClassID class_ID, ObjectID object_ID ) { Check(stream); Check(name_list); AttributeWatcher::BuildFromPage(stream, name_list, class_ID, object_ID); // // Store dump_value // MEM_STRM_WRITE_ENTRY(*stream, name_list, Logical, dump_value); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template void AttributeWatcherOf::PrimeWatcher() { Check(this); #if DEBUG_LEVEL>0 if (dumpValue) { DumpValue((T*)attributePointer); } #endif GrabCurrentValue(); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template void AttributeWatcherOf::Execute() { Check(this); Check_Pointer(attributePointer); if (!(currentValue == *(T*)attributePointer)) { #if DEBUG_LEVEL>0 if (dumpValue) { DumpValue((T*)attributePointer); } #endif GrabCurrentValue(); } Check_Fpu(); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template void AttributeWatcherOf::InitializeCurrentValue() { Check_Pointer(attributePointer); currentValue = *(T*)attributePointer; Verify(currentValue == *(T*)attributePointer); Check_Fpu(); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template void AttributeWatcherOf::GrabCurrentValue() { InitializeCurrentValue(); attributeChanged = True; if (sendNotificationOnChange) { SendNotificationOfChange(); } } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #if DEBUG_LEVEL>0 template void AttributeWatcherOf::DumpValue(const T *attribute_ptr) #else template void AttributeWatcherOf::DumpValue(const T*) #endif { Check_Pointer(attribute_ptr); Tell((void *)attribute_ptr << " = " << *attribute_ptr << "\n"); }