STL Help with Store/Retrieve .. Maps

F

forums_mp

I've got an STL class (see below) with two functions to store and
retrieve data - msg structs.

The "Store" function when called will copy the received message
(depending on which message) into the appropriate array. For instance
if I receive a RCVD_MSG1, I'll copy into msg1 [ 0 ]. Upon receipt of
the next RCVD_MSG1. I'll copy into msg1 [ 1 ].
The Retrieve function when called should retrieve the latest copy.
ie. msg1 [ 0 ] or msg1 [ 1 ].

I've made it this far and am unsure how best to implement this.
One other thing. I suspect maps (not too familiar with maps but I
vaguely recall seeing something similiar in a newsgroup recently .. )
would be provide an more elegant approach but i'm unsure how to modify
code to do this.

Thanks

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

#include <iostream.h>

const Max = 20;
const True = 1;
const False = 0;

struct {
unsigned rcvd_item1 : 16;
unsigned rcvd_item2 : 13;
unsigned rcvd_item3 : 10;
unsigned spare : 9;
} RCVD_MSG1;

struct {
unsigned rcvd_item1 : 11;
unsigned rcvd_item2 : 10;
unsigned spare : 11;
} RCVD_MSG2;

RCVD_MSG2 msg2 [ 2 ];
RCVD_MSG1 msg1 [ 2 ];


template<class T> class Vector
{
struct
{
T Data; //field Data is type T ..
// May need to make this T Data [ 2 ];

int Defined; //field Defined is type int
} Elements[Max]; //array of Max items

public:
Vector::Vector()
//Creates an empty abstract array.
{
int Index;

for (Index = 0; Index < Max; ++Index)
Elements[Index].Defined = False;
}

void Vector::Init(T X)
//Initialize all elements to X
//Pre : Vector created and X defined.
//Post: All elements set to X and marked as defined.
{
int Index;

for (Index = 0; Index < Max; ++Index)
{
Elements[Index].Data = X;
Elements[Index].Defined = True;
}
}

void Vector::Store (T X, int I)
//Stores X at position I in abstract array.
//Pre : Vector created; X and I are defined.
//Post: Elements[Index].Data is X and position I is defined.
//Need to implement ping/pong approach of the stored sturcts.
// i.e. first call to Store a RCVD_MSG1 would store at msg1 [ 0 ]
// Next call would store at msg1 [ 1 ]
{
Elements.Data = X;
Elements.Defined = True;
}

void Vector::Retrieve(T &X, int I, int &Success)
//Copies the value stored at vector position I to X.
//
//Post: Returns value stored at position I through X and
// set Success to True, if position defined; otherwise
// Success set to False.
// Retrieve the LATEST at all times .. msg1 [ 0 ] or msg1 [ 1 ];

{
Success = Elements.Defined;
if (Success)
X = Elements.Data;
}

};
 
C

Cy Edmunds

forums_mp said:
I've got an STL class (see below) with two functions to store and
retrieve data - msg structs.

The "Store" function when called will copy the received message
(depending on which message) into the appropriate array. For instance
if I receive a RCVD_MSG1, I'll copy into msg1 [ 0 ]. Upon receipt of
the next RCVD_MSG1. I'll copy into msg1 [ 1 ].
The Retrieve function when called should retrieve the latest copy.
ie. msg1 [ 0 ] or msg1 [ 1 ].

I've made it this far and am unsure how best to implement this.
One other thing. I suspect maps (not too familiar with maps but I
vaguely recall seeing something similiar in a newsgroup recently .. )
would be provide an more elegant approach but i'm unsure how to modify
code to do this.

Thanks

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

#include <iostream.h>

const Max = 20;
const True = 1;
const False = 0;

struct {
unsigned rcvd_item1 : 16;
unsigned rcvd_item2 : 13;
unsigned rcvd_item3 : 10;
unsigned spare : 9;
} RCVD_MSG1;

struct {
unsigned rcvd_item1 : 11;
unsigned rcvd_item2 : 10;
unsigned spare : 11;
} RCVD_MSG2;

RCVD_MSG2 msg2 [ 2 ];
RCVD_MSG1 msg1 [ 2 ];


template<class T> class Vector
{
struct
{
T Data; //field Data is type T ..
// May need to make this T Data [ 2 ];

int Defined; //field Defined is type int
} Elements[Max]; //array of Max items

public:
Vector::Vector()
//Creates an empty abstract array.
{
int Index;

for (Index = 0; Index < Max; ++Index)
Elements[Index].Defined = False;
}

void Vector::Init(T X)
//Initialize all elements to X
//Pre : Vector created and X defined.
//Post: All elements set to X and marked as defined.
{
int Index;

for (Index = 0; Index < Max; ++Index)
{
Elements[Index].Data = X;
Elements[Index].Defined = True;
}
}

void Vector::Store (T X, int I)
//Stores X at position I in abstract array.
//Pre : Vector created; X and I are defined.
//Post: Elements[Index].Data is X and position I is defined.
//Need to implement ping/pong approach of the stored sturcts.
// i.e. first call to Store a RCVD_MSG1 would store at msg1 [ 0 ]
// Next call would store at msg1 [ 1 ]
{
Elements.Data = X;
Elements.Defined = True;
}

void Vector::Retrieve(T &X, int I, int &Success)
//Copies the value stored at vector position I to X.
//
//Post: Returns value stored at position I through X and
// set Success to True, if position defined; otherwise
// Success set to False.
// Retrieve the LATEST at all times .. msg1 [ 0 ] or msg1 [ 1 ];

{
Success = Elements.Defined;
if (Success)
X = Elements.Data;
}

};


I don't see how a map would help. I think you could do everything you need
with std::vector:

#include <vector>
#include <exception>

template <typename RCVD_MSG>
class messages
{
private:
std::vector<RCVD_MSG> m_vec;

public:
void store(RCVD_MSG msg)
{
m_vvec.push_back(msg);
}

RCVD_MSG retrieve()
{
if (m_vec.size() == 0)
throw std::exception("oops");
return m_vec.back();
}

int size() const // check this before calling retrieve to avoid
exceptions
{
return m_vec.size();
}
};

messages<RCVD_MSG1> m1;
messages<RCVD_MSG2> m2;

<untested code>
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top