473,327 Members | 2,016 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,327 software developers and data experts.

Singleton without static : possible??

Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.

Nov 2 '06 #1
15 3853
eminemence <em********@gmail.comwrote:
>Is it possible to implement a Singleton without using static variables
or global variables.
Maybe by writing to a file. But it would be a major kludge.

Steve
Nov 2 '06 #2
Still any other way??
--eminemence.

Steve Pope wrote:
eminemence <em********@gmail.comwrote:
Is it possible to implement a Singleton without using static variables
or global variables.

Maybe by writing to a file. But it would be a major kludge.

Steve
Nov 2 '06 #3
eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.
No.
Nov 2 '06 #4
Thats quite sad as many mobile platforms need this singleton facility
but don't support global and static vaiables.
--eminemence.

Gianni Mariani wrote:
eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.

No.
Nov 2 '06 #5

eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.
That depends on exactly what you want to do and why. Why don't you want
to use statics or global variables?

Here is a simple singleton that stores one int. Whether it is of much
use or not depends on whether or not you can pass 'singleton' around to
every function that requires it.

int main() {
int singleton;
// use singleton here
return 0;
}
K

Nov 2 '06 #6
On 2 Nov 2006 01:50:07 -0800, "eminemence" <em********@gmail.com>
wrote:
>Gianni Mariani wrote:
>eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.

No.
Thats quite sad as many mobile platforms need this singleton facility
but don't support global and static vaiables.
--eminemence.
[Please, do not top-post]

Really? A C++ compiler with no possibility to define a static or
global variable? I have never found one, and I work in the embedded
field. May you tell me which one is it, to avoid it?

Zara
Nov 2 '06 #7
Gianni Mariani wrote:
eminemence wrote:
>Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.

No.
Why the hell not? As long as you have a storage you can do it. For example,

Single* obtain_single()
{
void* p = get_system_pool();
byte_t* i = static_cast<byte_t*>(p);
if (*(i-1) == 0)
{
Single* s = new(p) Single;
return s;
}

return static_cast<Single*>(p);
}

Regards,
Ben
Nov 2 '06 #8

Zara wrote:
On 2 Nov 2006 01:50:07 -0800, "eminemence" <em********@gmail.com>
wrote:
Gianni Mariani wrote:
eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.


No.
Thats quite sad as many mobile platforms need this singleton facility
but don't support global and static vaiables.
--eminemence.
[Please, do not top-post]

Really? A C++ compiler with no possibility to define a static or
global variable? I have never found one, and I work in the embedded
field. May you tell me which one is it, to avoid it?

Zara
Symbian does not allow.
So if you compile for the emulator it will allow the static and global
declarations but when the app is finally linked for the platform it
throws an error.
So such a facility is needed just to accommodate all platforms nothing
else.
--eminemence.

Nov 2 '06 #9
* eminemence:
Zara wrote:
>On 2 Nov 2006 01:50:07 -0800, "eminemence" <em********@gmail.com>
wrote:
>>Gianni Mariani wrote:
eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.
>
No.
Thats quite sad as many mobile platforms need this singleton facility
but don't support global and static vaiables.
--eminemence.
[Please, do not top-post]

Really? A C++ compiler with no possibility to define a static or
global variable? I have never found one, and I work in the embedded
field. May you tell me which one is it, to avoid it?

Zara

Symbian does not allow.
So if you compile for the emulator it will allow the static and global
declarations but when the app is finally linked for the platform it
throws an error.
So such a facility is needed just to accommodate all platforms nothing
else.
That's truly Evil(TM). I read up on it. And there seems to be no good
reason -- a lot of technical mumbo-jumbo talk about possible costs of
static data given the quite baffling decisions the Symbian OS designers
have landed on, but no discussion of the rationale of those decisions,
e.g. why application DLLs are treated the same as ordinary DLLs.

But then, I've never programmed the Symbian OS.

It seems you have three choices to get the equivalent of standard C++
functionality for static data on this OS:

* do not target the old combination EKA1 DLL (whatever "EKA1" means),
which is where this problem exists, or

* place all needed singletons or support for them in an EXE server,
or

* use thread local storage (in Symbian, Dll::SetTls() and Dll::Tls()).

<url:
http://developer.symbian.com/main/downloads/papers/static_data/SupportForWriteableStaticDataInDLLsv1.1.pdf>.

