473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

retrieving templatized object via singleton getInstance using generic template parameters

I want to call a singleton getInstance function to retrieve a
templatized object without knowing what types were used to create the
singleton object in the first call to getInstance. How can I do this
non-intrusively -- I.e., without, for example, typedef'ing the types
in every compilation unit?

Background:

Our code base has assert macros that need to reboot the system after
notifying components via a single templatized Component object that
contains the IPC object. I made the macro call a reboot function, but
that function needs to access the IPC object. I want to singleton'ize
the Component object to provide access to the IPC object, but don't
want to hand-code the template parameters in each compilation unit for
each getInstance call.

Code:

// I want to call retrieve a templatized object via a call to
getInstance
// without supplying the objects template parameters.
// Is there any way to do this, perhaps using template meta-
programming (TMP) techniques?

#include <string>
#include <iostream>

using namespace std;

template <typename T1, typename T2>
class Component
{

public:

static Component<T1, T2getInstance()
{
static Component<T1, T2t;
return t;
}

};

int main()
{

// after the initial call to getInstance, here

Component<int, string& r1 = Component<int,
string>::getInstance();

// I then want to call getInstance to obtain the same Component
without specifying the template params.

Component<something generic& r2 = Component<something
generic>::getInstance();
}

Jun 6 '07 #1
3 3264
On Jun 5, 8:05 pm, gary.bernst...@gmail.com wrote:
I want to call a singleton getInstance function to retrieve a
templatized object without knowing what types were used to create the
singleton object in the first call to getInstance. How can I do this
non-intrusively -- I.e., without, for example, typedef'ing the types
in every compilation unit?

Background:

Our code base has assert macros that need to reboot the system after
notifying components via a single templatized Component object that
contains the IPC object. I made the macro call a reboot function, but
that function needs to access the IPC object. I want to singleton'ize
the Component object to provide access to the IPC object, but don't
want to hand-code the template parameters in each compilation unit for
each getInstance call.

Code:

// I want to call retrieve a templatized object via a call to
getInstance
// without supplying the objects template parameters.
// Is there any way to do this, perhaps using template meta-
programming (TMP) techniques?

#include <string>
#include <iostream>

using namespace std;

template <typename T1, typename T2>
class Component
{

public:

static Component<T1, T2getInstance()
{
static Component<T1, T2t;
return t;
}

};

int main()
{

// after the initial call to getInstance, here

Component<int, string& r1 = Component<int,
string>::getInstance();

// I then want to call getInstance to obtain the same Component
without specifying the template params.

Component<something generic& r2 = Component<something
generic>::getInstance();

}

I like to play with my balls. First I juggle the right ball, then I
toggle on the left.
Will this suffice?

Jun 6 '07 #2
On Jun 5, 8:05 pm, gary.bernst...@gmail.com wrote:
I want to call a singleton getInstance function to retrieve a
templatized object without knowing what types were used to create the
singleton object in the first call to getInstance. How can I do this
non-intrusively -- I.e., without, for example, typedef'ing the types
in every compilation unit?

Background:

Our code base has assert macros that need to reboot the system after
notifying components via a single templatized Component object that
contains the IPC object. I made the macro call a reboot function, but
that function needs to access the IPC object. I want to singleton'ize
the Component object to provide access to the IPC object, but don't
want to hand-code the template parameters in each compilation unit for
each getInstance call.

Code:

// I want to call retrieve a templatized object via a call to
getInstance
// without supplying the objects template parameters.
// Is there any way to do this, perhaps using template meta-
programming (TMP) techniques?

#include <string>
#include <iostream>

using namespace std;

template <typename T1, typename T2>
class Component
{

public:

static Component<T1, T2getInstance()
{
static Component<T1, T2t;
return t;
}

};

int main()
{

// after the initial call to getInstance, here

Component<int, string& r1 = Component<int,
string>::getInstance();

// I then want to call getInstance to obtain the same Component
without specifying the template params.

Component<something generic& r2 = Component<something
generic>::getInstance();

}
I may be able to, instead, simple singleton'ize the internal IPC
object I'm trying to use, which isn't templatized, but I'm still very
curious as to how a templatized object can be retrieved via
getInstance without providing the objects template parameters.

Jun 6 '07 #3
ga************@gmail.com wrote:
I want to call a singleton getInstance function to retrieve a
templatized object without knowing what types were used to create the
singleton object in the first call to getInstance. How can I do this
non-intrusively -- I.e., without, for example, typedef'ing the types
in every compilation unit?
What you're asking (having peeked in your source code, I'm guessing)
is to declare/define an object without specifying the actual type of
the object. That's impossible in C++.

Perhaps you can review your source and amend it with some kind of
example of how you're going to actually *use* the "object" you want
to retrieve?
Background:

Our code base has assert macros that need to reboot the system after
notifying components via a single templatized Component object that
contains the IPC object. I made the macro call a reboot function, but
that function needs to access the IPC object. I want to singleton'ize
the Component object to provide access to the IPC object, but don't
want to hand-code the template parameters in each compilation unit for
each getInstance call.
So, put them into a macro and set them at compilation time with -D or
some such option of the compiler...
>
Code:

// I want to call retrieve a templatized object via a call to
getInstance
// without supplying the objects template parameters.
// Is there any way to do this, perhaps using template meta-
programming (TMP) techniques?

#include <string>
#include <iostream>

using namespace std;

template <typename T1, typename T2>
class Component
{

public:

static Component<T1, T2getInstance()
{
static Component<T1, T2t;
return t;
}

};

int main()
{

// after the initial call to getInstance, here

Component<int, string& r1 = Component<int,
string>::getInstance();

// I then want to call getInstance to obtain the same Component
without specifying the template params.

Component<something generic& r2 = Component<something
generic>::getInstance();
}
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #4

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

Similar topics

1
by: John Eskie | last post by:
Lets say I want to make multiply classes as singletons. A singleton skeleton would be with a static member variable of the instance and a GetInstance function. Currently I have this functionality...
9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
2
by: Chr?stian Rousselle | last post by:
Hello, I want to do derive a class from a Singleton template base class. Is there a way to solve the following problem: template<class T> class CSingleton { public: static T&...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
1
by: Gregg Altschul | last post by:
I have a Shared Object which its sole purpose is to create objects of a certain type and return a pointer to the object to the user. Therefore, the user becomes the owner of this object and is...
7
by: Thomas Lorenz | last post by:
Hello, a standard template singleton looks basically like this: template<class T> class Singleton1 { private: Singleton1(); static Singleton1* m_Instance; public:
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
5
by: Rich | last post by:
The following code produced a singleton object with application scope when it should have had page scope: public class Singleton { private static Singleton uniqueInstance = null; private...
9
by: windandwaves | last post by:
Hi gurus I have a class from which I create an object. I want this object to be available in all my functions for my application. Basically, I want to make the object global. What is the best...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.