Controlling name visibility in C++ using-directive

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

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"/>
  </restriction>
</simpleType>

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

class genre
{
public:
  enum value_type
  {
    romance,
    fiction,
    horror,
    history,
    philosophy
  };
 
  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.

ODB 1.1.0 released

January 26th, 2011

ODB 1.1.0 was released today. In case you are not familiar with ODB, it is an object-relational mapping (ORM) system for C++. It allows you to persist C++ objects to a relational database without having to deal with tables, columns, or SQL, or manually writing any of the mapping code.

This version took a bit longer than we initially planned, mainly because many of the new features depended on each other and, once we implemented one, then it also made sense to implement the others. For example, bidirectional object relationships (see below) are pretty much useless in any real-world scenario without support for lazy loading.

But, in the end, the result is a version that is packed with major new features, which I am going to examine next.

Containers

It is now possible to store containers in the database. Built-in support is provided for most standard C++ containers, namely, vector, list, map, multimap, set, and multiset. For example, now you can write:

#pragma db object
class person
{
  ...
 
  std::set<std::string> emails_;
};

And that’s it. That’s all you need to do. It is also very easy to add support for custom container types. All you need to do is provide a traits specialization for your container (see the ODB manual for details). Plus we are working on profile libraries that will provide support for containers found in popular framework and libraries, such as Boost and Qt.

Relationships

ODB now supports unidirectional (to-one, to-many) and bidirectional (one-to-one, one-to-many, many-to-many) object relationships. For example:

#pragma db object
class employer
{
  ...
 
  #pragma db inverse(employer_)
  std::vector<lazy_weak_ptr<employee> > employees_;
};
 
#pragma db object
class employee
{
  ...
 
  shared_ptr<employer> employer_;
};

In the above code fragment we used lazy_weak_ptr, which is a lazy pointer. The object that it points to is only loaded when requested.

In bidirectional relationships it is also possible to declare one side inverse, as shown in the code above. This results in the canonical relational model where only one table contains a reference to the other table.

Similar to containers, ODB provides built-in support (and the corresponding lazy pointers) for standard C++ pointers, namely, the raw pointer, auto_ptr, and TR1 shared_ptr/lazy_ptr. At the same time, profile libraries will provide support for smart pointers found in popular framework and libraries, such as Boost shared_ptr and Qt QSharedPointer. It is also easy to add support for your own smart pointers, as described in the ODB manual.

Composite Values

ODB prior to 1.1.0 only supported simple (single-column) value types. Now you can declare and use composite (multi-column) value types, for example:

#pragma db value
class name
{
  ...
 
  std::string first_;
  std::string last_;
};
 
#pragma db object
class person
{
  ...
 
  name name_;
};

Of course, all these features work well with each other. For example, you can have a container of object pointer or composite value types. Similarly, a composite value type can have members that are other composite value types, object pointers, or containers. Plus, these features are integrated into the query language so that you can use data members from composite values or related objects in query expressions, for example:

typedef odb::query<person> person_query;
typedef odb::query<employee> employee_query;
 
db.query<person> (person_query::name::last == "Doe");
db.query<employee> (employee_query::employer::name == "Example Inc");

There are also other important features in this release, including, optional object cache (session), customizable object pointers, native SQL statement execution, etc. For an exhaustive list of new features see the official ODB 1.1.0 release announcement.