473,804 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type identifier management and object factories


In Andrei Alexandrescu's book _Modern C++ Design_, Chapter 8 ("Object
Factories"), Section 8.4 ("Type Identifiers"), he states:
The only problem that remains is the management of type identifiers.
Still, adding type identifiers requires a fair amount of dicipline
and centralized control. [...]

The only conclusion that can be drawn here is that type identifier
management is not the business of the object factory itself.
Because C++ cannot guarantee a unique, persistent type ID, type ID
management becomes an extra-linguistic issue that must be left to
the programmers.
This strikes me as overly pessimistic. It seems to me that one could
use the address of a static variable defined within a new block, all
within a macro:
#define FACTORY_REGISTE R(fn, cb) \
do { \
static int foo = 42; \ <- foo's address is unique
fn(&foo, cb); \
} while(0)
Quoting ISO/IEC C++ 14882:2003, 3.7.1 (4):

If an object of static storage duration has initialization [...],
it shall not be eliminated even if it appears to be unused [...]
On non-pathological platforms where there is an integer size large
enough to hold this pointer value, it could simply convert it, and use
it, for efficiency. For more portability, though, I see no reason that
stringification of this value would not work. The block scope will
prevent collisions with another 'foo'.

I'd appreciate it if somebody could tell me specifically why this (or a
related derivation of it) is a bad idea. It would seem to be a fairly
obvious strategy, but I spend most of my time doing systems programming
in C, so that certainly colors my thinking on the matter.
Thanks,
Mark F. Haigh
mf*****@sbcglob al.net

Jul 22 '05 #1
1 1857
"Mark F. Haigh" <mf*****@sbcglo bal.ten> wrote in message
news:YY******** ********@newssv r21.news.prodig y.com...

In Andrei Alexandrescu's book _Modern C++ Design_, Chapter 8 ("Object
Factories"), Section 8.4 ("Type Identifiers"), he states:
The only problem that remains is the management of type identifiers.
Still, adding type identifiers requires a fair amount of dicipline
and centralized control. [...]

The only conclusion that can be drawn here is that type identifier
management is not the business of the object factory itself.
Because C++ cannot guarantee a unique, persistent type ID, type ID
management becomes an extra-linguistic issue that must be left to
the programmers.
This strikes me as overly pessimistic. It seems to me that one could
use the address of a static variable defined within a new block, all
within a macro:
#define FACTORY_REGISTE R(fn, cb) \
do { \
static int foo = 42; \ <- foo's address is unique
fn(&foo, cb); \
} while(0)
Quoting ISO/IEC C++ 14882:2003, 3.7.1 (4):

If an object of static storage duration has initialization [...],
it shall not be eliminated even if it appears to be unused [...]
On non-pathological platforms where there is an integer size large
enough to hold this pointer value, it could simply convert it, and use
it, for efficiency. For more portability, though, I see no reason that
stringification of this value would not work. The block scope will
prevent collisions with another 'foo'.

I'd appreciate it if somebody could tell me specifically why this (or a
related derivation of it) is a bad idea. It would seem to be a fairly
obvious strategy, but I spend most of my time doing systems programming
in C, so that certainly colors my thinking on the matter.


I'm not 100% sure that the approach you used is legal, but it really doesn't
matter. One could generate unique IDs by allocating objects dynamically and
using the pointers as keys. However, these approaches do not portably
generate _persistent_ ids, which is what Alexandrescu desired. Note that
one of his issues with using typeid(class).n ame() for this purpose was that
the string value is not guaranteed to be the same every time the application
is run. There may be non-portable ways to work around this, but he is
primarily interested in a portable approach.

--
David Hilsee
Jul 22 '05 #2

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

Similar topics

1
3003
by: Patrick Stinson | last post by:
I am trying to create a way to register static members of a **class**, not an object, for making an object factory. The main thing I want is to be able to make a call like class MyClass { public: MyClass *factory(); }; register(MyClass);
5
2126
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
6
2694
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is completed, for all declarations of that type, by ^^^ # declaring the same structure or union tag with its defining # content later in the same scope. ^^^^^ (6.2.5#23)
0
1116
by: Jeff Louie | last post by:
Well I finally finished a Chapter on the use of Type Based and Interface Based Factories at: http://www.geocities.com/jeff_louie/OOP/oop18.htm Happy Holidays, Jeff *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
1
1281
by: Fabiano Sidler | last post by:
Hi folks! As stated in subject, how do I decide wether to inherit <type 'type'> or <type 'object'>? Whenever I want to intantiate my derived type, I taked <type 'type'> here, but inheriting from <type 'object'> consequently would be reasonable in cases of pure static objects (i.e. objects/types using staticmethods exclusively), for whose I would prefer toplevel code outside a class definition in python, since python does not oblige...
0
1311
by: Laurent Deniau | last post by:
I know that everything related to OO is off topic here, but... I am designing a framework which allows to do OO in pure C, called COS (stands for 'C Object System' inspired by CLOS and Objective-C) and it will be mainly addressed to C users (publicly available) with idioms as close as possible to those of C. One thing which is highly subjective is identifiers' names, and one of the most used identifier is the type identifier of objects....
76
4934
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the uppercase 'B' anyway?) and the header you can include (apparently) to get a real "bool". This isn't my point, however -- it should have been there from the beginning. char is a small int. We all know that. However, "char some_bool = 0;" simply...
11
1793
by: Dinsdale | last post by:
I am trying to determine what the current cast of an object is. Essentially, if I use GetType, it always returns the original type of the object created but I want to know what the current cast of the object is (i.e.: is it still cast as a Derived class, or is it cast as a Base class?) Here is some pseudo code to show the example. Class Base .... End Class
0
160
by: Jeff Louie | last post by:
Robert.. You can use interfaces or an abstract base class to abstract out the functionality of the plug ins. You then program to the abstraction. In the case of an interface, you program to the public view, the interface. The interface defines a contract between the user of your plug in class and any concrete implementation of the class. The caller programs to the type. The caller does
0
10595
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
10335
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
10088
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
9169
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
5529
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...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
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
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.