// FileType.cxx #include "FileType.h" #include using std::ostream; using std::string; using hes::Index; using hes::FileType; //********************************************************************** // Local definitions. //********************************************************************** namespace { // Index indicating an invalid file type. const Index INVALID_INDEX = 0; // Range of valid indices. const Index MINIMUM_INDEX = 1; const Index MAXIMUM_INDEX = 3; // Names for the file types. const string NAME[MAXIMUM_INDEX+1] = { "INVALID", "TEXT", "REFERENCE", "BNL_ROOT" }; } //********************************************************************** // Static members. //********************************************************************** // Assign indices to known file types. // Existing entries should not be modified. // If new entries are added, then the modify the range above. const FileType FileType::INVALID(INVALID_INDEX); const FileType FileType::TEXT(1); const FileType FileType::REFERENCE(2); const FileType FileType::BNL_ROOT(3); //********************************************************************** // Static member functions. //********************************************************************** // Invalid index. const Index FileType::invalid_index() { return INVALID_INDEX; } //********************************************************************** // Minimum index. const Index FileType::minimum_index() { return MINIMUM_INDEX; } //********************************************************************** // Maximum index. const Index FileType::maximum_index() { return MAXIMUM_INDEX; } //********************************************************************** // Member functions. //********************************************************************** // Constructor. FileType::FileType(Index idx) : m_idx(idx) { // Restrict to known types listed above. if ( m_idx < MINIMUM_INDEX || m_idx > MAXIMUM_INDEX ) { m_idx = INVALID_INDEX; } } //********************************************************************** // Is this a valid file type? bool FileType::is_valid() const { return index() != INVALID_INDEX; } //********************************************************************** // Index. Index FileType::index() const { return m_idx; } //********************************************************************** // Name. string FileType::name() const { return NAME[m_idx]; } //********************************************************************** // Equality. bool FileType::operator==(const FileType& rhs) const { if ( is_valid() ) { if ( rhs.is_valid() ) { return m_idx == rhs.m_idx; } else { return false; } } else { return ! rhs.is_valid(); } } //********************************************************************** // Free functions. //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const FileType& rhs) { if ( ! rhs.is_valid() ) { lhs << "invalid file type"; return lhs; } string name = rhs.name(); if ( name == "" ) name = "UNKNOWN"; lhs << name; return lhs; } //**********************************************************************