IDL compiler front-end library
Loading...
Searching...
No Matches
Item.h
Go to the documentation of this file.
1
2/*
3* Nirvana IDL front-end library.
4*
5* This is a part of the Nirvana project.
6*
7* Author: Igor Popov
8*
9* Copyright (c) 2021 Igor Popov.
10*
11* This program is free software; you can redistribute it and/or modify
12* it under the terms of the GNU Lesser General Public License as published by
13* the Free Software Foundation; either version 3 of the License, or
14* (at your option) any later version.
15*
16* This program 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
19* GNU 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. If not, see <http://www.gnu.org/licenses/>.
23*
24* Send comments and/or bug reports to:
25* popov.nirvana@gmail.com
26*/
27#ifndef IDLFE_AST_ITEM_H_
28#define IDLFE_AST_ITEM_H_
29#pragma once
30
31#include <utility>
32#include <assert.h>
33
35namespace AST {
36
37template <class T> class Ptr;
38
40class Item
41{
42public:
73
75 Kind kind () const noexcept
76 {
77 return kind_;
78 }
79
81 bool is_type () const noexcept;
82
84 bool is_forward_decl () const noexcept;
85
86protected:
87 Item (Kind kind) :
88 kind_ (kind),
89 ref_cnt_ (1)
90 {}
91
92 Item (const Item& src) :
93 kind_ (src.kind_),
94 ref_cnt_ (1)
95 {}
96
97 virtual ~Item () {}
98
99 Item& operator = (const Item& src)
100 {
101 assert (kind_ == src.kind_);
102 return *this;
103 }
104
105private:
106 template <class T> friend class Ptr;
107
108 void* operator new (std::size_t cb)
109 {
110 return ::operator new (cb);
111 }
112
113 void _add_ref () const noexcept
114 {
115 ++ref_cnt_;
116 }
117
118 void _remove_ref () const noexcept
119 {
120 if (!--ref_cnt_)
121 delete this;
122 }
123
124private:
125 const Kind kind_;
126 mutable unsigned ref_cnt_;
127};
128
132template <class T>
133class Ptr
134{
135 template <class T1> friend class Ptr;
136public:
137 Ptr () noexcept :
138 p_ (nullptr)
139 {}
140
141 template <class T1, class ... Args>
142 static Ptr make (Args ... args)
143 {
144 return Ptr (new T1 (std::forward <Args> (args)...), Make ());
145 }
146
147 template <class T1>
148 Ptr (const Ptr <T1>& src) noexcept :
149 p_ (src.p_)
150 {
151 if (p_)
152 p_->_add_ref ();
153 }
154
155 Ptr (const Ptr& src) noexcept :
156 p_ (src.p_)
157 {
158 if (p_)
159 p_->_add_ref ();
160 }
161
162 template <class T1>
163 Ptr (Ptr <T1>&& src) noexcept :
164 p_ (src.p_)
165 {
166 src.p_ = nullptr;
167 }
168
169 Ptr (Ptr&& src) noexcept :
170 p_ (src.p_)
171 {
172 src.p_ = nullptr;
173 }
174
175 template <class T1>
176 Ptr (T1* p) noexcept :
177 p_ (p)
178 {
179 if (p_)
180 p_->_add_ref ();
181 }
182
183 Ptr (T* p) noexcept :
184 p_ (p)
185 {
186 if (p_)
187 p_->_add_ref ();
188 }
189
190 ~Ptr () noexcept
191 {
192 if (p_)
193 p_->_remove_ref ();
194 }
195
196 template <class T1>
197 Ptr& operator = (const Ptr <T1>& src) noexcept
198 {
199 if (p_ != src.p_) {
200 if (p_)
201 p_->_remove_ref ();
202 if ((p_ = src.p_))
203 p_->_add_ref ();
204 }
205 return *this;
206 }
207
208 Ptr& operator = (const Ptr <T>& src) noexcept
209 {
210 if (p_ != src.p_) {
211 if (p_)
212 p_->_remove_ref ();
213 if ((p_ = src.p_))
214 p_->_add_ref ();
215 }
216 return *this;
217 }
218
219 template <class T1>
220 Ptr& operator = (Ptr <T1>&& src) noexcept
221 {
222 if (p_ != src.p_) {
223 if (p_)
224 p_->_remove_ref ();
225 p_ = src.p_;
226 src.p_ = nullptr;
227 }
228 return *this;
229 }
230
231 Ptr& operator = (Ptr <T>&& src) noexcept
232 {
233 if (p_ != src.p_) {
234 if (p_)
235 p_->_remove_ref ();
236 p_ = src.p_;
237 src.p_ = nullptr;
238 }
239 return *this;
240 }
241
242 template <class T1>
243 Ptr& operator = (T1* p) noexcept
244 {
245 if (p_ != p) {
246 if (p_)
247 p_->_remove_ref ();
248 p_ = p;
249 }
250 return *this;
251 }
252
253 Ptr& operator = (T* p) noexcept
254 {
255 if (p_ != p) {
256 if (p_)
257 p_->_remove_ref ();
258 p_ = p;
259 }
260 return *this;
261 }
262
263 Ptr& operator = (std::nullptr_t) noexcept
264 {
265 if (p_) {
266 p_->_remove_ref ();
267 p_ = nullptr;
268 }
269 return *this;
270 }
271
272 T* operator -> () const noexcept
273 {
274 assert (p_);
275 return p_;
276 }
277
278 operator T* () const noexcept
279 {
280 return p_;
281 }
282
283 T& operator * () const noexcept
284 {
285 assert (p_);
286 return *p_;
287 }
288
289private:
290 struct Make {};
291
292 Ptr (T* p, const Make&) :
293 p_ (p)
294 {}
295
296private:
297 T* p_;
298};
299
300template <class T1, class T>
301T1* scast (const Ptr <T>& ptr) noexcept
302{
303 return static_cast <T1*> ((T*)ptr);
304}
305
306}
307
308#endif
An AST item.
Definition Item.h:41
bool is_forward_decl() const noexcept
Kind
The kind of item.
Definition Item.h:45
@ INCLUDE
class Include
@ MODULE
class Module
@ VALUE_BOX
class ValueBox
@ VALUE_FACTORY
class ValueFactory
@ VALUE_TYPE
class ValueType
@ MODULE_ITEMS
class ModuleItems
@ INTERFACE
class Interface
@ VALUE_TYPE_DECL
class ValueTypeDecl
@ ATTRIBUTE
class Attribute
@ INTERFACE_DECL
class InterfaceDecl
@ MEMBER
class Member
@ UNION_DECL
class UnionDecl
@ ENUM
class Enum
@ ENUM_ITEM
class EnumItem
@ STATE_MEMBER
class StateMember
@ CONSTANT
class Constant
@ STRUCT_DECL
class StructDecl
@ UNION_ELEMENT
class UnionElement
@ STRUCT
class Struct
@ PARAMETER
class Parameter
@ EXCEPTION
class Exception
@ TYPE_DEF
class TypeDef
@ OPERATION
class Operation
@ ROOT
class Root
@ UNION
class Union
@ NATIVE
class Native
bool is_type() const noexcept
Kind kind() const noexcept
Definition Item.h:75
AST item smart pointer.
Definition Item.h:134
Abstract Syntax Tree namespace.
Definition Array.h:34