00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _GLASS_PLUGIN_SHARED_HH_
00022 #define _GLASS_PLUGIN_SHARED_HH_
00023
00024 #include "exception.h"
00025 #include "packager.h"
00026 #include "types.h"
00027 #include "plugin.h"
00028 #include <boost/thread.hpp>
00029 #include <map>
00030 #include <set>
00031
00032 namespace libglass {
00033
00034 class _Shared;
00035
00061 class SharedBase : public PluginBase {
00062 PLUGIN(SharedBase);
00063 private:
00064 std::map<const string *, _Shared *, StringPointerCompare>
00065 vars;
00066 protected:
00067 friend class _Shared;
00068
00075 bool registerShared(_Shared *v);
00076
00083 bool unregisterShared(_Shared *v);
00084
00091 bool sendUpdate(_Shared *s);
00092
00100 bool getUpdate(_Shared *s);
00101
00108 bool sendUpdateAll(void);
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 #if defined(_MSC_VER) && (_MSC_VER < 1300) // earlier than .NET compiler (VC 7.0)
00119 public:
00120 #else
00121 protected:
00122 #endif
00123
00124
00128 virtual ~SharedBase();
00129
00130
00131 public:
00132 };
00133
00139 class LIBGLASS_API _Shared : public PluginInterface {
00140 public:
00141 typedef enum { NEVER, NEXT_ONLY, CHANGED, FORCED } UpdateMode;
00142
00143 protected:
00144 friend class SharedBase;
00145 friend class SharedPacket;
00146
00147 SharedBase *sb;
00148 string name;
00150
00151
00152
00153 bool changed;
00154 UpdateMode updatemode;
00155 boost::condition_variable trigger;
00157
00158 nodeId lastchanged;
00159 std::set<nodeId> owners;
00160 chain currentdata;
00161 chain nextdata;
00168 _Shared(const string &name) throw(Exception);
00169
00173 virtual ~_Shared();
00174
00179 virtual chain pack(void) { return chain(); }
00180
00187 virtual bool unpack(chain &s, unsigned int &used) { return false; }
00188 public:
00189
00190 void setUpdateMode(UpdateMode updatemode) {
00191 this->updatemode = updatemode;
00192 }
00193
00194 UpdateMode getUpdateMode(void) const {
00195 return this->updatemode;
00196 }
00202 bool hasChanged(void) const {
00203 return changed;
00204 }
00205
00206 bool sendUpdate(void);
00207
00214 bool getUpdate(void);
00215
00232 static bool sendUpdateAll(void);
00233 };
00234
00239 template<typename T,
00240 Serializer(*packf, T) = toString<T>,
00241 Unserializer(*unpackf, T) = toString<T> >
00242 class Shared : public _Shared {
00243 protected:
00244 T data;
00245
00246 chain pack(void) {
00247 chain c;
00248 packf(c, data, 1, NULL);
00249 return c;
00250 }
00251
00252 bool unpack(chain &c, unsigned int &used) {
00253 unsigned int initial = 0;
00254 return unpackf(c, initial, data, used, 1, NULL);
00255 }
00256
00257 public:
00263 Shared(const char *name, T data = T()) throw(Exception) : _Shared(name),
00264 data(data) {
00265
00266 }
00267
00273
00274
00275
00276
00277
00278
00279 const Shared &operator=(const T &b) {
00280 data = b;
00281 changed = true;
00282 return *this;
00283 }
00284
00290 const Shared &operator=(const Shared &b) {
00291 data = b.data;
00292 changed = true;
00293 return *this;
00294 }
00295
00300 void assign(T &b) {
00301 data = b;
00302 changed = true;
00303 return;
00304 }
00305
00310 const T &getData(void) const {
00311 return data;
00312 }
00313
00318 T operator*(void) const {
00319 return data;
00320 }
00321 };
00322
00323 }
00324
00325
00326 #endif // _GLASS_PLUGIN_SHARED_HH_