9.2.2 tieテンプレートの考察(C++)

idl2cppコンパイラは,コードサンプル9-1に示すように_tie_Accountテンプレートクラスを自動的に生成します。POA_Bank_Account_tieクラスは,オブジェクトサーバが実体化して,AccountImplのインスタンスで初期化します。POA_Bank_Account_tieクラスは,受信するオペレーション要求すべてを実際のインプリメンテーションであるAccountImplに任せます。このサンプルでは,AccountImplクラスはPOA_Bank::Accountクラスを継承しません。

コードサンプル9-1 POA_Bank_Account_tieテンプレートの考察

. . .
template <class T> class POA_Bank_Account_tie :
     public POA_Bank::Account {
  private:
     CORBA::Boolean _rel;
     PortableServer::POA_ptr _poa;
     T *_ptr;
     POA_Bank_Account_tie(const POA_Bank_Account_tie&) {}
     void operator=(const POA_Bank_Account_tie&) {}

  public:
     POA_Bank_Account_tie (T& t): _ptr(&t), _poa(NULL),
           _rel((CORBA::Boolean)0) {}

     POA_Bank_Account_tie (T& t, PortableServer::POA_ptr poa):
           _ptr(&t),
           _poa(PortableServer::_duplicate(poa)),
           _rel((CORBA::Boolean)0) {}

     POA_Bank_Account_tie (T *p, CORBA::Boolean release = 1) :
           _ptr(p), _poa(NULL),_rel(release) {}

     POA_Bank_Account_tie (T *p, PortableServer::POA_ptr poa,
           CORBA::Boolean release = 1)
           : _ptr(p), _poa(PortableServer::_duplicate(poa)),
           _rel(release) {}

     virtual ~POA_Bank_Account_tie() {
        CORBA::release(_poa);
        if (_rel) {
           delete _ptr;
        }
     }

     T* _tied_object() { return _ptr; }

     void _tied_object(T& t) {
        if (_rel) {
        delete _ptr;
     }
     _ptr = &t;
     _rel = 0;
  }

  void _tied_object(T *p, CORBA::Boolean release=1) {
     if (_rel) {
        delete _ptr;
     }
     _ptr = p;
     _rel = release;
  }

  CORBA::Boolean _is_owner() { return _rel; }

  void _is_owner(CORBA::Boolean b) { _rel = b; }

  CORBA::Float balance() {
     return _ptr->balance();
  }

  PortableServer::POA_ptr _default_POA() {
     if ( !CORBA::is_nil(_poa) ) {
        return _poa;
     } else {
        return PortableServer_ServantBase::_default_POA();
     }
  }
};