added initializer_list example

This commit is contained in:
Frank B. Brokken 2020-04-01 11:25:23 +02:00
parent b0a92c434a
commit 1b49cd3ba0
15 changed files with 196 additions and 0 deletions

View file

@ -0,0 +1 @@
inilist

View file

@ -0,0 +1,6 @@
#include "main.ih"
unordered_map<size_t, IniList> map
{
{1, IniList{} }
};

View file

@ -0,0 +1 @@
#include "main.ih"

View file

@ -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"

View file

@ -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';
}

View file

@ -0,0 +1,5 @@
//#define XERR
#include "inilist.ih"
size_t IniList::s_count = 0;

View file

@ -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("");
}
}

View file

@ -0,0 +1,6 @@
//#define XERR
#include "inilist.ih"
IniList::
{
}

View file

@ -0,0 +1,28 @@
#ifndef INCLUDED_INILIST_
#define INCLUDED_INILIST_
#include <vector>
class IniList
{
static size_t s_count; // counts the # of objects: used as
// object ID
static std::vector<bool> 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

View file

@ -0,0 +1,6 @@
#include "inilist.h"
#include <iostream>
using namespace std;

View file

@ -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";
}

View file

@ -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: ");
}

View file

@ -0,0 +1,43 @@
//#define XERR
#include "main.ih"
vector<bool> 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: ");
}

View file

@ -0,0 +1,9 @@
#include <iostream>
#include <unordered_map>
#include "inilist/inilist.h"
#include "xerr/xerr.ih"
using namespace std;
extern unordered_map<size_t, IniList> map;

View file

@ -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 <iostream>
#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