Skip to content

Commit db63a6b

Browse files
committed
added option to delay start of thread in createthread utility and added tagged serialisable objects
1 parent 95046f7 commit db63a6b

7 files changed

Lines changed: 196 additions & 8 deletions

File tree

UnitTests/TaggedSerialise.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <iostream>
2+
#include <TaggedSerialisableObject.h>
3+
#include <SerialisableObject.h>
4+
5+
using namespace ToolFramework;
6+
7+
struct Bob : public TaggedSerialisableObject{
8+
9+
int a;
10+
std::vector<int> vec;
11+
12+
std::string GetType(){ return "bob";}
13+
unsigned int GetVersion(){ return 1;}
14+
bool Print(){
15+
std::cout<<a<<std::endl;
16+
return true;
17+
}
18+
bool Serialise(BinaryStream &bs){
19+
20+
bs & a;
21+
bs & vec;
22+
23+
return true;
24+
}
25+
26+
};
27+
28+
struct Bill : public TaggedSerialisableObject{
29+
30+
std::vector<int> vec;
31+
32+
std::string GetType(){ return "bill";}
33+
unsigned int GetVersion(){ return 1;}
34+
bool Print(){ return true;}
35+
bool Serialise(BinaryStream &bs){
36+
37+
bs & vec;
38+
39+
return true;
40+
}
41+
42+
};
43+
44+
int main(){
45+
46+
Bob bob;
47+
Bill bill;
48+
49+
BinaryStream bs;
50+
bs.Bopen("test", NEW, UNCOMPRESSED);
51+
52+
bob.a=23;
53+
bs & bob;
54+
55+
bill.vec.resize(1000);
56+
bs & bill;
57+
58+
bob.a= 46;
59+
bs & bob;
60+
61+
bs & bill;
62+
bs.Bclose();
63+
64+
std::cout<<"read"<<std::endl;
65+
66+
BinaryStream bs2;
67+
bs2.Bopen("test", READ, UNCOMPRESSED);
68+
69+
Tag tag;
70+
Bob bob2;
71+
72+
bs2.m_write = false;
73+
74+
while (bs2 & tag){
75+
tag.Print();
76+
if(tag.type !="bob"){
77+
tag.Skip(bs2);
78+
continue;
79+
}
80+
bs2 & bob2;
81+
bob2.Print();
82+
83+
}
84+
85+
bs2.Bclose();
86+
87+
88+
return 0;
89+
}

src/DataModelBase/Utilities.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Utilities::Utilities(){
77
}
88

99

