G
gianguz
In the following example Global is able to create and manage access
to objects of any kind (even in a multithreading environment) with
and index value attached to. So a Global<0, string> is a unique
string instance object allocated into the system that can be accessed
from any point into the program simply 'creating' another instance of
Global<int, T> with the same id and type.Like globals, destruction
of these objects is delegated until the lifetime expiration of the
program.
Here is the example:
static FakeSemaphore _semaphore;
template<class LOCK> class FakeGuard {
private:
FakeSemaphore* sem;
FakeGuard();
public:
FakeGuard(FakeSemaphore* s) { sem = s; sem->lock(); }
~FakeGuard() { sem->unlock(); }
};
template<int I,class T> class Global {
public:
static const int Index = I;
typedef T Type;
Global() { }
Global(const T& t) { _set(t); }
const Global& operator =(const T& t) { _set(t); return *this; }
operator T&() { return _get(); }
~Global() { }
private:
static T& _get() {
FakeGuard<FakeSemaphore> guard(&_semaphore);
static T Value;
return Value;
}
static void _set(const T& t) {
FakeGuard<FakeSemaphore> guard(&_semaphore);
static T& Value = _get();
Value = t;
return;
}
};
int main() {
typedef Global<0, string> Global0;
typedef Global<1, string> Global1;
Global0 a;
Global1 b;
a = "I'm A";
b = "I'm B";
cout << (string&)a << " " << (string&)b << endl;
a = b;
cout << (string&)a << " " << (string&)b << endl;
typedef Global<b.Index, Global1::Type> Global2;
Global2 c;
c = "I'm C";
cout << (string&)a << " " << (string&)b << endl;
}
Question is:
it can be useful?
using globals instances of any kind in mutlithreading
is always an untouchable argument, but what about to rediscuss this?
Thanks,
Gianguglielmo
to objects of any kind (even in a multithreading environment) with
and index value attached to. So a Global<0, string> is a unique
string instance object allocated into the system that can be accessed
from any point into the program simply 'creating' another instance of
Global<int, T> with the same id and type.Like globals, destruction
of these objects is delegated until the lifetime expiration of the
program.
Here is the example:
static FakeSemaphore _semaphore;
template<class LOCK> class FakeGuard {
private:
FakeSemaphore* sem;
FakeGuard();
public:
FakeGuard(FakeSemaphore* s) { sem = s; sem->lock(); }
~FakeGuard() { sem->unlock(); }
};
template<int I,class T> class Global {
public:
static const int Index = I;
typedef T Type;
Global() { }
Global(const T& t) { _set(t); }
const Global& operator =(const T& t) { _set(t); return *this; }
operator T&() { return _get(); }
~Global() { }
private:
static T& _get() {
FakeGuard<FakeSemaphore> guard(&_semaphore);
static T Value;
return Value;
}
static void _set(const T& t) {
FakeGuard<FakeSemaphore> guard(&_semaphore);
static T& Value = _get();
Value = t;
return;
}
};
int main() {
typedef Global<0, string> Global0;
typedef Global<1, string> Global1;
Global0 a;
Global1 b;
a = "I'm A";
b = "I'm B";
cout << (string&)a << " " << (string&)b << endl;
a = b;
cout << (string&)a << " " << (string&)b << endl;
typedef Global<b.Index, Global1::Type> Global2;
Global2 c;
c = "I'm C";
cout << (string&)a << " " << (string&)b << endl;
}
Question is:
it can be useful?
using globals instances of any kind in mutlithreading
is always an untouchable argument, but what about to rediscuss this?
Thanks,
Gianguglielmo