473,386 Members | 1,720 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,386 software developers and data experts.

Phoenix Singletons

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
the development of the paper i came up with a Phoenix class for providing
Phoenixing behaviour (as defined in Alexandrescu's MC++D) to my Context
Singletons. (http://s11n.net/misccode/)

So what, right?

i'm here to ask the Gurus to please take a look at my Phoenix, and see if
i'm doing something Inherently Wrong here. My gut tells me that the
implementation is simply too straightforward to solve the problem
adequately, but practice shows that it works quite well :/.

The source code is here:

template < typename BaseType, typename ContextType = BaseType >
class phoenix : public BaseType {
public:
typedef ContextType context_type;
typedef BaseType base_type;
static base_type & instance() {
static this_type meyers;
if( this_type::m_destroyed ) {
new( &meyers ) this_type;
atexit( this_type::do_atexit );
}
return meyers;
}
private:
typedef phoenix<base_type,context_type> this_type;
static bool m_destroyed;
phoenix() { m_destroyed = false; }
~phoenix() { m_destroyed = true; }
static void do_atexit() {
if( m_destroyed ) return;
static_cast<this_type SPMamp;>(instance()).~phoenix();
}
};
template <typename T, typename C> bool phoenix<T,C>::m_destroyed = false;

This code is complete for purposes of the questions posed here, but the code
at http://s11n.net/misccode/phoenix.h is "more complete", with docs,
comments, one additional (optional) template parameter and debugging code.

Regarding the implementation, i have these questions:

- What happens when BaseType has a non-virtual destructor? We never use
delete(), relying on static destruction instead, so i think this is safe. i
have successfully used this class with, e.g., std::map<> as BaseType, but
maybe i just have a remarkably accomodating C++ platform.

- Is it possible that m_destroyed gets destroyed at a point such that the
above usages of it becomes invalid? If so, is there a way to phoenix that
bool, so to speak?
Any insights on these points would be greatly appreciated.
--
----- stephan beal
http://s11n.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 22 '05 #1
5 3123
It's undefined behaviour.
You obviously know that it is undefined behaviour.
Getting someone on this newsgroup to say it's OK wont make it so.

"stephan beal" <st*****@s11n.net> wrote in message
news:bs**********@ork.noris.net...
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 the development of the paper i came up with a Phoenix class for providing
Phoenixing behaviour (as defined in Alexandrescu's MC++D) to my Context
Singletons. (http://s11n.net/misccode/)

So what, right?

i'm here to ask the Gurus to please take a look at my Phoenix, and see if
i'm doing something Inherently Wrong here. My gut tells me that the
implementation is simply too straightforward to solve the problem
adequately, but practice shows that it works quite well :/.

The source code is here:

template < typename BaseType, typename ContextType = BaseType >
class phoenix : public BaseType {
public:
typedef ContextType context_type;
typedef BaseType base_type;
static base_type & instance() {
static this_type meyers;
if( this_type::m_destroyed ) {
new( &meyers ) this_type;
atexit( this_type::do_atexit );
}
return meyers;
}
private:
typedef phoenix<base_type,context_type> this_type;
static bool m_destroyed;
phoenix() { m_destroyed = false; }
~phoenix() { m_destroyed = true; }
static void do_atexit() {
if( m_destroyed ) return;
static_cast<this_type SPMamp;>(instance()).~phoenix();
}
};
template <typename T, typename C> bool phoenix<T,C>::m_destroyed = false;

This code is complete for purposes of the questions posed here, but the code at http://s11n.net/misccode/phoenix.h is "more complete", with docs,
comments, one additional (optional) template parameter and debugging code.

Regarding the implementation, i have these questions:

- What happens when BaseType has a non-virtual destructor? We never use
delete(), relying on static destruction instead, so i think this is safe. i have successfully used this class with, e.g., std::map<> as BaseType, but
maybe i just have a remarkably accomodating C++ platform.

- Is it possible that m_destroyed gets destroyed at a point such that the
above usages of it becomes invalid? If so, is there a way to phoenix that
bool, so to speak?
Any insights on these points would be greatly appreciated.
--
----- stephan beal
http://s11n.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 22 '05 #2
On Tue, 30 Dec 2003 09:16:12 +0000, Nick Hounsome wrote:

[ Please don't toppost. Thx, M4 ]
It's undefined behaviour.
You obviously know that it is undefined behaviour.
Getting someone on this newsgroup to say it's OK wont make it so.


Where is the UB? Why do you think he knows this?

M4

Jul 22 '05 #3
"stephan beal" <st*****@s11n.net> wrote...
[...]
template < typename BaseType, typename ContextType = BaseType >
class phoenix : public BaseType {
public:
typedef ContextType context_type;
typedef BaseType base_type;
static base_type & instance() {
static this_type meyers;
if( this_type::m_destroyed ) {
new( &meyers ) this_type;
atexit( this_type::do_atexit );
}
return meyers;
}
private:
typedef phoenix<base_type,context_type> this_type;
static bool m_destroyed;
phoenix() { m_destroyed = false; }
~phoenix() { m_destroyed = true; }
static void do_atexit() {
if( m_destroyed ) return;
static_cast<this_type SPMamp;>(instance()).~phoenix();
}
};
template <typename T, typename C> bool phoenix<T,C>::m_destroyed = false;


I have only a few questions: have you checked what happens when you call
'atexit' to register the function that is being executed at that moment?
I mean, the rest of it is working allegedly, but have you tried using
'exit' in your program? Does it actually exit or does it fall into
an endless loop?

Have you checked whether the number of d-tor calls matches the number of
c-tor calls?

In your 'instance' function, if the object has been destroyed, you never
set the flag back to 'false'. Is that intentional?
Jul 22 '05 #4
Victor Bazarov wrote:
I have only a few questions: have you checked what happens when you call
Hi, Victor! i appologize for the delayed response: i was out for the
holidays longer than i expected.
'atexit' to register the function that is being executed at that moment?
I mean, the rest of it is working allegedly, but have you tried using
'exit' in your program? Does it actually exit or does it fall into
an endless loop?
That's a good question. Now that i think about it, i never call exit()
manually - i return from main() instead. i'll give that a try.
Have you checked whether the number of d-tor calls matches the number of
c-tor calls?
Yes, from what i've seen they do match.
In your 'instance' function, if the object has been destroyed, you never
set the flag back to 'false'. Is that intentional?


That is set via the phoenix's ctor and dtor, so it will be togged to false
when the object is initially created or any time that the object is
phoenixed, and to true when the object is destroyed normally or destroyed
via do_atexit().
--
----- stephan beal
http://s11n.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 22 '05 #5
Nick Hounsome wrote:
It's undefined behaviour.
You obviously know that it is undefined behaviour.
Getting someone on this newsgroup to say it's OK wont make it so.


You'r mistaken: what i KNOW is that deleting a POINTER to a derived type
when the parent has a non-virtual dtor is undefined behaviour. What i DON'T
know is what happens for NON-POINTERS which are never delete()ed.

--
----- stephan beal
http://s11n.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 22 '05 #6

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

Similar topics

11
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...
3
by: Dominik Rau | last post by:
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...
1
by: FBergemann | last post by:
Hi, I wanted to start some samples with phonenix - but it doesn't work. (a polymorphic add function) Because of problems i cut it back and wanted to do just simple "reflection" 1st. But i have...
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
6
by: Steven Watanabe | last post by:
PEP 8 says, "Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators." I know that "is" is an identity operator, "==" and "!=" are the equality...
5
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...
6
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...
7
by: adam.timberlake | last post by:
I was reading an article on TalkPHP (http://www.talkphp.com/ showthread.php?t=1304) about singletons but I'm afraid I don't understand why I need to use them. I understand how to code them...
12
by: Craig Allen | last post by:
Hey, forgive me for just diving in, but I have a question I was thinking of asking on another list but it really is a general question so let me ask it here. It's about how to approach making...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.