473,756 Members | 1,964 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 3159
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
1461
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
3163
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 i discuss some of the uses of context-specific Singleton-like objects. During
11
1688
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 behaviour. But, how is a singleton meant to be inherited from? Is not the concrete class of the unique...
10
6192
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
4450
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
1495
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) about how singletons don't play nicely in clustered scenarios and would like to hear more on the
32
3052
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 if this is a trivial matter since the reserved word
6
10382
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 library. The libraries I'm trying to use all have Meyers Singletons in them, so they need to have...
14
6023
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
9462
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
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
10046
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...
1
9857
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
9722
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
8723
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...
0
5155
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
3817
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
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.