これまでの投稿で示した例題では無名パッケージのクラスだったが、命名規則 (naming convention)で示しているように softies projectではパッケージの概念を導入している。
たとえば、単体テストフレームワーク CUnitに Assertクラスがあるが、もしテスト対象で既に別の Assertという名前を使用していると、シンボルの衝突が発生してしまう。
そのため、CUnitは衝突が起こりにくいよう、lwd_unitパッケージに Assertクラスを置いている。
完全修飾名 (fully qualified name)
インポートを使わない場合の Assertクラスの使用例をつぎに示す。(example of using Assert class without import.)/* 25.Aug.2012 kei */
#include <lwd/unit/Assert.h>
int main() {
int x = 0;
lwd_unit_Assert_equalsInt("x equals 0.", 0, x);
return 0;
}
この様に、長い名前で equalsIntを呼び出さなければならない。シンボルの衝突は置きにくいが、冗長である。
インポート (import)
インポートを使用した場合の Assertクラスの使用例をつぎに示す。(example of using Assert class with import.)/* 25.Aug.2012 kei */
#define import_lwd_unit_Assert
#include <lwd/unit/Assert.h>
int main() {
int x = 0;
Assert_equalsInt("x equals 0.", 0, x);
return 0;
}
import_lwd_unit_Assertを定義するだけで、パッケージ名を省き、クラス名と関数名だけで関数呼び出しができるようになる。仕掛け (mechanism)
Assertクラスのヘッダには次のような記述が含まれており、import_lwd_unit_Assertを定義することにより、Assertクラスの関数名などの長い名前の省略形がマクロ定義されるようになっている。#ifdef import_lwd_unit_Assert
#define Assert_equalsInt lwd_unit_Assert_equalsInt
#define Assert_equalsLongLong lwd_unit_Assert_equalsLongLong
#define Assert_equalsDouble lwd_unit_Assert_equalsDouble
#define Assert_equalsString lwd_unit_Assert_equalsString
#define Assert_equals lwd_unit_Assert_equals
#define Assert_null lwd_unit_Assert_null
#define Assert_notNull lwd_unit_Assert_notNull
#define Assert_false lwd_unit_Assert_false
#define Assert_true lwd_unit_Assert_true
#define Assert_fail lwd_unit_Assert_fail
#endif /* import_lwd_unit_Assert */
仕掛けは単純だが、これがあるかどうかで使いやすさが違ってくる。通常はインポートを使用し、名前が衝突する場合にのみ FQNで記述すればよい。
なお、クラスのメンバが追加されたり名称に変更があった場合など、忘れずに省略形の修正も行わなければならない。
原発を灰色にするのは…



