| NOTE: |
| The licensing policy for distributing any software published in the book Practical Statecharts in C/C++, CMP Books 2002, ISBN 1-57820-110-1 has changed. Effective December 1, 2004 all this software has been released under dual licensing model. Specifically, if you distribute a new product using any version or part of the Quantum Platform (QP) after the effective date, you must either abide by the terms of the GNU General Public License (GPL), or you can purchase a commercial distribution license in case you want to retain the proprietary status of your code. |
Accompanying CD-ROM
|
The companion CD-ROM contains:
|
The disc comes with an HTML-based index page in its root directory, which is automatically activated if the CD autoinsert notification is enabled on your system. Please note that the code examples are only available on the original CD-ROM and are not accessible from this online demonstration.
The complete CD-ROM image is available for download to the owners of the book Practical Statecharts in C/C++. The ZIP file of the entire CD-ROM (55.1MB) is protected with a password that is easy to generate for the owners of the book. To generate the password you need any version of Practical Statecharts in C/C++: the original English version (ISBN 1-57820-110-1), the Chinese translation (ISBN 7-81077-414-8), or the Korean translation (ISBN 8-98997-549-2). You combine the first word in line 8 of Listing 4.1 and the first word in line 18 of Listing 9.6. Do not insert spaces between these two items.
The CD-ROM is designed for maximum usefulness, even without installing any of it on your hard drive. In particular, you can browse the source code, execute examples, and read PDF documents directly from the CD.
Top of pageCode Updates
|
Download QP/C 2.6.00 software (December 2004) This maintenance release contains all bug fixes mentioned in the Errata, regardless of the book printing. |
|
Download QP/C++ 2.6.00 software (December 2004) This maintenance release contains all bug fixes mentioned in the Errata, regardless of the book printing. |
Top of page![]() |
From the back cover... |
"...downright revolutionary... the title is a major understatement... "Quantum Programming"
may ultimately change the way embedded software is designed."
— Michael Barr, Editor-in-Chief Embedded Systems Programming magazine
(Click here to read Barr's entire column devoted to the book)
"Miro Samek accomplishes three mighty feats in Practical Statecharts in C/C++: He explains how hierarchical state machines work, converts those machines into C/C++, then provides a framework to run those machines in real-time environments or your favorite GUI. Along the way, he demolishes the opposition (how you used to do it), shows you the right way (with code to prove the point), and doesn't bore you to tears (even when explicating quantum mechanics).
Quite a feat by any stretch of the imagination."
— Ed Nisley, "Embedded Space" column, Dr. Dobb's Journal, December 2002.
"Beyond simply talking about concepts, Miro provides complete source code and code walkthroughs alleviating the difficult task of implementing abstract concepts.... a must-have for embedded software engineers who wish to leverage the power of hierarchical state machines."
Brian Schmidt, Sr. Design Engineer, Plexus Technology Group
Model your reactive systems without heavyweight, expensive CASE tools. The author's Quantum Programming is a new paradigm that treats statecharts as a way of design rather than the use of a particular tool. It includes a complete software infrastructure for executing statecharts tailored for multithreaded embedded applications. Flexible, efficient, portable, scalable, and maintainable, this lightweight Quantum Framework permits you to quickly hand-code working real-time systems in C or C++ directly from UML statecharts.
The Quantum Framework is a complete implementation environment - an active-object framework carefully designed to work with nearly any RTOS. It supports rapid prototyping, easy modification of states and transitions at any stage of development, and the choice of implementation language (C or C++) to match the resource constraints of the target system. Best of all, it is compact, with complete QF code typically requiring less than 5KB of code and data.
In Part I of this book, you get a clear, articulate description of the relevant concepts including traditional finite state machines, statecharts, and several statechart-based design patterns in the form of executable code examples. Learn how state nesting leads to behavioral inheritance and how it enables reuse through programming-by-difference. Part II fully describes the implementation of the Quantum Framework and explains how you can use it in your applications and port it to an RTOS of your choice.
The CD-ROM includes complete source code for the author's quantum Framework, answers to all exercises scattered throughout the book, and an evaluation version of RTOS-32 - a 32-bit real-time operating system for x86 processors.
Top of page![]() |
About the Author... |
|
Dr. Miro Samek is the President of Quantum Leaps, provider of modern, event-driven software for embedded systems. Dr. Samek is the author of numerous articles for magazines, and is a regular speaker at the Embedded Systems Conference. Before founding Quantum Leaps Miro worked as Software Architect at Global Locate (San Jose, CA), where he designed GPS software for mobile phones and before that designed GPS receiver software for IntegriNautics Corporation (now Novariant, Menlo Park, CA). Miro started his embedded career at GE Medical Systems, where he has developed safety-critical, real-time software for diagnostic imaging x-ray machines. He earned his Ph.D. in nuclear physics at GSI (Darmstadt, Germany). |
![]() |
Top of page![]() |
Readers' feedback... |
I am thoroughly enjoying your book on Quantum Programming. I have been designing software for embedded systems for over 10 years and I feel this is a true paradigm shift in the understanding of real-time embedded software..."
— Chris Knight, Sr. Software Engineer, Diehl Controls North America
My name is John Orlando and I purchased your book Practical Statecharts
in C/C++ several months ago. I read it cover to cover and thoroughly
enjoyed it...
— John Orlando, Senior Software Engineer, Motorola Labs
I'm a student working as "HIWI" at a department dedicated to embedded systems, communications and protocols. As my task is to implement the complete Bluetooth-Protocol-Stack for the EUROS RTOS we mainly use with our C167 universal control unit, I'm thankful as I can be that
I've been stuck on your book. I've nearly finished reading it and devour page by page because you really opened my mind concerning object oriented programming under C ("C+") and consequently, pure implementing of statecharts into my embedded system..."
— Heiko Hutschenreiter, Dept. LOMI, University of Ulm, Germany
I have been reading your Practical Statecharts in C/C++ book and I think that it is one of the best books I have ever read on software development..."
— Vesa Sarviluoma, Chief engineer, Bitwise Oy
Practical Statecharts in C/C++ von Miro Samek ist ein wirklich hervorragendes Buch. Es geht
um die Verwendung von Statecharts für die Implementierung von Embedded Systemen.
Zunächst bringt Samek eine der besten Einführungen in (den wirklich relevanten Teil von)
Statecharts. Er beschreibt dann verschiedene Implementierungsalternativen in C und C++.
Eine dieser Implementierungsalternativen – der aus seiner Erfahrung beste Kompromiss
aus Performance und Wartbarkeit – beschreibt er im Detail; Samek hat für diese Art der
Implementierung ein Framework entwickelt, welches auf der beiliegenden CD zu findenist. Im 2. Teil des Buches erläutert er die Funktionsweise des Frameworks und dessen
Implementierung auf verschiedenen Plattformen (RTKernel32, DOS, Win32). Wenn man
dieses Buch liest, bekommt man unweigerlich den Eindruck, dass da ein Praktiker seiner
Erfahrungen niedergeschrieben hat und man bekommt direkt Lust, mit diesem Framework
Embedded-Systeme zu entwickeln. Es ist sehr gut lesbar geschrieben (auch wenn glaube
ich mehr Klammer-Einschübe und Fußnoten in dem Buch zu finden sind, wie in allen
anderen Büchern in meinem Regal in Summe)."
— Markus Völter, Ingenieurbüro für Softwaretechnologie
I enjoy every single word when I read your book on state charts.
I have struggled for years against the new generation of programmers and bureaucrats who believe that tools and expensive bureaucracy can solve the problems for them. And I think it is a myth.
This book will help me to prove that I am right.
Thank you!" download PDF »
— Nils-Erik Thorell, Teleca AB, Stockholm, Sweden
At the outset, let me congratulate you on the excellent presentation of HSM's in your book mentioned above - it indeed can revolutionize the way embedded software is written, especially for mission critical applications & commercial applications as well in view of the constant demands from customers to expect zero defects—for example, in their cars..."
— Rajiv Bongirwar, Telecom & Embedded Systems Competency Center Datamatics Ltd, Mumbai, India.
I recently bought a copy of your Practical Statecharts in C/C++... and after one day's reading bought several more and distributed them to my staff. Congratulations on a wonderful work! It is a pleasure to read..."
— Geoffrey G. Parkhurst, EVP Engineering, ThinkEngine Networks
Thank you for posting your state machine articles from the C/C++ Users
Journal on the web, I have learned a great deal and I appreciate the
public posting. Of course, I now have your book on order..."
— Christopher Carlson
Thanks for writing the "Practical Statecharts in C/C++" book and your work on the Quantum Framework! I just returned from the Embedded Systems Conference in Boston. I wish you would have been there teaching your course!..."
— Brian Hutchinson, Senior Engineer - Software, Tyco M/A-COM Wireless Systems
Your book [Practical Statecharts in C/C++] sold the best of all the books at the Embedded Systems Conference CMP Books booth!... Cheers,"
— Michael Barr
I just read your book on state machines. It's great; very thorough,
well reasoned and useful. I wish I'd thought of your 'optimal flat
statemachine' two years ago instead of reaching for the GOF State
pattern. Oh well :) ..."
— Chris Newcombe, Valve Software
First, I'm sorry because my English language isn't perfect. I'm working in development domain and I read your article concerning "Portable Inheritance and Polymophism in C" on web site embedded.com. I found it really interesting. I would wish to translate this text in my native language (French)..."
— François-Xavier Michaud
I am Dr. Jesùs Cerquides from University of Barcelona. I write you to
congratulate you for your nice work. Your book is a really good
contribution. It is not common to find books that think across
"analysis-design-implementation" layers limits. In case you are interested
in translating it to Spanish, I can offer myself as a translator."
— Jesùs Cerquides, University of Barcelona, Spain
I would like to thank you for the excellent book of yours about quantum programming. It has definitely been one of the most interesting programming books that I have read recently...
— Erno Muuranto, R&D Engineer, Datex-Ohmeda Division, Instrumentarium Corp. Helsinki, Finland
Your excellent book Practical Statecharts in C/C++ was a big eye opener for me. I implemented a number of state machines before by typically applying the State design pattern, but always felt its shortcomings when more complex state machines needed to be implemented. Your QHsm clearly provides the solution. Since most of our current development is for the .Net platform, I ported your QHsm to C#..."
—Rainer Hessmer, Ph.D., Sr. Technical Manager and Architect, DataWorks Systems .
![]() |
About the book... |
This is the first book to provide what has been missing so far —
efficient, portable, and maintainable implementation of
statecharts that takes full advantage of state nesting (Preface,
Chapter 4). The key property is here the high maintainability of
the state machine code. The implementation technique allows easy
adding/modifying/removing states and transitions at any stage
of development (Chapter 4, Section 4.2.4, Exercises 4.5, 4.6).
This implementation breaks with the traditional techniques published
so far (described in Chapter 3), which require manual coding of
transition chains. Hardcoded transitions (chains of exit and entry
actions) are a maintenance nightmare and practically defeat the
benefit of using statecharts in the first place.
The high maintainability of the code does not mean poor performance.
In fact, the technique presented in Chapter 4 rivals best-in-class
code synthesizers. In particular, it is more efficient than the
traditional "Doubly-nested switch Statement" technique (because
it replaces one level of switch with much more efficient call
via a function-pointer). The exercises in Chapters 3 and 4 show
how to improve performance even further for critical state handler
methods.
This is the first book to describe a minimal implementation
of active object-based computing model in form of an application
framework (quantum Framework) geared towards embedded real-time
systems (Chapters 7, 8, 9 and 10).
The key point here is that traditional concurrent programming (with semaphores, mutexes, critical sections etc.) is incomparably more difficult and riskier than sequential programming (Chapter 7). Active object-based computing takes full advantage of concurrency while allowing to program internal structure of active objects purely sequentially.
The most important characteristics of the quantum Framework is the support for rapid prototyping. The framework is designed from ground up to allow building and executing intentionally incomplete models (Chapter 11).
This is the first book to notice two fundamental analogies:
(1) the quantum analogy between reactive software systems and
quantum systems, and (2) the object-oriented analogy between OO
classes taxonomies and state hierarchies (Chapters 1, 2, and 7).
Only recently, software community started to appreciate the role of
analogies and metaphors in programming (e.g., inventing a metaphor
is a key practice in eXtreme Programming).
This is the first book to present a small catalog of state patterns in
form of executable code examples (Chapter 5). The pioneering work of
Bruce Douglass in the area of state patterns (e.g., in Doing Hard
Time) captures the
patterns only at the level of diagrams, which is not enough to fully
assess, understand and apply the patterns. The concrete coding
techniques of qp enable presenting executable code alongside
the diagrams. Also, the patterns presented in Chapter 5 rely
on state nesting (behavioral inheritance) rather than on
orthogonal regions (as Douglass' patterns do).
Top of page![]() |
Key Points... |
Profound concepts in software such as hierarchical state
machines (statecharts) or the active object-based computing model are the
way of design rather than the use of a particular tool
or a language. The issue here is not the tools —
the issue is the understanding of profound concepts.
This is a recurring theme throughout the book. Starting from Chapter 1, the book points out the fundamental character of statecharts and draws the analogy with the fundamental OOP concepts. In the following chapters the book presents the statechart concept in detail and describes its implementations (the most efficient implementation, not by accident, is based on the same pointer-to-function construct as virtual functions in C++). Finally, Appendix A describes OOP techniques in C, which confirms that abstraction, inheritance, and polymorphism are just design patterns and do not require an OO language. (Just as statecharts do not require a CASE tool).
Understanding the implementation of the profound concepts
is crucial to truly understand and master the concepts and
for the efficient and confident use of the concepts.
Again, this theme recurs throughout the book. Specifically Chapters 4, 8, and Appendix A, discuss in detail (and illustrate with executable examples) the implementation of statecharts, active object-based computing model, and OOP, respectively.
All profound concepts in software are fundamentally simple
(In fact, the simplicity is the measure of their fundamental
character.) The most astonishing observation of the book is that
the implementation of hierarchical state machine and active object-
based computing model (the Quantum Framework) can be so simple.
Complete qF code takes typically less than 5KB of code and data
(Chapter 8).
Top of page![]() |
Selected Reviews |
Reviewer |
Review |
|
Chris Hills |
Practical Statecharts in C/C++ by Miro Samek [Recommended] This is not so much a book as a way of life. The book is part of the Quantum Programming way of life. It is based on UML state charts and active objects for multithreading. The best way to discover if this is for you is to spend a few minutes browsing http://www.state-machine.com where you will discover lots of information on Quantum Programming, resources and cook books, other books and magazine articles on QP, the QP community and discussion forum. They really have gone to town on this. The CD with the book is no exception. It is a joy to behold. One day all CDs with books will be of this standard! This is for both content and execution. It auto starts with a virtual web site logically arranged with links to all the software and resources with notes, links to other resources etc. So this is not 'just a book' on someone's wild ideas. The other important point is that the web site is (almost) complete. Sometimes many authors promise a web site that never materialises, this one was there before the book. By the time you have looked at the web site it makes my review of the book superfluous, as it contains much of the subject on the website. The good news is that whilst this system will work with most (they list eight) UML CASE tools it is designed to be a lightweight system that does not need a CASE tool. There is even a set of MS Visio Templates and a coding standard both downloadable so you could 'start with it on Monday' as they say in the book, without a major investment in tools. For those interested in QP this book will be ideal as a reference. I know many have broadband Internet and could download the source and the information on the website but a book is still better than printouts (even when bound up) and does contain a lot of additional information. The book itself is in two parts. The first covers UML state charts, real-time, etc. The second part introduces the Quantum methods and ideas. This is an event-message type system. The examples in the book are designed to work on x86 (under Win9x-2K, DOS or RTOS-32) with VC++ and Borland compilers (all code is on the CD). The evaluation version of RTOS-32 is provided though it is claimed that the QF source has been 'carefully designed to work with nearly any RTOS'. By coincidence, whilst writing this review there was a brief flurry of messages on comp.arch.embedded where someone asked about QP. There were only a few replies but everyone liked the method and had used it on at least one real world project. The method works for both C and C++ though personally I have always had reservations about using UML for C. As for the book itself; I like it. It is logical, clearly written and the exercises use the code on the CD and work to a practical example. They are not designed as exercises for students where only the lecturer has the answers. If you are using or looking at UML then look at the QP web site first. If you think QP is for you then definitely buy the book. This book is recommended to all interested in QP. Note from the author: The overlap between the book and the quantum-leaps.com website is not nearly as significant as this review would suggest. The mission of quantum-leaps.com is not to repeat the material already published in "Practical Statecharts in C/C++" ("PSiCC"). Rather, the website complements the book by offering different perspectives, related subjects, and recent developments in both the code and understanding of the concepts. |
|
Jonathan Kaye |
Since I am not from the embedded system world, I was a bit apprehensive about approaching this book. While I can see that author Miro Samek has a directed target for his audience, I strongly feel that this book is a "must read" for technical developers in all areas who want to improve their program design abilities or developers who want to understand the philosophy, use, and implementation of statecharts intimately. As the title indicates, this book brings the topic of statecharts from the realm of expensive design tools to the PRACTICAL realm, illustrating its points with full examples and extensive commentary. Essentially Samek postulates that the slow adoption by developers of best practices by statechart design is due to lack of understanding of the fundamental nature of statecharts and how it is perceived as requiring expensive tools to use well. Samek insightfully discusses how statecharts as a best practice embody "behavioral inheritance" as a fundamental design concept that stands as a peer alongside the conventional pillars of object-oriented programming, namely inheritance, encapsulation, and polymorphism. The book is very technical and written in an academic style, with ample references to original sources as well as detailed code reviews and many reader exercises. I would caution anyone from approaching this book as a quick or light read. For me, it took a seriousness and good understanding of C and C++ to follow Samek's examples and achieve the "a-ha", which was always worth it in the end. The two basic parts of the text are (1) an explanation of statecharts and their methodological implications, and (2) a description of how to apply statecharts as a data structure in real applications, namely embedded as control strategies for "active objects." In several places in the text, Samek makes an analogy between statechart (and active object) semantics and quantum mechanics. This parallel was an interesting philosophical argument, but didn't add much for me in terms of accepting his "quantum framework" as a best practice -- I was sold by his methodological arguments he had presented already. Speaking from experience in writing a book about using statecharts to build simulations (www.FlashSim.com), I can say Samek is a visionary who extended my perception of statecharts several steps. I know I will be quoting from it and referring to it in my work to come. This book has earned a prominent place on my bookshelf, and I would heartily recommend it to any other developer who wants to create correct, verifiable, scaleable, and solid designs (which should be ALL developers!).
|
|
Ed Nisley
|
By Ed Nisley Dr. Dobb's Journal December 2002 ... ...Miro Samek accomplishes three mighty feats in Practical Statecharts in C/C++: He explains how hierarchical state machines work, converts those machines into C/C++, then provides a framework to run those machines in real-time environments or your favorite GUI. Along the way, he demolishes the opposition (how you used to do it), shows you the right way (with code to prove the point), and doesn't bore you to tears (even when explicating quantum mechanics). Quite a feat by any stretch of the imagination. ...In under 200 pages, the first half of Practical Statecharts in C/C++ covers state machines, statecharts, the usual implementations, behavioral inheritance, the five key state-machine design patterns, and how to actually pull off inheritance in both C and C++, all with highlighted code samples. Whew! At that point, you have enough motivation to reform your current methods and enough knowledge to get it right. That's worth the price of admission and, frankly, you should read this part of the book just to open eyes to a different way of doing things. Even if you never use the code directly, it'll be a win. The second part of the book describes Samek's Quantum Framework and the techniques used to bolt it onto the operating system of your choice. He gives examples in DOS(!), Windows, and OnTime's RT-Kernel32, showing that, while you don't need an inherently multitasking OS to get the job done, it certainly makes the job a lot less tedious. State machines are a nearly perfect fit for real-time and highly reactive systems because they easily distribute the workload among many distinct states that inherently avoid compute-bound lumps of indivisible code. At the risk of oversimplifying, the Quantum Framework handles the hocus-pocus required to transfer control between the OS and the current state within your design. Even if you're not doing embedded programming (thanks for following along, though), you should read pages 215 through 248 of Chapter 8, where Samek provides a clear and concise discussion of the most common program design pitfalls — handling errors and exceptions, memory management problems, threads and blocking, and event passing. Expect to smack your forehead and go rummaging for an old source file when you realize that you've made a mistake he describes. The reminder of the book covers the implementation of the Quantum Framework in sufficient detail that you'll know not only how it works but why he did it that way. While not exactly a can't-put-it-down thriller, it's certainly well done and worth perusing, which I can't say about a lot of technical books these days Reentry Checklist CMP Books publishes Practical Statecharts in C/C++, which means, as nearly as I can tell, that I've been discussing a book put out by a relative of this very magazine. Other than sending me a (free) review copy, nobody's done any arm-twisting. ... |
|
Michael Barr
|
By Michael Barr Embedded Systems Programming (07/31/02, 06:23:47 PM EDT) Relatively few of the embedded systems books I receive are worthy of permanent space on my bookshelf. That's why I want to tell you about one recent standout. I don't buy many books about embedded systems. Most of the relevant ones find their way to me as review copies. I try to read, or at least skim, those that look promising. Unfortunately, I've found that relatively few are worthy of permanent space on my bookshelf or recommendation to others. That's why I want to tell you about one recent standout. Perhaps you recall the article "State-Oriented Programming," which appeared precisely two years ago in this magazine. It described a simple manual technique for turning hierarchical state machines into compact C or C++ code. Though I was certainly impressed by the authors' approach, I didn't then recognize the brilliance of the underlying ideas. The primary author's recent publication of a book-length treatment of the topic has helped me see the light. The book, by Miro Samek, is titled Practical Statecharts in C/C++ (CMP Books). The title of the book is a major understatement. What stands out in my mind is not the practical nature of the solution it provides to a common recurring problem, but the downright revolutionary ideas behind that solution. The author's so-called "Quantum Programming" may ultimately change the way embedded software is designed. Never before has there been a viable alternative to the traditional main()+ISR vs. RTOS problem. Preemptive multitasking only works well in specific scenarios, while main()+ISR comes with its own set of problems when you try to scale it. A third way is needed. This book offers a solution, based around state machines, that is compact (5KB is all that's typically required), realized in C and C++ (no fee or royalties), of theoretical value in any language, and can support multiple state machines running in parallel if necessary. It's also the first good way I've seen to deal with the inheritance of state behavior in hierarchical state machines. Before quantum programming, there were basically three approaches to state machine implementation: switch statements, tables of function pointers, and object-oriented programming constructs. The issue of substates in hierarchical state machines was complex in all of these approaches. Hierarchical state machines are common, with part of each state's behavior being determined by its parent state and the rest by the substate itself. This is difficult to implement in all of the traditional approaches, as it either requires duplication of code or additional function/method calls. At best, the results tend toward spaghetti code. In a nutshell, quantum programming is a design pattern for direct and efficient implementation of (hierarchical) state machines. It uses the popular and proven UML statecharts as its graphical specification language, and leaves the choice of implementation programming language up to the developer. Hierarchical states are implemented via an externally driven "event processor," the use of which ensures that substates need not duplicate the functionality of their parents (and grandparents). Dr. Samek calls his implementation the "Quantum Framework." I believe that these innovative techniques will quickly become popular, and have already put them to good use. If you read only one book about embedded systems this year, make it Practical Statecharts in C/C++. |
|
Brian Schmidt |
Great book! Here is my review... The laws of Quantum Physics provide an interesting background against which to test the correctness of the proposed Quantum Framework. In his book, Miro accomplishes his goals of providing a technique to implement state machines and providing a platform for a future programming paradigm or language. Miro's book serves in dual roles as a springboard to immediately start implementing better state machines and as an 'almost mathematical' proof of his Quantum Framework. Beyond simply talking about concepts, Miro provides complete source code and code walkthroughs alleviating the difficult task of implementing abstract concepts. As an experienced software engineer who has implemented several state machines I wanted to skip ahead to discover the proposed Quantum Framework and the Hierarchical State Machine design. Yet, I still found the beginning chapters riddled with tidbits of useful information. The added appendices of 'Object-Oriented Programming in C' and a 'Guide to [UML] Notation' provide an added bonus. This book is a must have for embedded software engineers who wish to leverage the power of hierarchical state machines. |
Top of page![]() |
Errata |
Errata for the first printing (June 2002)(193KB) |
Errata for the second and subsequent printings (October 2002)(102KB) |
Last updated: April 03, 2008






Read excerpts in PDF











The following review appeared on
The following review appeared on 
The following excerpts come from the Embedded Space column published in the 
This review appeared as the 
Errata for the first printing (June 2002)