Fire Core  8.0.0.alpha
GIS framework per tutti gli usi
 Tutto Classi Namespace Funzioni Variabili Ridefinizioni di tipo (typedef) Tipi enumerati (enum) Valori del tipo enumerato Friend
Shared.hpp
1 // Copyright © 2015 GEOSYSTEMS SRL
2 // All Rights Reserved.
3 
4 #ifndef FIRE_SHARED_SHARED_HPP_INCLUDED
5 #define FIRE_SHARED_SHARED_HPP_INCLUDED
6 
7 #include <boost/shared_ptr.hpp>
8 
9 #include <exception>
10 
11 #include <stdexcept>
12 
13 namespace fire {
14 namespace shared {
15 
16 template < class T >
17 class Shared {
18  public:
19  Shared(): _ptr() {
20  }
21 
22  explicit Shared(T* ptr): _ptr(ptr) {
23  }
24 
25  Shared(Shared const & c) {
26  this->operator =(c);
27  }
28 
29  inline Shared& operator=(Shared const & c) {
30  _ptr = c._ptr;
31  return *this;
32  }
33 
34  inline bool operator!() const {
35  return (NULL == _ptr);
36  }
37 
38  inline bool operator==(Shared const & r) const {
39  return (_ptr.get() == r._ptr.get());
40  }
41 
42  inline bool operator==(int const &) const {
43  return this->operator !();
44  }
45 
46  inline bool operator!=(Shared const & r) const {
47  return !(this->operator==(r));
48  }
49 
50  inline bool operator!=(int const & r) const {
51  return !(this->operator==(r));
52  }
53 
54  inline T& operator*() {
55  return this->operator T &();
56  }
57 
58  inline const T& operator*() const {
59  return this->operator const T &();
60  }
61 
62  // operatori di cast, pericoloso abilitarli per tutti i tipi shared
63  inline operator T &() {
64  return *_ptr;
65  }
66 
67  inline operator const T &() const {
68  return *_ptr;
69  }
70 
71  protected:
72  boost::shared_ptr< T > _ptr;
73 
74  virtual inline T* _getPtr() {
75  T* ret = _ptr.get();
76  if (NULL == ret)
77  throw std::runtime_error("Invalid pointer");
78 
79  return ret;
80  }
81 
82  virtual inline const T* _getPtr() const {
83  const T* ret = _ptr.get();
84  if (NULL == ret)
85  throw std::runtime_error("Invalid pointer");
86 
87  return ret;
88  }
89 };
90 
91 template<class T>
92  inline bool operator==(int const & nullPtr, Shared< T > const & r) {
93  return r.operator==(nullPtr);
94  }
95 
96 template<class T>
97  inline bool operator!=(int const & nullPtr, Shared< T > const & r) {
98  return r.operator!=(nullPtr);
99  }
100 
101 } // namespace shared
102 } // namespace fire
103 
104 #endif
Definition: Shared.hpp:17