RMF
ID.h
Go to the documentation of this file.
1/**
2 * \file RMF/ID.h
3 * \brief Declaration of RMF::ID.
4 *
5 * Copyright 2007-2022 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef RMF_ID_H
10#define RMF_ID_H
11
12#include "RMF/config.h"
14#include "exceptions.h"
15#include <limits>
16
17RMF_ENABLE_WARNINGS
18
19namespace RMF {
20
21struct FrameTag {
22 static std::string get_tag() { return "f"; }
23};
24struct NodeTag {
25 static std::string get_tag() { return "n"; }
26};
27struct CategoryTag {
28 static std::string get_tag() { return "c"; }
29};
30
31//! A general purpose ID in RMF used, with different tags, to identify things.
32template <class TagT>
33class ID {
34 int i_;
35 int compare(const ID<TagT>& o) const {
36 if (i_ < o.i_)
37 return -1;
38 else if (i_ > o.i_)
39 return 1;
40 else
41 return 0;
42 }
43 std::string get_string() const {
44 if (i_ == -1)
45 return Tag::get_tag() + "NULL";
46 else if (i_ == std::numeric_limits<int>::min())
47 return Tag::get_tag() + "INV";
48 else {
49 std::ostringstream oss;
50 oss << Tag::get_tag() << i_;
51 return oss.str();
52 }
53 }
54
55 public:
56 typedef TagT Tag;
57#if !defined(RMF_DOXGYGEN) && !defined(SWIG)
58 class SpecialTag {};
59 ID(int i, SpecialTag) : i_(i) {}
60 int get_index_always() const {
61 /*RMF_USAGE_CHECK(i_ != std::numeric_limits<int>::min(),
62 "get_index called on uninitialized ID");*/
63 return i_;
64 }
65#endif
66// filling a std::vector of IDs with ints doesn't work in some VC++ versions
67// making this implicit fixes it, but I don't want it to be so in general.
68#ifndef _MSC_VER
69 explicit
70#endif
71 ID(unsigned int i)
72 : i_(i) {
73 RMF_USAGE_CHECK(static_cast<int>(i_) >= 0,
74 Tag::get_tag() + ": Bad index passed on initialize");
75 }
76 ID() : i_(std::numeric_limits<int>::min()) {}
77 unsigned int get_index() const {
78 /*RMF_USAGE_CHECK(i_ != std::numeric_limits<int>::min(),
79 "get_index called on uninitialized ID");
80 RMF_USAGE_CHECK(i_ >= 0, "get_index called on special
81 ID.");*/
82 return i_;
83 }
85 RMF_HASHABLE(ID, return i_);
86 RMF_SHOWABLE(ID, get_string());
87#if !defined(RMF_DOXYGEN) && !defined(SWIG)
88 ID operator++() {
89 ++i_;
90 return *this;
91 }
92 // Needed to use std::distance or boost::distance
93 int operator-(const ID& other) const {
94 return i_ - other.i_;
95 }
96#endif
97};
98
99//! Produce hash values for boost hash tables.
100template <class TagT>
101inline std::size_t hash_value(const ID<TagT>& t) {
102 return t.__hash__();
103}
104
105/** Identify a node within a file. */
107/** Identify a frame within a file. */
109/** Identify a category within a file. */
111
112/** List of node ids. */
113typedef std::vector<NodeID> NodeIDs;
114/** List of frame ids. */
115typedef std::vector<FrameID> FrameIDs;
116/** List of categories. */
117typedef std::vector<Category> Categories;
118
119#if !defined(SWIG) && !defined(RMF_DOXYGEN)
120template <class Traits>
121inline std::ostream& operator<<(std::ostream& out, ID<Traits> null) {
122 null.show(out);
123 return out;
124}
125#endif
126
127} /* namespace RMF */
128
129RMF_DISABLE_WARNINGS
130
131#endif /* RMF_ID_H */
A general purpose ID in RMF used, with different tags, to identify things.
Definition ID.h:33
Declarations of the various exception types.
Various general useful macros for IMP.
#define RMF_HASHABLE(name, hashret)
Implement a hash function for the class.
#define RMF_COMPARISONS(Name)
Implement comparison in a class using field as the variable to compare.
The namespace for all functionality in the library.
std::vector< Category > Categories
Definition ID.h:117
ID< CategoryTag > Category
Definition ID.h:110
ID< NodeTag > NodeID
Definition ID.h:106
ID< FrameTag > FrameID
Definition ID.h:108
std::size_t hash_value(const BufferConstHandle &t)
Produce hash values for boost hash tables.
std::vector< NodeID > NodeIDs
Definition ID.h:113
std::vector< FrameID > FrameIDs
Definition ID.h:115