00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _GLASS_PLUGIN_ALIAS_HH_
00021 #define _GLASS_PLUGIN_ALIAS_HH_
00022
00023 #include <map>
00024 #include <utility>
00025 #include <set>
00026 #include "plugin.h"
00027 #include "exception.h"
00028
00029 namespace libglass {
00030
00031 class _Alias;
00032 class AliasPacket;
00033
00051 class AliasBase : public PluginBase {
00055 PLUGIN(AliasBase);
00056 private:
00057 std::map<const string *, _Alias *, StringPointerCompare> aliases;
00058
00059 protected:
00060 friend class _Alias;
00061
00062
00069 bool registerAlias(_Alias *a);
00070
00077 bool unregisterAlias(_Alias *a);
00078
00079
00080
00081
00082
00083
00084
00085
00086 #if defined(_MSC_VER) && (_MSC_VER < 1300) // earlier than .NET compiler (VC 7.0)
00087 public:
00088 #else
00089 protected:
00090 #endif
00091
00095 virtual ~AliasBase();
00096
00097 public:
00098 };
00099
00100
00101
00102 #if defined(_MSC_VER)
00103
00104 LIBGLASS_IMP_TEMPLATE_STL_SET(nodeId);
00105
00106 LIBGLASS_IMP_TEMPLATE_STL_MAP(libglass::nodeId, libglass::string);
00107 #endif // _MSC_VER
00108
00111 class LIBGLASS_API _Alias : public PluginInterface {
00112 private:
00113 void init(const string &name) throw(Exception);
00114
00115 protected:
00116 friend class AliasBase;
00117 friend class AliasPacket;
00118
00119 AliasBase *ab;
00121
00122 string name;
00123 bool associated;
00124 string current_association;
00125
00126
00127
00128 std::set<nodeId> owners;
00129 std::map<nodeId, string> associations;
00131 _Alias(const char *aliasname) throw(Exception);
00132 _Alias(const string &) throw(Exception);
00133
00137 virtual ~_Alias();
00138
00146 bool _associate(const string &association);
00147
00148 void unregisterNode(nodeId id);
00149
00150 public:
00156 string getName(void) const;
00157
00167 bool associate(nodeId id, string &name);
00168
00174 bool associate(nodeId id, const char *name);
00175
00180 bool associate(std::map<nodeId, string> &map);
00181
00182 #ifdef TODO //how to propagate to other nodes?
00183
00187 void associate(string (*func)(nodeId));
00188 #endif
00189
00190 bool dissociate(nodeId id);
00191 };
00192
00200 template<typename T>
00201 class Alias : public _Alias {
00202 private:
00203 std::map<string, T> aliases;
00204 T default_value;
00205
00206 public:
00210 Alias(const char *aliasname, T default_value) throw(Exception) :
00211 _Alias(aliasname), default_value(default_value) {
00212 }
00213
00217 ~Alias() {
00218 }
00219
00228 bool addAlias(string &name, T t) {
00229 aliases[name] = t;
00230 return true;
00231 }
00232
00236 bool addAlias(const char *name, T t) {
00237 if (!name)
00238 return false;
00239 aliases[string(name)] = t;
00240 return true;
00241 }
00242
00250 bool deleteAlias(const char *name) {
00251
00252
00253 return false;
00254 }
00255
00261 bool checkAlias(void) const {
00262 return associated;
00263 }
00264
00270 T getData(void) {
00271 if (associated == true) {
00272 typename std::map<string, T>::iterator it;
00273 it = aliases.find(current_association);
00274 if (it == aliases.end()) {
00275
00276 return default_value;
00277 }
00278 return it->second;
00279 }
00280 return default_value;
00281 }
00282 };
00283
00284 }
00285
00286
00287 #endif // _GLASS_PLUGIN_ALIAS_HH_