00001 // 00002 //------------------------------------------------------------------------ 00003 // 00004 // File : CallbackMap.h 00005 // Author : Patrick McCormick (pat@acl.lanl.gov) 00006 // 00007 // $Date: 2003/08/01 22:10:47 $ 00008 // $Revision: 1.3 $ 00009 // 00010 // 00011 // This class implements a simple lookup map from a CallbackID to a 00012 // callback (a function or a method). Using the map you can then do 00013 // a quick lookup from ID to callback. This code is really nothing 00014 // more than a wrapper around the STL map class. 00015 // 00016 // TODO: As it stands it is only possible to call a single function 00017 // for each ID. There might be situations where this limits your 00018 // flexibility. 00019 // 00020 // TODO: This code is currently dependent upon the return types and 00021 // parameters used in the callback classes (Callbacks.h). It will 00022 // currently be necessary to make changes here if the parameter types 00023 // in Callbacks.h change... 00024 // 00025 // This code is based on the VIF (Visualization Interconnection Framework) 00026 // that helped to drive TRex -- back when there were such beasts... 00027 // 00028 //------------------------------------------------------------------------ 00029 00030 #include "CallbackMap.h" 00031 00032 // ------------------- 00033 // --- CallbackMap --- 00034 // ------------------- 00035 // 00036 // 00037 // 00038 CallbackMap::CallbackMap(void) 00039 { 00040 00041 } 00042 00043 00044 // -------------------- 00045 // --- ~CallbackMap --- 00046 // -------------------- 00047 // 00048 // 00049 // 00050 CallbackMap::~CallbackMap(void) 00051 { 00052 CBMap::iterator first = cbmap.begin(); 00053 CBMap::iterator last = cbmap.end(); 00054 while(first != last) { 00055 delete (*first).second; 00056 first++; 00057 } 00058 cbmap.clear(); 00059 } 00060 00061 00062 // ------------------- 00063 // --- AddCallback --- 00064 // ------------------- 00065 // 00066 // 00067 // 00068 bool CallbackMap::Add(KokoTag tag, Callback *cb) 00069 { 00070 CBPair cbpair(tag, cb); 00071 CBBool flag = cbmap.insert(cbpair); 00072 if (flag.second == true) { 00073 return true; 00074 } else { 00075 return false; 00076 } 00077 } 00078 00079 00080 // ---------------------- 00081 // --- RemoveCallback --- 00082 // ---------------------- 00083 // 00084 // 00085 // 00086 bool CallbackMap::Remove(KokoTag tag) 00087 { 00088 CBIterator it = cbmap.find(tag); 00089 if (it == cbmap.end()) 00090 return false; 00091 else { 00092 delete (*it).second; 00093 cbmap.erase(it); 00094 return true; 00095 } 00096 } 00097 00098 00099 // -------------- 00100 // --- Invoke --- 00101 // -------------- 00102 // 00103 // 00104 // 00105 bool CallbackMap::Invoke(KokoTag tag, KokoBuffer &databuf) 00106 { 00107 CBIterator it = cbmap.find(tag); 00108 if (it == cbmap.end()) 00109 return false; 00110 else { 00111 (*(*it).second)(databuf); // Yack! What messy syntax... 00112 return true; 00113 } 00114 }