Simplifying an Extensible Class Library Interface with OpenC++ Joël Cavarroc ESSI -- BP 145 06903 Sophia Antipolis France +33 4 92 96 50 50 cavarroc@essi.fr Sabine Moisan INRIA Sophia Antipolis BP 93 06902 Sophia Antipolis France +33 4 92 38 78 47 Sabine.Moisan@sophia.inria.fr Jean-Paul Rigault ESSI and CNRS/I3S BP 145 06903 Sophia Antipolis France +33 4 92 96 51 25 jpr@essi.fr ABSTRACT In this paper we focus on the benefits that a metaobject protocol may bring to a C++ class library. This library, named FRAMELIB, implements “frames” as used in Artifi- cial Intelligence. It addresses four important aspects: frame structure and behavior, type safety, frame instance persis- tence, and sophisticated debugging mechanisms. Although frames may appear rather similar to classes as found in object-oriented languages, their implementation in C++ raises a number of technical challenges. The interface to our library introduces protocols and patterns which must be strictly followed to ensure the correctness and consis- tency of the code. To simplify the usage of the library we decided to recourse to a metaobject protocol (MOP), and more specifically to OpenC++. This paper describes our experience with this MOP, the promising results that we obtained, the problems we encountered, and the design issues that this technique raises. Keywords C++, OpenC++, metaobject protocol, library interface. INTRODUCTION It is now recognized that using complex object-oriented class libraries is not as straightforward as one could expect. One reason is that a class seldom is a (re)usability unit by itself. Instead, combinations and cooperation of classes must be considered and used altogether, as it has been dem- onstrated with the advent of the now popular “design pat- terns” [7]. Another reason is that most class libraries are used through extension, that is by adding new classes, de- rived from or composed of the classes provided by the li- brary. The derivation and the composition require commit- ting to precise protocols to ensure the consistency of the new classes. The library user must add members and meth- ods simply for the sake of making the whole class scaf- folding operational. The last remark is particularly true in a language like C++ which proposes a rich set of mechanisms to create and ma- nipulate objects and classes. For instance, each new class derived from a library class has to comply or, at least to cope, with the corresponding mechanisms implemented in the base class. This may force the user of the library, who is the designer of the new class, to introduce specific con- structors, destructor, operators, or other member-functions just to satisfy the library protocol. This is an example of the “loss of locality” that is sometimes experienced in class inheritance hierarchy. Of course, one may leave the toil of respecting the protocol to the user of the library. However, this is not practicable if the user is a C++ novice, if the non-respect of the protocol may produce erratic behavior, if the protocol is overly complex, or if it is simply composed of too many elements. A better approach is to provide a set of tools for automati- cally enforcing the protocol. We faced the problem of complex library usage protocol when we developed FRAMELIB, a C++ library of Artificial Intelligence “frames”. FRAMELIB targets two kinds of users. First, human experts 1 define new frame types; they are sel- dom C++ programmers and cannot use the library as-is. Second, a set of software tools collaborates with FRAMELIB to develop knowledge-based systems and, to follow good Software Engineering practice, they should be as independ- ent as possible from the library. For these two reasons we decided to investigate how a metaobject protocol could simplify the library interface for both experts and tools. Moreover, a metaprotocol approach helps hiding the tech- nical programming details from the experts, who can stay at 1 Expert is taken here in its Artificial Intelligence sense: the person who provides the knowledge about an application domain.