mirror of
https://gitlab.com/fbb-git/cppannotations
synced 2024-09-28 03:20:44 +02:00
added initializer_list example
This commit is contained in:
parent
b0a92c434a
commit
1b49cd3ba0
15 changed files with 196 additions and 0 deletions
1
annotations/yo/stl/examples/inilist/CLASSES
Normal file
1
annotations/yo/stl/examples/inilist/CLASSES
Normal file
|
@ -0,0 +1 @@
|
|||
inilist
|
6
annotations/yo/stl/examples/inilist/data.cc
Normal file
6
annotations/yo/stl/examples/inilist/data.cc
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "main.ih"
|
||||
|
||||
unordered_map<size_t, IniList> map
|
||||
{
|
||||
{1, IniList{} }
|
||||
};
|
1
annotations/yo/stl/examples/inilist/frame
Normal file
1
annotations/yo/stl/examples/inilist/frame
Normal file
|
@ -0,0 +1 @@
|
|||
#include "main.ih"
|
16
annotations/yo/stl/examples/inilist/icmconf
Normal file
16
annotations/yo/stl/examples/inilist/icmconf
Normal 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"
|
11
annotations/yo/stl/examples/inilist/inilist/check.cc
Normal file
11
annotations/yo/stl/examples/inilist/inilist/check.cc
Normal 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';
|
||||
}
|
5
annotations/yo/stl/examples/inilist/inilist/data.cc
Normal file
5
annotations/yo/stl/examples/inilist/inilist/data.cc
Normal file
|
@ -0,0 +1,5 @@
|
|||
//#define XERR
|
||||
#include "inilist.ih"
|
||||
|
||||
size_t IniList::s_count = 0;
|
||||
|
17
annotations/yo/stl/examples/inilist/inilist/destructor.cc
Normal file
17
annotations/yo/stl/examples/inilist/inilist/destructor.cc
Normal 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("");
|
||||
}
|
||||
}
|
6
annotations/yo/stl/examples/inilist/inilist/frame
Normal file
6
annotations/yo/stl/examples/inilist/inilist/frame
Normal file
|
@ -0,0 +1,6 @@
|
|||
//#define XERR
|
||||
#include "inilist.ih"
|
||||
|
||||
IniList::
|
||||
{
|
||||
}
|
28
annotations/yo/stl/examples/inilist/inilist/inilist.h
Normal file
28
annotations/yo/stl/examples/inilist/inilist/inilist.h
Normal 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
|
6
annotations/yo/stl/examples/inilist/inilist/inilist.ih
Normal file
6
annotations/yo/stl/examples/inilist/inilist/inilist.ih
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "inilist.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
15
annotations/yo/stl/examples/inilist/inilist/inilist1.cc
Normal file
15
annotations/yo/stl/examples/inilist/inilist/inilist1.cc
Normal 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";
|
||||
}
|
||||
|
||||
|
13
annotations/yo/stl/examples/inilist/inilist/inilist2.cc
Normal file
13
annotations/yo/stl/examples/inilist/inilist/inilist2.cc
Normal 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: ");
|
||||
}
|
43
annotations/yo/stl/examples/inilist/main.cc
Normal file
43
annotations/yo/stl/examples/inilist/main.cc
Normal 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: ");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
9
annotations/yo/stl/examples/inilist/main.ih
Normal file
9
annotations/yo/stl/examples/inilist/main.ih
Normal 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;
|
19
annotations/yo/stl/examples/inilist/xerr/xerr.ih
Normal file
19
annotations/yo/stl/examples/inilist/xerr/xerr.ih
Normal 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
|
||||
|
Loading…
Reference in a new issue