40 #ifndef _objectsettings_cpp
41 #define _objectsettings_cpp
43 #include "objectsettings.h"
44 #include "stringtool.h"
46 #include "Resource/resourceutilities.h"
77 this->
SubSets.push_back( NewSubSet );
85 this->
SubSets.push_back( ToBeAdded );
92 if( Name == (*it)->GetName() ) {
93 if(0 == Which)
return (*it);
102 return this->
SubSets.at(Index);
109 if( ToBeRemoved == (*it) ) {
122 if( Name == (*it)->GetName() ) {
136 (*it)->ParentSetOrGroup = NULL;
146 {
return this->
SubSets.begin(); }
149 {
return this->
SubSets.begin(); }
152 {
return this->
SubSets.end(); }
155 {
return this->
SubSets.end(); }
162 ParentSetOrGroup(NULL)
179 this->
Settings.insert( std::pair<String,String>(SettingName,StringValue) );
186 if( SetIt != this->
Settings.end() )
return (*SetIt).second;
261 {
return this->
File; }
280 if( Name == (*SaveIt)->GetName() )
290 if( Group == (*SaveIt) ) {
334 SettingsDoc.
Load(SettingsStream);
344 if( !RootNode.
Empty() ) {
346 if( !RootNode.
Empty() ) {
357 for( SettingGroupVector::iterator It = RetVec->begin() ; It != RetVec->end() ; ++It )
389 if( !CurrAttrib.
Empty() )
405 CurrAttrib.
SetValue( (*SetIt).second );
430 this->
SettingGroups.insert( std::pair<String,ObjectSettingGroup*>(Name,NewGroup) );
437 if( SetIt != this->
SettingGroups.end() )
return (*SetIt).second;
445 delete (*SetIt).second;
459 delete (*SetIt).second;
470 this->
SettingFiles.insert( std::pair<String,ObjectSettingFile*>(FileName,NewFile) );
477 if( SettingFileIt != this->
SettingFiles.end() )
return (*SettingFileIt).second;
502 ToDestroy = (*SettingFileIt).second;
511 if( FileName.empty() )
515 if( SettingFileIt != this->
SettingFiles.end() ) GroupFile = (*SettingFileIt).second;
519 this->
SettingFiles.insert( std::pair<String,ObjectSettingFile*>(FileName,GroupFile) );
522 GroupFile->AddGroup(Group);
527 if( FileName.empty() )
531 if( SettingFileIt != this->SettingFiles.end() ) (*SettingFileIt).second->RemoveGroup(Group);
538 void ObjectSettingsHandler::SetSettingsFilePath(
const String& Path)
540 size_t FirstDol = Path.find_first_of(
'$');
541 size_t SecondDol = Path.find_first_of(
'$',FirstDol+1);
542 if(FirstDol != String::npos && SecondDol != String::npos) {
543 String PathKeyWord = Path.substr( FirstDol + 1, ( SecondDol - FirstDol ) - 1 );
544 String RestOfPath = Path.substr( SecondDol + 1 );
545 String ActualPath = Resource::ResolveDataPathFromString(PathKeyWord);
546 this->SettingsFilePath = ActualPath + RestOfPath;
548 this->SettingsFilePath = Path;
551 if( this->AutoGenPath )
552 Resource::CreateDirectoryPath(this->SettingsFilePath);
555 const String& ObjectSettingsHandler::GetSettingsFilePath()
const
557 return this->SettingsFilePath;
560 void ObjectSettingsHandler::SetCurrentSettingsSaveFile(
const String& FileName)
562 this->CurrentSettingsSaveFile = FileName;
565 ConstString& ObjectSettingsHandler::GetCurrentSettingsSaveFile()
const
567 return this->CurrentSettingsSaveFile;
575 String FilePath = Resource::ResourceManager::GetSingletonPtr()->GetAssetPath(FileName,Group);
576 if( FilePath.empty() ) {
577 MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,
"Failed to find path for file \"" + FileName +
"\", in group \"" + Group +
"\".");
579 return this->LoadSettingsFromFile(FileName,FilePath);
584 return this->LoadSettingsFromFile(FileName,Path);
589 if( this->SettingsFilePath.empty() ) {
590 MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,
"Attempting to use a preset path that hasn't been set.");
592 return this->LoadSettingsFromFile(FileName,this->SettingsFilePath);
601 XML::Node CurrGroupNode = (*CurrGroupIt);
602 String SettingGroupName = CurrGroupNode.
Name();
604 if(
"Current" != SettingGroupName ) {
605 this->SettingGroups.insert( std::pair<String,ObjectSettingGroup*>(SettingGroupName,NewGroup) );
606 RetVec->push_back(NewGroup);
608 if(!SetAfter) SetAfter = NewGroup;
609 else {
MEZZ_EXCEPTION(ExceptionBase::II_DUPLICATE_IDENTITY_EXCEPTION,
"Multiple \"Current\" setting groups detected while loading settings."); }
616 this->LoadSettingSetFromXML( CurrSetNode, NewSet );
620 this->ApplySettingGroupImpl(SetAfter);
629 void ObjectSettingsHandler::SetAutoGenPath(
Boole Enable)
630 { this->AutoGenPath = Enable; }
632 Boole ObjectSettingsHandler::GetAutoGenPath()
const
633 {
return this->AutoGenPath; }
635 void ObjectSettingsHandler::SetAutoGenFiles(
Boole Enable)
636 { this->AutoGenFiles = Enable; }
638 Boole ObjectSettingsHandler::GetAutoGenFiles()
const
639 {
return this->AutoGenFiles; }
641 void ObjectSettingsHandler::SaveAllSettings()
643 if( this->SettingsFilePath.empty() ) {
644 MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,
"Attempting to use a preset path that hasn't been set.");
648 for(
SettingFilesIterator SettingFileIt = this->SettingFiles.begin() ; SettingFileIt != this->SettingFiles.end() ; ++SettingFileIt )
651 Boole IsCurrentSettingsFile = ( CurrentSettingsSaveFile == CurrFile->
GetFileName() );
652 if( CurrFile->
GetNeedsSave() || IsCurrentSettingsFile ) {
653 if( IsCurrentSettingsFile )
654 GroupNames.push_back(
"Current" );
657 GroupNames.push_back( (*SetGroupIt)->GetName() );
659 this->SaveSettingsToFile(GroupNames,(*SettingFileIt).second->GetFileName(),SettingsFilePath);
667 void ObjectSettingsHandler::SaveSettingsByFile(
const String& FileName,
const String& Path)
670 if( this->CurrentSettingsSaveFile == FileName )
671 GroupNames.push_back(
"Current" );
674 if( SetFileIt == this->SettingFiles.end() ) {
675 MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,
"Setting file \"" + FileName +
"\" was not found when attempting to save.");
680 GroupNames.push_back( (*SetGroupIt)->GetName() );
682 this->SaveSettingsToFile(GroupNames,FileName,Path);
683 (*SetFileIt).second->SetNeedsSave(
false);
686 void ObjectSettingsHandler::SaveSettingsByFile(
const String& FileName)
688 if( this->SettingsFilePath.empty() ) {
689 MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,
"Attempting to use a preset path that hasn't been set.");
693 if( this->CurrentSettingsSaveFile == FileName )
694 GroupNames.push_back(
"Current" );
697 if( SetFileIt == this->SettingFiles.end() ) {
698 MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,
"Setting file \"" + FileName +
"\" was not found when attempting to save.");
703 GroupNames.push_back( (*SetGroupIt)->GetName() );
705 this->SaveSettingsToFile(GroupNames,FileName,SettingsFilePath);
706 (*SetFileIt).second->SetNeedsSave(
false);
711 this->SaveSettingsToFile(GroupNames,FileName,Path);
716 if( this->SettingsFilePath.empty() ) {
717 MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,
"Attempting to use a preset path that hasn't been set.");
720 this->SaveSettingsToFile(GroupNames,FileName,SettingsFilePath);
723 void ObjectSettingsHandler::SaveSettingsToXML(
XML::Node& RootSettings,
Boole SaveCurrent)
728 GroupNames.push_back(
"Current" );
730 for(
SettingGroupIterator GroupNameIt = this->SettingGroups.begin() ; GroupNameIt != this->SettingGroups.end() ; ++GroupNameIt )
732 GroupNames.push_back( (*GroupNameIt).first );
734 this->SaveSettingsToXML(GroupNames,RootSettings);
739 for( StringVector::iterator StrIt = GroupNames.begin() ; StrIt != GroupNames.end() ; ++StrIt )
741 if(
"Current" == (*StrIt) ) {
742 this->AppendCurrentSettings(RootSettings);
745 if( GroupIt == this->SettingGroups.end() ) {
746 MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,
"Attempting to save setting group \"" + (*StrIt) +
"\", which does not exist.");
753 this->SaveSettingSetToXML( SetNode, (*SubSetIt) );
762 void ObjectSettingsHandler::ProtoSerialize(
XML::Node& SelfRoot)
const
781 void ObjectSettingsHandler::ProtoDeSerialize(
const XML::Node& SelfRoot)
787 if( !AutoGenNode.
Empty() ) {
789 if(!CurrAttrib.
Empty()) {
790 this->AutoGenPath = StringTools::ConvertToBool( CurrAttrib.
AsString() );
793 if(!CurrAttrib.
Empty()) {
794 this->AutoGenFiles = StringTools::ConvertToBool( CurrAttrib.
AsString() );
799 if( !PathNode.
Empty() ) {
801 if( !CurrAttrib.
Empty() ) {
805 if( !PathPreset.empty() ) {
806 this->SetSettingsFilePath(PathPreset);
811 if( !FilesNode.
Empty() ) {
814 String FileName, FilePath, FileGroup;
816 CurrAttrib = (*SetFileIt).GetAttribute(
"FileName");
817 if( !CurrAttrib.
Empty() ) {
821 CurrAttrib = (*SetFileIt).GetAttribute(
"Path");
822 if( !CurrAttrib.
Empty() ) {
825 CurrAttrib = (*SetFileIt).GetAttribute(
"Group");
826 if( !CurrAttrib.
Empty() ) {
831 if( FilePath.empty() ) {
832 if(FileGroup.empty()) this->LoadSettings(FileName);
833 else this->LoadSettingsFromGroup(FileName,FileGroup);
835 this->LoadSettings(FileName,FilePath);
841 String ObjectSettingsHandler::GetSerializableName()
842 {
return "ObjectSettingsHandler"; }
void ApplySettingGroup(const String &GroupName)
Applies all the settings (and their values) defined in a setting group.
~ObjectSettingFile()
Class destructor.
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
Thrown when duplicates of teh same identity string exist.
ConstString & GetName() const
Gets the name of this container.
Boole GetNeedsSave() const
Gets whether or not this file has been updated and needs to be saved.
A light-weight handle for manipulating attributes in DOM tree.
std::vector< String > StringVector
This is a simple datatype for a vector container of strings.
SettingFilesContainer::const_iterator ConstSettingFilesIterator
Const Iterator type for ObjectSettingGroup instances stored by this class.
attribute_iterator attributes_end() const
Get an Attribute iterator that references the one past the last Attribute on this Node...
ObjectSettingSetContainer * ParentSetOrGroup
A pointer to the parent container of this ObjectSettingSet.
const String & GetOptionalFileName() const
Gets the file name this group will be saved to.
ObjectSettingFile * CreateSettingFile(const String &FileName)
Creates a new Setting file that will track which groups are a part of it.
virtual String GetObjectRootNodeName() const =0
Gets the name to be given to a Node with this handlers serialized data.
ObjectSettingGroup(const String &Name)
Class constructor.
Clear the contents of the file when opening. Note that this will also create the file if it's not fou...
SettingFilesContainer SettingFiles
A container storing all the file saving configuration used by this handler.
bool Boole
Generally acts a single bit, true or false.
SubSetVector::const_iterator ConstSubSetIterator
Const Iterator type for ObjectSettingSet instances stored by this class.
SubSetVector::iterator SubSetIterator
Iterator type for ObjectSettingSet instances stored by this class.
void _SetOptionalFile(ObjectSettingFile *File)
Sets the file this group will be saved to.
SaveGroupsContainer & GetGroups()
Gets the container with the groups to be saved to this file.
ObjectSettingFile(const String &FileName)
Class constructor.
attribute_iterator attributes_begin() const
Get an Attribute iterator that references the first Attribute on this Node.
void LoadSettingSetFromXML(XML::Node &XMLNode, ObjectSettingSet *Set)
Populates an ObjectSettingSet with settings stored in XML.
SaveGroupsContainer::const_iterator ConstSaveGroupsIterator
Const Iterator type for ObjectSettingGroup instances stored by this class.
Thrown when the requested identity could not be found.
Thrown when a file was expected to be there, but was not.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
A class that store's a named set of settings for an object.
A simple reference counting pointer.
Declaration of FileStream.
void DestroyAllObjectSettingSets()
Destroys all child Sub-Sets of this container.
String File
The name of the file the stored ObjectSettingGroup instances will be saved to.
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
void SetSettingValue(const String &SettingName, const String &StringValue)
Sets a setting in this set...of settings.
SaveGroupsIterator SaveGroupsEnd()
Gets an iterator to one-passed the last group in this file.
ObjectSettingGroup * CreateSettingGroup(const String &Name)
Creates a new blank setting group that can have it's settings populated.
void AddGroup(ObjectSettingGroup *Group)
Adds a group to be saved to this file.
SettingsIterator SettingsEnd()
Gets an iterator to one passed the last setting in this set.
ObjectSettingFile * GetSettingFile(const String &FileName)
Gets a Setting file by name.
void DestroyAllSettingGroups()
Destroys all setting groups stored in this handler.
bool Empty() const
Is this storing anything at all?
SubSetIterator SubSetEnd()
Gets an iterator to one passed the last subset of settings in this set.
ParseResult Load(std::basic_istream< char, std::char_traits< char > > &stream, unsigned int options=ParseDefault, Encoding DocumentEncoding=EncodingAuto)
Load XML from a stream.
virtual ~ObjectSettingSetContainer()
Class destructor.
virtual ~ObjectSettingSet()
Class destructor.
ObjectSettingGroup * GetSettingGroup(const String &Name) const
Gets a setting group by name.
ObjectSettingsHandler()
Class constructor.
SettingGroupContainer::const_iterator ConstSettingGroupIterator
Const Iterator type for ObjectSettingGroup instances stored by this class.
virtual void RemoveChildObjectSettingSet(ObjectSettingSet *ToBeRemoved)
Removes a Sub-Set of this container.
This class represents a file to be saved containing one or more OhjectSettingGroup's.
void DestroyAllSettingFiles()
Destroys all Setting files in this handler.
void Save(Writer &WriterInstance, const Char8 *indent="\t", unsigned int flags=FormatDefault, Encoding DocumentEncoding=EncodingAuto) const
Save XML document to WriterInstance.
bool SetValue(const Char8 *rhs)
Set the value of this.
uint16_t UInt16
An 16-bit unsigned integer.
bool SetName(const Char8 *rhs)
Set the name of .
ObjectSettingSetContainer * GetParentSetOrGroup() const
Gets the Parent SettingsSet to this set.
CountedPtr< SettingGroupVector > LoadSettingsFromFile(const String &FileName, const String &Path)
Loads all ObjectSettingGroup instances serialized to a XML file into usable ObjectSettingGroup instan...
virtual void AddChildObjectSettingSet(ObjectSettingSet *ToBeAdded)
Adds a SettingSet as a child of this container.
Permit write operations on the stream.
Child node iterator (a bidirectional iterator over a collection of Node)
void SaveSettingSetToXML(XML::Node &XMLNode, ObjectSettingSet *Set)
Saves an ObjectSettingSet to an XML node.
void _MarkUpdated()
Marks this container and relevant parent containers as updated.
A light-weight handle for manipulating nodes in DOM tree.
iterator begin() const
Get a Child node iterator that references the first child Node.
const String ConstString
A Datatype used to a series of imutable characters.
void SaveSettingsToXML(XML::Node &RootSettings, Boole SaveCurrent=true)
Saves all the current setting groups as children of the provided XML node.
void SetName(const String &Name)
Sets the name of this container.
String GetSettingValue(const String &SettingName) const
Gets a String representing the Value currently assigned to the existing setting.
iterator end() const
Get a Child node iterator that references one past the last child Node.
bool Empty() const
Is this storing anything at all?
ObjectSettingSetContainer(const String &Name)
Class constructor.
ObjectSettingFile * GetOptionalFile() const
Gets the Setting file instance this group will be saved to.
Boole NeedsSave
Stores whether or not the settings have been updated and this file needs to be saved/refreshed.
SubSetVector SubSets
A container of ObjectSettingSet instances owned by this container.
The defintion of the Resource Manager.
void _MarkUpdated()
Marks this container and relevant parent containers as updated.
SubSetIterator SubSetBegin()
Gets an iterator to the first subset of settings in this set.
std::vector< ObjectSettingGroup * > SaveGroupsContainer
Basic container type for ObjectSettingGroup storage by this class.
SaveGroupsIterator SaveGroupsBegin()
Gets an iterator to the first group in this file.
void DestroyChildObjectSettingSet(const String &Name)
Destroys a Sub-Set of this container.
ObjectSettingSet * GetChildObjectSettingSet(const String &Name, UInt16 Which=0) const
Gets a Sub-Set of this container by name.
virtual ObjectSettingSet * CreateChildObjectSettingSet(const String &Name)
Creates a new SettingSet as a child of this container.
virtual void _MarkUpdated()=0
Marks this container and relevant parent containers as updated.
Thrown when the available information should have worked but failed for unknown reasons.
SettingGroupContainer::iterator SettingGroupIterator
Iterator type for ObjectSettingGroup instances stored by this class.
Attribute iterator (a bidirectional iterator over a collection of Attribute).
CountedPtr< SettingGroupVector > LoadSettingsFromXML(XML::Node &RootSettings)
Loads settings from an xml node.
The root node of any xml hierarchy is a Document.
void AddGroupToFile(ObjectSettingGroup *Group, const String &FileName)
Assigns a SettingGroup to a file name that will be used when settings are saved.
const Char8 * Name() const
Get the name of this Attribute.
Thrown when the identity string wasn't valid at all.
ObjectSettingSet(const String &Name)
Class constructor.
Document declaration, i.e. ''.
ObjectSettingGroup * GetGroup(const String &Name) const
Gets a group being saved to this file.
void SaveSettingsToFile(StringVector &GroupNames, const String &FileName, const String &Path)
Saves a collection of ObjectSettingGroups stored by this handler to a file as XML.
This represents a stream to a file on disk using the C++ file stream API.
virtual void ApplySettingGroupImpl(ObjectSettingGroup *Group)=0
Applies all the settings stored by a ObjectSettingGroup.
A base class for other classes that store ObjectSettingSet's.
The bulk of the engine components go in this namspace.
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
ObjectSettingFile * OptionalFile
A pointer to the ObjectSettingFile instance this setting group will be saved to.
void DestroySettingFile(const String &FileName)
Destroys a Setting file by name.
SaveGroupsContainer SaveGroups
A container of ObjectSettingGroup instances that will be saved to the file representing by this...
virtual ~ObjectSettingGroup()
Class destructor.
void RemoveGroup(ObjectSettingGroup *Group)
Removes a group from this file.
SettingFilesContainer::iterator SettingFilesIterator
Iterator type for ObjectSettingGroup instances stored by this class.
SettingsMap::const_iterator ConstSettingsIterator
Const Iterator type for settings stored by this class.
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
const String & GetFileName() const
Gets the name of the file.
SettingGroupContainer SettingGroups
A container storing all the SettingGroups owned by this handler.
void SetNeedsSave(Boole Save)
Sets whether or not this file needs saving.
String CurrentSettingsSaveFile
Stores the name of the file where the "Current" settings group will be saved to.
SaveGroupsContainer::iterator SaveGroupsIterator
Iterator type for ObjectSettingGroup instances stored by this class.
std::vector< ObjectSettingGroup * > SettingGroupVector
Convenience typedef for Load method returns.
virtual ~ObjectSettingsHandler()
Class destructor.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
A class that store's a collection of SettingSets that can be applied together.
SettingsMap::iterator SettingsIterator
Iterator type for settings stored by this class.
std::string String
A datatype used to a series of characters.
void DestroySettingGroup(const String &Name)
Destroys a setting group by name.
String ContainerName
The name of this container.
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
SettingsMap Settings
A map container storing the settings owned by this set and their values.
SettingsIterator SettingsBegin()
Gets an iterator to the first setting in this set.
const unsigned int FormatIndent
Indent the nodes that are written to output stream with as many indentation strings as deep the node ...