473,586 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singletons, Plugins, static and extern

Hi.
I've got the following problem here: In my application, I use a lot of
Singletons, that are implemented as described in Gamma et al. (shortened):

//.h
class Singleton{
public:
static Singleton* the();
private:
static Singleton* _instance;
}

//.cpp
Singleton* the(){
if(!_instance)
_instance=new Singleton;
return _instance;
}

There's no problem in my core application, which is statically linked
with the singleton classes, e.g. if I use Singleton::the( )->xyz() in
main(), the object is created, and a later call uses this object as
expected.

The problem arises, if I try to use these Singletons in plugins / parts
of the program that are loaded with - in my case - dlopen(). It is
impossible for me to access a Singleton object, that was already created
in the main application from a plugin. There are 2 Singletons
afterwards, one in the scope of the plugin and one in the scope of the
main application.

Example:
main(){
//first call, create Singleton by accessing it
Singleton::the( )->xxx();
Plugin* p=loadPluginWit hDlopen();
p->yyy(); //Call to a virtual method of class Plugin
}

//ConcretePlugin is derived of plugin, yyy declared virtual
void ConcretePlugin: :yyy(){
//Should use the same Singleton as above, but creates a new one.
Singleton::the( )->zzz();
}

The question: How can I access the Singleton used in the core
application with the plugin? I guess that there's an "extern" missing
somewhere, but I can't solve it. Any suggestions?!

In case that this is important: I'm working with gcc version 3.3.5 on
Debian/GNU Linux(testing).

Thanks & regards,
Dominik
Jul 23 '05 #1
3 3148
Dominik Rau wrote:
I've got the following problem here: In my application, I use a lot of
Singletons, that are implemented as described in Gamma et al. (shortened):
[...]
There's no problem in my core application, which is statically linked
with the singleton classes, e.g. if I use Singleton::the( )->xyz() in
main(), the object is created, and a later call uses this object as
expected.

The problem arises, if I try to use these Singletons in plugins / parts
of the program that are loaded with - in my case - dlopen(). It is
impossible for me to access a Singleton object, that was already created
in the main application from a plugin.
Do you know *why* it is "impossible "? Since "plugins" or "dlopen" are not
part of the language, you're in the platform-specific area, and whatever
you have to do there cannot be solved using C++ means. You apparently
already wrote the program so that singletons are singletons, AFA C++ is
concerned.
There are 2 Singletons
afterwards, one in the scope of the plugin and one in the scope of the
main application.
That's not something that can be explained in terms of C++ language.

Example:
main(){
//first call, create Singleton by accessing it
Singleton::the( )->xxx();
Plugin* p=loadPluginWit hDlopen();
p->yyy(); //Call to a virtual method of class Plugin
}

//ConcretePlugin is derived of plugin, yyy declared virtual
void ConcretePlugin: :yyy(){
//Should use the same Singleton as above, but creates a new one.
Singleton::the( )->zzz();
}

The question: How can I access the Singleton used in the core
application with the plugin? I guess that there's an "extern" missing
somewhere, but I can't solve it. Any suggestions?!
Pass your singleton as an argument. It's not a solution, it's a work-
around.
In case that this is important: I'm working with gcc version 3.3.5 on
Debian/GNU Linux(testing).


It's not, really. (Not in comp.lang.c++, anyway)/

V
Jul 23 '05 #2
Dominik Rau wrote:
Hi.
I've got the following problem here: In my application, I use a lot of
Singletons,
This in itself COULD be a problem - google 'Singleton pattern' for a
very long conversation about their pros and cons.

To clarify my view of them....Singlet ons are not evil - but they are
often over-used and mis-used.
that are implemented as described in Gamma et al. (shortened):

//.h
class Singleton{
public:
static Singleton* the();
private:
static Singleton* _instance;
}

//.cpp
Singleton* the(){
if(!_instance)
_instance=new Singleton;
return _instance;
}
Minor point but 'the()' is not the usual name for the instance retrieval
method - getInstance() is.

There's no problem in my core application, which is statically linked
with the singleton classes, e.g. if I use Singleton::the( )->xyz() in
main(), the object is created, and a later call uses this object as
expected.

The problem arises, if I try to use these Singletons in plugins / parts
of the program that are loaded with - in my case - dlopen(). It is
impossible for me to access a Singleton object, that was already created
in the main application from a plugin. There are 2 Singletons
afterwards, one in the scope of the plugin and one in the scope of the
main application.


Two birds with on stone here....to break your plugins use of the
Singleton (which is a good thing) pass a reference of the singleton, to
the plugin at load time. The plugin can then store/pass around the
reference in any manner it likes, but importantly, it doesn't know its a
singleton - its just another object.

snipped remaining
Jul 23 '05 #3
Hi again.
It works by linking the singleton classes as shared libraries with the
core application and the plugins. It was a linker problem and I
apologize for being completely offtopic here.

Thanks for your answers,
Dominik
Jul 23 '05 #4

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

Similar topics

5
1445
by: ThunderMusic | last post by:
Hi, I have some code to load some plug-ins, but the code requires me to know the name of the class to load (here: SamplePlugin, derived from IPlugin) : (Here is some C# code, but I use VB.Net to code my program) using System; using System.Reflection; public class Driver {
5
3141
by: stephan beal | last post by:
Good morning, C++ users, i've been hesitating to post this, primarily because i know that most of you here are *way* ahead of me in C++ and i'm a little embarassed about the possibility of some gross errors in what i'll be posting... That said, please go easy on me. :) About 2 weeks ago i wrote a paper, called Context Singletons, where...
11
1669
by: Tito | last post by:
I have two questions about the singletons' chapter of Alexei Alexandrescu's "C++ Modern Design". 1. In the beginning of the chapter Alexei states that a "singleton" class implementation made of static member functions has the problem that the functions are not virtual, so that you have to touch the class' code in order to change the...
10
6153
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
8
4435
by: 6tc1 | last post by:
Hi all, I'm having a problem where in my solution that contains multiple projects - I instantiate a singleton class in one assembly and then if another assembly tries to use that singleton class another instance of it is created. Basically: --Assembly 1 (the executable)-- -Main.cs-
5
1482
by: Omega | last post by:
I'm interested in seeing a bit of discussion about using singletons in ASP.NET 2.0. Currently I've designed a singleton that gets a reference to it's single instance stored inside the ASP.NET application object. This is done to persist and make available live information across multiple sessions. I've read a little bit (almost nothing)...
32
3027
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only the usage of static functions. I've been programming for years and I never felt a real need for the "static approach for functions". I don't know...
6
10350
by: =?Utf-8?B?R29yZG8=?= | last post by:
Hello everyone, I've been trying for some time now to move to C++/CLI, but I have several large legacy C++ static libraries I need to use. When I set up a simple solution with a C++/CLI Winforms app and a C++ native static library, they work well together _unless_ I have, it seems, any static variables defined in any function in the native...
14
5995
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7841
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...
0
8204
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. ...
0
8220
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...
1
5712
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...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3838
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...
1
2345
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
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.