473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

[CORBA] Getter and setter for CORBA sequence of objects

yakyak
3
Hi all!

I have a class:
Expand|Select|Wrap|Line Numbers
  1. class UserImpl : public POA_User
  2. {
  3.     private :
  4.         typedef vector<GroupImpl*> Groups;
  5.         Groups groups;
  6.  
  7.     public :
  8.  
  9.         User::Groups* getGroups();
  10.         void setGroups(const ::User::Groups& g);
  11. };
  12.  
  13. #endif
  14.  
  15. #include "UserImpl.h"
  16.  
  17. User::Groups* UserImpl::getGroups()
  18. {
  19.     const size_t size = this->groups.size();
  20.     User::Groups_var seqOfObjects = new User::Groups(size);
  21.     seqOfObjects->length(size);
  22.  
  23.     size_t i = 0;
  24.     vector<GroupImpl*>::const_iterator it = groups.begin();
  25.     while (it != groups.end())
  26.     {
  27.         seqOfObjects[i] = Group::_duplicate((*it)->_this());
  28.         ++it;
  29.         ++i;
  30.     }
  31.  
  32.    return seqOfObjects._retn();
  33. }
  34.  
  35. void UserImpl::setGroups(const ::User::Groups& g)
  36. {
  37.     const size_t size = g.length();
  38.     this->groups.resize(size);
  39.     this->groups.clear();
  40.  
  41.     for(size_t i = 0; i<size; i++)
  42.     {
  43.         GroupImpl *newGroup = new GroupImpl(g[i]->getID(), g[i]->getName(),
  44.                                             g[i]->getDescription(),g[i]->getFounder(),
  45.                                             *(g[i]->getUsers()));
  46.         this->groups.push_back(Groups::value_type(newGroup));
  47.     }
  48. }
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:)
Aug 10 '12 #1
3 2428
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 10 '12 #2
yakyak
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:

