TrueReality  v0.1.1912
EnumerationString.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Class Inspired by the Delta3D Engine
22 * http://delta3dengine.org/
23 *
24 * @author Matthew W. Campbell
25 * @author David Guthrie
26 * @author Maxim Serebrennik
27 */
28 #pragma once
29 
30 #include "Export.h"
31 
32 #include <trUtil/WarningUtils.h>
33 
34 #include <ostream>
35 #include <string>
36 #include <vector>
37 
43 
44 
49 namespace trUtil
50 {
58  {
59  public:
60 
68  const std::string& GetName() const
69  {
70  return mName;
71  }
72 
85  bool operator==(const EnumerationString& rhs) const
86  {
87  return this == &rhs;
88  }
89 
101  bool operator!=(const EnumerationString& rhs) const
102  {
103  return this != &rhs;
104  }
105 
119  bool operator==(const std::string& rhs) const;
120 
130  bool operator!=(const std::string& rhs) const;
131 
141  bool operator<(const std::string& rhs) const;
142 
152  bool operator>(const std::string& rhs) const;
153 
169  bool operator<(const EnumerationString& rhs) const
170  {
171  return this < &rhs;
172  }
173  protected:
174 
187  virtual int Compare(const std::string& nameString) const;
188 
194  virtual ~EnumerationString();
195 
206  EnumerationString(const std::string& name);
207  private:
208 
218  EnumerationString& operator=(const EnumerationString&);
219 
221  std::string mName;
222 
231  };
232 
243  TR_UTIL_EXPORT std::ostream& operator<<(std::ostream& os, const EnumerationString& e);
244 
246 
255 #define DECLARE_ENUM(EnumType) \
256 public: \
257  using EnumerateListType = std::vector<EnumType*>; \
258  \
259  static const EnumerateListType& EnumerateType() \
260  { \
261  return EnumType::mInstances; \
262  } \
263  \
264  static const std::vector<trUtil::EnumerationString*>& Enumerate() \
265  { \
266  return EnumType::mGenericInstances; \
267  } \
268  \
269  static EnumType* GetValueForName(const std::string& name); \
270  \
271 private: \
272  static EnumerateListType mInstances; \
273  static std::vector<trUtil::EnumerationString*> mGenericInstances; \
274  static void AddInstance(EnumType* instance); \
275 public:
276 
284 #define IMPLEMENT_ENUM(EnumType) \
285  EnumType::EnumerateListType EnumType::mInstances; \
286  std::vector<trUtil::EnumerationString*> EnumType::mGenericInstances; \
287  void EnumType::AddInstance(EnumType* instance) \
288  { \
289  EnumType::mInstances.push_back(instance); \
290  EnumType::mGenericInstances.push_back(instance); \
291  } \
292  EnumType* EnumType::GetValueForName(const std::string& name) \
293  { \
294  for (unsigned i = 0; i < mInstances.size(); ++i) \
295  { \
296  if ((*mInstances[i]) == name) \
297  { \
298  return mInstances[i]; \
299  } \
300  } \
301  return nullptr; \
302  }
303 
305 
312  template <typename T>
314  {
315  public:
317  using element_type = T;
318 
324  EnumerationPointer() : mEnum(nullptr)
325  {
326  }
327 
335  EnumerationPointer(T* ptr) : mEnum(ptr)
336  {
337  }
338 
346  EnumerationPointer(const EnumerationPointer& rp) : mEnum(rp.mEnum)
347  {
348  }
349 
359  template<class Other> EnumerationPointer(const EnumerationPointer<Other>& rp) : mEnum(rp.mEnum)
360  {
361  }
362 
369  {
370  mEnum = nullptr;
371  }
372 
380  operator T*() const
381  {
382  return mEnum;
383  }
384 
392  operator T&() const
393  {
394  return *mEnum;
395  }
396 
407  {
408  mEnum = rp.mEnum;
409  return *this;
410  }
411 
422  template<class Other> EnumerationPointer& operator=(const EnumerationPointer<Other>& rp)
423  {
425  mEnum = rp.mEnum;
427  return *this;
428  }
429 
440  {
441  if (mEnum == ptr) return *this;
442  mEnum = ptr;
443  return *this;
444  }
445 
455  inline EnumerationPointer& operator=(T& enumRef)
456  {
457  if (mEnum == &enumRef) return *this;
458  mEnum = &enumRef;
459  return *this;
460  }
461 
471  bool operator==(const EnumerationPointer& rp) const
472  {
473  return (mEnum == rp.mEnum);
474  }
475 
485  bool operator==(const T* ptr) const
486  {
487  return (mEnum == ptr);
488  }
489 
500  friend bool operator==(const T* ptr, const EnumerationPointer& rp)
501  {
502  return (ptr == rp.mEnum);
503  }
504 
514  bool operator!=(const EnumerationPointer& rp) const
515  {
516  return (mEnum != rp.mEnum);
517  }
518 
528  bool operator!=(const T* ptr) const
529  {
530  return (mEnum != ptr);
531  }
532 
543  friend bool operator!=(const T* ptr, const EnumerationPointer& rp)
544  {
545  return (ptr != rp.mEnum);
546  }
547 
557  bool operator<(const EnumerationPointer& rp) const
558  {
559  return (mEnum < rp.mEnum);
560  }
561 
571  bool operator>(const EnumerationPointer& rp) const
572  {
573  return (mEnum > rp.mEnum);
574  }
575 
583  T& operator*() const
584  {
585  return *mEnum;
586  }
587 
595  T* operator->() const
596  {
597  return mEnum;
598  }
599 
607  T* get() const
608  {
609  return mEnum;
610  }
611 
619  bool operator!() const
620  {
621  return mEnum == nullptr;
622  } // not required
623 
631  bool valid() const
632  {
633  return mEnum != nullptr;
634  }
635 
644  {
645  T* tmp = mEnum;
646  mEnum = rp.mEnum;
647  rp.mEnum = tmp;
648  }
649  private:
651  T* mEnum;
652  };
653 }
bool operator<(const EnumerationPointer &rp) const
Overloaded less than test for this EnumerationString pointers&#39;s string value.
T * operator->() const
Pointer method access operator.
T & operator*() const
Pointer operator that returns a reference.
bool operator==(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:187
bool operator!=(const T *ptr) const
Comparison between EnumerationPointers and a pointer.
bool valid() const
Returns FALSE if the pointer is nullptr.
friend bool operator==(const T *ptr, const EnumerationPointer &rp)
Comparison between EnumerationPointer and a pointer.
bool operator!() const
The NOT operator.
const std::string & GetName() const
Inlined because it&#39;s called frequently.
#define TR_DISABLE_WARNING_START_MSVC(...)
Definition: WarningUtils.h:62
bool operator!=(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:199
TR_BASE_EXPORT std::ostream & operator<<(std::ostream &ios, const Matrixd &q)
Stream insertion operator.
A class that specifies what kind of entity an object is in the Entity System.
Definition: EntityType.h:37
bool operator>(const EnumerationPointer &rp) const
Overloaded greater than test for this EnumerationString pointers&#39;s string value.
EnumerationPointer(T *ptr)
A constructor that creates a pointer from a passed in EnumerationString.
bool operator!=(const EnumerationString &rhs) const
Inequality test for an EnumerationString.
EnumerationString Pointer class holds a reference to a passed in Enumeration.
std::string mName
String representation of the EnumerationString.
#define TR_DISABLE_WARNING_END
Definition: WarningUtils.h:65
EnumerationPointer(const EnumerationPointer &rp)
A constructor that creates a pointer from another EnumerationString Pointer.
EnumerationPointer & operator=(const EnumerationPointer< Other > &rp)
Assignment by reference operator.
This class represents a type-safe EnumerationString pattern.
A class that represents date time utility.
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
friend bool operator!=(const T *ptr, const EnumerationPointer &rp)
Comparison between EnumerationPointer and a pointer.
void swap(EnumerationPointer &rp)
Swaps the internal object with the passed in.
bool operator<(const EnumerationString &rhs) const
Overloaded less than operator.
EnumerationPointer(const EnumerationPointer< Other > &rp)
A templated pointer constructor that can create an EnumerationString pointer from an EnumerationStrin...
EnumerationPointer & operator=(const EnumerationPointer &rp)
Assignment by reference operator.
bool operator==(const EnumerationPointer &rp) const
Comparison between two EnumerationPointers.
EnumerationPointer & operator=(T *ptr)
Assignment from a pointer operator.
EnumerationPointer()
A constructor that creates and empty pointer.
EnumerationPointer & operator=(T &enumRef)
Assignment from a reference operator.
bool operator==(const T *ptr) const
Comparison between EnumerationPointers and a pointer.
bool operator==(const EnumerationString &rhs) const
Equality test for an EnumerationString.
bool operator!=(const EnumerationPointer &rp) const
Comparison between two EnumerationPointers.