Hope this helps,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 2 '06 #10

Alf P. Steinbach wrote:
* eminemence:
Zara wrote:
On 2 Nov 2006 01:50:07 -0800, "eminemence" <em********@gmail.com>
wrote:

Gianni Mariani wrote:
eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.

No.
Thats quite sad as many mobile platforms need this singleton facility
but don't support global and static vaiables.
--eminemence.

[Please, do not top-post]

Really? A C++ compiler with no possibility to define a static or
global variable? I have never found one, and I work in the embedded
field. May you tell me which one is it, to avoid it?

Zara
Symbian does not allow.
So if you compile for the emulator it will allow the static and global
declarations but when the app is finally linked for the platform it
throws an error.
So such a facility is needed just to accommodate all platforms nothing
else.

That's truly Evil(TM). I read up on it. And there seems to be no good
reason -- a lot of technical mumbo-jumbo talk about possible costs of
static data given the quite baffling decisions the Symbian OS designers
have landed on, but no discussion of the rationale of those decisions,
e.g. why application DLLs are treated the same as ordinary DLLs.

But then, I've never programmed the Symbian OS.

It seems you have three choices to get the equivalent of standard C++
functionality for static data on this OS:

* do not target the old combination EKA1 DLL (whatever "EKA1" means),
which is where this problem exists, or

* place all needed singletons or support for them in an EXE server,
or

* use thread local storage (in Symbian, Dll::SetTls() and Dll::Tls()).

<url:
http://developer.symbian.com/main/downloads/papers/static_data/SupportForWriteableStaticDataInDLLsv1.1.pdf>.

Hope this helps,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Hi Alf,
As I have been programming for Symbian for the past 2 and half years
and was aware of the solution when its only for Symbian.
But what do we do if we want to have a single design and make that work
on most of the mobile platforms?
We cannot ignore the previous version of Symbian as lots of devices are
in the market which have the older version.
So nothing evil but just trying to have a generic design which can
work.
Best Regards
--eminemence.

Nov 2 '06 #11
On 2 Nov 2006 01:50:07 -0800, "eminemence" <em********@gmail.com>
wrote:
>Thats quite sad as many mobile platforms need this singleton facility
but don't support global and static vaiables.
Create a context object and pass it to the places where the context is
needed. BTW, global and static variables cause a lot of well known
problems and reduce reusability and testability of code. It's better
to avoid them even if you are not forced by the environment.

Best wishes,
Roland Pibinger
Nov 2 '06 #12
benben wrote:
Gianni Mariani wrote:
>eminemence wrote:
>>Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.

No.

Why the hell not? As long as you have a storage you can do it. For example,

Single* obtain_single()
{
void* p = get_system_pool();
byte_t* i = static_cast<byte_t*>(p);
if (*(i-1) == 0)
{
Single* s = new(p) Single;
return s;
}

return static_cast<Single*>(p);
}
Is get_system_pool() standard C++ ?

I assumed that the OP meant "using standard c++" since this NG is
specific on "standard" C++.
Nov 2 '06 #13
* eminemence:
As I have been programming for Symbian for the past 2 and half years
and was aware of the solution when its only for Symbian.
But what do we do if we want to have a single design and make that work
on most of the mobile platforms?
First, please don't quote signatures.

Well, the language standard can't help you when the problem is a
non-standard implementation of the language.

However, abstraction can. E.g. a static singleton manager library,
implemented differently for different platforms. But then, Symbian's
lack of support for exceptions and insistence on evil two-phase
construction makes things complicated, so some innovation required.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 2 '06 #14
eminemence <em********@gmail.comwrote:
>Symbian does not allow.
So if you compile for the emulator it will allow the static and global
declarations but when the app is finally linked for the platform it
throws an error.
So such a facility is needed just to accommodate all platforms nothing
else.
Consider devoting a thread or process to the functionality
you would use a singleton for.

Steve
Nov 2 '06 #15
eminemence wrote:
Is it possible to implement a Singleton without using static variables
or global variables.
Some operating systems have the facility to register a string,
and then you can check to see if the string has been registered
already when an attempt is made to create your object.

Nov 2 '06 #16

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

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
15
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
5
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
5
by: Markus Dehmann | last post by:
I need a Singleton for general program options so that all classes can access it. I use the code below (adapted from the Wikipedia singleton example). But the problem is if I change one variable...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.