Expand|Select|Wrap|Line Numbers
  1. // This file is generated by omniidl (C++ backend)- omniORB_4_1. Do not edit.
  2. #ifndef __User_hh__
  3. #define __User_hh__
  4.  
  5. #ifndef __CORBA_H_EXTERNAL_GUARD__
  6. #include <omniORB4/CORBA.h>
  7. #endif
  8.  
  9. #ifndef  USE_stub_in_nt_dll
  10. # define USE_stub_in_nt_dll_NOT_DEFINED_User
  11. #endif
  12. #ifndef  USE_core_stub_in_nt_dll
  13. # define USE_core_stub_in_nt_dll_NOT_DEFINED_User
  14. #endif
  15. #ifndef  USE_dyn_stub_in_nt_dll
  16. # define USE_dyn_stub_in_nt_dll_NOT_DEFINED_User
  17. #endif
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. #ifdef USE_stub_in_nt_dll
  25. # ifndef USE_core_stub_in_nt_dll
  26. #  define USE_core_stub_in_nt_dll
  27. # endif
  28. # ifndef USE_dyn_stub_in_nt_dll
  29. #  define USE_dyn_stub_in_nt_dll
  30. # endif
  31. #endif
  32.  
  33. #ifdef _core_attr
  34. # error "A local CPP macro _core_attr has already been defined."
  35. #else
  36. # ifdef  USE_core_stub_in_nt_dll
  37. #  define _core_attr _OMNIORB_NTDLL_IMPORT
  38. # else
  39. #  define _core_attr
  40. # endif
  41. #endif
  42.  
  43. #ifdef _dyn_attr
  44. # error "A local CPP macro _dyn_attr has already been defined."
  45. #else
  46. # ifdef  USE_dyn_stub_in_nt_dll
  47. #  define _dyn_attr _OMNIORB_NTDLL_IMPORT
  48. # else
  49. #  define _dyn_attr
  50. # endif
  51. #endif
  52.  
  53.  
  54.  
  55.  
  56.  
  57. #ifndef __Group__
  58. #define __Group__
  59.  
  60. class Group;
  61. class _objref_Group;
  62. class _impl_Group;
  63.  
  64. typedef _objref_Group* Group_ptr;
  65. typedef Group_ptr GroupRef;
  66.  
  67. class Group_Helper {
  68. public:
  69.   typedef Group_ptr _ptr_type;
  70.  
  71.   static _ptr_type _nil();
  72.   static _CORBA_Boolean is_nil(_ptr_type);
  73.   static void release(_ptr_type);
  74.   static void duplicate(_ptr_type);
  75.   static void marshalObjRef(_ptr_type, cdrStream&);
  76.   static _ptr_type unmarshalObjRef(cdrStream&);
  77. };
  78.  
  79. typedef _CORBA_ObjRef_Var<_objref_Group, Group_Helper> Group_var;
  80. typedef _CORBA_ObjRef_OUT_arg<_objref_Group,Group_Helper > Group_out;
  81.  
  82. #endif
  83.  
  84. #ifndef __User__
  85. #define __User__
  86.  
  87. class User;
  88. class _objref_User;
  89. class _impl_User;
  90.  
  91. typedef _objref_User* User_ptr;
  92. typedef User_ptr UserRef;
  93.  
  94. class User_Helper {
  95. public:
  96.   typedef User_ptr _ptr_type;
  97.  
  98.   static _ptr_type _nil();
  99.   static _CORBA_Boolean is_nil(_ptr_type);
  100.   static void release(_ptr_type);
  101.   static void duplicate(_ptr_type);
  102.   static void marshalObjRef(_ptr_type, cdrStream&);
  103.   static _ptr_type unmarshalObjRef(cdrStream&);
  104. };
  105.  
  106. typedef _CORBA_ObjRef_Var<_objref_User, User_Helper> User_var;
  107. typedef _CORBA_ObjRef_OUT_arg<_objref_User,User_Helper > User_out;
  108.  
  109. #endif
  110.  
  111. // interface User
  112. class User {
  113. public:
  114.   // Declarations for this interface type.
  115.   typedef User_ptr _ptr_type;
  116.   typedef User_var _var_type;
  117.  
  118.   static _ptr_type _duplicate(_ptr_type);
  119.   static _ptr_type _narrow(::CORBA::Object_ptr);
  120.   static _ptr_type _unchecked_narrow(::CORBA::Object_ptr);
  121.  
  122.   static _ptr_type _nil();
  123.  
  124.   static inline void _marshalObjRef(_ptr_type, cdrStream&);
  125.  
  126.   static inline _ptr_type _unmarshalObjRef(cdrStream& s) {
  127.     omniObjRef* o = omniObjRef::_unMarshal(_PD_repoId,s);
  128.     if (o)
  129.       return (_ptr_type) o->_ptrToObjRef(_PD_repoId);
  130.     else
  131.       return _nil();
  132.   }
  133.  
  134.   static _core_attr const char* _PD_repoId;
  135.  
  136.   // Other IDL defined within this scope.
  137.   class Groups_var;
  138.  
  139.   class Groups : public _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper >  {
  140.   public:
  141.     typedef Groups_var _var_type;
  142.     inline Groups() {}
  143.     inline Groups(const Groups& _s)
  144.       : _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > (_s) {}
  145.  
  146.     inline Groups(_CORBA_ULong _max)
  147.       : _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > (_max) {}
  148.     inline Groups(_CORBA_ULong _max, _CORBA_ULong _len, Group_ptr* _val, _CORBA_Boolean _rel=0)
  149.       : _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > (_max, _len, _val, _rel) {}
  150.  
  151.  
  152.  
  153.     inline Groups& operator = (const Groups& _s) {
  154.       _CORBA_Unbounded_Sequence_ObjRef< _objref_Group, _CORBA_ObjRef_Element< _objref_Group, Group_Helper> , Group_Helper > ::operator=(_s);
  155.       return *this;
  156.     }
  157.   };
  158.  
  159.   class Groups_out;
  160.  
  161.   class Groups_var {
  162.   public:
  163.     inline Groups_var() : _pd_seq(0) {}
  164.     inline Groups_var(Groups* _s) : _pd_seq(_s) {}
  165.     inline Groups_var(const Groups_var& _s) {
  166.       if( _s._pd_seq )  _pd_seq = new Groups(*_s._pd_seq);
  167.       else              _pd_seq = 0;
  168.     }
  169.     inline ~Groups_var() { if( _pd_seq )  delete _pd_seq; }
  170.  
  171.     inline Groups_var& operator = (Groups* _s) {
  172.       if( _pd_seq )  delete _pd_seq;
  173.       _pd_seq = _s;
  174.       return *this;
  175.     }
  176.     inline Groups_var& operator = (const Groups_var& _s) {
  177.       if( _s._pd_seq ) {
  178.         if( !_pd_seq )  _pd_seq = new Groups;
  179.         *_pd_seq = *_s._pd_seq;
  180.       } else if( _pd_seq ) {
  181.         delete _pd_seq;
  182.         _pd_seq = 0;
  183.       }
  184.       return *this;
  185.     }
  186.     inline _CORBA_ObjRef_Element< _objref_Group, Group_Helper>  operator [] (_CORBA_ULong _s) {
  187.       return (*_pd_seq)[_s];
  188.     }
  189.  
  190.  
  191.  
  192.     inline Groups* operator -> () { return _pd_seq; }
  193.     inline const Groups* operator -> () const { return _pd_seq; }
  194. #if defined(__GNUG__)
  195.     inline operator Groups& () const { return *_pd_seq; }
  196. #else
  197.     inline operator const Groups& () const { return *_pd_seq; }
  198.     inline operator Groups& () { return *_pd_seq; }
  199. #endif
  200.  
  201.     inline const Groups& in() const { return *_pd_seq; }
  202.     inline Groups&       inout()    { return *_pd_seq; }
  203.     inline Groups*&      out() {
  204.       if( _pd_seq ) { delete _pd_seq; _pd_seq = 0; }
  205.       return _pd_seq;
  206.     }
  207.     inline Groups* _retn() { Groups* tmp = _pd_seq; _pd_seq = 0; return tmp; }
  208.  
  209.     friend class Groups_out;
  210.  
  211.   private:
  212.     Groups* _pd_seq;
  213.   };
  214.  
  215.   class Groups_out {
  216.   public:
  217.     inline Groups_out(Groups*& _s) : _data(_s) { _data = 0; }
  218.     inline Groups_out(Groups_var& _s)
  219.       : _data(_s._pd_seq) { _s = (Groups*) 0; }
  220.     inline Groups_out(const Groups_out& _s) : _data(_s._data) {}
  221.     inline Groups_out& operator = (const Groups_out& _s) {
  222.       _data = _s._data;
  223.       return *this;
  224.     }
  225.     inline Groups_out& operator = (Groups* _s) {
  226.       _data = _s;
  227.       return *this;
  228.     }
  229.     inline operator Groups*&()  { return _data; }
  230.     inline Groups*& ptr()       { return _data; }
  231.     inline Groups* operator->() { return _data; }
  232.  
  233.     inline _CORBA_ObjRef_Element< _objref_Group, Group_Helper>  operator [] (_CORBA_ULong _i) {
  234.       return (*_data)[_i];
  235.     }
  236.  
  237.  
  238.  
  239.     Groups*& _data;
  240.  
  241.   private:
  242.     Groups_out();
  243.     Groups_out& operator=(const Groups_var&);
  244.   };
  245.  
  246.  
  247. };
  248.  
  249. class _objref_User :
  250.   public virtual ::CORBA::Object,
  251.   public virtual omniObjRef
  252. {
  253. public:
  254.   User::Groups* getGroups();
  255.   void setGroups(const ::User::Groups& g);
  256.  
  257.   inline _objref_User()  { _PR_setobj(0); }  // nil
  258.   _objref_User(omniIOR*, omniIdentity*);
  259.  
  260. protected:
  261.   virtual ~_objref_User();
  262.  
  263.  
  264. private:
  265.   virtual void* _ptrToObjRef(const char*);
  266.  
  267.   _objref_User(const _objref_User&);
  268.   _objref_User& operator = (const _objref_User&);
  269.   // not implemented
  270.  
  271.   friend class User;
  272. };
  273.  
  274. class _pof_User : public _OMNI_NS(proxyObjectFactory) {
  275. public:
  276.   inline _pof_User() : _OMNI_NS(proxyObjectFactory)(User::_PD_repoId) {}
  277.   virtual ~_pof_User();
  278.  
  279.   virtual omniObjRef* newObjRef(omniIOR*,omniIdentity*);
  280.   virtual _CORBA_Boolean is_a(const char*) const;
  281. };
  282.  
  283. class _impl_User :
  284.   public virtual omniServant
  285. {
  286. public:
  287.   virtual ~_impl_User();
  288.  
  289.  
  290.   virtual User::Groups* getGroups() = 0;
  291.   virtual void setGroups(const ::User::Groups& g) = 0;
  292.  
  293. public:  // Really protected, workaround for xlC
  294.   virtual _CORBA_Boolean _dispatch(omniCallHandle&);
  295.  
  296. private:
  297.   virtual void* _ptrToInterface(const char*);
  298.   virtual const char* _mostDerivedRepoId();
  299.  
  300. };
  301.  
  302.  
  303.  
  304.  
  305. class POA_User :
  306.   public virtual _impl_User,
  307.   public virtual ::PortableServer::ServantBase
  308. {
  309. public:
  310.   virtual ~POA_User();
  311.  
  312.   inline ::User_ptr _this() {
  313.     return (::User_ptr) _do_this(::User::_PD_repoId);
  314.   }
  315. };
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323. #undef _core_attr
  324. #undef _dyn_attr
  325.  
  326.  
  327.  
  328. inline void
  329. User::_marshalObjRef(::User_ptr obj, cdrStream& s) {
  330.   omniObjRef::_marshal(obj->_PR_getobj(),s);
  331. }
  332.  
  333.  
  334.  
  335.  
  336. #ifdef   USE_stub_in_nt_dll_NOT_DEFINED_User
  337. # undef  USE_stub_in_nt_dll
  338. # undef  USE_stub_in_nt_dll_NOT_DEFINED_User
  339. #endif
  340. #ifdef   USE_core_stub_in_nt_dll_NOT_DEFINED_User
  341. # undef  USE_core_stub_in_nt_dll
  342. # undef  USE_core_stub_in_nt_dll_NOT_DEFINED_User
  343. #endif
  344. #ifdef   USE_dyn_stub_in_nt_dll_NOT_DEFINED_User
  345. # undef  USE_dyn_stub_in_nt_dll
  346. # undef  USE_dyn_stub_in_nt_dll_NOT_DEFINED_User
  347. #endif
  348.  
  349. #endif  // __User_hh__
  350.  
  351.  
