SystemCでは、ユーザ定義型(クラス)をチャネルのテンプレート引数として使用できる。
ユーザ定義型には、①比較(==)、②標準出力(<<)、③sc_traceの3つの関数が必要とされる。
特に、①はコンパイルエラー回避のため必須である。
②、③は無くてもよいが、デバッグ容易化のために追加が望ましい。
以下では、位置を示す2変数を保持するユーザ定義型pointを例に、①、②を実装した。
まずは、ユーザ定義型のヘッダファイル。
oprerator<<はメンバ関数ではなく、friend関数とする。
#pragma once #include <iostream> class point { private: int x; int y; public: point(int x, int y); ~point(void); bool operator==(const point& obj) const; friend std::ostream& operator<<(std::ostream& str, const point& obj); };
次に、ユーザ定義型の実装ファイル。
#include "userdef.hpp" point::point(int x, int y) : x(x), y(y) { }; point::~point(void) { }; bool point::operator==(const point& obj) const { return ((this->x == obj.x) && (this->y == obj.y)); }; std::ostream& operator<<(std::ostream& str, const point& obj) { str << obj.x << "," << obj.y; return str; };
最後に、メイン関数からの呼び出し。
#include <iostream> #include "userdef.hpp" int main(void) { point p1( 10, 20 ); point p2( 11, 20 ); std::cout << p1 << std::endl; std::cout << p2 << std::endl; std::cout << (p1 == p2) << std::endl; return 0; };
実行結果は以下の通り。
10,20 11,20 0
次回は、久しぶりにSystemC環境を構築してみる。