CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Enumerator.hh
Go to the documentation of this file.
1 #ifndef CASM_Enumerator
2 #define CASM_Enumerator
3 
4 #include <string>
5 
6 #include "casm/casm_io/json/jsonParser.hh" // TODO remove this dependency
8 #include "casm/misc/CASM_TMP.hh"
11 
12 namespace CASM {
13 
94 // ---- Enumerator ---------------------
95 
106  public:
107  typedef long step_type;
108 
110  EnumeratorBase() : m_valid(false), m_step(0) {}
111 
112  virtual ~EnumeratorBase() {}
113 
115  step_type step() const { return m_step; }
116 
118  bool valid() const { return m_valid; }
119 
129  virtual jsonParser source(step_type step) const {
130  jsonParser src;
131  src["enumerated_by"] = this->name();
132  src["step"] = step;
133  return src;
134  }
135 
137  virtual std::string name() const = 0;
138 
139  protected:
144  void _initialize() {
145  m_step = 0;
146  m_valid = true;
147  }
148 
150  void _set_step(step_type val) { m_step = val; }
151 
153  void _increment_step() { ++m_step; }
154 
156  void _decrement_step() { --m_step; }
157 
159  void _invalidate() { m_valid = false; }
160 
162  void _validate() { m_valid = true; }
163 
164  private:
165  bool m_valid;
166 
168 };
169 
170 template <typename ValueType, bool IsConst = true>
172  public:
173  typedef ValueType value_type;
176 
177  ValEnumerator() : m_current_ptr(nullptr) {}
178 
179  virtual ~ValEnumerator() {}
180 
181  // -- from EnumeratorBase --
182 
183  public:
184  using EnumeratorBase::name; // pure virtual
185  using EnumeratorBase::source; // virtual
188 
189  protected:
191 
198  _set_current_ptr(_initial);
200  }
201 
206 
207  // -- For ValEnumerator --
208 
209  public:
211  reference current() const { return *m_current_ptr; }
212 
213  protected:
216  m_current_ptr = _new;
217  }
218 
219  private:
221 };
222 
224  public:
226 
228  EnumIteratorBase() : m_enum_ptr(nullptr) {}
229 
231  EnumIteratorBase(EnumeratorBase &enumerator) : m_enum_ptr(&enumerator) {}
232 
233  virtual ~EnumIteratorBase() {}
234 
238  virtual step_type step() const = 0;
239 
243  jsonParser source() const { return _enum().source(this->step()); }
244 
246  std::string name() const { return m_enum_ptr->name(); }
247 
249  virtual bool is_end() const = 0;
250 
251  std::unique_ptr<EnumIteratorBase> clone() const {
252  return std::unique_ptr<EnumIteratorBase>(this->_clone());
253  }
254 
255  protected:
256  void _assert_same_ptr(const EnumIteratorBase &other) const {
257  assert((m_enum_ptr == other.m_enum_ptr) &&
258  "Comparing EnumIterator referring to different enumerators is not "
259  "allowed!");
260  }
261 
262  void _assert_ptr() const {
263  assert(m_enum_ptr && "EnumIterator does not point to any enumerator!");
264  }
265 
266  void _assert_valid() const {
267  assert(m_enum_ptr->valid() &&
268  "EnumIterator points to an invalid enumerator!");
269  }
270 
275  bool equal(const EnumIteratorBase &other) const {
276  bool this_is_end = this->is_end();
277  bool other_is_end = other.is_end();
278 
279  if (this_is_end != other_is_end) {
280  return false;
281  }
282 
283  if (this_is_end) {
284  return true;
285  }
286 
287  return (this->step() == other.step());
288  }
289 
291 
292  EnumeratorBase *_enum_ptr() const { return m_enum_ptr; }
293 
294  private:
295  virtual EnumIteratorBase *_clone() const = 0;
296 
298 
299  EnumeratorBase &_enum() const { return *m_enum_ptr; }
300 
301  // pointer to Enumerator
303 };
304 
305 template <typename ValueType, bool IsConst = true>
307  public:
309  typedef ValueType value_type;
311 
313 
315  : EnumIteratorBase(enumerator) {}
316 
317  virtual ~ValEnumIterator() {}
318 
319  using EnumIteratorBase::is_end; // pure virtual
322  using EnumIteratorBase::step; // pure virtual
323 
324  std::unique_ptr<EnumIteratorBase> clone() const {
325  return std::unique_ptr<EnumIteratorBase>(this->_clone());
326  }
327 
328  protected:
334 
335  private:
336  virtual EnumIteratorBase *_clone() const = 0;
337  virtual reference dereference() const = 0;
338 };
339 
342 template <typename EnumeratorType>
343 bool is_guaranteed_for_database_insert(EnumeratorType const &enumerator) {
344  return false;
345 }
346 
349 } // namespace CASM
350 
351 #endif
EnumeratorBase * _enum_ptr() const
Definition: Enumerator.hh:292
EnumeratorBase * m_enum_ptr
Definition: Enumerator.hh:302
void _assert_same_ptr(const EnumIteratorBase &other) const
Definition: Enumerator.hh:256
EnumeratorBase * _enum_ptr()
Definition: Enumerator.hh:290
virtual ~EnumIteratorBase()
Definition: Enumerator.hh:233
EnumIteratorBase()
Default Constructor.
Definition: Enumerator.hh:228
EnumeratorBase & _enum()
Definition: Enumerator.hh:297
void _assert_valid() const
Definition: Enumerator.hh:266
EnumeratorBase::step_type step_type
Definition: Enumerator.hh:225
virtual bool is_end() const =0
Returns true if 'end' iterator.
void _assert_ptr() const
Definition: Enumerator.hh:262
std::string name() const
Uses enumerator class 'name' implementation.
Definition: Enumerator.hh:246
EnumIteratorBase(EnumeratorBase &enumerator)
Constructor.
Definition: Enumerator.hh:231
jsonParser source() const
Definition: Enumerator.hh:243
std::unique_ptr< EnumIteratorBase > clone() const
Definition: Enumerator.hh:251
EnumeratorBase & _enum() const
Definition: Enumerator.hh:299
bool equal(const EnumIteratorBase &other) const
boost::iterator_facade implementation
Definition: Enumerator.hh:275
virtual step_type step() const =0
virtual EnumIteratorBase * _clone() const =0
Abstract base class for enumerators.
Definition: Enumerator.hh:105
bool valid() const
Returns false if enumeration is complete.
Definition: Enumerator.hh:118
virtual std::string name() const =0
Derived enumerators must implement name, via ENUM_MEMBERS.
virtual jsonParser source(step_type step) const
Definition: Enumerator.hh:129
EnumeratorBase()
Default constructor.
Definition: Enumerator.hh:110
void _validate()
Used if random access enumerator step is moved into valid range.
Definition: Enumerator.hh:162
void _invalidate()
Call if enumeration complete.
Definition: Enumerator.hh:159
void _increment_step()
Increment current step value.
Definition: Enumerator.hh:153
step_type step() const
Increments with each enumerated object.
Definition: Enumerator.hh:115
void _set_step(step_type val)
Set current step value.
Definition: Enumerator.hh:150
virtual ~EnumeratorBase()
Definition: Enumerator.hh:112
void _decrement_step()
Decrement current step value.
Definition: Enumerator.hh:156
virtual EnumIteratorBase * _clone() const =0
virtual ~ValEnumIterator()
Definition: Enumerator.hh:317
ValEnumerator< ValueType, IsConst >::reference reference
Definition: Enumerator.hh:310
std::unique_ptr< EnumIteratorBase > clone() const
Definition: Enumerator.hh:324
virtual reference dereference() const =0
ValEnumIterator(ValEnumerator< ValueType, IsConst > &enumerator)
Definition: Enumerator.hh:314
void _set_current_ptr(CASM_TMP::ConstSwitch< IsConst, value_type > *_new)
Change the pointer.
Definition: Enumerator.hh:215
CASM_TMP::ConstSwitch< IsConst, value_type > * m_current_ptr
Definition: Enumerator.hh:220
reference current() const
Access the current ObjectType by reference.
Definition: Enumerator.hh:211
ValueType value_type
Definition: Enumerator.hh:173
void _initialize(CASM_TMP::ConstSwitch< IsConst, value_type > *_initial)
Definition: Enumerator.hh:197
virtual ~ValEnumerator()
Definition: Enumerator.hh:179
CASM_TMP::ConstSwitch< IsConst, ValueType > & reference
Definition: Enumerator.hh:174
bool is_guaranteed_for_database_insert(ConfigEnumAllOccupations const &enumerator)
typename std::conditional< IsConst, const T, T >::type ConstSwitch
Definition: CASM_TMP.hh:78
Main CASM namespace.
Definition: APICommand.hh:8