Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and set a class field in CORBA? (dealing with sequences of CORBA objects)

I have sth like this:

User.idl:

#ifndef __USER_IDL__
#define __USER_IDL__

interface Group;

interface User
{
    typedef sequence<Group> Groups;
    Groups getGroups();
    void setGroups(in Groups g);
};

#endif

UserImpl.h and UserImpl.cpp:

class UserImpl : public POA_User
{
    private :
        User::Groups groups;
    public :

        User::Groups* getGroups();
        void setGroups(const ::User::Groups& g);
};

#endif

#include "UserImpl.h"

User::Groups* UserImpl::getGroups()
{
    return &(this->groups);
}

void UserImpl::setGroups(const ::User::Groups& g)
{
    this->groups.length(g.length());
    for(int i=0; i<g.length(); i++)
    {
        this->groups[i] = this->groups[i];
    }
}

And Group.idl:

#ifndef __GROUP_IDL__
#define __GROUP_IDL__

#include "User.idl"

interface Group
{
    typedef sequence<User> Users;
    User getFounder();
    void setFounder(in User u);
    Users getUsers();
    void setUsers(in Users u);
};

#endif

GroupImpl.h, GroupImpl.cpp:

class UserImpl;

class GroupImpl : public POA_Group
{
    private :

        UserImpl *founder;
        Group::Users members;            

    public :

        User_ptr getFounder();
        void setFounder(::User_ptr u);
        Group::Users* getUsers();
        void setUsers(const ::Group::Users& u);
};

User_ptr GroupImpl::getFounder()
{
    return this->founder->_this();
}

void GroupImpl::setFounder(::User_ptr u)
{

}

Group::Users* GroupImpl::getUsers()
{

}

void GroupImpl::setUsers(const ::Group::Users& u)
{

}

The question I got here: did I do it right? I mean, is everything ok with this code? I still learn how to write in CORBA and sometimes have doubts especially if it comes to sequences...

The second question: how do I properly set group's founder and get and set group's members?

I mean, I would like to do sth like this in my main file:

 #include "UserImpl.h"
#include "GroupImpl.h"
#include <omniORB4/CORBA.h>
#include <omniORB4/Naming.hh>
#include <iostream>
using std::cout;
using std::cerr;

int main(int argc, char **argv)
{
    UserImpl u;
    u.setLogin("yak");
    u.setID(123);
    cout << u.getLogin() << "\n";
    cout << u.getID() << "\n";
    cout << u.toString() << "\n";


    GroupImpl **g = new GroupImpl*[1];
    for(int i=0; i<1; i++)
    {
        g[i] = new GroupImpl();
    }

    u.setGroups(g);

    return 0;
}

Please, help:) I use omniORB and C++ language

like image 739
yak Avatar asked Nov 13 '22 00:11

yak


1 Answers

Ok I think I figured out how to write an implementation of getGroups and getUsers:

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();
}

Is that right? But I still have problems with setUsers and setGroups implementations.

like image 153
yak Avatar answered Jan 09 '23 15:01

yak