// Created on: 2004-05-13
// Created by: Sergey ZARITCHNY <szy@opencascade.com>
// Copyright (c) 2004-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _BinMFunction_FunctionDriver_HeaderFile
#define _BinMFunction_FunctionDriver_HeaderFile
#include <Standard.hxx>
#include <Standard_Type.hxx>
#include <BinMDF_ADriver.hxx>
#include <BinObjMgt_RRelocationTable.hxx>
#include <BinObjMgt_SRelocationTable.hxx>
class Message_Messenger;
class TDF_Attribute;
class BinObjMgt_Persistent;
class BinMFunction_FunctionDriver;
DEFINE_STANDARD_HANDLE(BinMFunction_FunctionDriver, BinMDF_ADriver)
//! Function attribute Driver.
class BinMFunction_FunctionDriver : public BinMDF_ADriver
{
public:
Standard_EXPORT BinMFunction_FunctionDriver(const Handle(Message_Messenger)& theMessageDriver);
Standard_EXPORT virtual Handle(TDF_Attribute) NewEmpty() const Standard_OVERRIDE;
Standard_EXPORT virtual Standard_Boolean Paste(const BinObjMgt_Persistent& Source,
const Handle(TDF_Attribute)& Target,
BinObjMgt_RRelocationTable& RelocTable) const
Standard_OVERRIDE;
Standard_EXPORT virtual void Paste(const Handle(TDF_Attribute)& Source,
BinObjMgt_Persistent& Target,
BinObjMgt_SRelocationTable& RelocTable) const
Standard_OVERRIDE;
DEFINE_STANDARD_RTTIEXT(BinMFunction_FunctionDriver, BinMDF_ADriver)
protected:
private:
};
#endif // _BinMFunction_FunctionDriver_HeaderFile
// Created on: 2004-05-13
// Created by: Sergey ZARITCHNY
// Copyright (c) 2004-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <BinMDF_ADriver.hxx>
#include <BinMFunction_FunctionDriver.hxx>
#include <BinObjMgt_Persistent.hxx>
#include <BinObjMgt_RRelocationTable.hxx>
#include <BinObjMgt_SRelocationTable.hxx>
#include <Message_Messenger.hxx>
#include <Standard_Type.hxx>
#include <TDF_Attribute.hxx>
#include <TFunction_Function.hxx>
IMPLEMENT_STANDARD_RTTIEXT(BinMFunction_FunctionDriver, BinMDF_ADriver)
//=================================================================================================
BinMFunction_FunctionDriver::BinMFunction_FunctionDriver(
const Handle(Message_Messenger)& theMsgDriver)
: BinMDF_ADriver(theMsgDriver, STANDARD_TYPE(TFunction_Function)->Name())
{
}
//=================================================================================================
Handle(TDF_Attribute) BinMFunction_FunctionDriver::NewEmpty() const
{
return new TFunction_Function();
}
//=======================================================================
// function : Paste
// purpose : persistent -> transient (retrieve)
//=======================================================================
Standard_Boolean BinMFunction_FunctionDriver::Paste(const BinObjMgt_Persistent& theSource,
const Handle(TDF_Attribute)& theTarget,
BinObjMgt_RRelocationTable&) const
{
Handle(TFunction_Function) anAtt = Handle(TFunction_Function)::DownCast(theTarget);
Standard_GUID aGUID("00000000-0000-0000-0000-000000000000");
Standard_Boolean ok = theSource >> aGUID;
if (ok)
{
anAtt->SetDriverGUID(aGUID);
Standard_Integer aValue;
ok = theSource >> aValue;
if (ok)
anAtt->SetFailure(aValue);
}
return ok;
}
//=======================================================================
// function : Paste
// purpose : transient -> persistent (store)
//=======================================================================
void BinMFunction_FunctionDriver::Paste(const Handle(TDF_Attribute)& theSource,
BinObjMgt_Persistent& theTarget,
BinObjMgt_SRelocationTable&) const
{
Handle(TFunction_Function) aS = Handle(TFunction_Function)::DownCast(theSource);
theTarget << aS->GetDriverGUID();
theTarget << aS->GetFailure();
}
首先,从类名和继承关系来看:
- BinMFunction_FunctionDriver 继承自 BinMDF_ADriver
- 这是一个用于二进制序列化的驱动程序类
从头文件和实现文件可以看出:
- 类的职责:
- 这是一个Function attribute的二进制序列化驱动程序
- 负责将TFunction_Function属性在内存中的表示与二进制持久化格式之间进行转换
- 主要方法:
- 构造函数:接受Message_Messenger参数
- NewEmpty():创建空的TFunction_Function对象
- Paste()重载:两个版本,一个用于反序列化(persistent -> transient),一个用于序列化(transient -> persistent)
- 设计模式:
- 这明显使用了驱动程序模式(Driver Pattern)
- 也体现了策略模式的思想,为不同的属性类型提供不同的序列化策略
- 数据处理:
从实现可以看出,TFunction_Function对象包含两个主要数据:
- GUID:驱动程序的唯一标识符
- Failure:失败状态值(整数)
- 架构设计思想:
-
分离关注点:将序列化逻辑从业务逻辑中分离出来
-
可扩展性:通过继承BinMDF_ADriver可以为不同类型的属性提供专门的序列化驱动
-
标准化接口:确保所有序列化驱动遵循一致的处理方式
1. 整体架构设计
BinMFunction_FunctionDriver 是 OCCT 中二进制序列化框架的一个具体实现,专门负责 TFunction_Function
属性的序列化和反序列化操作。
// 继承关系体现了驱动程序模式
class BinMFunction_FunctionDriver : public BinMDF_ADriver
2. 核心设计模式
2.1 驱动程序模式 (Driver Pattern)
- 目的:为不同类型的数据对象提供统一的序列化接口
- 实现:通过继承
BinMDF_ADriver
基类,每种属性类型都有专门的驱动程序 - 优势:解耦了数据类型与序列化逻辑,易于扩展和维护
2.2 策略模式 (Strategy Pattern)
// 不同的属性类型使用不同的序列化策略
BinMFunction_FunctionDriver // 处理 TFunction_Function
BinMDataStd_NameDriver // 处理 TDataStd_Name
// ... 其他驱动程序
3. 关键方法设计
3.1 工厂方法 - NewEmpty()
Handle(TDF_Attribute) BinMFunction_FunctionDriver::NewEmpty() const
{
return new TFunction_Function();
}
- 设计思想:使用工厂方法模式创建空对象
- 职责单一:只负责对象创建,不涉及数据填充
- 类型安全:返回具体的属性类型实例
3.2 双向转换 - Paste() 方法重载
反序列化 (Persistent → Transient)
Standard_Boolean Paste(const BinObjMgt_Persistent& theSource,
const Handle(TDF_Attribute)& theTarget,
BinObjMgt_RRelocationTable&) const
序列化 (Transient → Persistent)
void Paste(const Handle(TDF_Attribute)& theSource,
BinObjMgt_Persistent& theTarget,
BinObjMgt_SRelocationTable&) const
4. 数据序列化策略
4.1 TFunction_Function 的数据结构
从实现可以看出,该类处理两个核心数据:
// 序列化数据
theTarget << aS->GetDriverGUID(); // GUID 标识符
theTarget << aS->GetFailure(); // 失败状态(整数)
// 反序列化数据
theSource >> aGUID; // 读取 GUID
theSource >> aValue; // 读取失败状态
4.2 错误处理机制
Standard_Boolean ok = theSource >> aGUID;
if (ok) {
// 继续处理...
ok = theSource >> aValue;
}
return ok; // 返回操作状态
5. 架构优势
5.1 分离关注点 (Separation of Concerns)
- 业务逻辑:
TFunction_Function
类专注于功能实现 - 序列化逻辑:
BinMFunction_FunctionDriver
专注于数据转换 - 存储格式:
BinObjMgt_Persistent
负责二进制格式
5.2 可扩展性
// 构造函数中注册类型信息
BinMFunction_FunctionDriver(const Handle(Message_Messenger)& theMsgDriver)
: BinMDF_ADriver(theMsgDriver, STANDARD_TYPE(TFunction_Function)->Name())
5.3 类型安全
// 使用强类型转换确保类型安全
Handle(TFunction_Function) anAtt = Handle(TFunction_Function)::DownCast(theTarget);
Handle(TFunction_Function) aS = Handle(TFunction_Function)::DownCast(theSource);
6. 设计意图总结
- 职责单一:每个驱动程序只负责一种属性类型的序列化
- 接口统一:所有驱动程序遵循相同的基类接口
- 易于扩展:添加新属性类型只需实现新的驱动程序
- 错误处理:提供完整的错误检查和状态返回机制
- 类型安全:通过强类型系统确保操作的正确性
这种设计使得 OCCT 的序列化框架具有良好的模块化、可维护性和可扩展性,是企业级 CAD 软件架构设计的典型范例。