The C Object System (COS) is a small C library which im- plements high-level concepts available in CLOS, OBJECTIVE- C and other object-oriented programming languages:uni-
form object model(class, metaclass and property-metaclass), generic functions, multi-methods, delegation, properties, ex- ceptions, contractsand closures. COS relies on the program-
mable capabilities of the C programming language to extend its syntax and to implement the aforementioned concepts as\ufb01rst-class objects. COS aims at satisfying several general principles likesimplicity, extensibility, reusability, ef\ufb01ciency andportability which are rarely met in a single program- ming language. Its design is tuned to provide ef\ufb01cient and portable implementation ofmessage multi-dispatch andmes-
bility and reusability. With COS features in hand, software should become as \ufb02exible and extensible as with scripting languages and as ef\ufb01cient and portable as expected with C programming. Likewise, COS concepts should signi\ufb01cantly simplifyadaptive andaspect-oriented programming as well asdistributed andservice-oriented computing.
Closure, Contract, Delegation, Design pattern, Exception, Generic function, Introspection, High-order message, Mes- sage forwarding, Meta class, Meta-object protocol, Multi- method, Open class model, Predicate dispatch, Program- ming language design, Properties, Uniform object model.
The C Object System (COS) is a small framework which adds anobject-oriented layer to the C programming lan- guage [1, 2, 3] using itsprogrammable capabilities1 while following the simplicity of OBJECTIVE-C [5, 6] and the extensibility of CLOS [8, 9, 10]. COS aims to ful\ufb01ll sev- eral general principles rarely met in a single programming language:simplicity, extensibility, reusability, ef\ufb01ciency and
COS has been developed in the hope to solve fundamen- tal programming problems encountered in scienti\ufb01c comput- ing and more speci\ufb01cally inapplied metrology [11, 12]. Al- though this domain looks simple at \ufb01rst glance, it involves nonetheless numerous \ufb01elds of computer science; fromlow-
machines, the control of hardware, the acquisition of data, the synchronization of concurrent processes, or the numer- ical analysis and modeling of huge data sets; tohigh-level tasks like the interaction with databases or web servers, the management of remote or distributed resources, the visual- ization of complex data sets or the interpretation of scripts to make the system con\ufb01gurable and controllable by non- programmers [13, 14, 15]. Not to mention that scienti\ufb01c projects commonly have to rely on sparse human resources to develop and maintain for the long term suchcontinually-
bitious but I \ufb01rmly believe that COS provides the required features to simplify the development and the support of such systems as well as a wide variety of software projects.
Given the context, it is essential to reduce the multiplicity of the technologies involved, to simplify the development pro- cess, to enhance the productivity, to guarantee the extensibil- ity and the portability of the code and to adapt the required skills to the available resources. Hence, thequalities of the programming language are essential for the success of such projects and should focus on the following principles:
The training curve for anaverage programmer should be as short as possible what implies in particular a clear and con- cise syntax. Simplicity should become an asset which guar- antees the quality of the code and allows to write complex constructions without being penalized by a complex formal- ism or by the multiplicity of the paradigms. COS can be learned within a few days by C programmers with some knowledge of object-oriented concepts, although exploiting the full power of COS requires some experience.
new features or the improvement of existing features with- out changing signi\ufb01cantly the code or the software archi- tecture. Concepts likepolymorphism, message dispatch and
namely the ability to reuse or quickly adapt existing compo- nents to unforeseen tasks. It is easier to achieve this goal if the language allows to writegeneric code, either by param- eterization, either by abstraction, to ease the componentiza- tion of design patterns [16, 17, 18]. To support the develop- ment ofgeneric components, COS provides multi-methods to handle dynamic and polymorphiccollaboration and dele- gation to handle dynamic and polymorphiccomposition.
be ef\ufb01cient, that is it must be able to translate all kinds of al- gorithms into programs running withpredictable resource usage (mainly CPU and memory) consistent with the pro- cesses carried out. In this respect, programming languages with an abstract machine close to the physical machine \u2014 alow-level language \u2014 offer generally better results. C is admittedly known to achieve good ef\ufb01ciency.
be portable, that is it must be widely available on many ar- chitectures and it must be accessible from almost any other languages (FFI). This point often neglected brings many ad- vantages: it improves the software reliability, it reduces the deployment cost, it enlarges the \ufb01eld of potential users and it helps to \ufb01nd trained programmers. Regarding this point,
COS extends the C programming language with concepts  mostly borrowed from OBJECTIVE-C and CLOS. The choice of designing the language as a C library instead of a compiler allowed to quickly explore various object models, but R.E. Johnson\u2019s paper on the dynamic object model  de\ufb01nitely focused my research towards the \ufb01nal design:
\u201cIf a system is continually changing, or if you want users to be able to extend it, then the Dynamic Object Model ar- chitecture is often useful. [...] Systems based on Dynamic Object Models can be much smaller than alternatives. [...] I am working on replacing a system with several millions lines of code with a system based on a dynamic object model that I predict will require about 20,000 lines of code. [...] This makes these systems easier to change by experts, and (in theory) should make them easier to understand and main- tain. But a Dynamic Object Model is hard to build. [...] A system based on a Dynamic Object Model is an interpreter, and can be slow.\u201d.
This adaptive object model [21, 22] is actually what COS provides, but at the level of the C programming languages without signi\ufb01cant ef\ufb01ciency loss. In particular, COS has been designed to support ef\ufb01ciently two key concepts \u2014
auniform object model where classes, generics and meth- ods are\ufb01rst-class objects. Incidentally, COS strengthens in- herently all the guidelines stated in  to build \u201c\ufb02exible,
syntax and features are directly available at the C source code level through the use of the language keywords de\ufb01ned in the header \ufb01le<cos/Object.h>.
programming languages is the heart of software extensibil- ity because it postpones to runtime the resolution of methods invocation and reduces coupling between callers and callees. Besides, if the polymorphic types are dynamic, the coupling becomes almost inexistent and code size and complexity are signi\ufb01cantly reduced. On one hand, these simpli\ufb01cations usuallyimprove the programmer understanding who makes less conceptual errors, draws simpler designs and increases its productivity. On the other hand, dynamic typing post- pones the detection of unknown messages at runtime, with the risk to see programs ending prematurely. But well tested software reduce this risk to exceptional situations.
ing collaborations between entities, namely objects. As soon as polymorphic objects are involved everywhere to ensure good software extensibility and reusability, one needspoly-
reduce strong coupling that exist in the Visitor pattern (or equivalent) as well as the amount of code needed to achieve the task. COS provides message multi-dispatch with an ef\ufb01- ciency in the range of the C++ virtual member function.
a well known key-concept in software design. It enhances software \ufb02exibility by introducing levels of indirection in objects and behaviors. Most structural and behavioral design patterns described in  introduce such indirections, but at the price of an increased code complexity and coupling and hence a decreased reusability of the components built. The
age the composition of both, objects and behaviors, without introducing coupling. COS provides delegation with the ef- \ufb01ciency of message dispatch,seemingly a unique feature.
ject models which, amongst others, allows to mimic the be- havior of interpreters. COS provides full introspection and limited intercession on polymorphic types and behaviors, that is classes, generics and methods, as well as object at- tributes through the de\ufb01nition of properties. Since all COS components are\ufb01rst-class objects, it is trivial to replace cre- ational patterns  by generic functions (section 8.1).
veloping libraries and large-scale software. COS enforces encapsulation of class implementation because encapsula- tion is not only a matter of managing coupling but also a design issue. Besides, the object behaviors are represented by generics which favors theseparation of concerns of inter- faces and reduces cross-interfaces dependencies . More- over, the open class model of COS allows to extend classes
a clear policy of ownership and scope rules. In languages like C and C++ where semanticby value prevails, the burden is put on the programmer\u2019s shoulders. In languages like JAVA, C# and D where semanticby reference prevails, the burden is put on the garbage collector. In this domain, COS lets the developer choose between garbage collection (e.g. Boehm GC ) andmanual reference counting with rich semantic (section 3.5).
The similarities between COS components and their equiv- alent C-forms let C programmers with some notions of object-oriented design be productive rapidly. The open ob- ject model allows to de\ufb01ne components in different places and therefore requires an extra linking iteration to collect theirexternal symbols: link\u2192 collect3\u2192 re-link. This fast iteration is automatically performed by the make\ufb01les com- ing with COS before the \ufb01nal compilation stage that builds the executable or the dynamic library.
COS introduces new keywords to extend the C language with a user-friendly syntax half-way between OBJECTIVE-C and CLOS. COSparses its syntax and generates code with the
a few hundred C macros which was developed for this pur- pose. It offers limited parsing capabilities, token recognition, token manipulation and algorithms likeeval, map, \ufb01lter, fold,
on tuples of tokens. As a rule of thumb, all COS symbols and macros are mangled to avoid unexpected collisions with other libraries, including keywords which can be disabled.
This action might not be possible to undo. Are you sure you want to continue?