gpt4 book ai didi

c++ - 如何获取和设置 CORBA 中的类字段? (处理 CORBA 对象序列)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:08 25 4
gpt4 key购买 nike

我有这样的东西:

用户.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 和 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];
}
}

和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)
{

}

我得到的问题:我做对了吗?我的意思是,这段代码一切正常吗?我仍然在学习如何用 CORBA 编写代码,但有时会有疑问,尤其是涉及到序列时...

第二个问题:如何正确设置群主和获取、设置群成员?

我的意思是,我想在我的主文件中做这样的事情:

 #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;
}

请帮助:) 我使用 omniORB 和 C++ 语言

最佳答案

好吧,我想我想出了如何编写 getGroupsgetUsers 的实现:

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

是吗?但是我在 setUserssetGroups 实现方面仍然存在问题。

关于c++ - 如何获取和设置 CORBA 中的类字段? (处理 CORBA 对象序列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11868128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com