The ACE programmer s guide practical design patterns for network and systems programming 5. print Edition Johnson 2024 scribd download
The ACE programmer s guide practical design patterns for network and systems programming 5. print Edition Johnson 2024 scribd download
com
https://ebookfinal.com/download/the-ace-programmer-s-guide-
practical-design-patterns-for-network-and-systems-
programming-5-print-edition-johnson/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookfinal.com/download/systems-programmer-s-guide-to-z-os-
system-logger-1st-ed-edition-ibm-redbooks/
ebookfinal.com
https://ebookfinal.com/download/the-theft-of-history-5-print-edition-
jack-goody/
ebookfinal.com
https://ebookfinal.com/download/mongodb-applied-design-patterns-
practical-use-cases-with-the-leading-nosql-database-1st-edition-rick-
copeland/
ebookfinal.com
The ACE Programmer’s Guide
This page intentionally left blank
The ACE Programmer’s Guide
Stephen D. Huston
James CE Johnson
Umar Syyid
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind
and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in
connection with or arising out of the use of the information or programs contained herein.
Figures 7.1, 7.2, 7.3, and 8.1 originally published in Schmidt/Huston, C++ Network Programming, Volume 2: Systematic Reuse
with ACE and Frameworks, Copyright © 2003 by Pearson Education, Inc. Reprinted with permission of Pearson Education, Inc.
The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information,
please contact:
U.S. Corporate and Government Sales
(800) 382-3419
[email protected]
For sales outside of the U.S., please contact:
International Sales
[email protected]
Visit Addison-Wesley on the Web: www.awprofessional.com
Library of Congress Cataloging-in-Publication Data
Huston, Stephen D.
The ACE programmer’s guide : practical design patterns for network and systems
programming / Stephen D. Huston, James CE Johnson and Umar Syyid.
p. cm.
ISBN 0-201-69971-0 (pbk. : alk. paper)
1. Computer software—Development. 2. Object-oriented programming (Computer science)
3. Software patterns. I. Johnson, James C. E. II. Syyid, Umar. III. Title.
QA76.76.D47H89 2003
005.1’17—dc21 2003014046
Illustrations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Part I: ACE Basics 1
1. Introduction to ACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 A History of ACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 ACE’s Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 ACE’s Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Patterns, Class Libraries, and Frameworks. . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Porting Your Code to Multiple Operating Systems . . . . . . . . . . . . . . . . 8
1.6 Smoothing the Differences among C++ Compilers . . . . . . . . . . . . . . . 11
1.7 Using Both Narrow and Wide Characters . . . . . . . . . . . . . . . . . . . . . . 19
1.8 Where to Find More Information and Support. . . . . . . . . . . . . . . . . . . 21
1.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2. How to Build and Use ACE in Your Programs . . . . . . . . . . . . . . . 25
2.1 A Note about ACE Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2 Guide to the ACE Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
v
vi Contents
xi
This page intentionally left blank
Tables
xiii
xiv Tables
3. Flexibility, to support a growing range of data types, traffic flows, and end-to-
end QoS requirements
4. Portability, to reduce the effort required to support applications on heteroge-
neous OS platforms and compilers
5. Predictability and efficiency, to provide low latency to delay-sensitive real-
time applications, high performance to bandwidth-intensive applications, and
usability over low-bandwidth networks, such as wireless links
6. Reliability, to ensure that applications are robust and tolerant of faults
7. Scalability, to enable applications to handle many users simultaneously
8. Trustworthiness, to ensure integrity, confidentiality, and availability in inter-
networked systems
I’ve encountered all these challenges while working on scores of research and
production performance-driven software systems during the past two decades. In
1992, I began creating a software toolkit called the ADAPTIVE Communication
Environment (ACE) to address concurrent network programming challenges.
ACE is portable object-oriented host infrastructure middleware written in C++.
ACE was designed to solve a very pragmatic problem—to save time imple-
menting my doctoral dissertation project on parallel protocol processing engines
in both SunOS 4.x and SunOS 5.x operating systems. Having worked on free soft-
ware projects for many years, I recognized immediately that making ACE avail-
able in open-source form would help to galvanize a community whereby other
developers and users could assist with the QA, documentation, and support activi-
ties needed to transform ACE into a high-quality production software toolkit.
Over the next decade, a core group of around 30 developers and I teamed up
with more than 1,700 contributors from around the world to evolve ACE so it
encapsulates and augments a wide range of native OS capabilities that are essen-
tial to support performance-driven software systems. The core frameworks and
wrapper facades in ACE provide an integrated collection of reusable object-
oriented classes that simplify and automate connection establishment, event
demultiplexing, secure and efficient interprocess communication, (de)marshaling,
dynamic configuration of application components, concurrency, and synchroniza-
tion. Most important, the ACE frameworks can be customized readily to support a
wide range of runtime contexts and application requirements.
Today‚ ACE runs on dozens of hardware and OS platforms, including most
versions of Windows and UNIX, as well as many real-time and embedded oper-
ating systems. Due to its maturity, efficiency, and scope, ACE is being used by
thousands of development teams, ranging from large Fortune 500 companies to
xviii Foreword
small start-ups to advanced research projects at universities and industry labs. Its
open-source development model and highly knowledgeable user community is
similar in spirit, enthusiasm, and productivity to that driving the Linux operating
system, the Apache web server, and the GNU compiler collection.
For many years, however, the only way to learn ACE was to read the source
code and example applications, technical papers that described its patterns and the
design of its frameworks, and online tutorials. Although a surprisingly large
number of ACE users managed to master ACE via this process, the learning curve
was time consuming and error prone. In 2002 and 2003, I teamed up with Steve
Huston to write a pair of books on C++ Network Programming (C++NP), which
cover the motivation, design, implementation, and use of the most popular
wrapper facade classes and frameworks in ACE.
The ACE Programmer’s Guide takes a different tack from the C++NP books,
explaining quickly and directly how to make the best use of ACE. In addition to its
nuts-and-bolts approach to explaining key ACE capabilities, this book addresses
important topics not covered in the C++NP books, including how to build ACE
and your programs that use ACE, ACE’s OS and compiler portability features, the
ACE logging facility, command line options processing, container classes, signal
handling, and shared memory. The book also covers certain material in greater
depth than the C++NP books, including concurrency models, bidirectional
streams, and asynchronous I/O. Throughout this book, many examples using ACE
show how key patterns and design principles can be applied to develop object-
oriented networked software successfully. By learning the material in this book‚
you’ll be better able to design and implement performance-driven networked soft-
ware that can’t be bought off-the-shelf, thereby staying ahead of your competition.
We are fortunate that Steve, James, and Umar have found time in their hectic
schedules to write this book. If you’re designing software and systems that must
be portable, flexible, extensible, predictable, reliable, and affordable, this book
and the ACE toolkit will enable you to be more effective in all these areas. Even
after spending more than a decade developing ACE and using it to build
networked software applications, I find that I’ve learned a great deal from this
book‚ and I’m confident that you will too.
—Douglas C. Schmidt
Professor of Electrical Engineering and Computer Science at
Vanderbilt University and Inventor of ACE
Nashville, Tennessee
Preface
Organization
This book is a hands-on, how-to guide to using ACE effectively. The many source
code examples illustrate proper use of the pieces of ACE being described. The
source code examples are kept fairly short and to the point. Sometimes, the
example source is abridged in order to focus attention on a topic. The complete
source code to all examples is on the included CD-ROM and is also available on
Riverace Corporation’s web site. The included CD-ROM also includes a copy of
ACE’s source kit, installable versions of ACE prebuilt for a number of popular
platforms, and complete reference documentation for all the classes in ACE.
The book begins with basic areas of functionality that many ACE users need
and then proceeds to build on the foundations, describing the higher-level features
that abstract behavior out into powerful patterns.
• Part I introduces ACE and provides some generally useful information about
the facilities ACE provides. Part I also explains how to configure and build
ACE, as well as how to build your applications that use ACE. Widely used
programming aids, such as logging and tracing, command line processing and
configuration access, and ACE’s container classes, are also described.
Preface xxi
Acknowledgments
We are indebted to the review team that read and commented on the entire manu-
script. Craig L. Ching, Dave Mercer, Johnny Willemsen, and Steven P. Witten
provided insightful and helpful feedback and ideas that improved the book greatly.
We are also grateful to the members of the ACE user community worldwide
who volunteered their free time to review a number of manuscript drafts and
provide helpful corrections and advice. Like the many contributors to ACE itself,
these individuals show the cooperative nature of many Open-Source developer/
user communities, and the ACE users in particular: Bill Cassanova, Ufuk Çoban,
Kobi Cohen-Arazi, Todd Cooper, Ning Cui, Alain Decamps, John Fowler, Chris
D. Gill, Andy Harvey, Dale Hawkins, Kelly F. Hickel, Don Hinton, Bill Hopkins,
Robert Kindred, Michael Kleck, Franz Klein, Sven Köster, Dieter Knüppel, Theo
Landman, Mark Levan, Alexander Libman, John Lilley, Stephen McDonald, Mike
Mullen, Mats Nilsson, Jaroslaw Nozderko, Rick Ohnemus, Wojtek Pilorz,
Sunanda C. Prasad, Dietrich Quehl, Irma Rastegayeva, Karen Regner, Michael
Searles, Rich Siebel, Chris Smith, Scott Smith, Edward Thompson, Alain
Totouom, Bill Trudell, and Lothar Werzinger.
Our editorial team at Addison-Wesley was very helpful and encouraging
during this long process as well. Thanks to our editors: Marina Lang, Debbie
Lafferty, Peter Gordon, and Bernie Gaffney. Thank you to our copy editor, Evelyn
Pyle, who did a marvelous job molding our differing approaches and styles into a
unified whole. Many, many thanks to our production coordinator, Elizabeth Ryan,
who has now ushered all three ACE-related books through production with great
skill, grace, and patience.
Steve’s Acknowledgments
As in all my work, I am deeply indebted to my wonderful wife, Jane, for her
abiding love, constant support, and much-needed help during the long process of
writing this book. You sacrificed much more than anyone should ever have to and
are truly God’s gift to me—thank you! As wise Solomon once said, “Of making
many books there is no end, and much study wearies the body” (Ecclesiastes
12:12, NIV). I’m a bit weary and thankful to God for the energy to complete this
work. I’m also grateful to my late mother, Karen L. Anderson, who would be
pleased with the lessons I’ve finally learned.
Preface xxiii
James’s Acknowledgments
I would like to thank my wife, Karla, and my son, Riley, who was born half-way
through this four-year process, for their patience and understanding every time I
disappeared into the basement office for hours on end. Without their constant
support and encouragement, I don’t think I would have ever made it to this point. I
would also like to thank my parents for always encouraging me to put forth my
best effort and to never settle for second best. And, finally, thanks to Doug
Schmidt for creating ACE in the first place and giving us all the opportunity to
create this text.
Umar’s Acknowledgments
First of all I would like to thank my wife, Ambreen, and my son, Hassan. If it
weren’t for them, I might not leave the computer at all for a few days; thanks for
calling me home. Ambreen deserves my special thanks for enduring my constant
babble over the years. Without her support, I would not have written a single page.
I would also like to thank my mother for her courage and support during the more
difficult times in my life. In addition, I would like to thank my father for making
me want to show him that I could. Finally, thanks to Doug Schmidt for driving
down to the University of Illinois to provide a graduate seminar that first taught
me about a framework called ACE.
Concluding Remarks
This book is a true coauthorship, with each of us writing equal amounts of the
text. Therefore, we all share equally in the blame for any problems you may find
in the text. Please report anything you do find to either the ACE users mailing list
([email protected]) or to us at [email protected].
Writing a book on a topic as broad as ACE is a very difficult task. The fact that we
had a team of authors made the task considerably easier. In addition, the help
available from the DOC group and the ACE user community has proved invalu-
able.
Steve Huston
James CE Johnson
Umar Syyid
This page intentionally left blank
Part I
ACE Basics
This page intentionally left blank
Chapter 1
Introduction to ACE
3
4 Chapter 1: Introduction to ACE
1. If you’d like more information on Open Source Software in general, please visit the Open Source
Initiative page on the web at http://www.opensource.org/.
1.2 ACE’s Benefits 5
directory is THANKS. It lists all the people who have contributed to ACE over the
years—more than 1,700 at the time of this writing! If you contribute improve-
ments or additions to ACE, you too can get your name listed.
Each layer reuses classes in lower layers, abstracting out more common func-
tionality. This means that any given task in ACE can usually be done in more than
one way, depending on your needs and design constraints. Although we some-
times approach a problem “bottom up” because existing knowledge helps transi-
tion smoothly to ACE programming techniques, we generally cover the higher-
level ways rather than the lower-level ways to approach problems in more depth. It
is relatively easy, however, to find the lower-level classes and interfaces for your
use when needed. The ACE reference documentation on the included CD-ROM
can be used to find all class relationships and complete programming references.
book. But what about other functions that are often taken for granted, such as
printf() and fseek()? Even when you need to perform a basic function, it is
safest to use the ACE_OS methods rather than native APIs. This usage guarantees
that you won’t be surprised by a small change in arguments to the native calls
when you compile your code on a different platform.
The ACE OS adaptation layer is implemented in the ACE_OS class. The
methods in this class are all static. You may wonder why a separate namespace
wasn’t used instead of a class, as that’s basically what the class achieves. As we’ll
soon see, one of ACE’s strengths is that it works with a wide variety of old and
new C++ compilers, some of which do not support namespaces at all. We are not
going to list all the supplied functions here. You won’t often use these functions
directly. They are still at a very low level, not all that much different from writing
in C. Rather, you’ll more often use high-level classes that themselves call
ACE_OS methods to perform the requested actions. Therefore, we’re going to
leave a detailed list of these methods to the ACE_OS reference pages.
As you might imagine, ACE contains quite a lot of conditionally compiled
code, especially in the OS adaptation layer. ACE does not make heavy use of
vendor-supplied compiler macros for this, for a couple of reasons. First, a number
of the settings deal with features that are missing or broken in the native platform
or compiler. The missing or broken features may change over time; for instance,
the OS is fixed or the compiler is updated. Rather than try to find a vendor-
supplied macro for each possible item and use those macros in many places in
ACE, any setting and vendor-supplied macro checking is done in one place and
the result remembered for simple use within ACE. The second reason that vendor-
supplied macros are not used extensively is that they may either not exist or may
conflict with another vendor’s macros. Rather than use a complicated combination
of ACE-defined and vendor-supplied macros, a set of ACE-defined macros is used
extensively and is sometimes augmented by vendor-supplied macros, although
this is relatively rare.
The setting of all the ACE-defined compile-time macros is done in one file,
ace/config.h, which we’ll look at in Chapter 2. Many varieties of this config-
uration file are supplied with ACE, matching all the platforms ACE has been
ported to. If you obtain a prebuilt version of ACE for installation in your environ-
ment, you most likely will never need to read or change this file. But sometime
when you’re up for some adventure, read through it anyway for a sampling of the
range of features and issues ACE handles for you.
1.6 Smoothing the Differences among C++ Compilers 11
1.6.1 Templates
C++ templates are a powerful form of generic programming, and ACE makes
heavy use of them, allowing you to combine and customize functionality in
powerful ways. This brief discussion introduces you to class templates. If
templates are a new feature to you, we suggest that you also read an in-depth
discussion of their use in a good C++ book, such as Bjarne Stroustrup’s The C++
Programming Language, 3rd Edition [11] or C++ Templates: The Complete
Guide by David Vandevoorde and Nicolai M. Josuttis [14]. As you’re reading,
keep in mind that the dialect of C++ described in your book and that implemented
by your compiler may be different. Check your compiler documentation for
details, and stick to the guidelines documented in this book to be sure that your
code continues to build and run properly when you change compilers.
C++’s template facility allows you to generically define a class or function,
and have the compiler apply your template to a given set of data types at compile
time. This increases code reuse in your project and enables you to reuse code
across projects. For example, let’s say that you need to design a class that remem-
bers the largest value of a set of values you specify over time. Your system may
have multiple uses for such a class—for example, to track integers, floating-point
12 Chapter 1: Introduction to ACE
numbers, text strings, and even other classes that are part of your system. Rather
than write a separate class for each possible type you want to track, you could
write a class template:
template <class T> class max_tracker {
public:
void track_this (const T val);
private:
T max_val_;
};
Template Instantiation
Many modern C++ compilers automatically “remember” which template
instantiations your code needs and generate them as a part of the final link phase.
In these cases, you’re all set and don’t need to do anything else. For one or more
of the following reasons, however, you may need to take further steps regarding
instantiation.
• You need to port your system to another platform on which the compiler isn’t
so accommodating.
1.6 Smoothing the Differences among C++ Compilers 13
• Your compiler is really slow when doing its magical template autoinstantia-
tion, and you’d like to control it yourself, speeding up the build process.
Compilers provide a variety of directives and methods to specify template
instantiation. Although the intricacies of preprocessors and compilers make it
extremely difficult to provide one simple statement for specifying instantiation,
ACE provides a boilerplate set of source lines you should use to control explicit
template instantiation. The best way to explain it is to show its use. In the
preceding example code, the following code would be added at the end of the
source file:
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class max_tracker<int>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate max_tracker<int>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
This code will successfully build on all compilers that correctly handle automatic
instantiation, as well as all compilers ACE has been ported to that customarily use
explicit template instantiation for one reason or another. If you have more than
one class to instantiate, you add more template class and #pragma
instantiate lines to cover them.
Be careful when deciding to explicitly instantiate templates in projects that use
the standard C++ library. The templates in the standard library often use other, not
so obvious, templates, and it’s easy to end up in a maze of instantiation statements
that change between compilers. It’s much safer to let the compiler automatically
instantiate template classes whenever possible.
macros for the cases in ACE when the information is required together. In the case
of the ACE_SOCK_Stream class’s usage, when the addressing trait is also
needed, ACE defines ACE_SOCK_STREAM, which expands to include the
addressing type for compilers that don’t support typedefs in classes used as
template arguments. It expands to ACE_SOCK_Stream for compilers that do
support it. The many uses of this tactic in ACE will be noted for you at the points
in the book where their use comes up.
2. It wouldn’t surprise us if there were also other possible values, such as 8 bits or 64 bits. The point
is: Don’t count on a particular size.
1.6 Smoothing the Differences among C++ Compilers 15
Type Meaning
ACE_INT16, ACE_UINT16 16-bit integer, signed and unsigned
ACE_INT32, ACE_UINT32 32-bit integer, signed and unsigned
ACE_UINT64 64-bit unsigned integer; on platforms without native
64-bit integer support, ACE provides a class to
emulate it
ACE_SIZEOF_CHAR Number of bytes in a character
ACE_SIZEOF_WCHAR Number of bytes in a wide character
ACE_SIZEOF_SHORT Number of bytes in a short int
ACE_SIZEOF_INT Number of bytes in an int
ACE_SIZEOF_LONG Number of bytes in a long int
ACE_SIZEOF_LONG_LONG Number of bytes in a long long int; on plat-
forms without native long long support, this is 8,
and ACE_UINT64 is ACE_U_LongLong
ACE_SIZEOF_VOID_P Number of bytes in a pointer
ACE_SIZEOF_FLOAT Number of bytes in a float
ACE_SIZEOF_DOUBLE Number of bytes in a double
ACE_SIZEOF_LONG_DOUBLE Number of bytes in a long double
ACE_BYTE_ORDER Has a value of either ACE_BIG_ENDIAN or
ACE_LITTLE_ENDIAN
make use of the Object Manager by registering objects that must be destroyed.
The Object Manager destroys all the objects registered with it at rundown
time, in reverse order of their registration.
• ACE_Cleanup. ACE_Object_Manager uses this class’s interface to
manage object life cycles. Each object to be registered with the Object
Manager must be derived from ACE_Cleanup.
• ACE_Singleton. The Singleton pattern [3] is used to provide a single
instance of an object to a program and provide a global way to access it. This
object instance is similar to a global object in that it’s not hidden from any part
of the program. However, the instantiation and deletion of the object is under
control of your program, not the platform’s runtime environment. ACE’s
16 Chapter 1: Introduction to ACE
singleton class also adds thread safety to the basic Singleton pattern by using
the Double-Checked Locking Optimization pattern [5]. The double-checked
lock ensures that only one thread initializes the object in a multithreaded
system.
The two most common ways to be sure that your objects are properly cleaned
up at program rundown are described next. Which one you use depends on the
situation. If you want to create a number of objects of the same class and be sure
that each is cleaned up, use the ACE_Cleanup method. If you want one instance
of your object accessible using the Singleton pattern, use the ACE_Singleton
method.
• ACE_Cleanup method. To be able to create a number of objects of a class
and have them be cleaned up by the ACE Object Manager, derive your class
from ACE_Cleanup, which has a cleanup() method that the Object
Manager calls to do the cleanup. To tell the Object Manager about your
object—so it knows to clean it up—you make one simple call, probably from
your object’s constructor:
ACE_Object_Manager::at_exit (this);
TYPE is name of the class you are turning into a singleton. ACE_LOCK is the type
of lock the Double-Checked Locking Optimization pattern implementation uses to
serialize checking when the object needs to be created. For code that operates in a
multithreaded environment, use ACE_Recursive_Thread_Mutex. If you are
writing code without threads, you should use ACE_Null_Mutex, which
provides the proper interface but does not lock anything. This would be useful if
your main factor in choosing the Singleton pattern is to provide its “instantiate
when needed” semantics, as well as have the Object Manager clean up the object.
As an example, assume that you have a class named SystemController and
that you want a single instance of it. You would define your class and then
typedef ACE_Singleton<SystemController,
ACE_Recursive_Thread_Mutex> TheSystemController;
1.6 Smoothing the Differences among C++ Compilers 17
When you need access to the instance of that object, you get a pointer to it from
anywhere in your system by calling its instance() method. For example:
SystemController *s = TheSystemController::instance();
As we’ve seen, the ACE Object Manager is quite a useful and powerful object.
You need to remember only two rules in order to successfully make use of this
facility.
1. Never call exit() directly.
2. Make sure that the Object Manager is initialized successfully.
The first is easy. Remember all the registrations with the Object Manager to
request proper cleanup at rundown time? If you call exit(), your program will
terminate directly, without the Object Manager’s having a chance to clean
anything up. Instead, have your main() function simply do a return to end. If
your program encounters a fatal condition inside a function call tree, either return,
passing error indications, or throw an exception that your main() function will
catch so it can cleanly return after cleaning up. In a pinch, you may call
ACE_OS::exit() instead of the “naked” exit(). That will ensure that the
Object Manager gets a chance to clean up.
Object Manager initialization is an important concept to understand. Once you
understand it, you can often forget about it for a number of platforms. The Object
Manager is, effectively, a Singleton. Because it is created before any code has a
chance to create threads, the Object Manager doesn’t need the same protection
that Singletons generally require. And, what object would the Object Manager
register with for cleanup? The cleanup chain has to end somewhere, and this is
where. As we said, statics are generally evil.
However, on some platforms, statics are not completely evil, and the Object
Manager is in fact a static object, initialized and destroyed properly by the plat-
form’s runtime environment, even when used in combination with shared
libraries, some of which are possibly loaded and unloaded dynamically. On these
platforms,3 you can usually ignore the rule for properly initializing the Object
Manager.4 On the others, however, the Object Manager needs to be initialized and
terminated explicitly. For use cases in which you are writing a regular C++ appli-
cation that has the standard int main (int argc, char *argv[])
entry point or the wide-character-enabled ACE_TMAIN entry point shown in
Section 1.7, ACE magically redefines your main() function and inserts its own,
which instantiates the Object Manager on the runtime stack and then calls your
main() function. When your main() function returns, the Object Manager is
run down as well. (This is an example of why your code should not call exit():
You’d bypass the Object Manager cleanup.)
If your application does not have a standard main() function but needs to
initialize and run down the Object Manager, you need to call two functions:
1. ACE::init() to initialize the Object Manager before any other ACE opera-
tions are performed.
2. ACE::fini() to run down the Object Manager after all your ACE opera-
tions are complete. This call will trigger the cleanup of all objects registered
with the Object Manager.
This may be necessary for Windows programs that have a WinMain() function
rather than the standard main() function and for libraries that make use of ACE
but the users of the library do not. For libraries, it is advantageous to add initialize
and finalize functions to the library’s API and have those functions call
ACE::init() and ACE::fini(), respectively. In case you’re writing a
Windows DLL (dynamic link library) and thinking you can make the calls from
DllMain(), don’t go there. It’s been tried.
You can also build ACE on Windows with
#define ACE_HAS_NONSTATIC_OBJECT_MANAGER 0
in your config.h file before including ace/config-win32.h. (Chapter 2
describes ACE’s configuration.) This removes the need to explicitly call
ACE::init() and ACE::fini() but may cause problems when using
dynamic services.
Macro Action
ACE_NEW(p, c) Allocate memory by using constructor c and
assign pointer to p. On failure, p is 0 and
return;
ACE_NEW_RETURN(p, c, r) Allocate memory by using constructor c and
assign pointer to p. On failure, p is 0 and
return r;
ACE_NEW_NORETURN(p, c) Allocate memory by using constructor c and
assign pointer to p. On failure, p is 0 and con-
trol continues at next statement.
Macro Purpose
ACE_HAS_WCHAR Configuration setting that enables ACE’s
wide-character methods
ACE_USES_WCHAR Configuration setting that directs ACE to
use wide characters internally
ACE_TCHAR Matches ACE’s internal character width;
defined as either char or wchar_t,
depending on the lack or presence of
ACE_USES_WCHAR
ACE_TMAIN Properly defines program’s main entry
point for command line argument type,
based on ACE_USES_WCHAR
ACE_TEXT(str) Defines the string literal str correctly,
based on ACE_USES_WCHAR
ACE_TEXT_CHAR_TO_TCHAR(str) Converts a char * string to
ACE_TCHAR format, if needed
ACE_TEXT_WCHAR_TO_TCHAR(str) Converts a wchar_t * string to
ACE_TCHAR format, if needed
ACE_TEXT_ALWAYS_CHAR(str) Converts an ACE_TCHAR string to
char * format, if needed
1.8 Where to Find More Information and Support 21
capable of. Moreover, ACE must be built with the ACE_USES_WCHAR setting if
ACE should also use wide characters internally. The ACE_TCHAR and
ACE_TEXT macros are illustrated in examples throughout this book.
ACE also supplies two string classes—ACE_CString and
ACE_WString—that hold narrow and wide characters, respectively. These
classes are analogous to the standard C++ string class but can be configured to
use custom memory allocators and are more portable. ACE_TString is a
typedef for one of the two string types depending on the ACE_USES_WCHAR
configuration setting.
• Kits for the currently supported ACE versions are available at Riverace’s web
site: http://www.riverace.com.
• Complete sources for the current release, current BFO (bug fix only) beta, and
the most recent beta versions of ACE are available from the DOC group’s web
site: http://download.dre.vanderbilt.edu/. The various types
of kits are explained in Section 2.1.
A number of ACE developer forums are available via e-mail and Usenet news.
The authors and the ACE developers monitor traffic to these forums. These
people, along with the ACE user community at large, are very helpful with ques-
tions and problems. If you post a question or a problem to any of the following
forums, please include the information in the PROBLEM-REPORT-FORM file
located in the top-level directory of the ACE source kit:
• comp.soft-sys.ace newsgroup.
• [email protected] mailing list. To join this list, send a request
to [email protected]. Include the following
command in the body of the e-mail:
subscribe ace-users [emailaddress@domain]
1.9 Summary
This chapter introduced the ACE toolkit’s organization and described some foun-
dational concepts and techniques that you’ll need to work with ACE. The chapter
covered some helpful macros and facilities provided to smooth out the differences
among C++ compilers and runtime environments, showed how to take advantage
of both narrow and wide characters, and, finally, listed some sources of further
information about ACE and the available services to help you make the most of
ACE.
This page intentionally left blank
Chapter 2
How to Build and Use ACE in Your
Programs
AAMUN SYDÄN.
Anders Österling.
ONNEN HETKI.
Erik Lindorm.
Käsivarteni lastako tuudittaa, ja sen silmissä kuvani on? Ja
ulapat välkkyy, ja lämmin on maa ja taivas hattaraton!
SATAA.
Ragnar Jändel.
Uuno Kailas.
RUNO RISTIINNAULITUILLE.
RUNO RUNOSTA.
TYHMÄT JA VIISAAT.
(Sikermästä lapsifantasioja.)
SUOMALAINEN SONETTIPARAATI.
E. Ahtela.
Nuortti!
Joo, kai!
*****
*****
Liikettä, kirjavaa eloa ja meurua oli Kelloselän kaarteella. Miehet
touhusivat kajahtavia huutoja päästellen, poikaset kahloivat
poljetussa lumessa heikompia poroja kokoillen. Vaikka uskalsivathan
he isonkin hirvaan sarviin suopungin sinkauttaa: heittäytyen
istumaan ja painaen vankat jalat syvälle lumeen hallitsivat he
jotakuinkin väkevimmänkin elukan. Ja hallit seurasivat tarkkaavaisina
poromiesten liikkeitä, he lopettelivat nyt vain jo ammoin sitten
alkamaansa työtä. Touhuten olivat he viikkokausia pitäneet tuota
häristelevää karjaa koolla, kyliä sivu, saloja halki olivat he sitä
saatelleet. Lyhyt sapara liikkumattomana olivat he karjan eli tokan
liikkeitä seuranneet, ettei se päässyt mitään säikähtämään ja
hajoamaan, pelkkä velvollisuuden tunto kiinnitti heidän kaiken
huomionsa.
— Oon, on se Tuomas!
— Se, se on!
— Tuomas kaarteella!
— No — no pysytään aisoissa.
— Missä!
— Oliko ne verekset?
— Missä
— — Kenen se on?
— Pekka Aatsingin — —
— Mitä!
— Mikä se on?
— Se on — Vinokaula!
Mutta nuo kolme miestä olivat ne, jotka viimeksi näkivät Pekka
Aatsingin elossa.
*****
Pekka Aatsinki jatkoi ajoa. Pekka Aatsinki tahtoi näyttää, että vielä
oli Ihmisellä mahti yli luomakunnan, että mitä hän lupasi, sen hän
myös täytti. Hän ei aikonut tulla ilman taljaa, — kävipä sitten miten
kävi.
Io-hui oli vankempi kuin koskaan ennen, vankempi kuin itse oli
luullutkaan. Hän kompuroi reippaasti eteenpäin, viskoen
etukäpälillään lunta ylös sivuille. Neljäs iltapäivä oli jo käsissä.
Aatsinki hätkähti:
— Jo kävi.
Noustiin kankaalle. Näki heti, että lumi alkoi syvetä, kun metsään
tultiin — hourasiko jo Aatsinki. Puolitiehen puita, niin syvää. Mahtoi
olla pohja syvällä. Ja leppoisaa ja pehmeää oli lumi.
Ei hellitä vähällä.
Mitä ihmettä.
*****
*****
Nuorttiin.
E. A. Saarimaa.