ODIN
tjvallist.h
1 /***************************************************************************
2  tjvallist.h - description
3  -------------------
4  begin : Wed Apr 14 2004
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 TJVALLIST_H
19 #define TJVALLIST_H
20 
21 #include <tjutils/tjvector.h>
22 #include <tjutils/tjlabel.h>
23 
35 template <class T>
36 class ValList : public virtual Labeled {
37 
38 
42  struct ValListData {
43 
44  ValListData() : val(0), times(1), sublists(0), elements_size_cache(0), references(0) {}
45 
46  ValListData(const ValListData& vld) : times(vld.times), elements_size_cache(vld.elements_size_cache), references(0) {
47  if(vld.val) val=new T(*vld.val);
48  else val=0;
49  if(vld.sublists) sublists=new STD_list< ValList<T> >(*vld.sublists);
50  else sublists=0;
51  }
52 
53  ~ValListData() {
54  if(sublists) delete sublists;
55  if(val) delete val;
56  }
57 
58 
59  T* val; // zero pointer indicates 'value not set'
60  unsigned int times;
61  STD_list< ValList<T> >* sublists; // zero pointer indicates 'list not set'
62  unsigned int elements_size_cache; // cache for the number of values of one repetition of this node
63  unsigned short references; // reference counter
64  };
65 
66 
67  public:
68 
72  ValList(const STD_string& object_label="unnamedValList", unsigned int repetitions=1);
73 
77  ValList(T value);
78 
82  ValList(const ValList<T>& vl);
83 
84  ~ValList();
85 
90 
94  ValList& set_value(T value);
95 
100 
104  ValList& multiply_repetitions(unsigned int reptimes) {copy_on_write(); data->times*=reptimes; return *this;}
105 
109  STD_vector<T> get_values_flat() const;
110 
114  STD_vector<T> get_elements_flat() const;
115 
119  bool operator == (const ValList<T>& vl) const;
120 
124  bool operator < (const ValList<T>& vl) const;
125 
129  T operator [] (unsigned int i) const;
130 
131 
135 // unsigned int elements_size() const;
136 
140  unsigned int size() const {return data->times*data->elements_size_cache;}
141 
145  STD_string printvallist() const;
146 
150  bool parsevallist(const STD_string& str);
151 
155  STD_ostream& print2stream(STD_ostream& os) const;
156 
160  void clear();
161 
162  private:
163  friend class ValListTest;
164 
165  bool equalelements (const ValList<T>& vl) const;
166 
167  ValList& increment_repetitions(unsigned int reptimes) {copy_on_write(); data->times+=reptimes; return *this;}
168 
169  unsigned int get_repetitions() const {return data->times;}
170 
171  void flatten_sublists();
172 
173  void copy_on_write();
174 
175  ValListData* data;
176 };
177 
179 
182 #endif
bool parsevallist(const STD_string &str)
ValList & operator=(const ValList< T > &vl)
ValList(const ValList< T > &vl)
ValList(T value)
STD_vector< T > get_elements_flat() const
ValList(const STD_string &object_label="unnamedValList", unsigned int repetitions=1)
STD_vector< T > get_values_flat() const
void clear()
ValList & multiply_repetitions(unsigned int reptimes)
Definition: tjvallist.h:104
ValList & add_sublist(const ValList< T > &vl)
bool operator==(const ValList< T > &vl) const
ValList & set_value(T value)
STD_ostream & print2stream(STD_ostream &os) const
bool operator<(const ValList< T > &vl) const
unsigned int size() const
Definition: tjvallist.h:140
STD_string printvallist() const
T operator[](unsigned int i) const