uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Uuid.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 #ifndef U_CORE_UUID_H
29 #define U_CORE_UUID_H
30 
31 #include <iostream>
32 #include <vector>
33 
34 #include <boost/uuid/uuid.hpp>
35 #include <boost/uuid/name_generator.hpp>
36 #include <boost/uuid/random_generator.hpp>
37 #include <boost/uuid/uuid_io.hpp>
38 
39 #include "Core/Mpl.h"
40 #include "Core/Object.h"
41 
42 
43 namespace uLib {
44 
45 
46 
50 // Object Registration //
51 
52 
53 typedef boost::uuids::uuid uuid_t;
54 
55 extern uuid_t uLib_dns_uuid;
56 
57 template < typename T >
58 class type_id : public boost::uuids::uuid {
59 public:
60  type_id() :
61  m_size(sizeof(T)),
62  uuid(boost::uuids::name_generator(uLib_dns_uuid)(typeid(T).name()))
63  {
64  std::cout << "Request for register new type\n" <<
65  "name: " << typeid(T).name() << "\n" <<
66  "uuid: " << to_string(*this) << "\n";
67  }
68 
69  explicit type_id(boost::uuids::uuid const& u)
70  : boost::uuids::uuid(u) {}
71 
72  operator boost::uuids::uuid() {
73  return static_cast<boost::uuids::uuid&>(*this);
74  }
75 
76  operator boost::uuids::uuid() const {
77  return static_cast<boost::uuids::uuid const&>(*this);
78  }
79 
80  unsigned int size() const { return m_size; }
81 
82 private:
83  unsigned int m_size;
84 };
85 
86 
87 
88 
92 // TYPE REGISTER //
93 
94 namespace detail {
95 
96 class TypeRegister {
97  typedef boost::uuids::name_generator IDGen_t;
98 public:
99  struct RegisterEntry {
100  uuid_t id;
101  int size;
102  };
103 
104  TypeRegister(uuid_t const &dns) :
105  gen(dns) {}
106 
107 
108  template< typename T >
109  RegisterEntry * AddType(T *t = NULL) {
110  RegisterEntry en = { gen(typeid(T).name()), sizeof(T) };
111  for(int i=0; i < m_registry.size(); ++i)
112  if(en.id == m_registry[i].id) return &(m_registry[i]);
113  m_registry.push_back(en);
114  return &m_registry.back();
115  }
116 
117  void PrintSelf(std::ostream &o) {
118  std::cout << "RegisterController: \n";
119  for (int i=0; i<m_registry.size(); ++i)
120  o << "type [" << i << "]: "
121  << to_string(m_registry[i].id) << " "
122  << m_registry[i].size << "\n";
123  o << "\n";
124  }
125 
126 
127 
128 private:
129  IDGen_t gen;
130  std::vector<RegisterEntry> m_registry;
131 
132 };
133 
134 } // detail
135 
136 
137 
138 
139 class TypeRegister : public detail::TypeRegister {
140 public:
141  typedef detail::TypeRegister BaseClass;
142  typedef detail::TypeRegister::RegisterEntry Entry;
143 
144  static TypeRegister* Controller();
145 
146 private:
147  TypeRegister(); // Blocks constructor
148  static TypeRegister *s_Instance; // Singleton instance
149 };
150 
151 
152 
153 
157 // OBJECT REGISTER //
158 
159 
160 
161 } // uLib
162 
163 #endif // UUID_H