[CORBA] Getter and setter for CORBA sequence of objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yakyak
    New Member
    • Aug 2012
    • 3

    [CORBA] Getter and setter for CORBA sequence of objects

    Hi all!

    I have a class:
    Code:
    class UserImpl : public POA_User
    {
        private :
            typedef vector<GroupImpl*> Groups;
            Groups groups;
    
        public :
    
            User::Groups* getGroups();
            void setGroups(const ::User::Groups& g);
    };
    
    #endif
    
    #include "UserImpl.h"
    
    User::Groups* UserImpl::getGroups()
    {
        const size_t size = this->groups.size();
        User::Groups_var seqOfObjects = new User::Groups(size);
        seqOfObjects->length(size);
    
        size_t i = 0;
        vector<GroupImpl*>::const_iterator it = groups.begin();
        while (it != groups.end())
        {
            seqOfObjects[i] = Group::_duplicate((*it)->_this());
            ++it;
            ++i;
        }
    
       return seqOfObjects._retn();
    }
    
    void UserImpl::setGroups(const ::User::Groups& g)
    {
        const size_t size = g.length();
        this->groups.resize(size);
        this->groups.clear();
    
        for(size_t i = 0; i<size; i++)
        {
            GroupImpl *newGroup = new GroupImpl(g[i]->getID(), g[i]->getName(),
                                                g[i]->getDescription(),g[i]->getFounder(),
                                                *(g[i]->getUsers()));
            this->groups.push_back(Groups::value_type(newGroup));
        }
    }
    And I have some troubles how to write a getter and setter for my class field "groups". I did it, but I have dooubts
    if it's a good way to do this. It compiles with no error but I'm a beginner in CORBA and would like get some help from anybody who is good at it. Thanks:)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Generaly, getter and setter functions are not good programming practice. The idea behind the class is that the class member functions protect the data (which is private).

    Therefore, if the data gets screwed up then it's the member functions that did it. Using a getter/setter means that any code anywhere in your program can mess things up.

    You may as well skip classes altogether and use a global variable, itself a bad practice.

    Aso, a vector manages its own memory so you don't have to be doing your own memoery alloctions using the new operator. Ny time you allocate memory, some one has to delete it. When you delete a pointer you need to know there are no other copies of it laying around otherwise you will crash your code. Knowing if you are deleting the only copy of the pointer involves another level to control and this usually brings you to using smart pointers.

    I don't know what POA_User does but if you post it, that could shed some light on your design.

    Comment

    • yakyak
      New Member
      • Aug 2012
      • 3

      #3
      It's a small part of a bigger project. I can only say that I have a class which need to set groups. Class POA_User is a class generated by omniorb's IDL compiler, omniidl from User interface written in IDL language. It looks like this:

      Code:
      // This file is generated by omniidl (C++ backend)- omniORB_4_1. Do not edit.
      #ifndef __User_hh__
      #define __User_hh__
      
      #ifndef __CORBA_H_EXTERNAL_GUARD__
      #include <omniORB4/CORBA.h>
      #endif
      
      #ifndef  USE_stub_in_nt_dll
      # define USE_stub_in_nt_dll_NOT_DEFINED_User
      #endif
      #ifndef  USE_core_stub_in_nt_dll
      # define USE_core_stub_in_nt_dll_NOT_DEFINED_User
      #endif
      #ifndef  USE_dyn_stub_in_nt_dll
      # define USE_dyn_stub_in_nt_dll_NOT_DEFINED_User
      #endif
      
      
      
      
      
      
      #ifdef USE_stub_in_nt_dll
      # ifndef USE_core_stub_in_nt_dll
      #  define USE_core_stub_in_nt_dll
      # endif
      # ifndef USE_dyn_stub_in_nt_dll
      #  define USE_dyn_stub_in_nt_dll
      # endif
      #endif
      
      #ifdef _core_attr
      # error "A local CPP macro _core_attr has already been defined."
      #else
      # ifdef  USE_core_stub_in_nt_dll
      #  define _core_attr _OMNIORB_NTDLL_IMPORT
      # else
      #  define _core_attr
      # endif
      #endif
      
      #ifdef _dyn_attr
      # error "A local CPP macro _dyn_attr has already been defined."
      #else
      # ifdef  USE_dyn_stub_in_nt_dll
      #  define _dyn_attr _OMNIORB_NTDLL_IMPORT
      # else
      #  define _dyn_attr
      # endif
      #endif
      
      
      
      
      
      #ifndef __Group__
      #define __Group__
      
      class Group;
      class _objref_Group;
      class _impl_Group;
      
      typedef _objref_Group* Group_ptr;
      typedef Group_ptr GroupRef;
      
      class Group_Helper {
      public:
        typedef Group_ptr _ptr_type;
      
        static _ptr_type _nil();
        static _CORBA_Boolean is_nil(_ptr_type);
        static void release(_ptr_type);
        static void duplicate(_ptr_type);
        static void marshalObjRef(_ptr_type, cdrStream&);
        static _ptr_type unmarshalObjRef(cdrStream&);
      };
      
      typedef _CORBA_ObjRef_Var<_objref_Group, Group_Helper> Group_var;
      typedef _CORBA_ObjRef_OUT_arg<_objref_Group,Group_Helper > Group_out;
      
      #endif
      
      #ifndef __User__
      #define __User__
      
      class User;
      class _objref_User;
      class _impl_User;
      
      typedef _objref_User* User_ptr;
      typedef User_ptr UserRef;
      
      class User_Helper {
      public:
        typedef User_ptr _ptr_type;
      
        static _ptr_type _nil();
        static _CORBA_Boolean is_nil(_ptr_type);
        static void release(_ptr_type);
        static void duplicate(_ptr_type);
        static void marshalObjRef(_ptr_type, cdrStream&);
        static _ptr_type unmarshalObjRef(cdrStream&);
      };
      
      typedef _CORBA_ObjRef_Var<_objref_User, User_Helper> User_var;
      typedef _CORBA_ObjRef_OUT_arg<_objref_User,User_Helper > User_out;
      
      #endif
      
      // interface User
      class User {
      public:
        // Declarations for this interface type.
        typedef User_ptr _ptr_type;
        typedef User_var _var_type;
      
        static _ptr_type _duplicate(_ptr_type);
        static _ptr_type _narrow(::CORBA::Object_ptr);
        static _ptr_type _unchecked_narrow(::CORBA::Object_ptr);
        
        static _ptr_type _nil();
      
        static inline void _marshalObjRef(_ptr_type, cdrStream&);
      
        static inline _ptr_type _unmarshalObjRef(cdrStream& s) {
          omniObjRef* o = omniObjRef::_unMarshal(_PD_repoId,s);
          if (o)
            return (_ptr_type) o->_ptrToObjRef(_PD_repoId);
          else
            return _nil();
        }
      
        static _core_attr const char* _PD_repoId;
      
        // Other IDL defined within this scope.
        class Groups_var;
      
        class Groups : public _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper >  {
        public:
          typedef Groups_var _var_type;
          inline Groups() {}
          inline Groups(const Groups& _s)
            : _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > (_s) {}
      
          inline Groups(_CORBA_ULong _max)
            : _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > (_max) {}
          inline Groups(_CORBA_ULong _max, _CORBA_ULong _len, Group_ptr* _val, _CORBA_Boolean _rel=0)
            : _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > (_max, _len, _val, _rel) {}
      
        
      
          inline Groups& operator = (const Groups& _s) {
            _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > ::operator=(_s);
            return *this;
          }
        };
      
        class Groups_out;
      
        class Groups_var {
        public:
          inline Groups_var() : _pd_seq(0) {}
          inline Groups_var(Groups* _s) : _pd_seq(_s) {}
          inline Groups_var(const Groups_var& _s) {
            if( _s._pd_seq )  _pd_seq = new Groups(*_s._pd_seq);
            else              _pd_seq = 0;
          }
          inline ~Groups_var() { if( _pd_seq )  delete _pd_seq; }
            
          inline Groups_var& operator = (Groups* _s) {
            if( _pd_seq )  delete _pd_seq;
            _pd_seq = _s;
            return *this;
          }
          inline Groups_var& operator = (const Groups_var& _s) {
            if( _s._pd_seq ) {
              if( !_pd_seq )  _pd_seq = new Groups;
              *_pd_seq = *_s._pd_seq;
            } else if( _pd_seq ) {
              delete _pd_seq;
              _pd_seq = 0;
            }
            return *this;
          }
          inline _CORBA_ObjRef_Element< _objref_Group, Group_Helper>  operator [] (_CORBA_ULong _s) {
            return (*_pd_seq)[_s];
          }
      
        
      
          inline Groups* operator -> () { return _pd_seq; }
          inline const Groups* operator -> () const { return _pd_seq; }
      #if defined(__GNUG__)
          inline operator Groups& () const { return *_pd_seq; }
      #else
          inline operator const Groups& () const { return *_pd_seq; }
          inline operator Groups& () { return *_pd_seq; }
      #endif
            
          inline const Groups& in() const { return *_pd_seq; }
          inline Groups&       inout()    { return *_pd_seq; }
          inline Groups*&      out() {
            if( _pd_seq ) { delete _pd_seq; _pd_seq = 0; }
            return _pd_seq;
          }
          inline Groups* _retn() { Groups* tmp = _pd_seq; _pd_seq = 0; return tmp; }
            
          friend class Groups_out;
          
        private:
          Groups* _pd_seq;
        };
      
        class Groups_out {
        public:
          inline Groups_out(Groups*& _s) : _data(_s) { _data = 0; }
          inline Groups_out(Groups_var& _s)
            : _data(_s._pd_seq) { _s = (Groups*) 0; }
          inline Groups_out(const Groups_out& _s) : _data(_s._data) {}
          inline Groups_out& operator = (const Groups_out& _s) {
            _data = _s._data;
            return *this;
          }
          inline Groups_out& operator = (Groups* _s) {
            _data = _s;
            return *this;
          }
          inline operator Groups*&()  { return _data; }
          inline Groups*& ptr()       { return _data; }
          inline Groups* operator->() { return _data; }
      
          inline _CORBA_ObjRef_Element< _objref_Group, Group_Helper>  operator [] (_CORBA_ULong _i) {
            return (*_data)[_i];
          }
      
        
      
          Groups*& _data;
      
        private:
          Groups_out();
          Groups_out& operator=(const Groups_var&);
        };
      
      
      };
      
      class _objref_User :
        public virtual ::CORBA::Object,
        public virtual omniObjRef
      {
      public:
        User::Groups* getGroups();
        void setGroups(const ::User::Groups& g);
      
        inline _objref_User()  { _PR_setobj(0); }  // nil
        _objref_User(omniIOR*, omniIdentity*);
      
      protected:
        virtual ~_objref_User();
      
        
      private:
        virtual void* _ptrToObjRef(const char*);
      
        _objref_User(const _objref_User&);
        _objref_User& operator = (const _objref_User&);
        // not implemented
      
        friend class User;
      };
      
      class _pof_User : public _OMNI_NS(proxyObjectFactory) {
      public:
        inline _pof_User() : _OMNI_NS(proxyObjectFactory)(User::_PD_repoId) {}
        virtual ~_pof_User();
      
        virtual omniObjRef* newObjRef(omniIOR*,omniIdentity*);
        virtual _CORBA_Boolean is_a(const char*) const;
      };
      
      class _impl_User :
        public virtual omniServant
      {
      public:
        virtual ~_impl_User();
      
        
        virtual User::Groups* getGroups() = 0;
        virtual void setGroups(const ::User::Groups& g) = 0;
        
      public:  // Really protected, workaround for xlC
        virtual _CORBA_Boolean _dispatch(omniCallHandle&);
      
      private:
        virtual void* _ptrToInterface(const char*);
        virtual const char* _mostDerivedRepoId();
        
      };
      
      
      
      
      class POA_User :
        public virtual _impl_User,
        public virtual ::PortableServer::ServantBase
      {
      public:
        virtual ~POA_User();
      
        inline ::User_ptr _this() {
          return (::User_ptr) _do_this(::User::_PD_repoId);
        }
      };
      
      
      
      
      
      
      
      #undef _core_attr
      #undef _dyn_attr
      
      
      
      inline void
      User::_marshalObjRef(::User_ptr obj, cdrStream& s) {
        omniObjRef::_marshal(obj->_PR_getobj(),s);
      }
      
      
      
      
      #ifdef   USE_stub_in_nt_dll_NOT_DEFINED_User
      # undef  USE_stub_in_nt_dll
      # undef  USE_stub_in_nt_dll_NOT_DEFINED_User
      #endif
      #ifdef   USE_core_stub_in_nt_dll_NOT_DEFINED_User
      # undef  USE_core_stub_in_nt_dll
      # undef  USE_core_stub_in_nt_dll_NOT_DEFINED_User
      #endif
      #ifdef   USE_dyn_stub_in_nt_dll_NOT_DEFINED_User
      # undef  USE_dyn_stub_in_nt_dll
      # undef  USE_dyn_stub_in_nt_dll_NOT_DEFINED_User
      #endif
      
      #endif  // __User_hh__
      I have problems with implementation only a setter to the sequence and I'm not so sure if a getter is ok here.

      Comment

      • yakyak
        New Member
        • Aug 2012
        • 3

        #4
        Anybody?

        I thougth to do it in a different way:

        Code:
        class UserImpl : public POA_User
        {
        	User::Groups groups;
        	//..some other things
        };
        
        User::Groups* UserImpl::getGroups()
        {
            const size_t size = this->groups.length();
            User::Groups_var seqOfObjects = new User::Groups(size);
            seqOfObjects->length(size);
            for(size_t i=0; i<size; i++)
            {
                seqOfObjects[i] = groups[i];
            }
            return seqOfObjects._retn();
        }
        
        void UserImpl::setGroups(const ::User::Groups& g)
        {
            const size_t size =  g.length();
            this->groups.length(size);
            for(size_t i = 0; i<size; i++)
            {
                this->groups[i] = g[i];
            }
        }
        Is that right?

        Comment

        Working...