RuntimeId.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 #include <unordered_map>
6 #include <functional>
7 #include <vector>
8 #include <typeindex>
9 
10 #include "static_warning.hpp"
11 #include "hash.hpp"
12 
13 namespace iv
14 {
15 
16 class Context;
17 
18 namespace runtime_id
19 {
20 
21 //------------------------------ RuntimeId ------------------------------------
23 {
24 public:
25  static const constexpr int InvalidRuntimeId = 0;
26 
27  Dictionary();
28  int create( const char * persistent_name, char const * type_name );
29 
30  std::string runtime_to_persistent( int runtime );
31  int persistent_to_runtime( const char * persistent );
32  int runtime_validate( int runtime );
33  size_t ids_count();
34  void lock();
35 
36 private:
37  int next_free_runtime_id;
38  bool locked;
39  std::unordered_map< std::string, int > p2r;
40  std::unordered_map< int, std::string > r2p;
41 };
42 
43 }
44 
45 //-----------------------------------------------------------------------------
50 {
51 public:
53 
54 private:
55  runtime_id::Dictionary * _dict;
56 };
57 
58 //--------------------------------- RuntimeId ---------------------------------
74 template< class Me >
75 class RuntimeId
76 {
77 public:
78  // type control - should be overloaded in type Me
79  static constexpr char TypeName[] = "Unknown";
80 
81  // instance control
82  RuntimeId();
83  explicit RuntimeId( int runtime );
84  explicit RuntimeId( const char * persistent );
85  explicit RuntimeId( const char * persistent, Context * context );
86 
87  static Me create( const char * persistent_name );
88  static void lock();
89  static size_t ids_count();
90 
91  // Relation operators, defined mostly for use in structured containers, more can be added if required.
92  bool operator==( Me const & other ) const;
93  bool operator!=( Me const & other ) const;
94  bool operator<( Me const & other ) const;
95 
96  // value access
97  int runtime_value() const;
98  std::string persistent_value() const;
99  bool valid() const;
100 
101 private:
102  static runtime_id::Dictionary * dict();
103  int _runtime_value;
104 };
105 
106 // used internally by RuntimeId
107 void RuntimeId_WarningIdDoesNotExist( Context * context, char const * type_name, char const * persistent_name );
108 
109 template< class Me >
110 struct hash< Me, std::enable_if_t< std::is_base_of< iv::RuntimeId< Me >, Me >::value > >
111 {
112  size_t operator()( Me const & val ) const
113  {
114  return std::hash< int >()( val.runtime_value() );
115  }
116 };
117 
118 }
119 
120 template< class Me >
121 std::ostream & operator<<( std::ostream & out, iv::RuntimeId< Me > const & id );
122 
123 
124 #include "RuntimeId.inl"