📄 stdtypes.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: stdtypes.cpp,v $ * PRODUCTION Revision 1000.3 2004/06/01 19:41:52 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.44 * PRODUCTION * =========================================================================== *//* $Id: stdtypes.cpp,v 1000.3 2004/06/01 19:41:52 gouriano Exp $* ===========================================================================** PUBLIC DOMAIN NOTICE* National Center for Biotechnology Information** This software/database is a "United States Government Work" under the* terms of the United States Copyright Act. It was written as part of* the author's official duties as a United States Government employee and* thus cannot be copyrighted. This software/database is freely available* to the public for use. The National Library of Medicine and the U.S.* Government have not placed any restriction on its use or reproduction.** Although all reasonable efforts have been taken to ensure the accuracy* and reliability of the software and data, the NLM and the U.S.* Government do not and cannot warrant the performance or results that* may be obtained by using this software or data. The NLM and the U.S.* Government disclaim all warranties, express or implied, including* warranties of performance, merchantability or fitness for any particular* purpose.** Please cite the author in any work or product based on this material.** ===========================================================================** Author: Eugene Vasilchenko** File Description:* CTypeInfo classes for primitive leaf types.*/#include <ncbi_pch.hpp>#include <serial/serialdef.hpp>#include <serial/stdtypesimpl.hpp>#include <serial/typeinfoimpl.hpp>#include <serial/objistr.hpp>#include <serial/objostr.hpp>#include <serial/objcopy.hpp>#include <limits.h>#if HAVE_WINDOWS_H// In MSVC limits.h doesn't define FLT_MIN & FLT_MAX# include <float.h>#endif#include <math.h>BEGIN_NCBI_SCOPEclass CPrimitiveTypeFunctionsBase{public: static bool IsNegative(Int4 value) { return value < 0; } static bool IsNegative(Uint4 /*value*/) { return false; }#if SIZEOF_LONG == 4 // add variants with long to avoid ambiguity static bool IsNegative(long value) { return value < 0; } static bool IsNegative(unsigned long /*value*/) { return false; }#endif static bool IsNegative(Int8 value) { return value < 0; } static bool IsNegative(Uint8 /*value*/) { return false; }};template<typename T>class CPrimitiveTypeFunctions : public CPrimitiveTypeFunctionsBase{public: typedef T TObjectType; static TObjectType& Get(TObjectPtr object) { return CTypeConverter<TObjectType>::Get(object); } static const TObjectType& Get(TConstObjectPtr object) { return CTypeConverter<TObjectType>::Get(object); } static void SetIOFunctions(CPrimitiveTypeInfo* info) { info->SetIOFunctions(&Read, &Write, &Copy, &Skip); } static void SetMemFunctions(CPrimitiveTypeInfo* info) { info->SetMemFunctions(&Create, &IsDefault, &SetDefault, &Equals, &Assign); } static TObjectPtr Create(TTypeInfo ) { return new TObjectType(0); } static bool IsDefault(TConstObjectPtr objectPtr) { return Get(objectPtr) == TObjectType(0); } static void SetDefault(TObjectPtr objectPtr) { Get(objectPtr) = TObjectType(0); } static bool Equals(TConstObjectPtr obj1, TConstObjectPtr obj2, ESerialRecursionMode) { return Get(obj1) == Get(obj2); } static void Assign(TObjectPtr dst, TConstObjectPtr src, ESerialRecursionMode) { Get(dst) = Get(src); } static void Read(CObjectIStream& in, TTypeInfo , TObjectPtr objectPtr) { in.ReadStd(Get(objectPtr)); } static void Write(CObjectOStream& out, TTypeInfo , TConstObjectPtr objectPtr) { out.WriteStd(Get(objectPtr)); } static void Skip(CObjectIStream& in, TTypeInfo ) { TObjectType data; in.ReadStd(data); } static void Copy(CObjectStreamCopier& copier, TTypeInfo ) { TObjectType data; copier.In().ReadStd(data); copier.Out().WriteStd(data); }};#define EPSILON_(n) 1e-##n#define EPSILON(n) EPSILON_(n)#ifndef DBL_EPSILON# define DBL_EPSILON EPSILON(DBL_DIG)#endifEMPTY_TEMPLATEbool CPrimitiveTypeFunctions<double>::Equals(TConstObjectPtr obj1, TConstObjectPtr obj2, ESerialRecursionMode){ const double& x = Get(obj1); const double& y = Get(obj2); return (x == y || fabs(x - y) < fabs(x + y) * DBL_EPSILON);}#ifndef FLT_EPSILON# define FLT_EPSILON EPSILON(FLT_DIG)#endifEMPTY_TEMPLATEbool CPrimitiveTypeFunctions<float>::Equals(TConstObjectPtr obj1, TConstObjectPtr obj2, ESerialRecursionMode){ const float& x = Get(obj1); const float& y = Get(obj2); return (x == y || fabs(x - y) < fabs(x + y) * FLT_EPSILON);}#if SIZEOF_LONG_DOUBLE != 0EMPTY_TEMPLATEbool CPrimitiveTypeFunctions<long double>::Equals(TConstObjectPtr obj1, TConstObjectPtr obj2, ESerialRecursionMode){ const long double& x = Get(obj1); const long double& y = Get(obj2); // We use DBL_EPSILON because I/O is double-based anyway. return (x == y || fabs(x - y) < fabs(x + y) * DBL_EPSILON);}#endifvoid CVoidTypeFunctions::ThrowException(const char* operation, TTypeInfo objectType){ string message("cannot "); message += operation; message += " object of type: "; message += objectType->GetName(); NCBI_THROW(CSerialException,eIllegalCall, message);}bool CVoidTypeFunctions::IsDefault(TConstObjectPtr ){ return true;}bool CVoidTypeFunctions::Equals(TConstObjectPtr , TConstObjectPtr, ESerialRecursionMode ){ ThrowIllegalCall(); return false;}void CVoidTypeFunctions::SetDefault(TObjectPtr ){}void CVoidTypeFunctions::Assign(TObjectPtr , TConstObjectPtr, ESerialRecursionMode ){ ThrowIllegalCall();}void CVoidTypeFunctions::Read(CObjectIStream& in, TTypeInfo , TObjectPtr ){ in.ThrowError(in.fIllegalCall, "CVoidTypeFunctions::Read cannot read");}void CVoidTypeFunctions::Write(CObjectOStream& out, TTypeInfo , TConstObjectPtr ){ out.ThrowError(out.fIllegalCall, "CVoidTypeFunctions::Write cannot write");}void CVoidTypeFunctions::Copy(CObjectStreamCopier& copier, TTypeInfo ){ copier.ThrowError(CObjectIStream::fIllegalCall, "CVoidTypeFunctions::Copy cannot copy");}void CVoidTypeFunctions::Skip(CObjectIStream& in, TTypeInfo ){ in.ThrowError(in.fIllegalCall, "CVoidTypeFunctions::Skip cannot skip");}TObjectPtr CVoidTypeFunctions::Create(TTypeInfo objectType){ ThrowException("CVoidTypeFunctions::Create cannot create", objectType); return 0;}CVoidTypeInfo::CVoidTypeInfo(void) : CParent(0, ePrimitiveValueSpecial){}CPrimitiveTypeInfo::CPrimitiveTypeInfo(size_t size, EPrimitiveValueType valueType, bool isSigned) : CParent(eTypeFamilyPrimitive, size), m_ValueType(valueType), m_Signed(isSigned){ typedef CVoidTypeFunctions TFunctions; SetMemFunctions(&TFunctions::Create, &TFunctions::IsDefault, &TFunctions::SetDefault, &TFunctions::Equals, &TFunctions::Assign);}CPrimitiveTypeInfo::CPrimitiveTypeInfo(size_t size, const char* name, EPrimitiveValueType valueType, bool isSigned) : CParent(eTypeFamilyPrimitive, size, name), m_ValueType(valueType), m_Signed(isSigned){ typedef CVoidTypeFunctions TFunctions; SetMemFunctions(&TFunctions::Create, &TFunctions::IsDefault, &TFunctions::SetDefault, &TFunctions::Equals, &TFunctions::Assign);}CPrimitiveTypeInfo::CPrimitiveTypeInfo(size_t size, const string& name, EPrimitiveValueType valueType, bool isSigned) : CParent(eTypeFamilyPrimitive, size, name), m_ValueType(valueType), m_Signed(isSigned){ typedef CVoidTypeFunctions TFunctions; SetMemFunctions(&TFunctions::Create, &TFunctions::IsDefault, &TFunctions::SetDefault, &TFunctions::Equals, &TFunctions::Assign);}void CPrimitiveTypeInfo::SetMemFunctions(TTypeCreate create, TIsDefaultFunction isDefault, TSetDefaultFunction setDefault, TEqualsFunction equals, TAssignFunction assign){ SetCreateFunction(create); m_IsDefault = isDefault; m_SetDefault = setDefault; m_Equals = equals; m_Assign = assign;}void CPrimitiveTypeInfo::SetIOFunctions(TTypeReadFunction read, TTypeWriteFunction write, TTypeCopyFunction copy, TTypeSkipFunction skip){ SetReadFunction(read); SetWriteFunction(write); SetCopyFunction(copy); SetSkipFunction(skip);}bool CPrimitiveTypeInfo::GetValueBool(TConstObjectPtr /*objectPtr*/) const{ ThrowIncompatibleValue(); return false;}bool CPrimitiveTypeInfo::IsDefault(TConstObjectPtr objectPtr) const{ return m_IsDefault(objectPtr);}void CPrimitiveTypeInfo::SetDefault(TObjectPtr objectPtr) const{ m_SetDefault(objectPtr);}bool CPrimitiveTypeInfo::Equals(TConstObjectPtr obj1, TConstObjectPtr obj2, ESerialRecursionMode how) const{ return m_Equals(obj1, obj2, how);}void CPrimitiveTypeInfo::Assign(TObjectPtr dst, TConstObjectPtr src, ESerialRecursionMode how) const{ m_Assign(dst, src, how);}void CPrimitiveTypeInfo::SetValueBool(TObjectPtr /*objectPtr*/, bool /*value*/) const{ ThrowIncompatibleValue();}char CPrimitiveTypeInfo::GetValueChar(TConstObjectPtr /*objectPtr*/) const{ ThrowIncompatibleValue(); return 0;}void CPrimitiveTypeInfo::SetValueChar(TObjectPtr /*objectPtr*/, char /*value*/) const{ ThrowIncompatibleValue();}Int4 CPrimitiveTypeInfo::GetValueInt4(TConstObjectPtr /*objectPtr*/) const{ ThrowIncompatibleValue(); return 0;}void CPrimitiveTypeInfo::SetValueInt4(TObjectPtr /*objectPtr*/, Int4 /*value*/) const{ ThrowIncompatibleValue();}Uint4 CPrimitiveTypeInfo::GetValueUint4(TConstObjectPtr /*objectPtr*/) const{ ThrowIncompatibleValue(); return 0;}void CPrimitiveTypeInfo::SetValueUint4(TObjectPtr /*objectPtr*/, Uint4 /*value*/) const{ ThrowIncompatibleValue();}Int8 CPrimitiveTypeInfo::GetValueInt8(TConstObjectPtr /*objectPtr*/) const{ ThrowIncompatibleValue(); return 0;}void CPrimitiveTypeInfo::SetValueInt8(TObjectPtr /*objectPtr*/, Int8 /*value*/) const{ ThrowIncompatibleValue();}Uint8 CPrimitiveTypeInfo::GetValueUint8(TConstObjectPtr /*objectPtr*/) const{ ThrowIncompatibleValue(); return 0;}void CPrimitiveTypeInfo::SetValueUint8(TObjectPtr /*objectPtr*/, Uint8 /*value*/) const{ ThrowIncompatibleValue();}double CPrimitiveTypeInfo::GetValueDouble(TConstObjectPtr /*objectPtr*/) const{ ThrowIncompatibleValue(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -