473,836 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Globals initialization using templates


The subject is a bit misleading because I really can't figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.

I mean I can understand code-wise what is going on but I am missing the
larger picture.

In what circumstances would I use this?

template<typena me T>
class Globals
{
public:
Globals()
{
if (impl == NULL)
{
init();
}
}
T* operator->() const
{
return impl;
}
private:
static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignme nt ;
} data ;
impl = new (data.buf) T;
}
};

template<typena me T>
T* Globals<T>::imp l = NULL;

Nov 16 '06 #1
15 1412

Dilip wrote:
The subject is a bit misleading because I really can't figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.

I mean I can understand code-wise what is going on but I am missing the
larger picture.

In what circumstances would I use this?

template<typena me T>
class Globals
{
public:
Globals()
{
if (impl == NULL)
{
init();
}
}
T* operator->() const
{
return impl;
}
private:
static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignme nt ;
} data ;
impl = new (data.buf) T;
}
};

template<typena me T>
T* Globals<T>::imp l = NULL;
Defining Globals in a multithreaded environment (even though toe code
here doesnt have any multi threading constructs)wher e you could control
the order of Initialization especially when you oculd be possibly
having a bunch of globals depending upon each other.

Nov 16 '06 #2
ampar...@gmail. com wrote:
Defining Globals in a multithreaded environment (even though toe code
here doesnt have any multi threading constructs)wher e you could control
the order of Initialization especially when you oculd be possibly
having a bunch of globals depending upon each other.
can you give me a short example?

I thought the order of initialization for global variables are defined
within a translation unit?
Do you mean cases where global vars defined across translation units
somehow end up depending on one another for initialization? If so, how
does that class solve the problem?

Nov 16 '06 #3
Dilip wrote:
The subject is a bit misleading because I really can't figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.

I mean I can understand code-wise what is going on but I am missing the
larger picture.

In what circumstances would I use this?

template<typena me T>
class Globals
{
public:
Globals()
{
if (impl == NULL)
{
init();
}
}
T* operator->() const
{
return impl;
}
// missing destructor
// this is necessary in case T manages some kind of resource.
~Globals()
{
impl->T::~T();
}
private:
static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignme nt ;
} data ;
impl = new (data.buf) T;
}
};

template<typena me T>
T* Globals<T>::imp l = NULL;
Nov 16 '06 #4

Dilip wrote:
ampar...@gmail. com wrote:
Defining Globals in a multithreaded environment (even though toe code
here doesnt have any multi threading constructs)wher e you could control
the order of Initialization especially when you oculd be possibly
having a bunch of globals depending upon each other.

can you give me a short example?

I thought the order of initialization for global variables are defined
within a translation unit?
Do you mean cases where global vars defined across translation units
somehow end up depending on one another for initialization? If so, how
does that class solve the problem?
what is the following code doing ?

static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignme nt ;
} data ;
impl = new (data.buf) T;
}

How many times do you think the Init function is called per type ?

Nov 16 '06 #5
red floyd wrote:
// missing destructor
// this is necessary in case T manages some kind of resource.
~Globals()
{
impl->T::~T();
}
That's not clearly the right thing. impl is a static and so it does not
necessarily need destruction from a memory leaking perspective.
Nov 16 '06 #6

Gianni Mariani wrote:
red floyd wrote:
// missing destructor
// this is necessary in case T manages some kind of resource.
~Globals()
{
impl->T::~T();
}

That's not clearly the right thing. impl is a static and so it does not
necessarily need destruction from a memory leaking perspective.
impl might be static....but it might be holding a pointer to an object
T which might be holding up stuff. Therefore you need to call the T's
destructor.

Nov 16 '06 #7
Dilip wrote:
The subject is a bit misleading because I really can't figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.

I mean I can understand code-wise what is going on but I am missing the
larger picture.

In what circumstances would I use this?

