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