LT Project: A convenience class for shared data
We are gradually working our way upwards towards a full LibraryBookRecord: we've reached the point where we can look at the core common data.
There are two implementations of the LibraryBookRecord interface, depending on whether they were read in directly from file (in which case they have to intern all the data in each record) or whether there is an in-memory database corresponding to the file, in which case the full set of fields can be held by reference. In both cases, though, they use a core set of data for sorting and searching.
The LibraryBookRecordSecondaryData class handles that common data.
class LibraryBookRecordSecondaryData : public ISettableAuthorContainer,
public IMinimalLibraryBookRecord
{
public:
LibraryBookRecordSecondaryData(const int inId, const bool inIsBibliographic)
: m_bookId(inId), m_secondaryAuthors(inIsBibliographic)
{ }
~LibraryBookRecordSecondaryData() override;
Author getAuthor() const override
{
return m_authorKey;
}
void setAuthor(const std::string &inVal) override
{
m_authorKey.set(inVal);
}
bool matchesOnAuthor(std::string_view inName) const
{
return m_authorKey.containsName(inName);
}
bool matchesOnCollection(const std::string &inName) const
{
return m_collections.matches(inName);
}
bool matchesOnTag(const std::string &inName) const
{
return m_tags.matches(inName);
}
bool isDeaccessioned() const
{
return m_collections.matches("Deaccessioned");
}
bool lessThan(const IMinimalLibraryBookRecord &inFirstRec,
const IMinimalLibraryBookRecord &inSecondRec) const;
std::string_view getTitle() const override
{
return m_title;
}
void setTitle(const std::string &inTitle)
{
m_title = inTitle;
}
constexpr int getId() const
{
return m_bookId;
}
int getTitleSortOffset() const override
{
return m_titleSortOffset;
}
static void SetBibliographicSort()
{
m_sortStrategy = &s_BibliographicStrategy;
}
static void SetTitleSort()
{
m_sortStrategy = &s_TitleStrategy;
}
std::string_view getAuthorLastName() const
{
return m_authorKey.getLastName();
}
void setBookId(const int inID)
{
m_bookId = inID;
}
void setTitleSortOffset(const int inVal)
{
m_titleSortOffset = inVal;
}
void rationalizeTitleSortOffset()
{
if (m_titleSortOffset < 0)
m_titleSortOffset = 0;
}
void setCollections(const std::string &inVal)
{
m_collections.addEncodedList(inVal);
}
void setTags(const std::string &inVal)
{
m_tags.addEncodedList(inVal);
}
const ISecondaryAuthorSet & getSecondaryAuthors() const
{
return m_secondaryAuthors;
}
void setSecondaryAuthors(const std::string &inVal);
void setSecondaryAuthorRoles(const std::string &inVal);
const std::string & getPostTitleContent() const
{
return m_postTitleContent;
}
void setPostTitleContent(const std::string &inVal) override
{
m_postTitleContent = inVal;
}
void expandEntry(const bool inBreakOutCollections, const bool inBreakoutTags,
const ELibraryRecord inType, const std::string &inVal);
private:
int m_bookId;
int m_titleSortOffset = 0;
CollectionSearchableSet m_collections;
TagSearchableSet m_tags;
Author m_authorKey;
std::string m_title;
SecondaryAuthorSet m_secondaryAuthors;
mutable std::string m_postTitleContent;
inline constinit static Log::ThreadedLogger s_Logger;
inline static TitleRecordSortStrategy s_TitleStrategy{ s_Logger };
inline static BibliographicRecordSortStrategy s_BibliographicStrategy{
s_Logger
};
inline static IRecordSortStrategy *m_sortStrategy = &s_TitleStrategy;
};
It tends fairly heavily towards being a data class, with lots of accessors, some setters (because of the incremental creation of record objects from the line records), and a few functions with more extensive logic:
bool LibraryBookRecordSecondaryData::lessThan(const IMinimalLibraryBookRecord &inFirstRec,
const IMinimalLibraryBookRecord &inSecondRec) const
{
return m_sortStrategy->isLessThan(inFirstRec, inSecondRec);
}
void LibraryBookRecordSecondaryData::setSecondaryAuthors(const std::string &inVal)
{
std::ranges::for_each(boost::tokenizer<boost::char_separator<char>>(inVal, pipeSeparator), [&](const auto &inSAVal) {
m_secondaryAuthors.addAuthorName(inSAVal);
});
}
void LibraryBookRecordSecondaryData::setSecondaryAuthorRoles(
const std::string &inVal)
{
int i = 0;
std::ranges::for_each(boost::tokenizer<boost::char_separator<char>>(inVal, pipeSeparator), [&](const auto &inSARVal) {
if (!inSARVal.empty())
m_secondaryAuthors.addAuthorRole(inSARVal, i++);
else
++i;
});
}
void LibraryBookRecordSecondaryData::expandEntry(const bool inBreakOutCollections,
const bool inBreakoutTags,
const ELibraryRecord inType,
const std::string &inVal)
{
switch (inType)
{
case ELibraryRecord::Collections:
if (inBreakOutCollections)
setCollections(inVal);
break;
case ELibraryRecord::Tags:
if (inBreakoutTags)
setTags(inVal);
break;
case ELibraryRecord::Secondary_Author:
{
setSecondaryAuthors(inVal);
}
break;
case ELibraryRecord::Secondary_Author_Roles:
{
setSecondaryAuthorRoles(inVal);
}
break;
default:
break;
}
}
Except for the sorting function lessThan() these are invoked during the data initialization phase.
This is essentially a convenience grouping of other objects, principally to reduce duplication of data models.
Comments
Post a Comment