Size of enumeration type in C++

In C++ enumeration is used to define set of named constants. Each enumeration is a distinct compound type (enumerated type) and is subject to all rules of type system defined in C++ language. Internally, enumeration is represented with so called underlying type. The C++ Standard ISO/IEC 14882:2003 (Section 7.1/5) specifies it as follows:

The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration.

It is not specified which particular integral type is used to represent an enumerated type. Given that number of bytes representing fundamental integral types is implementation defined in C++ and only sizeof(char), sizeof(signed char) and sizeof(unsigned char) are guaranteed to be always 1, value reported by sizeof operator applied on enumeration is considered as implementation defined as well. Sometimes one may need to control size of enumerated type in memory, for instance to serialise and de-serialise objects easier.

Some compilers allow to specify underlying type of enumeration. For instance, Microsoft Visual C++ provides dedicated language extension:

enum Color : unsigned char
{
   red, green, blue
};
// assert(sizeof(Color) == 1);

Obviously, this technique is not portable across other implementations of C++ language.

In order to solve this problem in portable way, a simple class template can be defined:

template <class E, class T>
struct enumeration
{
    typedef T type;
    typedef E enum_type;

    enumeration()
        : e_(E())
    {}

    enumeration(E e)
        : e_(static_cast<T>(e))
    {}

    operator E() const
    { return static_cast<E>(e_); }

private:
    T e_;
};

The enumeration class can be used as a portable way to wrap any enumerated type and to specify (or limit) amount of bytes used to represent it. It is also interchangeable with actual enumeration being wrapped:

#include <cassert>

enum Color { red, green, blue };

int main()
{
    enumeration<Color, unsigned char> color(blue);
    assert(sizeof(color) == 1);

    Color c = color;
    assert(c == blue);
}

It’s also possible to provide default argument for template parameter T specifying underlying type of wrapped enumeration. For instance, if most constant values in a project range from 0 to 255, default type value of T can be unsigned char.

Selling iMac 24-inch

My fiancée Pantera as has brand new Apple iMac for sell. The machine was released in 2008 and bought in July 2008. Pantera has used it for 3 days only and put it back to the cartoon box (boxes from Apple are really cool ;-)). It is standard specification: Intel Core 2 Duo (2.8 GHz); 2 GB DDR2 SDRAM; HDD 320 GB. We have left the machine in our apartment in Poland, but we can ship it internationally.

How much? It’s the matter for negotiation but the prices is surely much lower than average current price.

Here is complete review of Apple iMac (24-inch, 2.8GHz)

QGIS tracks on BoostCon’09

In one of presentations from this year’s edition of BoostCon, I found a small track of Quantum GIS project. This is Bill Hoffman‘s presentation titled A CMake-Based Software Process for Development and Integration Testing (PDF) and the track is citation taken from Martin Dobias’ blog post Having fun with CMake:

I was quite surprised with the speed of building Quantum GIS codebase in comparison to Autotools.

All presentations from the BoostCon’09 are available to download including Barend‘s slides and article with about Generic Geometry Library. There are much more interesting materials from the conference available and as another one somehow related to geospatial works, I’d like to point Graphics Programming Workshop about programming with Adobe’s Graphics Interface Library which is a part of Boost C++ Libraries (>= 1.35.0). There are both, slides and source codes, available to download.

Hi COM! How’s it going?

In June 2000 at PDC in Orlando Microsoft announced .NET Framework project. Shortly after that developers concentrated around products and technologies made by Microsoft started debate on what’s the future of software development. One of questions and quite obvious was: So does this mean that COM is dead?. A few months later, Don Box addressed that concern in his extensive article Is COM Dead?:

It depends on what your definition of COM is. COM is many things to many people. To me, COM is a programming model based on integrating components based on type. Period. This was COM’s primary contribution to the field of component software, and that contribution has changed the way millions of programmers build systems today.

Don is right and the answer is historically and technically correct, however this is an indirect answer that didn’t cheer up at all. Given the closing words in that article:

(…) CLR provides significant benefits to developers who are using COM today. Virtually all aspects of the COM programming model have survived (…) I look at the CLR as breathing new life into the programming model that I’ve spent the last seven years of my life working with, and I know there are other programmers out there who share this sentiment.

overall conclusion becomes more an abstruse philosophy than binary answer. What about all these cryptic terms of Component Object Model technology made by Microsoft, like OLE, ActiveX, DCOM and other? Silent.

Nearly ten years later, developers are still asking the same question (a few days ago, similar discussion took place on ACCU mailing list) about COM. Alf. P. Steinbach gave an interesting answer that, however, sounds very similar to the one given by Don Box:

COM is one of the few successful C++ component technologies, if not the only one (depending on one’s definition of “successful (…) Some reduced and slightly modified versions of COM are used in e.g. Linux user interface and in Firefox browser (XCOM). Original COM itself is however a Windows-specific technology. But while it’s necessarily used to interface to the operating system and at higher levels in e.g. scripting, it’s my impression that it’s now now not much used as a general C++ component technology, i.e., that use of COM is something forced, not something desired and freely chosen.

Even though both are similar, it’s been decade since the question was asked for the first time and now we have 10 years of experience based on observations what has changed after .NET was thrown on the marked. The managed baby seems to be winning the market of Microsoft-oriented (not only, though) developers and users. COM is widely used as components of Windows systems and other already existing software but are new COM servers and objects still being developed? Or, has it started to suffer of COM programmer species extinction?

The www.microsoft.com/COM/ website confirms that:

Microsoft recommends that developers use the .NET Framework rather than COM for new development.

In my humble opinion, Mr. COM is like an old veteran, alive, but most of his comrades-in-arms have died, so he usually goes to pub alone.

Got new toy

I’ve just got new toy tool. Since yesterday, I’m a happy owner of the most standards-conformant C++ compiler that ever existed – Comeau C/C++ from Comeau Computing.

I’ve successfully installed Comeau C/C++ 4.3.10.1 Beta compiler and libcomo36, accompanying Standard C++ Library, on 32-bit version of latest Ubuntu 9.04.

I’m going to use Comeau C/C++ for purposes like analysis and review of C++ source code I write or use, verification of code portability as well as for learning elements of C++ and experimenting. This is excellent compiler for learning C++ language. Among loads of features, one of the coolest is ability to give clear and informative error messages. This is very helpful while working with complex class and function templates.

Continue reading

PostGIS In Action

It really must be very hot and fresh news, so the virtual devil spirit of social networking hasn’t fished it out yet and also Google (check this if you haven’t heard yet about this local family company) lists less than 15 pages.

PostGIS provides over 300 spatial operators, spatial functions, spatial data types and spatial indexing enhancements. If you add to the mix the complimentary features that PostgreSQL and other PostgreSQL related projects provide, then you’ve got one jam-packed powerhouse at your disposal well suited for hardcore work as well as a valuable training tool for spatial concepts.

Three words: PostGIS In Action. The first book about PostGIS spatial database being written by Regina O. Obe and Leo S. Hsu with release planned for the beginning of 2010. First chapter of the book has been published and is freely available as PDF file through the Manning Early Access Program. Chapter two and three are also available for MAEP subscribers.

Next to the early access, another cool thing about the way Manning Publications release their books is possibility to comment chapters and discuss with authors directly through Manning Sandbox forums. There is no exception for the PostGIS in Action :-)

Update 2009-05-08T23:08:21+00:00: The book official announcement has been posted on postgis-devel and postgresqlonline.com.

I’m looking forward to grab the book!

Pierre, check the TOC for chapter thirteen. Cool, isn’t it?