I have problems with implementation only a setter to the sequence and I'm not so sure if a getter is ok here.
Aug 10 '12 #3
yakyak
3
Anybody?

I thougth to do it in a different way:

Expand|Select|Wrap|Line Numbers
  1. class UserImpl : public POA_User
  2. {
  3.     User::Groups groups;
  4.     //..some other things
  5. };
  6.  
  7. User::Groups* UserImpl::getGroups()
  8. {
  9.     const size_t size = this->groups.length();
  10.     User::Groups_var seqOfObjects = new User::Groups(size);
  11.     seqOfObjects->length(size);
  12.     for(size_t i=0; i<size; i++)
  13.     {
  14.         seqOfObjects[i] = groups[i];
  15.     }
  16.     return seqOfObjects._retn();
  17. }
  18.  
  19. void UserImpl::setGroups(const ::User::Groups& g)
  20. {
  21.     const size_t size =  g.length();
  22.     this->groups.length(size);
  23.     for(size_t i = 0; i<size; i++)
  24.     {
  25.         this->groups[i] = g[i];
  26.     }
  27. }
Is that right?
Aug 11 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kiwi | last post by:
Hello. I know a getter can return other thing than a field. I know a setter can do more things than setting a field. I know there are "setter only" cases and "getter only" cases. I do use...
2
by: Krzysztof Opała | last post by:
Hi I've got my CORBA application written in Borland C++ Builder, using Visibroker. What I wanna do is get from server a set of strings (the number of string is not constant). I was trying to do it...
14
by: tshad | last post by:
I have people telling me that I should set up objects for my tables, but I am finding the Null problem makes that difficult. It isn't a big problem if you are not updating the table, but if you...
5
by: Greg Scharlemann | last post by:
For some reason, I am having trouble retrieving the data that I store in the object that I have created from a database query. I created this class Lead (see below). In the php page, I create and...
25
by: Michal Kwiatkowski | last post by:
Hi, Code below shows that property() works only if you use it within a class. ------------------------------------------------ class A(object): pass a = A() a.y = 7
1
by: Steve | last post by:
I generate C# webservices proxy code from WSDL file, it turns out the classes generated have public member variables and no getter/setter methods as follows, and I am able to get data when...
2
by: Hemant Shah | last post by:
Folks, How can I copy sequence objects from one database to another? I have a script that exports data from one database and loads it into another. Some of the columns in the database are...
5
by: kronrn | last post by:
Hi Folks Can anyone confirm that the code public string Name { get; set; }
0
by: Softhideki | last post by:
Good Day!!! For alll I have problem for create a script to copy a sequence of way automatic to copy a sequence of oracle database for other oracle database. Anyone help me , how can...
3
by: nelsonbrodyk | last post by:
Hey all, Just curious, I want to make the "set" in a property obsolete, but not the get or the property. Is there a way to do this? Public string FirstName { get; set; } works fine, but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.