03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Convenience functions are trivial wrappers for things that could otherwise be done with a<br />

smaller API. If a class contains two functions Foo() and Bar(), which do all that's required<br />

by the class, but you often find that code using your API contains sequences such as<br />

x.Foo();<br />

x.Bar();<br />

or this<br />

x.Foo(x.Bar());<br />

y = (x.Foo() + x.Bar()) / 2;<br />

then you may wish to code some kind of convenience function FooAnd- Bar() that<br />

represents the sequence. This will reduce code size, reduce mistakes, and make code<br />

easier to read.<br />

The cost of convenience is another function to design and document, and the risk of being<br />

tempted to produce many convenience functions that aren't really all that necessary or even<br />

convenient – and then being forced to maintain them for ever more, because people depend<br />

on them. This is a fine judgment call: sometimes we provide too few convenience functions<br />

and sometimes too many.<br />

3.4.2 DLLs and Other API Elements<br />

An object-oriented system delivers APIs mainly as C++ classes, together with all their<br />

member functions and data. Classes that form part of an API are declared in header files,<br />

implemented in C++ source files, and delivered in DLLs.<br />

Library APIs (or the library parts of a framework API) are delivered in shared library DLLs<br />

with a .dll file extension. The DLL's exported functions are made available in a .lib file to<br />

the linker at program build time.<br />

Framework APIs are usually defined in terms of C++ classes containing virtual functions and<br />

an interface specification for a polymorphic DLL with an extension other than .dll (for<br />

instance, .app for a GUI-based application).<br />

It's important to make sure that only the interface – not the implementation – is made<br />

available to programs that use the API. Classes that are not part of the API should not be<br />

declared in API header files, and their functions should not be exported from the DLLs that<br />

implement them. Functions and data that belong to the API classes, but are not part of the<br />

API, should be marked private.<br />

Besides classes, C++ APIs may contain enumerations, constants, template functions, and<br />

even nonmember functions.<br />

3.4.3 Exported Functions<br />

For a nonvirtual, noninline member function to be part of an API, it must be<br />

� declared public in a C++ class that appears in a public header file<br />

� exported from its DLL.<br />

You will see exported functions marked in their header files with IMPORT_C, like this<br />

class RTimer : public RHandleBase<br />

{

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!