From 8b92935b1e2964889d76fe64374be1ab3b040027 Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Sun, 6 Nov 2022 15:01:47 +0100 Subject: Add Fl_Int_Vector: "an STL-ish vector without templates" Code copied from Fl_Table and reformatted. Thanks to Greg for the original code. --- FL/Fl_Int_Vector.H | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 1 + src/Fl_Int_Vector.cxx | 43 ++++++++++++++++++++++++++ src/Makefile | 1 + 4 files changed, 128 insertions(+) create mode 100644 FL/Fl_Int_Vector.H create mode 100644 src/Fl_Int_Vector.cxx diff --git a/FL/Fl_Int_Vector.H b/FL/Fl_Int_Vector.H new file mode 100644 index 000000000..1ab092a18 --- /dev/null +++ b/FL/Fl_Int_Vector.H @@ -0,0 +1,83 @@ +// +// An STL-ish vector without templates for the Fast Light Tool Kit (FLTK). +// +// Copyright 2002 by Greg Ercolano. +// Copyright 2022 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#ifndef Fl_Int_Vector_H +#define Fl_Int_Vector_H + +#include + +class FL_EXPORT Fl_Int_Vector { + int *arr_; + unsigned int size_; + void init() { + arr_ = 0; + size_ = 0; + } + void copy(int *newarr, unsigned int newsize); + +public: + Fl_Int_Vector() { + init(); + } + + ~Fl_Int_Vector(); + + // copy constructor + Fl_Int_Vector(Fl_Int_Vector &o) { + init(); + copy(o.arr_, o.size_); + } + + // assignment operator + Fl_Int_Vector &operator=(Fl_Int_Vector &o) { + init(); + copy(o.arr_, o.size_); + return *this; + } + + int operator[](int x) const { + return arr_[x]; + } + + int &operator[](int x) { + return arr_[x]; + } + + unsigned int size() { + return size_; + } + + void size(unsigned int count); + + int pop_back() { + int tmp = arr_[size_ - 1]; + size_--; + return tmp; + } + + void push_back(int val) { + unsigned int x = size_; + size(size_ + 1); + arr_[x] = val; + } + + int back() { + return arr_[size_ - 1]; + } +}; + +#endif // Fl_Int_Vector_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bfeb13c56..c5a82bcc8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,6 +48,7 @@ set (CPPFILES Fl_Input.cxx Fl_Input_.cxx Fl_Input_Choice.cxx + Fl_Int_Vector.cxx Fl_Light_Button.cxx Fl_Menu.cxx Fl_Menu_.cxx diff --git a/src/Fl_Int_Vector.cxx b/src/Fl_Int_Vector.cxx new file mode 100644 index 000000000..361dd2634 --- /dev/null +++ b/src/Fl_Int_Vector.cxx @@ -0,0 +1,43 @@ +// +// An STL-ish vector without templates for the Fast Light Tool Kit (FLTK). +// +// Copyright 2002 by Greg Ercolano. +// Copyright 2022 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#include +#include + +void Fl_Int_Vector::copy(int *newarr, unsigned int newsize) { + size(newsize); + memcpy(arr_, newarr, newsize * sizeof(int)); +} + +Fl_Int_Vector::~Fl_Int_Vector() { + if (arr_) + free(arr_); +} + +void Fl_Int_Vector::size(unsigned int count) { + if (count <= 0) { + if (arr_) + free(arr_); + arr_ = 0; + size_ = 0; + return; + } + if (count > size_) { + arr_ = (int *)realloc(arr_, count * sizeof(int)); + size_ = count; + } +} diff --git a/src/Makefile b/src/Makefile index cb3421414..0f820287e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -51,6 +51,7 @@ CPPFILES = \ Fl_Input.cxx \ Fl_Input_.cxx \ Fl_Input_Choice.cxx \ + Fl_Int_Vector.cxx \ Fl_Light_Button.cxx \ Fl_Menu.cxx \ Fl_Menu_.cxx \ -- cgit v1.2.3