8 #ifndef IMPKERNEL_KEY_H
9 #define IMPKERNEL_KEY_H
12 #include "internal/key_helpers.h"
21 IMPKERNEL_BEGIN_NAMESPACE
43 template <
unsigned int ID>
49 static internal::KeyData& get_key_data() {
50 #ifndef IMPKERNEL_INTERNAL_OLD_COMPILER
51 static internal::KeyData static_key_data_(ID);
52 return static_key_data_;
54 return IMP::internal::get_key_data(ID);
60 static const internal::KeyData::Map& get_map() {
61 return get_key_data().get_map();
63 static const internal::KeyData::RMap& get_rmap() {
64 IMP::internal::KeyData
const& kd=get_key_data();
65 IMP::internal::KeyData::RMap
const& ret=kd.get_rmap();
70 static unsigned int find_or_add_index(std::string
const& sc) {
74 if (get_map().find(sc) == get_map().end()) {
75 val = get_key_data().add_key(sc);
77 val = get_map().find(sc)->second;
84 static unsigned int find_index(std::string
const& sc) {
89 <<
" You must explicitly create the type first: "
91 val = get_map().find(sc)->second;
97 bool is_default()
const;
100 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
101 static unsigned int get_ID() {
return ID; }
103 static const std::string get_string(
int i) {
106 if (static_cast<unsigned int>(i) < get_rmap().size()) {
112 << i <<
" with a table of size " << get_rmap().size());
134 explicit Key(std::string
const& c,
bool is_implicit_add_permitted=
true)
135 : str_(is_implicit_add_permitted ? find_or_add_index(c) : find_index(c))
138 #if !defined(IMP_DOXYGEN)
141 explicit Key(
unsigned int i) : str_(i) {
147 static unsigned int add_key(std::string sc) {
152 val = get_key_data().add_key(sc);
157 static
bool get_key_exists(std::
string sc) {
160 val = get_map().find(sc) != get_map().end();
166 if (is_default())
return std::string(
"nullptr");
168 val = get_string(str_);
185 std::string new_name) {
187 "The name is already taken with an existing key or alias");
188 get_key_data().add_alias(new_name, old_key.get_index());
189 return Key<ID>(new_name.c_str());
192 static unsigned int get_number_of_keys() {
193 return get_rmap().size();
199 "Cannot get index on defaultly constructed Key");
205 static void show_all(std::ostream& out);
211 static Vector<std::string> get_all_strings();
231 Key operator+(
int o)
const {
242 template <
unsigned int ID>
243 inline std::ostream& operator<<(std::ostream& out, Key<ID> k) {
248 template <
unsigned int ID>
249 inline bool Key<ID>::is_default()
const {
253 template <
unsigned int ID>
254 inline void Key<ID>::show_all(std::ostream& out) {
256 get_key_data().
show(out);
259 template <
unsigned int ID>
260 Vector<std::
string> Key<ID>::get_all_strings() {
261 Vector<std::string> str;
263 for (internal::KeyData::Map::const_iterator it = get_map().begin();
264 it != get_map().end(); ++it) {
265 str.push_back(it->first);
271 IMPKERNEL_END_NAMESPACE
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Various general useful macros for IMP.
#define IMP_FAILURE(message)
A runtime failure for IMP.
#define IMP_HASHABLE_INLINE(name, hashret)
Key()
make a default key in a well-defined null state
#define IMP_LOG_PROGRESS(expr)
const std::string get_string() const
Turn a key into a pretty string.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Base for a simple primitive-like type.
static unsigned int get_number_unique()
Get the total number of keys of this type.
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
Various general useful macros for IMP.
Logging and error reporting support.
For backwards compatibility.
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
Exception definitions and assertions.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
static Key< ID > add_alias(Key< ID > old_key, std::string new_name)
Make new_name an alias for old_key.
#define IMP_COMPARISONS_1(Name, field)
Implement comparison in a class using field as the variable to compare.
Key(std::string const &c, bool is_implicit_add_permitted=true)
Generate a key object from the given string.
#define IMP_OMP_PRAGMA(x)