From 1b49cd3ba08c199364a05087f4a0d6af4ff2961f Mon Sep 17 00:00:00 2001 From: "Frank B. Brokken" Date: Wed, 1 Apr 2020 11:25:23 +0200 Subject: [PATCH] added initializer_list example --- annotations/yo/stl/examples/inilist/CLASSES | 1 + annotations/yo/stl/examples/inilist/data.cc | 6 +++ annotations/yo/stl/examples/inilist/frame | 1 + annotations/yo/stl/examples/inilist/icmconf | 16 +++++++ .../yo/stl/examples/inilist/inilist/check.cc | 11 +++++ .../yo/stl/examples/inilist/inilist/data.cc | 5 +++ .../examples/inilist/inilist/destructor.cc | 17 ++++++++ .../yo/stl/examples/inilist/inilist/frame | 6 +++ .../yo/stl/examples/inilist/inilist/inilist.h | 28 ++++++++++++ .../stl/examples/inilist/inilist/inilist.ih | 6 +++ .../stl/examples/inilist/inilist/inilist1.cc | 15 +++++++ .../stl/examples/inilist/inilist/inilist2.cc | 13 ++++++ annotations/yo/stl/examples/inilist/main.cc | 43 +++++++++++++++++++ annotations/yo/stl/examples/inilist/main.ih | 9 ++++ .../yo/stl/examples/inilist/xerr/xerr.ih | 19 ++++++++ 15 files changed, 196 insertions(+) create mode 100644 annotations/yo/stl/examples/inilist/CLASSES create mode 100644 annotations/yo/stl/examples/inilist/data.cc create mode 100644 annotations/yo/stl/examples/inilist/frame create mode 100644 annotations/yo/stl/examples/inilist/icmconf create mode 100644 annotations/yo/stl/examples/inilist/inilist/check.cc create mode 100644 annotations/yo/stl/examples/inilist/inilist/data.cc create mode 100644 annotations/yo/stl/examples/inilist/inilist/destructor.cc create mode 100644 annotations/yo/stl/examples/inilist/inilist/frame create mode 100644 annotations/yo/stl/examples/inilist/inilist/inilist.h create mode 100644 annotations/yo/stl/examples/inilist/inilist/inilist.ih create mode 100644 annotations/yo/stl/examples/inilist/inilist/inilist1.cc create mode 100644 annotations/yo/stl/examples/inilist/inilist/inilist2.cc create mode 100644 annotations/yo/stl/examples/inilist/main.cc create mode 100644 annotations/yo/stl/examples/inilist/main.ih create mode 100644 annotations/yo/stl/examples/inilist/xerr/xerr.ih diff --git a/annotations/yo/stl/examples/inilist/CLASSES b/annotations/yo/stl/examples/inilist/CLASSES new file mode 100644 index 00000000..ffa046b1 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/CLASSES @@ -0,0 +1 @@ +inilist diff --git a/annotations/yo/stl/examples/inilist/data.cc b/annotations/yo/stl/examples/inilist/data.cc new file mode 100644 index 00000000..abe98678 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/data.cc @@ -0,0 +1,6 @@ +#include "main.ih" + +unordered_map map +{ + {1, IniList{} } +}; diff --git a/annotations/yo/stl/examples/inilist/frame b/annotations/yo/stl/examples/inilist/frame new file mode 100644 index 00000000..cea3988b --- /dev/null +++ b/annotations/yo/stl/examples/inilist/frame @@ -0,0 +1 @@ +#include "main.ih" diff --git a/annotations/yo/stl/examples/inilist/icmconf b/annotations/yo/stl/examples/inilist/icmconf new file mode 100644 index 00000000..a9bcadb8 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/icmconf @@ -0,0 +1,16 @@ +#define CLS +#define MAIN "main.cc" +#define SOURCES "*.cc" +#define OBJ_EXT ".o" +#define TMP_DIR "tmp" +#define USE_ECHO ON +#define IH ".ih" +#define CXX "g++" +#define CXXFLAGS " --std=c++2a -Wall -O2 " \ + " -fdiagnostics-color=never " +#define REFRESH +#define LDFLAGS "-s" +#define ADD_LIBRARIES "" +#define ADD_LIBRARY_PATHS "" + +#define DEFCOM "program" diff --git a/annotations/yo/stl/examples/inilist/inilist/check.cc b/annotations/yo/stl/examples/inilist/inilist/check.cc new file mode 100644 index 00000000..cede793e --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/check.cc @@ -0,0 +1,11 @@ +//#define XERR +#include "inilist.ih" + +void IniList::check(char const *label) +{ + cout << label << "object " << d_id << ": memory owned by " << d_owner << + ". Memory " << + ( + s_memory[d_owner] ? "available" : "NOT AVAILABLE" + ) << '\n'; +} diff --git a/annotations/yo/stl/examples/inilist/inilist/data.cc b/annotations/yo/stl/examples/inilist/inilist/data.cc new file mode 100644 index 00000000..e544e835 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/data.cc @@ -0,0 +1,5 @@ +//#define XERR +#include "inilist.ih" + +size_t IniList::s_count = 0; + diff --git a/annotations/yo/stl/examples/inilist/inilist/destructor.cc b/annotations/yo/stl/examples/inilist/inilist/destructor.cc new file mode 100644 index 00000000..4dcd6a49 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/destructor.cc @@ -0,0 +1,17 @@ +//#define XERR +#include "inilist.ih" + +IniList::~IniList() +{ + if (d_id == d_owner) + { + s_memory[d_index] = false; + cout << "destroying object " << d_id << " and memory[" << + d_index << "]\n"; + } + else + { + cout << "destroying object " << d_id << ".\n"; + check(""); + } +} diff --git a/annotations/yo/stl/examples/inilist/inilist/frame b/annotations/yo/stl/examples/inilist/inilist/frame new file mode 100644 index 00000000..4c2f63f4 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/frame @@ -0,0 +1,6 @@ +//#define XERR +#include "inilist.ih" + +IniList:: +{ +} diff --git a/annotations/yo/stl/examples/inilist/inilist/inilist.h b/annotations/yo/stl/examples/inilist/inilist/inilist.h new file mode 100644 index 00000000..3c03f5c0 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/inilist.h @@ -0,0 +1,28 @@ +#ifndef INCLUDED_INILIST_ +#define INCLUDED_INILIST_ + +#include + +class IniList +{ + static size_t s_count; // counts the # of objects: used as + // object ID + static std::vector s_memory; // values 'true' indicate available + // memory. Each constructed obj. has + // its own index; copied objects use + // their 'other' index value + + size_t d_id; // using s_count + size_t d_owner; // owner's ID: the owner deletes the memory + size_t d_index; // index into s_memory + + public: + IniList(); + IniList(IniList const &other); + ~IniList(); + + void check(char const *label); // checks the availability of its + // memory +}; + +#endif diff --git a/annotations/yo/stl/examples/inilist/inilist/inilist.ih b/annotations/yo/stl/examples/inilist/inilist/inilist.ih new file mode 100644 index 00000000..45c98bdd --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/inilist.ih @@ -0,0 +1,6 @@ +#include "inilist.h" + +#include + +using namespace std; + diff --git a/annotations/yo/stl/examples/inilist/inilist/inilist1.cc b/annotations/yo/stl/examples/inilist/inilist/inilist1.cc new file mode 100644 index 00000000..47c28476 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/inilist1.cc @@ -0,0 +1,15 @@ +//#define XERR +#include "inilist.ih" + +IniList::IniList() +: + d_id(s_count++), // set the object ID + d_owner(d_id), // this is the owner of the memory + d_index(s_memory.size()) // and the index of its memory element +{ + s_memory.push_back(true); + + cout << "created object " << d_id << " using memory[" << d_index << "]\n"; +} + + diff --git a/annotations/yo/stl/examples/inilist/inilist/inilist2.cc b/annotations/yo/stl/examples/inilist/inilist/inilist2.cc new file mode 100644 index 00000000..c0930be5 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/inilist/inilist2.cc @@ -0,0 +1,13 @@ +//#define XERR +#include "inilist.ih" + +IniList::IniList(IniList const &other) +: + d_id(s_count++), // set the object ID + d_owner(other.d_owner), // this object doesn't own the memory + d_index(other.d_index) // and use other's memory index +{ + cout << "CC: object " << d_id << " using memory[" << d_index << + "], owned by object " << d_owner << '\n'; + check("copy: "); +} diff --git a/annotations/yo/stl/examples/inilist/main.cc b/annotations/yo/stl/examples/inilist/main.cc new file mode 100644 index 00000000..54ebe967 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/main.cc @@ -0,0 +1,43 @@ +//#define XERR +#include "main.ih" + +vector IniList::s_memory; + +IniList factory(IniList const &src) +{ + cout << "factor(IniList const &)\n"; + return src; +} + +int main(int argc, char **argv) +{ + cout << "\n" + "starting main, after constructing 'map'\n" + "\n"; + + IniList i1; + + { + IniList i2(i1); + i2.check("copy: "); + } + + i1.check("check: "); + + cout << '\n'; + + IniList i0(factory(IniList{})); // i0 has lost access to its memory! + i0.check("via factory: "); + + cout << "\n" + "Now the map:\n" + "\n"; + + map.find(1)->second.check("element from map: "); +} + + + + + + diff --git a/annotations/yo/stl/examples/inilist/main.ih b/annotations/yo/stl/examples/inilist/main.ih new file mode 100644 index 00000000..715c0616 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/main.ih @@ -0,0 +1,9 @@ +#include +#include + +#include "inilist/inilist.h" +#include "xerr/xerr.ih" + +using namespace std; + +extern unordered_map map; diff --git a/annotations/yo/stl/examples/inilist/xerr/xerr.ih b/annotations/yo/stl/examples/inilist/xerr/xerr.ih new file mode 100644 index 00000000..64ce5436 --- /dev/null +++ b/annotations/yo/stl/examples/inilist/xerr/xerr.ih @@ -0,0 +1,19 @@ +// define X to activate the xerr/xerr2 macros: +// xerr(insertion) +// inserts the '<<' concatenated elements into std::cerr +// preceded by the name of the source file, and ended by '\n' +// xerr2(insertion, code) +// performs the insertion if X is defined, and (unconditionally) +// executes the statement(s) in `code'. `code' must be valid +// C(++) code. +// +#ifdef XERR + #include + #define xerr(insertion) std::cerr << __FILE__": " << insertion << '\n' + #define xerr2(insertion, b) \ + { std::cerr << __FILE__": " << insertion << '\n'; b; } +#else + #define xerr(insertion) + #define xerr2(insertion, b) b +#endif +