mirror of
https://github.com/allankoechke/cparse.git
synced 2026-12-02 17:18:03 +00:00
* Add a new CppFunction constructor for funcs with 0 args * Update `pack` class API * Remove `packMap` type in favor of the `TokenMap` class The `packMap` type was defined as a `pack<TokenMap>`, so it would manage memory of all Maps used on the system. TLDR; This is a better design choice than it was before. The reason for this change is to solve this 2 problems: 1. It creates 2 maps that do almost the same thing: packMap and TokenMap and it might confuse a reader of the code. 2. It could not count all TokenMap references, since some of these maps were referenced from inside the TokenMap class, i.e. the parent TokenMap of each TokenMap. For those reasons we modified the TokenMap class. Now it inherits from the pack<> class and manage a reference to a struct called `MapData_t` ---- This commit also makes TokenMap inherit from TokenBase, as well as the Iterable interface. This way it will be possible to cast a TokenBase* to an Iterable* if the token type is an iterator, i.e. if `IT & type != 0`. * Remove `packList` type in favor of `TokenList` class Now TokenList inherits from: - pack<TokenList_t> - TokenBase - Iterable The reasons for this commit are the same of the last commit. * Change "arglist" function keyword to "args" * Refactor: Iterable is now a public TokenBase Now there is no need to make TokenMap and TokenList inherit from TokenBase and Iterable. Now the Iterable class means "an Iterable Token" * Make Iterator inherit from TokenBase * Fix Travis build
93 lines
1.9 KiB
C++
93 lines
1.9 KiB
C++
#include <iostream>
|
|
#include <utility>
|
|
#include <list>
|
|
|
|
#ifndef PACK_H_
|
|
#define PACK_H_
|
|
|
|
// Pack a pointer to be automatically deleted:
|
|
template<typename T> class pack {
|
|
// Reference Counter type:
|
|
struct RC_t {
|
|
int64_t count;
|
|
T* obj;
|
|
bool _delete;
|
|
};
|
|
|
|
public:
|
|
typedef std::list<RC_t> RefList_t;
|
|
typedef typename RefList_t::iterator Ref_t;
|
|
|
|
public:
|
|
static RefList_t* rlist() {
|
|
static RefList_t* list = new RefList_t();
|
|
return list;
|
|
}
|
|
|
|
protected:
|
|
// Reference counter:
|
|
Ref_t ref;
|
|
|
|
protected:
|
|
void dropRef() {
|
|
--(ref->count);
|
|
|
|
// If the reference count gets to 0:
|
|
if (ref->count == 0) {
|
|
if (ref->_delete) {
|
|
delete ref->obj;
|
|
}
|
|
rlist()->erase(ref);
|
|
}
|
|
}
|
|
|
|
// Copy a reference from another
|
|
// packed object:
|
|
void getRef(const pack<T>& p) {
|
|
ref = p.ref;
|
|
++(ref->count);
|
|
}
|
|
|
|
Ref_t build(T* val, bool _delete = true) {
|
|
return rlist()->insert(rlist()->end(), {1, val, _delete});
|
|
}
|
|
|
|
public:
|
|
friend void swap(pack<T>& first, pack<T>& second) {
|
|
using std::swap;
|
|
swap(first.ref, second.ref);
|
|
}
|
|
|
|
public:
|
|
pack() : ref(build(new T())) {}
|
|
pack(const T& t) : ref(build(new T(t))) {}
|
|
pack(const pack<T>& p) { getRef(p); }
|
|
pack(pack<T>&& p) : pack() { swap(*this, p); }
|
|
virtual ~pack() { dropRef(); }
|
|
|
|
// This constructor should not be used frequently
|
|
// It won't delete the pointer, but will
|
|
// update the reference counter anyway, so its inefficient.
|
|
pack(T* t) : ref(build(t, false)) {}
|
|
|
|
// Copy assignment operator
|
|
pack<T>& operator=(pack<T> p) {
|
|
swap(*this, p);
|
|
return *this;
|
|
}
|
|
|
|
operator T*() const { return ref->obj; }
|
|
T* operator->() const { return ref->obj; }
|
|
T& operator*() const { return *(ref->obj); }
|
|
T* data() const { return ref->obj; }
|
|
int64_t count() const { return ref->count; }
|
|
|
|
friend bool operator==(pack<T> first, pack<T> second) {
|
|
return first.ref == second.ref;
|
|
}
|
|
};
|
|
|
|
|
|
#endif // PACK_H_
|
|
|