00001 /**** 00002 Active Object compiled file 00003 Copyright (C) 2006-2010 Werner Van Belle 00004 Do not modify. Changes might be lost 00005 -------------------------------------------- 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 ****/ 00016 00017 #ifndef __AO_TRACKER_H 00018 #define __AO_TRACKER_H 00019 #include "active-objects.h" 00020 using namespace std; 00021 #include <set> 00022 #include <string> 00023 class AoTracker; 00024 class ActiveAoTracker; 00025 #ifdef TRACE_MESSAGES 00026 #define ENTER_MSG cerr << "Start " << declaration() << "\n"; 00027 #define LEAVE_MSG cerr << "Stop " << declaration() << "\n"; 00028 #endif 00029 #ifndef ENTER_MSG 00030 #define ENTER_MSG ; 00031 #endif 00032 #ifndef LEAVE_MSG 00033 #define LEAVE_MSG ; 00034 #endif 00035 00036 //------------------------------------- 00037 // Active Object base messaging classes 00038 //------------------------------------- 00048 class ActiveAoTracker_msg_ 00049 { 00050 public: 00055 virtual elementResult run(ActiveAoTracker * /* caller */) 00056 { 00057 assert(0); 00058 } 00063 virtual string declaration() 00064 { 00065 return "Unknown message"; 00066 } 00067 }; 00068 00069 00070 //------------------------------------- 00071 // Main object definition 00072 //------------------------------------- 00073 class ActiveAoTracker: public ActiveObject< ActiveAoTracker_msg_* > 00074 { 00075 friend class AoTracker; 00076 AoTracker * self; 00077 virtual elementResult handle( ActiveAoTracker_msg_* cmd) 00078 { 00079 if (cmd) return cmd->run(this); 00080 else return Done; 00081 }; 00082 set < string > alive; 00083 public: elementResult sunset(string s); 00084 protected: void queue_sunset(string s); 00085 public: elementResult sunrise(string s); 00086 protected: void queue_sunrise(string s); 00087 public: elementResult print(); 00088 protected: void queue_print(); 00089 protected: 00090 ActiveAoTracker(AoTracker* s, string name): 00091 ActiveObject< ActiveAoTracker_msg_ * >(name), self(s) 00092 { 00093 }; 00094 }; 00095 00096 00097 //------------------------------------- 00098 // Specific messaging classes 00099 //------------------------------------- 00100 class ActiveAoTracker_msg_sunset: public ActiveAoTracker_msg_ 00101 { 00102 string s; 00103 public: 00104 ActiveAoTracker_msg_sunset(string s) : s(s) 00105 { 00106 }; 00107 virtual string declaration() 00108 { 00109 return "AoTracker::sunset(string s)"; 00110 } 00111 virtual elementResult run(ActiveAoTracker * ao) 00112 { 00113 ENTER_MSG; 00114 elementResult res = ao->sunset(s); 00115 LEAVE_MSG; 00116 return res; 00117 }; 00118 }; 00119 00120 class ActiveAoTracker_msg_sunrise: public ActiveAoTracker_msg_ 00121 { 00122 string s; 00123 public: 00124 ActiveAoTracker_msg_sunrise(string s) : s(s) 00125 { 00126 }; 00127 virtual string declaration() 00128 { 00129 return "AoTracker::sunrise(string s)"; 00130 } 00131 virtual elementResult run(ActiveAoTracker * ao) 00132 { 00133 ENTER_MSG; 00134 elementResult res = ao->sunrise(s); 00135 LEAVE_MSG; 00136 return res; 00137 }; 00138 }; 00139 00140 class ActiveAoTracker_msg_print: public ActiveAoTracker_msg_ 00141 { 00142 ; 00143 public: 00144 ActiveAoTracker_msg_print() 00145 { 00146 }; 00147 virtual string declaration() 00148 { 00149 return "AoTracker::print()"; 00150 } 00151 virtual elementResult run(ActiveAoTracker * ao) 00152 { 00153 ENTER_MSG; 00154 elementResult res = ao->print(); 00155 LEAVE_MSG; 00156 return res; 00157 }; 00158 }; 00159 00160 00161 //------------------------------------- 00162 // Active Object wrapper 00163 //------------------------------------- 00169 class AoTracker 00170 { 00171 private: 00177 ActiveAoTracker object; 00178 public: 00189 AoTracker(string name="AoTracker"): object(this, name) {}; 00190 public: 00191 void sunset(string s) 00192 { 00193 object.queue_sunset(s); 00194 }; 00195 public: 00196 void sunrise(string s) 00197 { 00198 object.queue_sunrise(s); 00199 }; 00200 public: 00201 void print() 00202 { 00203 object.queue_print(); 00204 }; 00205 }; 00206 00207 00208 //------------------------------------- 00209 // Active Object Methods 00210 //------------------------------------- 00211 inline void ActiveAoTracker::queue_sunset(string s) 00212 { 00213 push(new ActiveAoTracker_msg_sunset(s)); 00214 }; 00215 inline void ActiveAoTracker::queue_sunrise(string s) 00216 { 00217 push(new ActiveAoTracker_msg_sunrise(s)); 00218 }; 00219 inline void ActiveAoTracker::queue_print() 00220 { 00221 push(new ActiveAoTracker_msg_print()); 00222 }; 00223 #endif // __AO_TRACKER_H