ODIN
tjindex.h
1 /***************************************************************************
2  tjindex.h - description
3  -------------------
4  begin : Tue Aug 13 2002
5  copyright : (C) 2000-2021 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef TJINDEX_H
19 #define TJINDEX_H
20 
21 #include <tjutils/tjutils.h>
22 #include <tjutils/tjstatic.h>
23 #include <tjutils/tjlabel.h>
24 #include <tjutils/tjhandler.h>
25 
26 
27 
34 class Index {
35  public:
36  static const char* get_compName();
37 };
38 
39 
41 
42 // mapping between UniquIndex types and typenames
43 struct UniqueIndexMap : public STD_map<STD_string, STD_list<unsigned int> >, public Labeled {
44 
45  UniqueIndexMap() : contiguous(true) {}
46 
47  unsigned int get_index(STD_list<unsigned int>::iterator& index, const STD_string& type, unsigned int max_instances);
48 
49  void remove_index(const STD_list<unsigned int>::iterator& index, const STD_string& type);
50 
51 
52  private:
53  bool contiguous; // cache whether list of indices is contiguous for fast assignment of indices
54  unsigned int assign_index(STD_list<unsigned int>::iterator& index, const STD_string& type); // Get the next lowest index which is available
55 };
56 
57 
59 
60 // keeps track of all index lists
61 class UniqueIndexBase : public StaticHandler<UniqueIndexBase> {
62 
63  public:
64 
65  // for StaticHandler
66  static void init_static() {indices_map.init("indices_map");}
67  static void destroy_static() {indices_map.destroy();}
68 
69  protected:
70 
71  UniqueIndexBase() {} // Use only as base class
72 
73  static SingletonHandler<UniqueIndexMap,true> indices_map; // global lists of indices
74 
75 };
76 
77 
79 
80 
93 template<class T>
94 class UniqueIndex : public UniqueIndexBase {
95 
96  public:
97  UniqueIndex() {init();}
98 
99  UniqueIndex(const UniqueIndex<T>&) {init();}
100 
101  UniqueIndex<T>& operator = (const UniqueIndex<T>&) {erase(); init(); return *this;}
102 
103  ~UniqueIndex() {erase();}
104 
105 
109  unsigned int get_index() const {
110  return indices_map->get_index(index, T::get_typename(), T::get_max_instances());
111  }
112 
113 
114  private:
115  void init() { // Start with unassigned index (pointing to end())
116  index=indices_map->operator[](T::get_typename()).end();
117  }
118 
119  void erase() { // Remove index
120  indices_map->remove_index(index, T::get_typename());
121  }
122 
123 
124  mutable STD_list<unsigned int>::iterator index;
125 
126 
127 };
128 
131 #endif
unsigned int get_index() const
Definition: tjindex.h:109