21#include <vsg/core/Export.h>
22#include <vsg/core/ref_ptr.h>
23#include <vsg/core/type_name.h>
32 class RecordTraversal;
39 constexpr bool has_read_write() {
return false; }
54 explicit operator bool()
const noexcept {
return duplicate.valid(); }
78 static void*
operator new(std::size_t count);
79 static void operator delete(
void* ptr);
86 virtual bool is_compatible(
const std::type_info& type)
const noexcept {
return typeid(
Object) == type; }
89 T* cast() {
return is_compatible(
typeid(T)) ?
static_cast<T*
>(
this) : nullptr; }
92 const T* cast()
const {
return is_compatible(
typeid(T)) ?
static_cast<const T*
>(
this) : nullptr; }
102 virtual void traverse(
Visitor&) {}
107 virtual void accept(RecordTraversal& visitor)
const;
108 virtual void traverse(RecordTraversal&)
const {}
110 virtual void read(Input& input);
111 virtual void write(Output& output)
const;
114 inline void ref() const noexcept { _referenceCount.fetch_add(1, std::memory_order_relaxed); }
115 inline void unref() const noexcept
117 if (_referenceCount.fetch_sub(1, std::memory_order_seq_cst) <= 1) _attemptDelete();
119 inline void unref_nodelete() const noexcept { _referenceCount.fetch_sub(1, std::memory_order_seq_cst); }
120 inline unsigned int referenceCount() const noexcept {
return _referenceCount.load(); }
125 void setValue(
const std::string& key,
const T& value);
132 bool getValue(
const std::string&
key, T& value)
const;
170 Auxiliary* getAuxiliary() {
return _auxiliary; }
171 const Auxiliary* getAuxiliary()
const {
return _auxiliary; }
176 virtual void _attemptDelete()
const;
177 void setAuxiliary(Auxiliary* auxiliary);
180 friend class Auxiliary;
182 mutable std::atomic_uint _referenceCount;
184 Auxiliary* _auxiliary;
187 template<
class T,
class R>
188 T* cast(
const ref_ptr<R>&
object)
190 return object ?
object->template cast<T>() : nullptr;
193 template<
class T,
class R>
196 return object ?
object->template cast<T>() : nullptr;
200 constexpr bool has_read_write<Object>() {
return true; }
202 using RefObjectPath = std::vector<ref_ptr<Object>>;
203 using ObjectPath = std::vector<Object*>;
208 using DuplicateMap = std::map<const Object*, ref_ptr<Object>>;
209 using iterator = DuplicateMap::iterator;
210 using key_type = DuplicateMap::key_type;
211 using mapped_type = DuplicateMap::mapped_type;
213 DuplicateMap duplicates;
215 inline iterator find(
const key_type&
key) {
return duplicates.find(
key); }
216 inline iterator begin() {
return duplicates.begin(); }
217 inline iterator end() {
return duplicates.end(); }
218 std::size_t size()
const {
return duplicates.size(); }
219 inline mapped_type& operator[](
const Object*
object) {
return duplicates[object]; }
221 bool contains(
const Object*
object)
const {
return duplicates.count(
object) != 0; }
223 void clear() { duplicates.clear(); }
227 for (
auto itr = duplicates.begin();
itr != duplicates.end(); ++
itr)
237 if (ptr && duplicate)
239 if (
auto itr = duplicate->find(ptr);
itr != duplicate->end())
241 if (!
itr->second)
itr->second = ptr->clone(*
this);
244 warn(
"Unable to clone ", ptr);
253 if (!duplicate)
return src;
256 dest.reserve(
src.size());
257 for (
auto& ptr :
src)
259 dest.push_back(
operator()(ptr));
Definition Auxiliary.h:26
Definition ConstVisitor.h:172
ref_ptr< T > operator()(ref_ptr< T > ptr) const
copy/clone pointer
Definition Object.h:235
void setObject(const std::string &key, ref_ptr< Object > object)
assign an Object associated with key
const T * getObject(const std::string &key) const
get const object pointer of specified type associated with key, return nullptr if no object associate...
Definition Object.h:149
const ref_ptr< const T > getRefObject(const std::string &key) const
get ref_ptr<const T> of specified type associated with key, return nullptr if no object associated wi...
Definition Object.h:163
ref_ptr< Object > getRefObject(const std::string &key)
get ref_ptr<Object> associated with key, return nullptr if no object associated with key has been ass...
void setValue(const std::string &key, const char *value)
specialization of setValue to handle passing C strings
Definition Object.h:128
Object * getObject(const std::string &key)
get Object pointer associated with key, return nullptr if no object associated with key has been assi...
virtual ref_ptr< Object > clone(const CopyOp ©op={}) const
ref_ptr< T > getRefObject(const std::string &key)
get ref_ptr<T> of specified type associated with key, return nullptr if no object associated with key...
Definition Object.h:159
virtual int compare(const Object &rhs) const
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
ref_ptr< const Object > getRefObject(const std::string &key) const
get ref_ptr<const Object> pointer associated with key, return nullptr if no object associated with ke...
T * getObject(const std::string &key)
get object pointer of specified type associated with key, return nullptr if no object associated with...
Definition Object.h:145
const Object * getObject(const std::string &key) const
get const Object pointer associated with key, return nullptr if no object associated with key has bee...
void removeObject(const std::string &key)
remove meta object or value associated with key
virtual const std::type_info & type_info() const noexcept
return the std::type_info of this Object
Definition Object.h:85
std container adapter for allocating with MEMORY_AFFINITY_NODES
Definition Allocator.h:138