template<typena me T>
class Globals
{
public:
Globals()
{
if (impl == NULL)
{
init();
}
}
T* operator->() const
{
return impl;
}
private:
static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignme nt ;
} data ;
impl = new (data.buf) T;
}
};

template<typena me T>
T* Globals<T>::imp l = NULL;
I don't see what some of the constructs are used - why not use this:

template<typena me T>
class Globals
{
public:
T* operator->() const
{
static T * x = new T;
return x;
}
};

OK - so the memory for x is dynamically allocated in this example while
the OP example is not.

The use synax would be simply:

Globals<Type>()->Member ...
This below is probably equivalent to what was posted. (i.e. No dynamic
memory allocation.)

template<typena me T>
class Globals
{
public:

T* operator->() const
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignme nt ;
} data ;
static T * x = new (data.buf) T;
return x;
}
};

Nov 16 '06 #8
am******@gmail. com wrote:
Gianni Mariani wrote:
>red floyd wrote:
>>// missing destructor
// this is necessary in case T manages some kind of resource.
~Globals()
{
impl->T::~T();
}
That's not clearly the right thing. impl is a static and so it does not
necessarily need destruction from a memory leaking perspective.

impl might be static....but it might be holding a pointer to an object
T which might be holding up stuff. Therefore you need to call the T's
destructor.
That would mean that impl will point to unallocated memory if used like
this:
Globals<Type>()->X(); // ok works first time
Globals<Type>()->Y(); // oops - broken this time
Nov 16 '06 #9
Gianni Mariani wrote:
red floyd wrote:
>// missing destructor
// this is necessary in case T manages some kind of resource.
~Globals()
{
impl->T::~T();
}

That's not clearly the right thing. impl is a static and so it does not
necessarily need destruction from a memory leaking perspective.
Who said anything about memory? T could be a type that manages a
resource that could conceivably live beyond the lifetime of the program,
e.g. a SysV Semaphore.

When the program ends, Global<my_semap hore_manager_cl assshould bloody
well call the destructor for the my_semaphore_ma nager_class object it
created in impl, so that it can release the resource it holds.

Nov 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4326
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would be better to promote better programming than rely on PHP to compensate for lazy programming?
5
2842
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains a value for 'href', 'desc', and 'info' but when I try to pass one of these "columns" to array_multisort() it whinges about it not being an array or a sort value :-( Can PHP not understand arrays stored in $GLOBALS or something?
8
2192
by: Ron_Adam | last post by:
Is there a way to hide global names from a function or class? I want to be sure that a function doesn't use any global variables by mistake. So hiding them would force a name error in the case that I omit an initialization step. This might be a good way to quickly catch some hard to find, but easy to fix, errors in large code blocks. Examples: def a(x):
3
5927
by: Brian Ross | last post by:
Hi, I am having a problem writing a constructor/member initialization with VC.NET7.1. Here is the code: --- namespace Library
5
1860
by: jgraveskc | last post by:
I know this sounds nasty, but let me explain. I have what was a class that contains a whole buncha function pointers (for OpenGL extensions) that are used in the app. I was porting this app to macs, where the extensions are properly present (just normal functions) so the class is not needed. I was wanting a way to make the same code work on both. For example, if I could do this: glGenBuffersARB(bla); on both. On the mac its just...
6
1254
by: srblinux | last post by:
Hi all, If a program-1 is written with Globals without initilisation and program-2 was written with Globals with initilisation, But program-2 is working fine but program-1 is giving problem why is it so? Thanks in advance, with regards, Bab
15
7879
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
3
1787
by: Gianni Mariani | last post by:
So I was put straight that double(v) is the same as (double)(v) recently and I'm somewhat surprised. I'm not the only one. I think T(V) should have exactly the same semantics as static_cast<T>(V) for POD T and I'm willing to bet you'll find few C++ developers that would object. What I'd like to propose is that the c-style cast interpretation of T(V) for POD T be the same as initialization of a temporary object T with a value V. i.e....
0
9670
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10548
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9374
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.