Alphabetical Class Index  Class Hierarchy   File Members   Compound Members   File List  

SmartSuRef.h
00001 // Copyright (c) 1998-2014 by Tech Soft 3D, Inc.
00002 //
00003 // The information contained herein is confidential and proprietary to Tech Soft 3D, Inc.,
00004 // and considered a trade secret as defined under civil and criminal statutes.
00005 // Tech Soft 3D, Inc. shall pursue its civil and criminal remedies in the event of
00006 // unauthorized use or misappropriation of its trade secrets.  Use of this information
00007 // by anyone other than authorized employees of Tech Soft 3D, Inc. is granted only under
00008 // a written non-disclosure agreement, expressly prescribing the scope and manner of such use.
00009 
00010 #pragma once
00011 
00012 #include <slapi/model/model.h>
00013 #include <slapi/unicodestring.h>
00014 #include <slapi/model/texture_writer.h>
00015 #include <slapi/model/mesh_helper.h>
00016 
00017 template<typename Ref_t, SUResult (*Allocate)(Ref_t*), SUResult (*Free)(Ref_t*)>
00018 class SmartSuRef
00019 {
00020 public:
00021     SmartSuRef() { SUSetInvalid(m_ref); }
00022     SmartSuRef(Ref_t& ref) : m_ref(ref) { }
00023 
00024     ~SmartSuRef() { Release(); }
00025 
00026     bool Create()
00027     {
00028         Release();
00029 
00030         SUResult result = Allocate(&m_ref);
00031 
00032         return (result == SU_ERROR_NONE);
00033     }
00034 
00035     void Release()
00036     {
00037         if(!SUIsInvalid(m_ref)) {
00038             Free(&m_ref);
00039             SUSetInvalid(m_ref);
00040         }
00041     }
00042 
00043     void Swap(SmartSuRef& other)
00044     {
00045         Ref_t ref = other.m_ref;
00046 
00047         other.m_ref = m_ref;
00048         m_ref = ref;
00049     }
00050 
00051     bool IsInvalid() const { return SUIsInvalid(m_ref); }
00052 
00053     Ref_t& GetReference() { return m_ref; }
00054     Ref_t* GetAddress() { return &m_ref; }
00055 
00056 private:
00057     SmartSuRef(const SmartSuRef&);
00058     SmartSuRef& operator=(const SmartSuRef&);
00059 
00060 private:
00061     Ref_t m_ref;
00062 };
00063 
00064 typedef SmartSuRef<SUModelRef, SUModelCreate, SUModelRelease>     SmartModelRef;
00065 typedef SmartSuRef<SUStringRef, SUStringCreate, SUStringRelease>  SmartStringRef;
00066 typedef SmartSuRef<SUTextureWriterRef, SUTextureWriterCreate, SUTextureWriterRelease>
00067     SmartTextureWriterRef;
00068 typedef SmartSuRef<SUMeshHelperRef, nullptr, SUMeshHelperRelease> SmartMeshHelperRef;