Archive for February, 2011

Controlling name visibility in C++ using-directive

Monday, February 21st, 2011

Have you ever wanted to control which names become visible when your namespace is the subject of a using-directive (e.g., using namespace std;)? Maybe you have some less-commonly used names in your namespace that you don’t want to bring into the user’s namespace in order to avoid possible conflicts with other libraries?

The other day I ran into a similar problem in ODB, the C++ ORM system I am working on. We are implementing the so-called profile libraries which provide ODB persistence support for containers, smart pointers, and value types that are found in various third-party libraries and frameworks, such as C++ TR1, Boost, and Qt. We’ve decide that the most natural way to organize these profile libraries is for each profile to have a separate namespace inside the odb namespace that will correspond to the library. So the C++ TR1 support will be in odb::tr1, Boost will be in odb::boost, etc. The advantage of this schema is that the user code looks nice and logical, for example:

typedef boost::shared_ptr<employer> employer_ptr;
typedef odb::boost::shared_ptr<employee> employee_ptr;

But, as it turns out, there is a big problem with such parallel namespace hierarchies. Consider this innocent-looking code fragment:

using namespace odb;
typedef boost::shared_ptr<employer> employer_ptr;

The reference to the boost namespace in the second line is now ambiguous because the using-directive in the previous line brought in the boost namespace from odb.

Asking users not to use using-directives with the odb namespace is not an option since its use to bring in all the DB-related names, such as database, transaction, etc., is quite handy. In fact, we use it ourselves in all ODB examples and tests.

The next thing we looked at while searching for a solution is changing the namespace hierarchy somehow. For example, we could rename the Boost namespace inside odb (e.g., to odb::boost_profile) to avoid the ambiguity. Or, we could hide the Boost namespace with an intermediate namespace (e.g., odb::profile::boost). This way the using-directive would only bring in the intermediate namespace saving us from the ambiguity. The major problem with these solutions is inelegance; with them the user code will no longer be as succinct.

Then we started thinking about the root of this issue. The problem is not the namespace hierarchy that is somehow bad. The problem is the indiscreet C++ using-directive mechanism. Yes, it is convenient since it allows you to bring in a whole bunch of names that you need in one fell swoop. But it will also bring in a lot of names that you don’t need or never even knew existed. Usually this is harmless but in some cases these extraneous names may collide with the ones that we actually want to use. And that’s exactly what happened in ODB.

After this analysis the ideal solution became obvious: for a given namespace we need a way to control which names are subject to the using-directive. If we had such a C++ mechanism, we could have excluded the profile namespaces from this list and the ambiguity problem would have been solved (if, for some reason, someone wanted to have, say, the odb::boost namespace brought into their current namespace, they would have been able to achieve this with a namespace alias: namespace boost = odb::boost;).

As we all know there is no such mechanism in C++ and this is probably for the best (the language is complex enough as it is). Fortunately, we can get pretty close using what I call, for a lack of a better term, a “using-directive namespace”. In a nutshell, the idea is to create a nested namespace whose sole purpose is to collect a list of names that should be “exported” with a using-directive. This list is assembled with using-declarations (and namespace aliases, if you wish to include nested namespace). In ODB we called this namespace core since it contains core ODB API names that should be sufficient for most applications. Below is an outline of the odb namespace:

namespace odb
  class database {...};
  class transaction {...};
  namespace core
    using odb::database;
    using odb::transaction;
  namespace boost
    // Boost profile.
  namespace tr1
    // TR1 profile.

On the client side, we now use odb::core instead of odb in using-directives. Note also that using-declarations and qualified names can (and should) continue using the original odb namespace; odb::core is purely for using-directives:

using namespace odb::core;
using odb::tr1::lazy_shared_ptr;
typedef odb::boost::shared_ptr<employee> employee_ptr;
void f (database& db);

While some users may find the odb::core syntax somewhat strange, I actually like the extra assurance it implies: you are going to get the core set of names necessary for this particular functionality instead of everything that have accumulated in this namespace, maybe even the kitchen sink. We can also create several using-directive namespaces for different parts of the library. For example, the std namespace could have been partitioned like this into std::containers, std::iostream, etc.

What do you think? Anything obvious (or not so obvious) that I missed?

XSD/e 3.2.0 released

Wednesday, February 16th, 2011

XSD/e 3.2.0 was released yesterday. In case you are not familiar with XSD/e, it is a dependency-free XML Schema to C++ compiler for mobile, embedded, and light-weight C++ applications. It provides XML parsing, serialization, XML Schema validation and XML data binding while maintaining a small footprint and portability.

This version includes a number of major new features (examined in more detail next), small improvements, and bug fixes. It also adds official support, instructions, and sample configuration files for the following platforms and toolchains:

  • Android/Android NDK
  • Symbian/CSL-GCC (GCCE)
  • Integrity 178b/Green Hills MULTI C/C++

It is now also possible to build the XSD/e runtime library for iPhoneOS/iOS with the XCode project.

Enum mapping

Probably the most visible new feature is the mapping of XML Schema enumerations to C++ enum. Consider, for example, the following schema fragment:

<simpleType name="genre">
  <restriction base="string">
    <enumeration value="romance"/>
    <enumeration value="fiction"/>
    <enumeration value="horror"/>
    <enumeration value="history"/>
    <enumeration value="philosophy"/>

The new interface for the genre C++ class will look like this:

class genre
  enum value_type
  genre ();
  genre (value_type);
  void value (value_type);
  operator value_type () const
  const char* string () const;

And we can use this class like this:

genre g (genre::fiction);
if (g != genre::philosophy)
  cout << g.string () << endl;

Memory allocators

You can now configure the XSD/e runtime and generated code to perform memory management using custom memory allocator functions provided by your application instead of the standard operator new/delete. The allocator example provided with the XSD/e distribution uses this feature to completely eliminate dynamic memory allocations. In particular, the resulting object model is placed into a memory block allocated on the stack. Cool, huh?

Schema evolution

Often new features that are added to the application require changes to the corresponding schemas. In mobile and embedded systems this poses a significant challenge since it is often impossible to upgrade the devices that are out in the field. As a result, it is a common requirement that devices running older versions of the software be able to cope with data that is based on the newer versions of the schema. In this release XSD/e adds support for such schema evolution using the substitution groups mechanism. Both the ignore and passthrough models for unknown content are supported.

Configurable character encoding

It is now possible to configure the character encoding used in the application with the currently supported options being UTF-8 and ISO-8859-1. Note that this encoding is not the same as the XML document encoding that is being parsed or serialized. Rather, it is the encoding that is used inside the application. When an XML document is parsed, the character data is automatically converted to the application encoding. Similarly, when an XML document is serialized, the data in the application encoding is automatically converted to the resulting document encoding.

There are also other important features in this release, including the generation of clone functions for variable-length types, improved support for XML Schema facet validation, including xs:pattern, etc. For an exhaustive list of new features see the official XSD/e 3.2.0 release announcement.