Accessor Class Reference


Detailed Description

Class handling access to adsk::Data::Structures and adsk::Data::Associations located in a file.

This class is used to read and rewrite a collection of adsk::Data::Structure and adsk::Data::Associtions from/to an existing file.

#include <adskDataAccessor.h>

List of all members.

Classes

struct  StructureNameLessThan
 Functor for Structure ordering in a std::set. More...

Public Types

typedef std::set< const
Structure
*, StructureNameLessThan
StructureSet
 Structures are kept in a set the forbids duplicated names.
typedef std::map< std::string,
Associations
AssociationsMap
 Associations are kept in a map that forbids duplicated names.

Public Member Functions

virtual ~Accessor ()
 Releases any Structure and Associations objects currently held.
bool read (const std::string &fileName, std::string &errors)
 Reads the structures and associations from the specified file.
virtual bool write (std::string &errors) const
 Rewrites the file with the edited structures and associations.
const std::string & fileName () const
 Returns the file name passed to the last invocation of read().
const StructureSetstructures () const
 Get the adsk::Data::Structures currently held by this instance.
void setStructures (const StructureSet &structures)
 Changes the adsk::Data::Structures currently held by this class.
const AssociationsMapassociations () const
 Get the adsk::Data::Associations currently held by this instance.
AssociationsMapassociations ()
 Get the adsk::Data::Associations currently held by this instance.
void clear ()
 Clears the structures and associations present in the instance.

Static Public Member Functions

static std::set< std::string > supportedExtensions ()
 Returns the supported filename extensions.
static std::auto_ptr< AccessorreadFile (const std::string &fileName, std::string &errors)
 Creates and returns an accessor that can handle the specified file.

Protected Member Functions

 Accessor ()
 Constructs an empty accessor.
virtual bool performRead (std::string &errors)=0
 Invoked by read() to access the current file.

Member Typedef Documentation

typedef std::map< std::string, Associations > AssociationsMap

Associations are kept in a map that forbids duplicated names.

The actual file format defines the semantic for the Associations names. For example, for certain formats, an empty-name may mean file-level associations rather than per-object associations.


Constructor & Destructor Documentation

~Accessor ( ) [virtual]

Releases any Structure and Associations objects currently held.

If the object gets destroyed before any call to write() is made, the changes will be dropped without any change done to the file.

Accessor ( ) [protected]

Constructs an empty accessor.

Upon construction, the accessor is not bound to any file. Calls to read() will load the Structure/Associations object from a given file, provided they are of the format supported by this instance.


Member Function Documentation

bool read ( const std::string &  fileName,
std::string &  errors 
)

Reads the structures and associations from the specified file.

Structures and associations are read into the current accessor instance and can then be read and changed through other methods in this API prior to rewriting them in the file, if needed, through write().

Previous data will be cleared and will not be written to the previous file. Unseccessful read will cause the structures and associations to be emptied, but the new file name will stay.

Parameters:
[in]fileNameName of the file to open and read the structures/associations from.
[out]errorsErrors that happened during reading, if any.
Returns:
true if the read was sucessful.
bool write ( std::string &  errors) const [virtual]

Rewrites the file with the edited structures and associations.

The file is changed in place, so if the caller requires any type of versioning, a copy of the original file must first be taken prior to this call as this does not do it. However, concrete implementations may choose to do so.

The default implementation does nothing and returns false.

Parameters:
[out]errorsErrors that happened during writing, if any.
Returns:
false if there was an error while writing into the file.
const std::string & fileName ( ) const

Returns the file name passed to the last invocation of read().

Returns:
The file name currently used. The reference stays valid until the next call to read().
const Accessor::StructureSet & structures ( ) const

Get the adsk::Data::Structures currently held by this instance.

These are the structures found on the last call to read(), or that were set through setStructures.

Returns:
A reference to the internal structures container. The container cannot be edited in place because structures are ref-counted by this class. The reference stays valid until the Accessor instance is destroyed.
void setStructures ( const StructureSet structures)

Changes the adsk::Data::Structures currently held by this class.

These are the Structures that will be written to the file when it will be written via write(). It is the caller's responsibility to make sure all structures used by the Associations found in this instance's Associations map are present in this set.

Previous adsk::Data::Structures will be unref()'ed, and the new structures will be ref()'ed.

Parameters:
[in]structuresThe new structures.
const Accessor::AssociationsMap & associations ( ) const

Get the adsk::Data::Associations currently held by this instance.

Returns:
A reference to the internal associations container. The container cannot be edited through this reference, but any changes made through the non-const version of this accessor will be visible through the reference.
Accessor::AssociationsMap & associations ( )

Get the adsk::Data::Associations currently held by this instance.

Returns:
A reference to the internal associations container. The container can be edited in place.
void clear ( )

Clears the structures and associations present in the instance.

The file name stays unchanged.

std::set< std::string > supportedExtensions ( ) [static]

Returns the supported filename extensions.

For debugging or informational purposes. The extensions are converted to lowercase.

Returns:
The supported filename extensions, in lowercase.
std::auto_ptr< Accessor > readFile ( const std::string &  fileName,
std::string &  errors 
) [static]

Creates and returns an accessor that can handle the specified file.

Finds the factory whose file name extension matches that of the file name passed in, if any, and creates a new Accessor with the factory's create() method. On success it calls read() on the Accessor and only returns the newly created Accessor if the read has succeeded.

Parameters:
[in]fileNameName of the file to open and read.
[out]errorsErrors that occured during file read, if any.
Returns:
On success, returns an accesor with the file's data already read. Returns a null pointer if no accessor could be found for this type or reading the file fails.
bool performRead ( std::string &  errors) [protected, pure virtual]

Invoked by read() to access the current file.

The current file's name is obtained with fileName(). Concrete implementations must read the structures/associations. Upon success, the structures are read from adsk::Data::Structure::allStructures() and kept in this instance. The associations, on the other hand, must be set by the derived class by editing the associations map returned from associations().

The caller (the read() method) deals with saving/restoring the content of adsk::Data::Structure::allStructures() before/after the call, and clearing the existing structures/associations present in the instance before calling performRead().

Parameters:
[out]errorsErrors that happended during reading, if any.
Returns:
True upon success.

Accessor Accessor Accessor Accessor Accessor Accessor Accessor Accessor Accessor Accessor
Accessor Accessor Accessor Accessor Accessor Accessor Accessor Accessor Accessor Accessor