Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
OrderedInitializer.h
1 /*
2  * OrderedInitializer class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2015, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_ORDERED_INITIALIZER_H
22 #define ELM_ORDERED_INITIALIZER_H
23 
24 namespace elm {
25 
26 // OrderedInitializer class
27 template <class T>
29  typedef struct node_t {
30  struct node_t *next;
31  T *object;
32  inline node_t(T *_object, struct node_t *_next)
33  : object(_object), next(_next) { }
34  } node_t;
35 
36  static node_t *list;
37  static bool initialized;
38 public:
39  OrderedInitializer(bool start = true);
40  ~OrderedInitializer(void);
41  void record(T *object);
42  void startup(void);
43 };
44 
45 // Statics
46 template <class T> typename Initializer<T>::node_t *Initializer<T>::list = 0;
47 template <class T> bool Initializer<T>::initialized = false;
48 
49 // Initializer<T>::Initializer
50 template <class T>
51 Initializer<T>::Initializer(bool start) {
52  if(start)
53  startup();
54 }
55 
56 // Initializer<T>::~Initializer
57 template <class T>
59  for(node_t *node = list, *next; node; node = next) {
60  next = node->next;
61  delete node;
62  }
63 }
64 
65 // Initializer<T>::record
66 template <class T>
67 void Initializer<T>::record(T *object) {
68  if(initialized)
69  object->initialize();
70  else {
71  list = new node_t(object, list);
72  }
73 }
74 
75 // Initializer<T>::startup()
76 template <class T>
77 void Initializer<T>::startup(void) {
78  if(!initialized) {
79  initialized = true;
80  for(node_t *node = list, *next; node; node = next) {
81  next = node->next;
82  node->object->initialize();
83  delete node;
84  }
85  list = 0;
86  }
87 }
88 
89 } // elm
90 
91 #endif // ELM_INITIALIZER_H
92 
elm::Initializer::~Initializer
~Initializer(void)
Definition: Initializer.h:44
elm::OrderedInitializer::OrderedInitializer
OrderedInitializer(bool start=true)
elm::Initializer::record
void record(T *object)
Definition: Initializer.h:53
elm::OrderedInitializer
Definition: OrderedInitializer.h:28
elm::io::list
ListPrinter< T > list(const T &l, cstring s="", typename ListPrinter< T >::fun_t f=ListPrinter< T >::asis)
Definition: Output.h:321
elm
Definition: adapter.h:26
elm::OrderedInitializer::startup
void startup(void)
elm::Initializer::startup
void startup(void)
Definition: Initializer.h:63
elm::OrderedInitializer::~OrderedInitializer
~OrderedInitializer(void)
elm::Initializer
Definition: Initializer.h:14
elm::Initializer::Initializer
Initializer(bool start=true)
Definition: Initializer.h:37
elm::OrderedInitializer::record
void record(T *object)