TAO Sequence分析

 

TAO Sequence分析

 

目的是为了在使用CORBA参数返回的时候,避免拷贝参数的数据结构,直接使用返回的参数内存,自己控制参数的内存释放,好处是内存的高效使用,不用数据结构之间拷贝。

ProblemCauseList_T

 

简单是继承模板类,提供三个构造函数和一个拷贝构造函数

 

          class DLL_EXPORT ProblemCauseList_T

            : public

                TAO_Unbounded_Sequence<

                    ProblemCause_T

                  >

          {

          public:

            ProblemCauseList_T (void);

            ProblemCauseList_T (CORBA::ULong max);

            ProblemCauseList_T (

                CORBA::ULong max,

                CORBA::ULong length,

                ProblemCause_T* buffer,

                CORBA::Boolean release = 0

              );

            ProblemCauseList_T (const ProblemCauseList_T &);

            ~ProblemCauseList_T (void);

           

            static void _tao_any_destructor (void *);

           

            typedef ProblemCauseList_T_var _var_type;

          };

 

 

 

ProblemCauseList_T   继承了模板类

|--TAO_Unbounded_Sequence

 

 


下面看TAO_Unbounded_Sequence实现

 

 

TAO_Unbounded_Sequence

   |--TAO_Unbounded_Base_Sequence

 

三个构造函数和一个拷贝构造函数,赋值=操作符,数组操作符[]

他提供了数组操作符[]和内存分配方法allocbuf, freebuf,另外提供扩展方法T * get_buffer方法来脱离类对T*的内存控制。

 

       template <typename T>

       class TAO_Unbounded_Sequence : public TAO_Unbounded_Base_Sequence

       {

       public:

         TAO_Unbounded_Sequence (void);

         TAO_Unbounded_Sequence (CORBA::ULong max);

         TAO_Unbounded_Sequence (CORBA::ULong max,

                                                   CORBA::ULong length,

                                                   T * data, CORBA::Boolean release = 0);

         TAO_Unbounded_Sequence (const TAO_Unbounded_Sequence<T> &);

         TAO_Unbounded_Sequence<T> & operator= (const TAO_Unbounded_Sequence<T> &);

         ~TAO_Unbounded_Sequence (void);

 

         T & operator[] (CORBA::ULong);

         const T & operator[] (CORBA::ULong) const;

 

         /// Allocate storage for the sequence.

         static T * allocbuf (CORBA::ULong);

         static void freebuf (T *);

 

         virtual void _allocate_buffer (CORBA::ULong length);

         virtual void _deallocate_buffer (void);

 

         T * get_buffer (CORBA::Boolean orphan = 0);

         const T * get_buffer (void) const;

         void replace (CORBA::ULong max,

                                   CORBA::ULong length,

                                   T * data,

                                   CORBA::Boolean release = 0);

       };

 

内存的分配函数

 

_allocate_buffer 它是调用了本类的:allocbuf分配函数分配内存。

_deallocate_buffer同样也调用了本类的freebuf释放内存函数。

 

       template<typename T>

       void

       TAO_Unbounded_Sequence<T>::_allocate_buffer (CORBA::ULong length)

       {

         T * tmp = TAO_Unbounded_Sequence<T>::allocbuf (length);

         if (this->buffer_ != 0)

              {

                T * old = ACE_reinterpret_cast (T *, this->buffer_);

                for (CORBA::ULong i = 0; i < this->length_; ++i)

                     {

                       tmp[i] = old[i];

                     }

                if (this->release_)

                     {

                       TAO_Unbounded_Sequence<T>::freebuf (old);

                     }

              }

         this->buffer_ = tmp;

       } 

 

 

 

下面看看提供的扩展函数,提供脱离内存控制的方法。

 

get_buffer (CORBA::Boolean orphan)

如果参数orphan==0那么返回你成员的this->buffer_缓冲指针。

如果orphan==1类将失去内存的控制。

 

 

 

template <typename T>

T * TAO_Unbounded_Sequence<T>::get_buffer (CORBA::Boolean orphan)

{

  T * result = 0;

  if (orphan == 0)

    {

      if (this->buffer_ == 0)

        {

          if (this->length_ > 0)

            {

              result = TAO_Unbounded_Sequence<T>::allocbuf (this->length_);

              this->buffer_ = result;

              this->release_ = 1;

            }

        }

      else

        {

          result = ACE_reinterpret_cast (T *,this->buffer_);

        }

    }

  else

    {

      result = ACE_reinterpret_cast (T *,this->buffer_);

      if (this->release_ != 0)

        {

          this->maximum_ = 0;

          this->length_ = 0;

          this->buffer_ = 0;

          this->release_ = 0;

        }

    }

  return result;

}

 

 

 

 

 

 

 

TAO_Unbounded_Base_Sequence             

       |--TAO_Base_Sequence

 

 

class TAO_Export TAO_Unbounded_Base_Sequence : public TAO_Base_Sequence

{

public:

  void length (CORBA::ULong length);

  CORBA::ULong length (void) const;

  virtual ~TAO_Unbounded_Base_Sequence (void);

 

protected:

  TAO_Unbounded_Base_Sequence (void);

  TAO_Unbounded_Base_Sequence (CORBA::ULong maximum,

                               CORBA::ULong length,

                               void *buffer,

                               CORBA::Boolean release = 0);

  TAO_Unbounded_Base_Sequence (CORBA::ULong maximum,

                               void *buffer);

};

 

 

TAO_Base_Sequence

实质是维护了一个内存缓冲区的指针管理void *buffer_

 

class TAO_Export TAO_Base_Sequence

{

public:

  friend class TAO_Marshal_Sequence;

  virtual ~TAO_Base_Sequence (void);

  CORBA::ULong maximum (void) const;

 

  //内存分配的纯虚函数

  virtual void _allocate_buffer (CORBA::ULong length) = 0;

  virtual void _deallocate_buffer (void) = 0;

 

  //内存收缩函数

  virtual void _shrink_buffer (CORBA::ULong new_length,

  virtual void _downcast (void *target,

                          CORBA::Object *src

                          ACE_ENV_ARG_DECL_WITH_DEFAULTS);

  virtual CORBA::Object *_upcast (void *src) const;

  CORBA::Boolean release (void) const;

 

protected:  // 构造函数

  TAO_Base_Sequence (void);

  TAO_Base_Sequence (CORBA::ULong maximum,

                     CORBA::ULong length,

                     void *buffer,

                     CORBA::Boolean release = 0);

  TAO_Base_Sequence (CORBA::ULong maximum,

                     void *buffer);

 

  void check_bounds(

      char const * filename, unsigned long lineno,

      CORBA::ULong tao_idx, CORBA::ULong tao_max) const;

 

public:

  TAO_Base_Sequence (const TAO_Base_Sequence &rhs);

  TAO_Base_Sequence &operator= (const TAO_Base_Sequence &rhs);

 

protected:

  CORBA::ULong maximum_;

  CORBA::ULong length_;

  void *buffer_;

  CORBA::Boolean release_;

};

一个简单的使用范例

 

获取内存

void setpc(const com::cattsoft::idl::almsvc::globaldefs::ProblemCauseList_T& pc){

       ....

       length = pc.length(); eType = ALARM_PC;

       com::cattsoft::idl::almsvc::globaldefs::ProblemCause_T*

ppc= ((com::cattsoft::idl::almsvc::globaldefs::ProblemCauseList_T*)&pc)->get_buffer(TRUE);

}

 

 

释放内存

com::cattsoft::idl::almsvc::globaldefs::ProblemCauseList_T::freebuf(ppc);                       

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值