📄 dictionary.cpp
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........: 960630
// Description...:
// Revisions.....:
//===================================================================
#include <stdafx.h> // Precompiled headers.
#include <copyright.h>
#include <kernel/structures/dictionary.h>
#include <kernel/structures/integerattribute.h>
#include <kernel/structures/floatattribute.h>
#include <kernel/structures/stringattribute.h>
#include <kernel/utilities/iokit.h>
#include <kernel/utilities/creator.h>
#include <kernel/basic/message.h>
//-------------------------------------------------------------------
// Methods for class Dictionary.
//===================================================================
//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================
//-------------------------------------------------------------------
// Method........: Copy constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Dictionary::Dictionary(const Dictionary &in) : Structure(in) {
int i;
// Duplicate all attributes.
for (i = 0; i < in.GetNoAttributes(); i++)
AppendAttribute(dynamic_cast(Attribute *, in.GetAttribute(i)->Duplicate()));
}
//-------------------------------------------------------------------
// Method........: Constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Dictionary::Dictionary() {
}
//-------------------------------------------------------------------
// Method........: Destructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Dictionary::~Dictionary() {
}
//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================
IMPLEMENTIDMETHODS(Dictionary, DICTIONARY, Structure)
//-------------------------------------------------------------------
// Methods inherited from Persistent.
//===================================================================
//-------------------------------------------------------------------
// Method........: Load
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::Load(ifstream &stream) {
Handle<Attribute> attribute;
int no_attributes;
String type;
// Clear the present dictionary.
if (!RemoveAllAttributes())
return false;
// Load the number of attributes.
if (!IOKit::Load(stream, no_attributes))
return false;
int i;
// Load each individual attribute.
for (i = 0; i < no_attributes; i++) {
if (!IOKit::Load(stream, type))
return false;
attribute = dynamic_cast(Attribute *, Creator::Create(IdHolder::GetId(type)));
if (!attribute->Load(stream))
return false;
if (!AppendAttribute(attribute.GetPointer()))
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: Save
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: The type information is really superfluous (since
// attribute saves this, too). However, saving it
// here also avoids having to rollback during loading.
// Revisions.....:
//===================================================================
bool
Dictionary::Save(ofstream &stream) const {
// Save the number of attributes.
if (!IOKit::Save(stream, GetNoAttributes()))
return false;
if (!IOKit::Save(stream, '\n'))
return false;
int i;
// Save each individual attribute, preceeded by its type.
for (i = 0; i < GetNoAttributes(); i++) {
if (!IOKit::Save(stream, IdHolder::GetClassname(GetAttribute(i)->GetId())))
return false;
if (!IOKit::Save(stream, '\n'))
return false;
if (!GetAttribute(i)->Save(stream))
return false;
}
return true;
}
//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================
//-------------------------------------------------------------------
// Method........: Duplicate
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Structure *
Dictionary::Duplicate() const {
return new Dictionary(*this);
}
//-------------------------------------------------------------------
// Method........: Clear
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
void
Dictionary::Clear() {
int i;
// Clear all attributes.
for (i = 0; i < GetNoAttributes(); i++)
GetAttribute(i)->Clear();
}
//-------------------------------------------------------------------
// Attribute administration.
//===================================================================
//-------------------------------------------------------------------
// Method........: GetNoAttributes
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns the number of attributes in the dictionary.
// Comments......:
// Revisions.....:
//===================================================================
int
Dictionary::GetNoAttributes() const {
return attributes_.size();
}
//-------------------------------------------------------------------
// Method........: GetAttribute
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns the specified attribute.
// Comments......:
// Revisions.....:
//===================================================================
Attribute *
Dictionary::GetAttribute(int i) const {
#ifdef _DEBUG
// Index in range?
if ((i < 0) || (i >= GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return NULL;
}
#endif
return const_cast(Attribute *, attributes_[i].GetPointer());
}
//-------------------------------------------------------------------
// Method........: InsertAttribute
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::InsertAttribute(const Attribute *attribute, int i) {
#ifdef _DEBUG
// Index in range? Note that an out-of-range by one at the end is allowed.
if ((i < 0) || (i > GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return false;
}
// Valid attribute object?
if (attribute == NULL) {
Message::Error("Invalid attribute passed to dictionary.");
return false;
}
#endif
// Safeguard due to possible compiler bug..?
Handle<Attribute> handle(const_cast(Attribute *, attribute));
// Do the insertion.
attributes_.insert(attributes_.begin() + i, handle);
return true;
}
//-------------------------------------------------------------------
// Method........: AppendAttribute
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::AppendAttribute(const Attribute *attribute) {
return InsertAttribute(attribute, GetNoAttributes());
}
//-------------------------------------------------------------------
// Method........: RemoveAttribute
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::RemoveAttribute(int i) {
#ifdef _DEBUG
// Index in range?
if ((i < 0) || (i >= GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return false;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -