Skip to content

API: Namespaces and Pointers

Craig Minihan edited this page Nov 3, 2016 · 13 revisions

#Namespaces libscriptobject is defined in the rs::scriptobject namespace. There are a bunch of other libraries we've developed, they all live in their own namespace below the rs namespace.

Apologies if you find the nested namespaces a PITA, they are there for good reason.

#Pointers libscriptobject doesn't create normal C++ objects. They are packed into a single allocated block regardless of how many fields, their types and values your supply.

Underneath we use placement new which makes using std smart pointers a bit funky. To keep things simple libscriptobject uses its own smart pointer type: ScriptItemPtr. The main benefit here is that the reference count value is part of the allocated memory block and not allocated and managed separately.

ScriptItemPtr is defined as follows:

template <typename T>
class ScriptItemPtr final {
public:        
    inline T* get() const noexcept { return ptr_; }
    inline typename T::allocated_type* getRawPtr() const noexcept;
    inline void reset() noexcept;
    inline typename ScriptItemPtrBase<T>::count_type count() const noexcept;
    
    inline void operator=(ScriptItemPtr<T>&& rhs) noexcept;    
    inline void operator=(const ScriptItemPtr<T>& rhs) noexcept;
    
    inline T& operator*() const;
    inline T* operator->() const noexcept;
    inline bool operator!() const noexcept;
    
    inline void swap(ScriptItemPtr<T>& rhs) noexcept;
};

You are most likely to encounter this pointer type when dealing with object or array instances. Integer, float, string and boolean fields are standard C++ data types.

But why not raw pointers? Well the problem here is that objects can contain other objects and arrays can contain objects so we need a way of controlling object lifetime - reference counted smart pointers are the best way to go under C++ without getting even more funky and electing for garbage collected objects.

ScriptItemPtr is thread safe on all platforms.

Clone this wiki locally