G
Gianguz
I'd like to discuss about the opportunity to have a global objects
creator that introduces into a general framework
(suited for multithreading) a controlled semantic to manage
globals variables (objects and scalar-types).
In the following example Global is able to create objects of any kind
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:
#include <string>
#include <iostream>
using namespace std;
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); }
operator T&() { return __get(); }
~Global() { }
private:
static T& __get() {
//access can be serialized here for multithreading
static T Value;
return Value;
}
static void __set(const T& t) {
//access can be serialized here for multithreading
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<1, string> Global2;
Global2 c;
c = "I'm C";
cout << (string&)a << " " << (string&)b << endl;
}
Someone thinks that it can be useful? Or something like that is
already used. In that case can you give me some reference about.
Thanks,
Gianguglielmo
creator that introduces into a general framework
(suited for multithreading) a controlled semantic to manage
globals variables (objects and scalar-types).
In the following example Global is able to create objects of any kind
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:
#include <string>
#include <iostream>
using namespace std;
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); }
operator T&() { return __get(); }
~Global() { }
private:
static T& __get() {
//access can be serialized here for multithreading
static T Value;
return Value;
}
static void __set(const T& t) {
//access can be serialized here for multithreading
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<1, string> Global2;
Global2 c;
c = "I'm C";
cout << (string&)a << " " << (string&)b << endl;
}
Someone thinks that it can be useful? Or something like that is
already used. In that case can you give me some reference about.
Thanks,
Gianguglielmo