'Ello. I'm having a concern about the ability for several objects that require access to one another. Visual studio intellisence shows no errors, but the compiler does. Mostly that classes become unrecognized. I can get different errors by shuffling around the classes within the sapa namespace. Sorry, its a bit long.
#include <string>
#include <iostream>
#include <vector>
#include <Windows.h>
using namespace std;
#define DEBUG true
/*
SAPA developmental classes and process
to demonstrate class and functioning
/thread conjunctions
*/
//definitions
typedef unsigned int UINT;
enum{second, minute, hour};
#define EXITSAPA 100
#define SECOND 200
#define MINUTE 201
#define HOUR 202
#define MakeNewThread 300
namespace sapa{
struct SubPacket{
UINT signal;
vector <string> target;
vector <string> t_data;
vector <double> data;
};
struct Base_thread_data{//for hardcode handles
int m_id;
};
struct thread_data{//for enumerated handles
int m_id;
thread_data(int id) : m_id(id){}
};
namespace SystemFunctions{
void SAPAEXIT(Packet input){
input.sys->quit=true;
}
void NewThread(Packet input){//create new thread and run instructions
//information on occurence
UINT TimeTemp=0;
UINT Period=0;//time needed to elpse in order to run
while (!input.sys->quit){
}
}
void UpdataTimeSeconds(Packet input){
input.sys->TimeElapsed++;
}
}
class ConsoleIO{
string raw;
vector <string> Vraw;
vector <UINT> signal;
vector <string> Target;
vector <double> data;
vector <string> t_data;
public:
void ConsoleIn(string input){
raw=input;
}
Packet PushDemoPacket(){
SubPacket demo;
demo.signal=EXITSAPA;
}
vector <SubPacket> packetqeue;
};
class Clock{
public:
vector <SubPacket> qeue;
void MakeSubPacket(unsigned int idata){
SubPacket sp;
sp.signal=idata;
qeue.push_back(sp);
}
Clock(){
sec=0;
min=0;
hr=0;
}
int sec,min,hr;
void UpdateSec(){//updates timecode
sec++;
MakeSubPacket(second);
if (sec>=60){
sec=0;
min++;
MakeSubPacket(min);
if (min>=60){
min=0;
hr++;
MakeSubPacket(hr);
}
}
}//update time code
string get_timecode(){
string hold;
hold=sec+':'+min+':'+hr;
return hold;
}
};
class System{//holds information for program operation
public:
bool quit;
System(){
quit=false;
TimeElapsed=0;
}
UINT TimeElapsed;//seconds passed since program run
int Threads;//number of user threads running
vector <int> ThreadIDs;//holds the ids of all running threads so none are duplicated and old exited ids can be recycled
};
class messenger{
public:
System * sys;
Clock * clk;
ConsoleIO*Cio;
bool SystemLink;
bool ClockLink;
bool ConsoleLink;
public:
messenger(){
SystemLink=false;
ClockLink=false;
ConsoleLink=false;
Threads=0;
}
void setSystem(System*psys){
sys=psys;
delete psys;
psys=NULL;
SystemLink=true;
}
void setClock(Clock*pclk){
clk=pclk;
delete pclk;
pclk=NULL;
ClockLink=true;
}
void setConsoleIO(ConsoleIO*CIO){
Cio=CIO;
delete CIO;
CIO=NULL;
ConsoleLink=true;
}
void ProcessPacket(Packet data){//process information
/*
PROCESSING METHOD
switch(signal) to identify correct function
pass data packet to function (implied)
Yeaah!
*/
}
void ProcessSubPacket(SubPacket sdata){//convert subpacet to packet and process
Packet data;
data.sys=sys;
data.data=sdata.data;
data.signal=sdata.signal;
data.target=sdata.target;
data.t_data=sdata.t_data;
ProcessPacket(data);
}
};
struct Packet{
System*sys;
UINT signal;
vector <string> target;
vector <string> t_data;
vector <double> data;
Packet():sys(){sys=NULL;}
};
static DWORD WINAPI ClockThread(LPVOID lpParameter){//runs messenger thread with handle
Clock*clk = (Clock*)lpParameter;
//does nothing more than tick signals and qeue them
cout<<"Clock Thread Launched\n";
while (1!=2){
Sleep(1000);
clk->UpdateSec();
}
return 0;
}
static DWORD WINAPI MessengerThread(LPVOID lpParameter){//runs messenger thread with handle
messenger*msg = (messenger*)lpParameter;
cout<<"Messenger Thread Launched\n";
for (UINT objloop=0;!msg->sys->quit;objloop++){//process and delete accumulated information
if (msg->Cio->packetqeue.size()>0){msg->ProcessSubPacket(msg->Cio->packetqeue[0]);//process
msg->Cio->packetqeue.erase(msg->Cio->packetqeue.begin());//delete packet from qeue
}
if (msg->clk->qeue.size()>0){ msg->ProcessSubPacket(msg->clk->qeue[0]);
msg->clk->qeue.erase(msg->clk->qeue.begin());
}
objloop=0;//
}
return 0;
}
void StartMessenger(messenger*msg){
CreateThread(NULL,0,MessengerThread, msg,0,0);
delete msg;
msg=NULL;
}
void StartClock(Clock*clk){
CreateThread(NULL,0,ClockThread, clk,0,0);
delete clk;
clk=NULL;
}
};
using namespace sapa;
void main(){
//create primary system objects
System sys;
Clock clk;
messenger msg;
ConsoleIO Cio;
//Give messenger access to data
msg.setClock(&clk);
msg.setConsoleIO(&Cio);
msg.setSystem(&sys);
//starts running independant system threads threads
StartMessenger(&msg);
StartClock(&clk);
cout<<"SAPA developmental build:\n";
string usin;
while (!sys.quit){
getline(cin,usin);
Cio.ConsoleIn(usin);//insert into Cio
}
cout<<endl<<endl;
cout<<"Closing process threads\n\n";
system("pause");
}
About the program: It runs on 3 threads: A command driven thread, a timer thread, and a continous thread. The command driven thread accepts a user input, breaks down the input into several catergorized vectors (not shown in this sample) and then puts them into a 'subpacket' class stored in a vector qeue. The timer thread makes a 'subpacket' for every second, minute, hour, and day that passes, also adding to a qeue. The messenger class runs continously. It has a pointer to the timer and command classes. Its supposed to take an item from the qeue, add in the pointer to the 'system' class, and pass it off to a function.
I've tried all sorts of different methods, but i just can't seem to get the kind of memory access i want. Its supposed to eventually all go into a static library and act almost like managed code. Im almost thinking Java might be better suited for this. Think anyone can help?