M
MPowell
Lets suppose the return value on new_command is MSG2_STATUS
My intent then is to memcpy the data via the call to CMDHolder and
have a function that'll retrieve a copy of the 'stored' data. The
latter I'm unsure how to do. Any help appreaciated.
#include <iostream>
#include <map>
using namespace std;
int msg2_status_index = 0;
int msg1_status_index = 0;
int msg1_cmd_index = 0;
MSG2_STATUS _msg2_stat[ 2 ]; // used for double buffering
MSG1_STATUS _msg1_stat[ 2 ]; // used for double buffering
MSG1_CMD _msg1_cmd[ 2 ]; // used for double buffering
typedef enum
{
_msg1_cmd = 0,
_msg1_status,
_msg2_status
} MSG_ID;
typedef struct // this is ++. Why use typedef. 'details' I suppose
{
MSG_ID source;
unsigned int msg_length : 12;
unsigned int count : 4;
} MSG_HEADER;
typedef struct _MSG2_STATUS
{
MSG_HEADER header;
unsigned int idx : 1;
unsigned int jdx : 3;
unsigned int kdx : 4;
unsigned int spare : 24;
unsigned int ldx : 32;
} MSG2_STATUS;
typedef struct _MSG1_STATUS
{
MSG_HEADER header;
unsigned int idx : 1;
unsigned int jdx : 3;
unsigned int kdx : 4;
unsigned int spare : 8;
} MSG1_STATUS;
typedef struct _MSG1_CMD
{
MSG_HEADER header;
unsigned int idx : 1;
unsigned int jdx : 3;
} MSG1_CMD;
class CMDBase
{
public:
virtual ~CMDBase(){}
/* what operations do you perform on these commands?
virtual void operation1() = 0;
virtual void operation2() = 0; */
};
template <class CMD>
class CMDHolder: public CMDBase
{
public:
CMDHolder(unsigned char const* buffer)
{
memcpy(&m_CMDStruct, buffer, sizeof m_CMDStruct);
}
/* virtual void operation1()
{
//use overloaded global functions?
structOperation1(m_CMDStruct);
}
virtual void operation2()
{
structOperation2(m_CMDStruct);
} */
private:
CMD m_CMDStruct;
};
class CMDFactory
{
public:
static CMDFactory& instance()
{
CMDFactory fact;
return fact;
}
// Register the creator
template <class T> void Register(int Id)
{
m_creators[Id] = CMDCreator<T>;
}
// unregister creator
bool Unregister( const int& Id )
{
return m_creators.erase( Id ) == 1;
}
CMDBase* Create(unsigned char const* buffer)
{
//MSG_HEADER* msg_id = reinterpret_cast<MSG_HEADER*>(buffer);
MSG_HEADER* msg_id; // only here because reinterpret_cast error
complaint
//insert error checking!
return (m_creators[msg_id->source])(buffer);
}
reinterpret_cast generates an error with respect to the constant
buffer and the non constant MSG_HEADER*. ??
return (m_creators[msg_id->source])(buffer); // return here might be
a bit confusing
private:
typedef CMDBase* (*CMDCreator_t)(unsigned char const*);
template <class T>
static CMDBase* CMDCreator(unsigned char const* buffer)
{
return new T(buffer);
}
std::map<int, CMDCreator_t> m_creators;
};
int main ( void )
{
//called once, in initialization function:
CMDFactory::instance().Register<MSG2_STATUS> (1);
CMDFactory::instance().Register<MSG1_STATUS> (2);
// inside your command processing loop:
unsigned char buffer[512];
// Read_1553_Data(CHANNEL_FIVE, &buffer);
CMDBase* newCommand = CMDFactory::instance().Create(buffer);
// newCommand->operation1(); //whatever you want to do with it
//maybe add it to a queue<CMDBase*>?
//myqueue.push(newCommand);
return EXIT_SUCCESS;
}
My intent then is to memcpy the data via the call to CMDHolder and
have a function that'll retrieve a copy of the 'stored' data. The
latter I'm unsure how to do. Any help appreaciated.
#include <iostream>
#include <map>
using namespace std;
int msg2_status_index = 0;
int msg1_status_index = 0;
int msg1_cmd_index = 0;
MSG2_STATUS _msg2_stat[ 2 ]; // used for double buffering
MSG1_STATUS _msg1_stat[ 2 ]; // used for double buffering
MSG1_CMD _msg1_cmd[ 2 ]; // used for double buffering
typedef enum
{
_msg1_cmd = 0,
_msg1_status,
_msg2_status
} MSG_ID;
typedef struct // this is ++. Why use typedef. 'details' I suppose
{
MSG_ID source;
unsigned int msg_length : 12;
unsigned int count : 4;
} MSG_HEADER;
typedef struct _MSG2_STATUS
{
MSG_HEADER header;
unsigned int idx : 1;
unsigned int jdx : 3;
unsigned int kdx : 4;
unsigned int spare : 24;
unsigned int ldx : 32;
} MSG2_STATUS;
typedef struct _MSG1_STATUS
{
MSG_HEADER header;
unsigned int idx : 1;
unsigned int jdx : 3;
unsigned int kdx : 4;
unsigned int spare : 8;
} MSG1_STATUS;
typedef struct _MSG1_CMD
{
MSG_HEADER header;
unsigned int idx : 1;
unsigned int jdx : 3;
} MSG1_CMD;
class CMDBase
{
public:
virtual ~CMDBase(){}
/* what operations do you perform on these commands?
virtual void operation1() = 0;
virtual void operation2() = 0; */
};
template <class CMD>
class CMDHolder: public CMDBase
{
public:
CMDHolder(unsigned char const* buffer)
{
memcpy(&m_CMDStruct, buffer, sizeof m_CMDStruct);
}
/* virtual void operation1()
{
//use overloaded global functions?
structOperation1(m_CMDStruct);
}
virtual void operation2()
{
structOperation2(m_CMDStruct);
} */
private:
CMD m_CMDStruct;
};
class CMDFactory
{
public:
static CMDFactory& instance()
{
CMDFactory fact;
return fact;
}
// Register the creator
template <class T> void Register(int Id)
{
m_creators[Id] = CMDCreator<T>;
}
// unregister creator
bool Unregister( const int& Id )
{
return m_creators.erase( Id ) == 1;
}
CMDBase* Create(unsigned char const* buffer)
{
//MSG_HEADER* msg_id = reinterpret_cast<MSG_HEADER*>(buffer);
MSG_HEADER* msg_id; // only here because reinterpret_cast error
complaint
//insert error checking!
return (m_creators[msg_id->source])(buffer);
}
reinterpret_cast generates an error with respect to the constant
buffer and the non constant MSG_HEADER*. ??
return (m_creators[msg_id->source])(buffer); // return here might be
a bit confusing
private:
typedef CMDBase* (*CMDCreator_t)(unsigned char const*);
template <class T>
static CMDBase* CMDCreator(unsigned char const* buffer)
{
return new T(buffer);
}
std::map<int, CMDCreator_t> m_creators;
};
int main ( void )
{
//called once, in initialization function:
CMDFactory::instance().Register<MSG2_STATUS> (1);
CMDFactory::instance().Register<MSG1_STATUS> (2);
// inside your command processing loop:
unsigned char buffer[512];
// Read_1553_Data(CHANNEL_FIVE, &buffer);
CMDBase* newCommand = CMDFactory::instance().Create(buffer);
// newCommand->operation1(); //whatever you want to do with it
//maybe add it to a queue<CMDBase*>?
//myqueue.push(newCommand);
return EXIT_SUCCESS;
}