10-
Thread_args* Utilities::CreateThread(std::string ThreadName, void (*func)(Thread_args*), Thread_args* args){
10+
Thread_args* Utilities::CreateThread(std::string ThreadName, void (*func)(Thread_args*), Thread_args* args, bool start){
1111

1212
if(Threads.count(ThreadName)==0){
1313

1414
if(args==0) args = new Thread_args();
1515

1616
args->ThreadName=ThreadName;
1717
args->func=func;
18-
args->running=true;
18+
args->running=start;
1919
args->kill=false;
2020

2121
pthread_create(&(args->thread), NULL, Utilities::Thread, args);

src/DataModelBase/Utilities.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace ToolFramework{
6060
public:
6161

6262
Utilities(); ///< Simple constructor
63-
Thread_args* CreateThread(std::string ThreadName, void (*func)(Thread_args*), Thread_args* args); ///< Create a thread with more complicated data exchange definned by arguments
63+
Thread_args* CreateThread(std::string ThreadName, void (*func)(Thread_args*), Thread_args* args, bool start=true); ///< Create a thread with more complicated data exchange definned by arguments
6464
bool KillThread(Thread_args* &args); ///< Kill a thread assosiated to args
6565
bool KillThread(std::string ThreadName); ///< Kill a thread by name
6666

src/Store/BinaryStream.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,26 @@ void BinaryStream::zerr(int ret){
452452
#endif
453453
}
454454

455+
size_t BinaryStream::Size(){
456+
457+
return buffer.size();
458+
459+
}
460+
461+
455462
bool SerialisableObject::SerialiseWrapper(BinaryStream &bs){
456463
// if(!m_serialise) return false; //not sure i should ahve a serialise flag, causes major issues with empty mapps and vector elements!!!! so remove. People wouldnt be calling serialise method if they didnt want to serialse
457-
//std::cout<<"in serialise wrapper"<<std::endl;
464+
/*
465+
std::cout<<"in serialise wrapper"<<std::endl;
458466
if(bs.m_write){
459-
if(!(bs << GetVersion())) return false;
467+
// if(!(bs << GetVersion())) return false;
460468
}
461469
else{
462470
std::string version="";
463471
if(!(bs >> version)) return false;
464472
if(version!=GetVersion()) return false;
465473
}
474+
*/
466475
return Serialise(bs);
467476
}
477+

src/Store/BinaryStream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ namespace ToolFramework{
3939
bool Print();
4040
bool Serialise(BinaryStream &bs);
4141
std::string GetVersion();
42-
42+
size_t Size();
43+
4344
enum_endpoint m_endpoint;
4445
FILE* pfile;
4546
#ifdef ZLIB

src/Store/SerialisableObject.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ namespace ToolFramework{
2222

2323
public:
2424
virtual ~SerialisableObject(){};
25-
bool SerialiseWrapper(BinaryStream &bs);
25+
virtual bool SerialiseWrapper(BinaryStream &bs);
2626
virtual bool Serialise(BinaryStream &bs)=0;
27-
virtual std::string GetVersion()=0;
2827
virtual bool Print()=0; ///< Simple virtual Pritn function to ensure inhereted classes have one
2928
// virtual ~SerialisableObject(){}; ///< Destructor
3029
//bool m_serialise; ///< Denotes if the calss should be serialised or not when added to a BoostStore.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef TAGGED_SERIALISABLE_OBJECT_H
2+
#define TAGGED_SERIALISABLE_OBJECT_H
3+
4+
#include <BinaryStream.h>
5+
#include <SerialisableObject.h>
6+
7+
8+
9+
//fixme use zie_t for sizes but have todo in binary stream as well;
10+
11+
namespace ToolFramework{
12+
13+
class Tag : public SerialisableObject{
14+
15+
public:
16+
17+
//unsigned int size = 0;
18+
size_t size = 0;
19+
std::string type = "";
20+
unsigned int version = 0;
21+
22+
bool Print(){
23+
24+
std::cout<<"size:type:version = "<<size<<":"<<type<<":"<<version<<std::endl;
25+
26+
return true;
27+
}
28+
29+
bool Serialise(BinaryStream &bs){
30+
31+
if(!(bs & type)) return false;
32+
if(!(bs & version)) return false;
33+
if(!(bs & size)) return false;
34+
35+
return true;
36+
}
37+
38+
void Skip(BinaryStream &bs){
39+
40+
bs.Bseek(size, SEEK_CUR);
41+
42+
}
43+
44+
45+
};
46+
47+
48+
class TaggedSerialisableObject : public SerialisableObject{
49+
50+
public:
51+
52+
virtual std::string GetType()=0;
53+
unsigned int GetSize(){
54+
55+
BinaryStream bs;
56+
Serialise(bs);
57+
return bs.Size();
58+
}
59+
virtual unsigned int GetVersion()=0;
60+
bool SerialiseWrapper(BinaryStream &bs){
61+
62+
if(bs.m_write){
63+
//size_t size= GetSize();
64+
unsigned int version = GetVersion();
65+
if(!(bs << GetType())) return false;
66+
if(!(bs << version)) return false;
67+
//if(!(bs << size)) return false;
68+
BinaryStream tmp_bs;
69+
Serialise(tmp_bs);
70+
return bs << tmp_bs;
71+
}
72+
73+
return Serialise(bs);
74+
75+
76+
}
77+
78+
79+
80+
private:
81+
82+
83+
84+
85+
};
86+
87+
}
88+
89+
#endif

0 commit comments

Comments
 (0)