#ifndef _SECURITY_SECRUNTIME_H_
#define _SECURITY_SECRUNTIME_H_
#include <CoreFoundation/CFRuntime.h>
#include <Security/refcount.h>
namespace Security
{
namespace KeychainCore
{
class SecCFObject : public RefCount
{
public:
virtual ~SecCFObject();
virtual bool equal(SecCFObject &other);
virtual CFHashCode hash();
};
class SecCFType : public CFRuntimeBase
{
public:
SecCFType(SecCFObject *obj);
~SecCFType();
RefPointer<SecCFObject> mObject;
};
class CFClassBase : protected CFRuntimeClass
{
protected:
CFClassBase(const char *name);
const SecCFType *makeNew(SecCFObject *obj);
const SecCFType *handle(SecCFObject *obj);
SecCFObject *required(const SecCFType *type, OSStatus errorCode);
private:
static void finalizeType(CFTypeRef cf);
static Boolean equalType(CFTypeRef cf1, CFTypeRef cf2);
static CFHashCode hashType(CFTypeRef cf);
public:
CFTypeID typeId;
};
template <class Object, class APITypePtr, OSStatus ErrorCode>
class CFClass : public CFClassBase
{
public:
CFClass(const char *name) : CFClassBase(name) {}
APITypePtr handle(Object &obj)
{
return APITypePtr(CFClassBase::handle(&obj));
}
Object *required(APITypePtr type)
{
Object *object = dynamic_cast<Object *>(CFClassBase::required
(reinterpret_cast<const SecCFType *>(type), ErrorCode));
if (!object)
MacOSError::throwMe(ErrorCode);
return object;
}
APITypePtr operator () (Object *obj)
{ return handle(*obj); }
APITypePtr operator () (const RefPointer<Object> &obj)
{ return handle(*obj); }
Object * operator () (APITypePtr ref)
{ return required(ref); }
};
}
}
#endif // !_SECURITY_SECRUNTIME_H_