private領域を使用する場合のプログラミング例を以下に示します。
IDLでattributeを定義すると、インプリメンテーションクラス用のprivateデータ域にマッピングされます。この領域はサーバアプリケーションが管理します。attributeの詳細は、“4.5.10 属性宣言(attribute)のマッピング”を参照してください。
(1) サーバアプリケーションの処理
サーバアプリケーションのprivate領域の設定/参照メソッドの実装例を以下に示します。
// attribute x の設定(固定長データ) void ODsample_intf1_impl::x( CORBA::Long l, CORBA::Environment &env ) throw( CORBA::Exception ) { __x = l; } // attribute x の参照(固定長データ) CORBA::Long ODsample_intf1_impl::x( CORBA::Environment &env ) throw( CORBA::Exception ) { return( __x ); } // attribute y の設定(可変長データ) void ODsample_intf1_impl::y( const CORBA::Char* s, CORBA::Environment &env ) throw( CORBA::Exception ) { __y = (const CORBA::Char *)s; } // attribute y の参照(可変長データ) CORBA::Char * ODsample_intf1_impl::y( CORBA::Environment &env ) throw( CORBA::Exception ) { CORBA::Char *tmp; tmp = CORBA::string_alloc(strlen(__y)); strcpy( tmp, __y ); return( tmp ); }
また、IDLコンパイラの生成するヘッダファイルに直接privateデータを設定することもできます。そのとき、文字列等の可変長データを使用する場合は、獲得した領域を解放する必要があります(“11.1.5 終了処理のプログラミング”を参照してください)。
class ODsample_intf1_impl : public _sk_ODsample_intf1 { public: ODsample_intf1_impl(); ~ODsample_intf1_impl(); // Attribute Method ( for _get ) CORBA::Long x( ....... private : CORBA::Long __x; CORBA::String_var __y; // ユーザ追加領域 long user_data; char *data; };
private領域を使用した場合は、アプリケーション実行時にクライアントごとにインスタンスを持たせるため、OD_impl_instコマンド実行時(インプリメンテーションリポジトリ登録)に“iswitch=ON”を指定する必要があります(詳細は“リファレンスマニュアル(コマンド編)”の“OD_impl_inst”を参照してください)。
(2) クライアントアプリケーションの処理
クライアントアプリケーションでのattributeの使用例を以下に示します。
// attribute x の設定(固定長データ) target->x( 3, env ); // attribute x の参照(固定長データ) CORBA::Long result = target->x( env ); // attribute y の設定(可変長データ) CORBA::Char *dummy = CORBA::string_alloc(7); strcpy( dummy, "testabc" ); target->y( dummy, env ); // attribute y の参照(可変長データ) CORBA::Char *data = target->y( env ); cout << "data = " << data << endl;