By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Singleton without static : possible??

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

Nov 2 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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

P: n/a
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

P: n/a
eminemence wrote:
Hi,
Is it possible to implement a Singleton without using static variables
or global variables.
Thanks.
--eminemence.
No.
Nov 2 '06 #4

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
* 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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.