diff --git a/CMakeLists.txt b/CMakeLists.txt index 2012b0b..40bb7b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,10 @@ add_subdirectory(include) #install(TARGETS cpplab RUNTIME DESTINATION bin) +# CPack instructions for bundling purposes set(CPACK_PACKAGE_CONTACT "Balhau") set(CPACK_GENERATOR "STGZ;TGZ;TZ;DEB") SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "CppLab") SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE.txt") +set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md") include(CPack) diff --git a/include/misc/CMakeLists.txt b/include/misc/CMakeLists.txt index f70a548..245716d 100644 --- a/include/misc/CMakeLists.txt +++ b/include/misc/CMakeLists.txt @@ -1 +1,2 @@ -add_subdirectory(memory) \ No newline at end of file +add_subdirectory(memory) +add_subdirectory(util) \ No newline at end of file diff --git a/include/misc/memory/model.h b/include/misc/memory/model.h new file mode 100644 index 0000000..faf190a --- /dev/null +++ b/include/misc/memory/model.h @@ -0,0 +1,63 @@ +#pragma once + +#include +#include "../util/debug.h" + + +template struct SmartPointer{ + Object *ref; + int counter; + ~SmartPointer(){ + print("SmartPointer Desctructor called") + if(counter==0){ + delete ref; + } + } +}; + +template +using SPointer = struct SmartPointer; + +template +class Box +{ +public: + Box(Object *); //constructor + ~Box(); //destructor + Box(const Box&); //copy constructor + Object *operator->(); + Object *operator*(); //deref operator + int getRefCounter(){ + return this->sref->counter; + }; + friend std::ostream &operator<<(std::ostream &stream, Box const &c) + { + return stream << "Boxed[v=" << *c.sref->ref << ",r=" << c.sref->ref << "]"; + } + +private: + SPointer* sref; +}; + +template +class Point2D +{ +private: + Number _x; + Number _y; + +public: + Point2D(Number x, Number y) : _x(x), _y(y){ + print("Point2D default constructor called: " << this)}; //Default constructor + Point2D(const Point2D &); //Copy constructor + Point2D(Point2D &&); //Move constructor + ~Point2D(); + //Destructor + friend std::ostream &operator<<(std::ostream &stream, Point2D const &c) + { + return stream << "(" << c._x << "," << c._y << ")"; + } + + Number getX() { return _x; } + Number getY() { return _y; } +}; \ No newline at end of file diff --git a/include/misc/memory/smartpointer.h b/include/misc/memory/smartpointer.h deleted file mode 100644 index 07b537f..0000000 --- a/include/misc/memory/smartpointer.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -class DummyObject { - public: - DummyObject(int); //Constructor - ~DummyObject(); //destructor - DummyObject(const DummyObject&); //copy constructor - DummyObject(DummyObject&&); //move constructor - - void doSomething(); - - - private: - int size; - int* list_of_numbers; - -}; \ No newline at end of file diff --git a/include/misc/util/CMakeLists.txt b/include/misc/util/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/include/misc/util/debug.h b/include/misc/util/debug.h new file mode 100644 index 0000000..08ed307 --- /dev/null +++ b/include/misc/util/debug.h @@ -0,0 +1,9 @@ +#pragma once + +#define DEBUG_MODE + +#ifdef DEBUG_MODE + #define print(x) std::cout << x << std::endl; +#else + #define print(x) +#endif \ No newline at end of file diff --git a/src/bmath/complex.cpp b/src/bmath/complex.cpp index 238ace3..e9123c9 100644 --- a/src/bmath/complex.cpp +++ b/src/bmath/complex.cpp @@ -40,9 +40,9 @@ BMath::Complex BMath::Complex::operator+(const Complex &rightOp) this->imaginary + rightOp.imaginary); }; -BMath::Complex BMath::Complex::operator*(const Complex &rigthOp) +BMath::Complex BMath::Complex::operator*(const Complex &rightOp) { return Complex( - this->real * rigthOp.real - this->imaginary * rigthOp.imaginary, - this->imaginary * rigthOp.real + this->real * rigthOp.imaginary); + this->real * rightOp.real - this->imaginary * rightOp.imaginary, + this->imaginary * rightOp.real + this->real * rightOp.imaginary); }; diff --git a/src/misc/memory/CMakeLists.txt b/src/misc/memory/CMakeLists.txt index 919ea12..e124e94 100644 --- a/src/misc/memory/CMakeLists.txt +++ b/src/misc/memory/CMakeLists.txt @@ -1,4 +1,2 @@ -add_executable( - spointer - smartpointer.cpp -) +add_executable(memorymodel model.cpp) +add_executable(volatile volatile.cpp) diff --git a/src/misc/memory/model.cpp b/src/misc/memory/model.cpp new file mode 100644 index 0000000..eb8f9a1 --- /dev/null +++ b/src/misc/memory/model.cpp @@ -0,0 +1,99 @@ +#include "../../../include/misc/memory/model.h" +#include "../../../include/misc/util/debug.h" +#include + +#define UPtr std::unique_ptr +#define SPtr std::shared_ptr + +template +Box::Box(Object *ref) +{ + this->sref = new SPointer{ref,0}; + this->sref->ref=ref; + print("Box constructor called: " << this) +} + +template +Box::Box(const Box &other){ + print("Box Copy constructor: " << this) + this->sref=new SPointer{other.sref->ref,other.sref->counter+1}; +} + +template +Object* Box::operator->() +{ + return this->ref; +} + +template +Object* Box::operator*(){ + return this->sref->ref; +} + +template +Box::~Box() +{ + print("Box Destructor called: " << this) delete this->sref; +} + +//Copy constructor +template +Point2D::Point2D(const Point2D &other) +{ + print("Point2D copy constructor called: " << this) this->_x = other._x; + this->_y = other._y; +} +//Move constructor +template +Point2D::Point2D(Point2D &&other) +{ + print("Point2D move constructor called: " << this) +} + +//Destructor +template +Point2D::~Point2D() +{ + print("Point2D destructor called: " << this) +} + +template +void printPointByValue(Point2D point) +{ + print(point << " : " << &point) +} + +template +void printPointByRef(Point2D *point){ + print(*point << ": " << point) +} + +template +void printBoxContent(Box box){ + print("Box: Content: " << *box << ":" << box << ":" << box.getRefCounter()) +} + +int main(void) +{ + print("Stack memory management") + Point2D p1={1,2}; + printPointByValue(p1); + print("") + + print("Heap memory management") + Point2D *p2 = new Point2D{3, 4}; + printPointByRef(p2); + delete p2; + print("") + + print("Heap/Stack memory management") + Point2D *ptrPt3 = new Point2D(10,20); + Box> box1(ptrPt3); + print(box1) + print("")print("") + + printBoxContent(box1); + Box> box2=box1; + printBoxContent(box2); + +} \ No newline at end of file diff --git a/src/misc/memory/smartpointer.cpp b/src/misc/memory/smartpointer.cpp deleted file mode 100644 index 0816ec4..0000000 --- a/src/misc/memory/smartpointer.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "../../../include/misc/memory/smartpointer.h" -#include - -#define UPtr std::unique_ptr -#define SPtr std::shared_ptr - -DummyObject::DummyObject(int num_elements) -{ - std::cout << "Constructor called " << num_elements << std::endl; - size = num_elements; - list_of_numbers = new int[size]; -} - -DummyObject::~DummyObject() -{ - std::cout << "Destructor called " << size << std::endl; - delete[] list_of_numbers; -} - -DummyObject::DummyObject(const DummyObject &other) -{ - std::cout << "Copy constructor called " << other.size << std::endl; - list_of_numbers = new int[other.size]; - for (int i = 0; i < other.size; i++) - { - list_of_numbers[i] = other.list_of_numbers[i]; - } - size = other.size; -} - -DummyObject::DummyObject(DummyObject&& moved) -{ - std::cout << "Move constructor called " << moved.size << std::endl; -} - -void DummyObject::doSomething() -{ - std::cout << "This do amazing stuff: " << size << std::endl; -} - -DummyObject createDummy(int size) -{ - DummyObject d(size); - return d; -} - -int main(void) -{ - std::cout << "Smart pointer examples" << std::endl; - - DummyObject d(15); - DummyObject copy = d; - - DummyObject *dmtPtr = new DummyObject(20); - UPtr dmySmtPtr = UPtr(new DummyObject(30)); - - dmySmtPtr->doSomething(); - dmtPtr->doSomething(); - - SPtr firstSmtPtr = SPtr(dmtPtr); - SPtr secondSmtPtr = firstSmtPtr; - - DummyObject retDummy = createDummy(32); - DummyObject secondDummy = std::move(retDummy); - - retDummy.doSomething(); - secondDummy.doSomething(); - - firstSmtPtr->doSomething(); - secondSmtPtr->doSomething(); -} \ No newline at end of file diff --git a/src/misc/memory/volatile.cpp b/src/misc/memory/volatile.cpp new file mode 100644 index 0000000..b72debe --- /dev/null +++ b/src/misc/memory/volatile.cpp @@ -0,0 +1,22 @@ +#include + +inline unsigned long getTimestamp(){ + unsigned hi,lo; + asm ("rdtsc": "=a" (lo), "=d" (hi) ); + return ((unsigned long) lo )| ((unsigned long) hi << 32); +} + +inline unsigned long getTimestampVolatile(){ + unsigned hi,lo; + asm volatile("rdtsc": "=a" (lo), "=d" (hi) ); + return ((unsigned long) lo )| ((unsigned long) hi << 32); +} + +int main(void){ + unsigned long t1=getTimestamp(); + unsigned long t2=getTimestamp(); + std::cout << "Hello 1 -- " << t1 < +#include +#include +#include +#include + +#define PT 1 +#define STROKE 2 +#define END 3 + +typedef struct charpoint +{ + GLfloat x, y; + int type; +} CP; + +CP Adata[] = { + {0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}}; + +CP Edata[] = { + {5, 0, PT}, {0, 0, PT}, {0, 10, PT}, {5, 10, STROKE}, {0, 5, PT}, {4, 5, END}}; + +CP Pdata[] = { + {0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, {4, 5, PT}, {0, 5, END}}; + +CP Rdata[] = { + {0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, {4, 5, PT}, {0, 5, STROKE}, {3, 5, PT}, {5, 0, END}}; + +CP Sdata[] = { + {0, 1, PT}, {1, 0, PT}, {4, 0, PT}, {5, 1, PT}, {5, 4, PT}, {4, 5, PT}, {1, 5, PT}, {0, 6, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT}, {5, 9, END}}; + +/* drawLetter() interprets the instructions from the array + * for that letter and renders the letter with line segments. + */ +static void drawLetter(CP *l) +{ + glBegin(GL_LINE_STRIP); + while (1) + { + switch (l->type) + { + case PT: + glVertex2fv(&l->x); + break; + case STROKE: + glVertex2fv(&l->x); + glEnd(); + glBegin(GL_LINE_STRIP); + break; + case END: + glVertex2fv(&l->x); + glEnd(); + glTranslatef(8.0, 0.0, 0.0); + return; + } + l++; + } +} + +/* Create a display list for each of 6 characters */ +static void init(void) +{ + GLuint base; + + glShadeModel(GL_FLAT); + + base = glGenLists(128); + glListBase(base); + glNewList(base + 'A', GL_COMPILE); + drawLetter(Adata); + glEndList(); + glNewList(base + 'E', GL_COMPILE); + drawLetter(Edata); + glEndList(); + glNewList(base + 'P', GL_COMPILE); + drawLetter(Pdata); + glEndList(); + glNewList(base + 'R', GL_COMPILE); + drawLetter(Rdata); + glEndList(); + glNewList(base + 'S', GL_COMPILE); + drawLetter(Sdata); + glEndList(); + glNewList(base + ' ', GL_COMPILE); + glTranslatef(8.0, 0.0, 0.0); + glEndList(); +} + +const char *test1 = "A SPARE SERAPE APPEARS AS"; +const char *test2 = "APES PREPARE RARE PEPPERS"; + +static void printStrokedString(char *s) +{ + GLsizei len = strlen(s); + glCallLists(len, GL_BYTE, (GLbyte *)s); +} + +void display(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + glColor3f(1.0, 1.0, 1.0); + glPushMatrix(); + glScalef(2.0, 2.0, 2.0); + glTranslatef(10.0, 30.0, 0.0); + printStrokedString((char *)test1); + glPopMatrix(); + glPushMatrix(); + glScalef(2.0, 2.0, 2.0); + glTranslatef(10.0, 13.0, 0.0); + printStrokedString((char *)test2); + glPopMatrix(); + glFlush(); +} + +void reshape(int w, int h) +{ + glViewport(0, 0, (GLsizei)w, (GLsizei)h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h); +} + +void keyboard(unsigned char key, int x, int y) +{ + switch (key) + { + case ' ': + glutPostRedisplay(); + break; + case 27: + exit(0); + } +} + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + glutInitWindowSize(440, 120); + glutCreateWindow(argv[0]); + init(); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyboard); + glutDisplayFunc(display); + glutMainLoop(); + return 0; +} \ No newline at end of file diff --git a/src/opengl/gl3.cpp b/src/opengl/gl3.cpp new file mode 100644 index 0000000..0789f38 --- /dev/null +++ b/src/opengl/gl3.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include + +GLubyte space[] = + {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +G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, 0xcf, 0xdf, 0xdb, 0xfb, 0xf3, 0xf3, 0xe3, 0xe3}, + {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x7e}, + {0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, + {0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c}, + {0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, + {0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e}, + {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff}, + {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, + {0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, + {0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, + {0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, + {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, + {0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff}}; + +GLuint fontOffset; + +void makeRasterFont(void) +{ + GLuint i, j; + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + fontOffset = glGenLists(128); + for (i = 0,j = 'A'; i < 26; i++,j++) { + glNewList(fontOffset + j, GL_COMPILE); + glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]); + glEndList(); +} +glNewList(fontOffset + ' ', GL_COMPILE); +glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space); +glEndList(); +} + +void init(void) +{ + glShadeModel(GL_FLAT); + makeRasterFont(); +} + +void printString(char *s) +{ + glPushAttrib(GL_LIST_BIT); + glListBase(fontOffset); + glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *)s); + glPopAttrib(); +} + +/* Everything above this line could be in a library + * that defines a font. To make it work, you've got + * to call makeRasterFont() before you start making + * calls to printString(). + */ +void display(void) +{ + GLfloat white[3] = {1.0, 1.0, 1.0}; + const char* p1="LINDA MERDA"; + const char* p2="PUTA MERDA"; + + glClear(GL_COLOR_BUFFER_BIT); + glColor3fv(white); + + glRasterPos2i(20, 60); + printString((char*)p1); + glRasterPos2i(20, 40); + printString((char*)p2); + glFlush(); +} + +void reshape(int w, int h) +{ + glViewport(0, 0, (GLsizei)w, (GLsizei)h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0.0, w, 0.0, h, -1.0, 1.0); + glMatrixMode(GL_MODELVIEW); +} + +void keyboard(unsigned char key, int x, int y) +{ + switch (key) + { + case 27: + exit(0); + } +} + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + glutInitWindowSize(300, 100); + glutInitWindowPosition(100, 100); + glutCreateWindow(argv[0]); + init(); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyboard); + glutDisplayFunc(display); + glutMainLoop(); + return 0; +} \ No newline at end of file