RMF
Enum.h
Go to the documentation of this file.
1 /**
2  * \file RMF/Enum.h
3  * \brief Declaration of RMF::Enum.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef RMF_ENUM_H
10 #define RMF_ENUM_H
11 
12 #include "RMF/config.h"
13 #include "infrastructure_macros.h"
14 #include "exceptions.h"
15 #include "internal/small_set_map.h"
16 
17 RMF_ENABLE_WARNINGS
18 
19 namespace RMF {
20 
21 struct RMFEXPORT FrameTypeTag {
22 // to make linking easy to get right
23 #ifndef IMP_DOXYGEN
24  static RMF_SMALL_UNORDERED_MAP<std::string, int>& get_from();
25  static RMF_SMALL_UNORDERED_MAP<int, std::string>& get_to();
26 #endif
27 };
28 struct RMFEXPORT NodeTypeTag {
29 #ifndef IMP_DOXYGEN
30  static RMF_SMALL_UNORDERED_MAP<std::string, int>& get_from();
31  static RMF_SMALL_UNORDERED_MAP<int, std::string>& get_to();
32 #endif
33 };
34 
35 struct RMFEXPORT RepresentationTypeTag {
36 #ifndef IMP_DOXYGEN
37  static RMF_SMALL_UNORDERED_MAP<std::string, int>& get_from();
38  static RMF_SMALL_UNORDERED_MAP<int, std::string>& get_to();
39 #endif
40 };
41 
42 //! A strong enum with an associated string name for each value
43 /** A general purpose Enum that has associated names and is type checked in
44  * python.*/
45 template <class TagT>
46 class Enum {
47  int i_;
48  int compare(const Enum<TagT>& o) const {
49  if (i_ < o.i_)
50  return -1;
51  else if (i_ > o.i_)
52  return 1;
53  else
54  return 0;
55  }
56  std::string get_string() const { return TagT::get_to().find(i_)->second; }
57 
58  public:
59  typedef TagT Tag;
60  Enum() : i_(-1) {}
61  explicit Enum(int i) : i_(i) {
62  RMF_USAGE_CHECK(TagT::get_to().find(i) != TagT::get_to().end(),
63  "Enum value not defined");
64  }
65  Enum(bool, int i) : i_(i) {
66  // Map out of range values to the undefined type, rather than throwing
67  // an error. This can be used, for example, to handle reading files
68  // made with a newer version of RMF.
69  if (TagT::get_to().find(i) == TagT::get_to().end()) {
70  i_ = -1;
71  }
72  }
73  Enum(std::string name) {
74  RMF_USAGE_CHECK(TagT::get_from().find(name) != TagT::get_from().end(),
75  "Enum name not defined");
76  i_ = TagT::get_from().find(name)->second;
77  }
78  Enum(int i, std::string name) : i_(i) {
79  TagT::get_to()[i] = name;
80  TagT::get_from()[name] = i;
81  }
83  RMF_HASHABLE(Enum, return i_);
84  RMF_SHOWABLE(Enum, get_string());
85 #if !defined(RMF_DOXYGEN) && !defined(SWIG)
86  operator int() const { return i_; }
87 #endif
88 };
89 
90 //! Produce hash values for boost hash tables.
91 template <class TagT>
92 inline std::size_t hash_value(const Enum<TagT>& t) {
93  return t.__hash__();
94 }
95 
96 #if !defined(SWIG) && !defined(RMF_DOXYGEN)
97 template <class Traits>
98 inline std::ostream& operator<<(std::ostream& out, Enum<Traits> v) {
99  v.show(out);
100  return out;
101 }
102 template <class Traits>
103 inline std::istream& operator>>(std::istream& in, Enum<Traits>& v) {
104  std::string val;
105  in >> val;
106  v = Enum<Traits>(val);
107  return in;
108 }
109 #endif
110 
111 /** The type for frames.
112 
113  See \ref frametypes "Frame Types" for a complete list of the possible
114  values.
115 */
117 /** The type for nodes.
118 
119  See \ref nodetypes "Node Types" for a complete list of the possible values.
120  */
122 /** The type for representations used in decorator::Alternatives.
123 
124  See \ref representationtypes "Representation Types" for a complete list of
125  the possible values.
126 */
128 
129 #ifndef RMF_DOXYGEN
130 typedef std::vector<FrameType> FrameTypes;
131 typedef std::vector<NodeType> NodeTypes;
132 typedef std::vector<RepresentationType> RepresentationTypes;
133 #endif
134 
135 } /* namespace RMF */
136 
137 RMF_DISABLE_WARNINGS
138 
139 #endif /* RMF_ENUM_H */
Enum< RepresentationTypeTag > RepresentationType
Definition: Enum.h:127
Enum< FrameTypeTag > FrameType
Definition: Enum.h:116
std::size_t hash_value(const BufferConstHandle &t)
Produce hash values for boost hash tables.
Declarations of the various exception types.
#define RMF_COMPARISONS(Name)
Implement comparison in a class using field as the variable to compare.
A strong enum with an associated string name for each value.
Definition: Enum.h:46
#define RMF_HASHABLE(name, hashret)
Implement a hash function for the class.
Various general useful macros for IMP.
Enum< NodeTypeTag > NodeType
Definition: Enum.h:121