Free Electron
Instance.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __core_Instance_h__
8 #define __core_Instance_h__
9 
10 namespace fe
11 {
12 
13 /** @brief Smart pointer used with types represented by BaseType
14 
15  @ingroup core
16 
17  This highly specialized smart pointer is designed to deal with instances
18  with types represented by BaseType.
19  In particular, intended for record attribute
20  instances with respect to the script binding.
21 
22  Whenever dealing directly with Instances be very careful about reference
23  count management. Instance cannot deal with reference counting unless it
24  is explicitly given a pointer to the count itself (via set() or
25  construction). So be sure to manage the memory properly if this cannot be
26  done.
27  */
28 class FE_DL_EXPORT Instance
29 {
30  public:
31  Instance(void);
32  Instance(void *instance, const sp<BaseType>& rspBT,
33  std::atomic<int> *pRefCnt);
34  Instance(const Instance &other);
35  ~Instance(void);
36 
37  Instance &operator=(const Instance &other);
38 
39  bool operator==(const Instance &other);
40 
41  void acquire(void) const;
42  void release(void);
43  void assign(const Instance &rvalue);
44 
45  template<class T>
46  T &cast(void) const;
47 
48  template<class T>
49  bool is(void) const;
50 
51  sp<BaseType> type(void) const;
52  void *data(void) const;
53 
54  void set(void *instance, const sp<BaseType>& rspBT,
55  std::atomic<int> *pRefCnt);
56  template<class T>
57  T &set(sp<TypeMaster> spTM, T &a_t);
58 
59  template<class T>
60  T &create(sp<TypeMaster> spTM);
61  void create(sp<BaseType> spBT);
62 
63  BWORD unique(void) const
64  { return m_pRefCnt? ((*m_pRefCnt)<2): TRUE; }
65 
66  private:
67  sp<BaseType> m_spType;
68  void *m_pData;
69  std::atomic<int> *m_pRefCnt;
70 };
71 
72 template<class T>
73 T &Instance::cast(void) const
74 {
75  if(m_spType.isNull())
76  {
77  feX("fe::Instance::cast","unknown type");
78  }
79  if(TypeInfo(getTypeId<T>()) != m_spType->typeinfo())
80  {
81  feX("fe::Instance::cast",
82  "(template '%s') type mismatch casting to '%s' from '%s'",
83  FE_TYPESTRING(T).c_str(),
84  TypeInfo(getTypeId<T>()).ref().name(),
85  m_spType->typeinfo().ref().name());
86  }
87  return *(reinterpret_cast<T *>(m_pData));
88 }
89 
90 template<class T>
91 bool Instance::is(void) const
92 {
93  if(m_spType.isNull())
94  {
95  return false;
96  }
97  if(TypeInfo(getTypeId<T>()) != m_spType->typeinfo())
98  {
99  return false;
100  }
101  return true;
102 }
103 
104 template<class T>
105 T &Instance::set(sp<TypeMaster> spTM, T &a_t)
106 {
107  release();
108  m_spType = spTM->lookupType(TypeInfo(getTypeId<T>()));
109  if(m_spType.isNull())
110  {
111  feX("fe::Instance::cast","unknown type");
112  }
113  m_pData = &a_t;
114  acquire();
115  return a_t;
116 }
117 
118 template<class T>
119 T &Instance::create(sp<TypeMaster> spTM)
120 {
121  release();
122  m_spType = spTM->lookupType(TypeInfo(getTypeId<T>()));
123  if(m_spType.isNull())
124  {
125  feX("fe::Instance::cast","unknown type");
126  }
127  m_pData = allocate(sizeof(T) + sizeof(std::atomic<int>));
128  m_pRefCnt = (std::atomic<int> *)((char *)m_pData + sizeof(T));
129  *m_pRefCnt = 0;
130  if(m_spType->getConstruct())
131  {
132  m_spType->construct(m_pData);
133  }
134  acquire();
135 
136  return *(reinterpret_cast<T *>(m_pData));
137 }
138 
139 /** Instance print
140  @relates Instance
141  */
142 inline String print(const Instance& a_instance)
143 {
144  return a_instance.type()->getInfo()->print(a_instance.data());
145 }
146 
147 } /* namespace */
148 #endif /* __core_Instance_h__ */
149 
Smart pointer used with types represented by BaseType.
Definition: Instance.h:28
kernel
Definition: namespace.dox:3
C++ type_info wrapper.
Definition: Type.h:20
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s.
Definition: DualString.h:208
Automatically reference-counted string container.
Definition: String.h:128
String print(const Instance &a_instance)
Instance print.
Definition: Instance.h:142
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53