//Shmem_test.cpp


#include <conio.h>
#include <stdio.h>
#include <DML_child.h>
#include <DML_shmem.h>
#include "shmem_child.h"


dummy::goForIt()
{
	i++;
	j--;
}


class dummy2 : public dummybase
{
  public:
  	goForIt();
};

dummy2::goForIt()
{
	i*=2;
	j*=3;
}

void main(void)
{
    ShMem<dummy> *  Foo = new("foo") ShMem<dummy>;
	ShMem<dummy2> * Foo2 = new("foo") ShMem<dummy2>;

//	ShMem<int> *testInt = new("test_int") ShMem<int>;	//this crashes! (like it's supposed to... good thing!)
		
	dummybase *FooAbstract;
	
    Foo->i = 10;
    Foo->j = 15;


    printf("initial i,j: %d, %d\n",Foo->i, Foo->j);
	printf("initial q,r: %d, %d\n",Foo2->i, Foo2->j);
	printf("virtual function address: %x\n", Foo->goForIt);
	printf("virtual function address 2: %x\n", Foo2->goForIt);
	printf("destructor address: %x\n", ShMem<dummy>::~ShMem);
	printf("destructor address 2: %x\n", ShMem<dummy2>::~ShMem); 
    printf("hit a key to start the child.\n");    
 

    getch();        
    DML_child child("shmem_child", WAIT_RESPONSE);

    printf("i,j value after child: %d, %d\n",Foo->i, Foo->j);
	printf("virtual function address: %x\n", Foo->goForIt);
	printf("virtual destructor address: %x\n", ShMem<dummy>::~ShMem);
	printf("press a key to unlink Foo in shmem_test and re-link.\n");

    getch();
	delete Foo;
	Foo = new("foo") ShMem<dummy>;

    printf("i,j value after unlink/remap: %d, %d\n",Foo->i,Foo->j);
	printf("press a key to change the values:\n");

	getch();
	Foo->goForIt();

	printf(" i,j: %d, %d\n", Foo->i, Foo->j);
	printf("press a key to change values using dummy2 (2*, 3*)\n");
	
	getch();
	Foo2->goForIt();
	printf(" i,j: %d, %d\n", Foo->i, Foo->j);

	printf("press a key to change values using the abstract foo (1 then 2)\n");
	getch();
	
	FooAbstract = Foo;
	FooAbstract->goForIt();
	printf(" i,j: %d, %d\n", Foo->i, Foo->j);
	FooAbstract = Foo2;
	FooAbstract->goForIt();
	printf(" i,j: %d, %d\n", Foo->i, Foo->j);
	
	printf("now press a key to have the child delete and re-make it\n");

	getch();
	child.kick();
	
	printf("now press a key to have the child change the values\n");
	
    while(getch() != 'q')
    {
		FooAbstract->goForIt();
		child.kick();
    }

	destroy_shm("foo");	
}
