ODIN
tjembed.h
1 /***************************************************************************
2  tjembed.h - description
3  -------------------
4  begin : Tue Mar 11 2003
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 TJEMBED_H
19 #define TJEMBED_H
20 
21 #include <tjutils/tjutils.h>
22 #include <tjutils/tjstring.h> // for itos
23 
29 template<class T,class E>
30 class Embed {
31 
32  public:
33  Embed() {}
34 
35  ~Embed() {
36  clear_instances();
37  }
38 
39  void clear_instances() {
40  for(typename STD_list<T*>::iterator it=instances.begin();it!=instances.end();++it) delete (*it);
41  instances.clear();
42  }
43 
44  T& set_embed_body (const E& embeddedBody) {
45  T* embedptr=static_cast<T*>(this);
46  T* newinst;
47  if(embedptr) newinst=new T(*embedptr);
48  else newinst=new T();
49  newinst->set_body(embeddedBody);
50  newinst->set_label(STD_string(newinst->get_label())+itos(instances.size()));
51  instances.push_back(newinst);
52  return *newinst;
53  }
54 
55  T& set_embed_body (E& embeddedBody) {
56  T* embedptr=static_cast<T*>(this);
57  T* newinst;
58  if(embedptr) newinst=new T(*embedptr);
59  else newinst=new T();
60  newinst->set_body(embeddedBody);
61  newinst->set_label(STD_string(newinst->get_label())+itos(instances.size()));
62  instances.push_back(newinst);
63  return *newinst;
64  }
65 
66 
67  typedef typename STD_list<T*>::iterator institer;
68  institer get_inst_begin() {return instances.begin();}
69  institer get_inst_end() {return instances.end();}
70 
71  typedef typename STD_list<T*>::const_iterator constinstiter;
72  constinstiter get_const_inst_begin() const {return instances.begin();}
73  constinstiter get_const_inst_end() const {return instances.end();}
74 
75  private:
76  STD_list<T*> instances;
77 };
78 
81 #endif
STD_string itos(int i, unsigned int maxabs=0)