From 82407c1c4c9526e7dc408e936478c8619ded8c66 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 11 May 2015 18:39:56 +0200 Subject: Fix up makefile and add functional insert and find implementation --- Makefile | 14 +++++++++++++- Maybe.h | 21 +++++++++++++++++++++ engine.cpp | 11 ++++++++--- enginedata.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++-------- enginedata.h | 24 +++++++++++++++++------- 5 files changed, 108 insertions(+), 19 deletions(-) create mode 100644 Maybe.h diff --git a/Makefile b/Makefile index 1f83663..eb34638 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ CFLAGS=-Wall -O2 CPPFLAGS=-Wall -O2 -std=c++11 BINARIES=engine +OBJS=enginedata.o +DEPFILES=$(OBJS:.o=.d) .PHONY: all clean @@ -11,8 +13,18 @@ BINARIES=engine all: $(BINARIES) clean: - rm $(BINARIES) + rm -f $(BINARIES) $(OBJS) $(DEPFILES) + + +-include $(OBJS:.o=.d) engine: engine.cpp enginedata.o $(CXX) $(CPPFLAGS) -o $@ $^ + +#enginedata: enginedata.cpp enginedata.h +# $(CXX) $(CPPFLAGS) -o %@ enginedata.cpp + +%.o: %.cpp + $(CXX) $(CPPFLAGS) $*.cpp -c -o $*.o + $(CXX) -MM $(CPPFLAGS) $*.cpp > $*.d diff --git a/Maybe.h b/Maybe.h new file mode 100644 index 0000000..df12478 --- /dev/null +++ b/Maybe.h @@ -0,0 +1,21 @@ +#pragma once + +template +class Maybe{ + typedef struct { + T v; + } t_v_wrapper; + + bool is_something; + t_v_wrapper *wr; +public: + Maybe(void):is_something(false),wr(nullptr){} + Maybe(const T &_v):is_something(true),wr(new t_v_wrapper({_v})){} + + operator bool() const{return this->is_something;} + + T value(void){ + if(is_something)return wr->v; + throw "Nothing"; + } +}; diff --git a/engine.cpp b/engine.cpp index dd0f5f4..bc4165a 100644 --- a/engine.cpp +++ b/engine.cpp @@ -2,7 +2,9 @@ #include #include #include +#include #include "enginedata.h" +#include "Maybe.h" using namespace std; @@ -18,11 +20,14 @@ int main(int argc,char **argv){ tables.emplace("hoi",Table("hoi",3,header)); Table *hoitb=&tables.at("hoi"); Row row(3,hoitb); - row.items[0].u.rh_int32=42; - row.items[1].u.rh_uint32=42; + row.items[0].u.rh_int32=UINT_MAX; + row.items[1].u.rh_uint32=UINT_MAX; row.items[2].u.rh_bytes=new unsigned char[10]; strcpy((char*)row.items[2].u.rh_bytes,"hallo daar"); hoitb->insert(row); - cout<<"hoitb has "<rows.size()<<" row"<<(hoitb->rows.size()==1?"":"s")< found=hoitb->find(serialise((int32_t)-1)); + cout<<"hoitb has "<rows.size()<<" row"<<(hoitb->rows.size()==1?"":"s")<<'.'< +#include using namespace std; -Row::Row(const int nc,const Table *const _t):table(_t){ +Row::Row(const int _nc,const Table *const _t):nc(_nc),table(_t){ items=new RowItem[nc]; } Row::~Row(void){ if(items)delete[] items; } -Row::Row(Row &&other):table(other.table),items(other.items){ //move constr - other.table=NULL; - other.items=NULL; +Row::Row(Row &&other):nc(other.nc),table(other.table),items(other.items){ //move constr + other.table=nullptr; + other.items=nullptr; } -Row::Row(const Row &other):table(other.table){ //copy constr +Row::Row(const Row &other):nc(other.nc),table(other.table){ //copy constr items=new RowItem[table->nc]; memcpy(items,other.items,table->nc*sizeof(RowItem)); } +ostream& operator<<(ostream &os,const Row &r){ + os<<'['; + for(int i=0;iheader[i],r.items[i]); + } + os<<']'; + return os; +} //copies from _hd, so you can freely delete or edit the passed colheader array @@ -27,12 +38,42 @@ Table::~Table(void){ if(header)delete[] header; } Table::Table(Table &&other):name(move(other.name)),nc(other.nc),header(other.header),rows(move(other.rows)){ - other.header=NULL; + other.header=nullptr; } void Table::insert(Row &&row){ - rows.push_back(move(row)); + string key=serialise(this->header[0],row.items[0]); + cerr<<"Inserting into table '"<name<<"' key="<name<<"' key="< rows; + map rows; //map key is serialised version of the first column Table(const string &_n,const int _nc,const ColHeader *const _hd); ~Table(void); @@ -49,4 +58,5 @@ struct Table{ void insert(Row &&row); void insert(Row &row); + Maybe find(const string &key); }; -- cgit v1.2.3-70-g09d2