iterating over fields

A

Andrea Crotti

Supposing I have a class with many objects which are also of class
Serializable.

I've been wondering for a long time how I can iterate over them, and
maybe I found a solution:

auto_ptr<Serializable> fields[NUM_FIELDS];

and then during initialization do

fields[0] = auto_ptr<Serializable>(&currentLocation);
fields[1] = auto_ptr<Serializable>(&address);

and supposing they all have the method "check" I could do

for (int i = 0; i < NUM_FIELDS; ++i)
if (! (fields->check()))
return false;

return true;

for example, which looks quite nice.
Does it make sense?
Maybe I should give completely to auto_ptr the control?

And also I tried first with a vector but it doesn't work, I read on the
internet something but I really didn't get why

vector<auto_ptr<Serializable> > is not digested by C++...
 
E

Ebenezer

Supposing I have a class with many objects which are also of class
Serializable.

I've been wondering for a long time how I can iterate over them, and
maybe I found a solution:

    auto_ptr<Serializable> fields[NUM_FIELDS];

and then during initialization do

    fields[0] = auto_ptr<Serializable>(&currentLocation);
    fields[1] = auto_ptr<Serializable>(&address);

and supposing they all have the method "check" I could do

for (int i = 0; i < NUM_FIELDS; ++i)
    if (! (fields->check()))
       return false;

return true;

for example, which looks quite nice.
Does it make sense?
Maybe I should give completely to auto_ptr the control?

And also I tried first with a vector but it doesn't work, I read on the
internet something but I really didn't get why

vector<auto_ptr<Serializable> > is not digested by C++...


The C++ Middleware Writer (CMW) automates the creation of
marshalling functions. Given this type as input:


struct cmw_account_info {
uint32_t accountnumber_;
lil_string password_;

cmw_account_info()
{}
template <typename R>
explicit cmw_account_info(ReceiveCompressedBuffer<R>& buf);

void CalculateMarshallingSize(Counter& cntr) const;
void SendMemberData(SendCompressedBuffer& buf) const;
void SendTypeNum(SendCompressedBuffer& buf) const;
void
Send(SendCompressedBuffer& buf, bool = false) const
{
SendMemberData(buf);
}
};

-----------------------------------------------------------
it writes the following as output:


uint16_t const cmw_account_info_num = 7001;

template <typename R>
inline
cmw_account_info::cmw_account_info(ReceiveCompressedBuffer<R>& buf)

{
buf.Give(accountnumber_);
password_ = lil_string(buf);
}


inline void
cmw_account_info::CalculateMarshallingSize(Counter& cntr) const
{
cntr.Add(sizeof(uint32_t));
password_.CalculateMarshallingSize(cntr);
}


inline void
cmw_account_info::SendMemberData(SendCompressedBuffer& buf) const
{
buf.Receive(&accountnumber_, sizeof(uint32_t));
password_.Send(buf, false);
}


inline void
cmw_account_info::SendTypeNum(SendCompressedBuffer& buf) const
{
buf.Receive(&cmw_account_info_num, sizeof(cmw_account_info_num));
}

----------------------------------------------------------

I don't think the Boost Serialization library or other
serialization libraries have this sort of automation.
In some cases, one needs to disable the automated creation
of marshalling code and provide hand-written
implementations of those functions. That can be done with
the CMW by adding the following to the body of a type:
// hand_written_marshalling_code


Brian Wood
Ebenezer Enterprises
http://webEbenezer.net
http://wnd.com
 
J

James Kanze

Supposing I have a class with many objects which are also of class
Serializable.
I've been wondering for a long time how I can iterate over them, and
maybe I found a solution:
auto_ptr<Serializable> fields[NUM_FIELDS];
and then during initialization do
fields[0] = auto_ptr<Serializable>(&currentLocation);
fields[1] = auto_ptr<Serializable>(&address);

It's very suspicious to take the address of something in the
constructor of an auto_ptr. It can be legal, if e.g. you have:

Serializable& currentLocation = *new Serializable;

But that's a very unusual idiom.
and supposing they all have the method "check" I could do
for (int i = 0; i < NUM_FIELDS; ++i)
if (! (fields->check()))
return false;

return true;
for example, which looks quite nice.
Does it make sense?

I don't know. First, I'd like to know why
std::vector<Serializable> doesn't work. Or if polymorphism is
required, std::vector<std::shared_ptr<Serializable> >, or better
yet, a wrapper vector_ptr which takes care of the memory
management and the extra layer of indirection.
Maybe I should give completely to auto_ptr the control?

In what way?
And also I tried first with a vector but it doesn't work,

Why not?
I read on the internet something but I really didn't get why

vector<auto_ptr<Serializable> > is not digested by C++...

You cannot have a vector of auto_ptr, because auto_ptr isnt
CopyConstructable, in the sense required by vector. Other smart
pointers are, or if you really want a vector of polymorphic
objects owned by the vector, say boost::ptr_vector (a much
better solution).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top