Skip to content

Commit

Permalink
Region* refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ctapmex committed Sep 28, 2024
1 parent 977f515 commit d482973
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 223 deletions.
8 changes: 3 additions & 5 deletions src/colorer/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

/** Exception class.
Defines throwable exception.
@ingroup common
*/
class Exception : public std::exception
{
Expand All @@ -22,17 +21,16 @@ class Exception : public std::exception
Exception(Exception&&) = default;
Exception& operator=(Exception&&) = default;

/** Returns exception message
*/
[[nodiscard]] const char* what() const noexcept override;
/** Returns exception message */
[[nodiscard]]
const char* what() const noexcept override;

protected:
std::string what_str;
};

/**
InputSourceException is thrown, if some IO error occurs.
@ingroup common
*/
class InputSourceException : public Exception
{
Expand Down
49 changes: 26 additions & 23 deletions src/colorer/Region.h
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
#ifndef COLORER_REGION_H
#define COLORER_REGION_H

#include <utility>
#include "colorer/Common.h"

/**
HRC Region implementation.
Contains information about HRC Region and it attributes:
<ul>
<li>name
<li>description
<li>parent
</ul>
@ingroup colorer
Contains information about HRC Region and it attributes.
*/
class Region
{
public:
/** Full Qualified region name (<code>def:Text</code> for example) */
[[nodiscard]] virtual const UnicodeString& getName() const
[[nodiscard]]
virtual const UnicodeString& getName() const
{
return *name;
return name;
}

/** Region description */
[[nodiscard]] virtual const UnicodeString& getDescription() const
[[nodiscard]]
virtual const UnicodeString& getDescription() const
{
return *description;
}

/** Direct region ancestor (parent) */
[[nodiscard]] virtual const Region* getParent() const
[[nodiscard]]
virtual const Region* getParent() const
{
return parent;
}
/** Quick access region id (incrementable) */
[[nodiscard]] virtual size_t getID() const

/** Quick access region id (incremental) */
[[nodiscard]]
virtual size_t getID() const
{
return id;
}
/** Checks if region has the specified parent in all of it's ancestors.

/** Checks if region has the specified parent in all of its ancestors.
This method is useful to check if region has specified parent,
and use this information, as region type specification.
For example, <code>def:Comment</code> has <code>def:Syntax</code> parent,
so, some syntax checking can be made with it's content.
so, some syntax checking can be made with its content.
*/
bool hasParent(const Region* region) const
{
const Region* elem = this;
auto elem = this;
while (elem != nullptr) {
if (region == elem) {
return true;
Expand All @@ -53,32 +56,32 @@ class Region
}
return false;
}

/**
Basic constructor.
Used only by HrcLibrary.
*/
Region(const UnicodeString& _name, const UnicodeString* _description, const Region* _parent,
size_t _id)
Region(UnicodeString _name, const UnicodeString* _description, const Region* _parent, const size_t _id)
: name(std::move(_name)),parent(_parent), id(_id)
{
name = std::make_unique<UnicodeString>(_name);
if (_description != nullptr) {
description = std::make_unique<UnicodeString>(*_description);
}
parent = _parent;
id = _id;
}

virtual ~Region() = default;

Region(Region&&) = delete;
Region(const Region&) = delete;
Region& operator=(const Region&) = delete;
Region& operator=(Region&&) = delete;

protected:
/** Internal members */
uUnicodeString name;
UnicodeString name;
uUnicodeString description;
const Region* parent;

/** unique id of Region */
size_t id;
};

Expand Down
1 change: 0 additions & 1 deletion src/colorer/common/Exception.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "colorer/Exception.h"
#include "colorer/Common.h"

Exception::Exception(const char* msg) noexcept : what_str {msg} {}

Expand Down
22 changes: 10 additions & 12 deletions src/colorer/handlers/RegionDefine.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#ifndef _COLORER_REGIONDEFINE_H_
#define _COLORER_REGIONDEFINE_H_
#ifndef COLORER_REGIONDEFINE_H
#define COLORER_REGIONDEFINE_H

/**
* Object contains information about region mapping into real colors or other properties.
* This class represents abstract mapping information and declares required methods
* to be implemented in it's subclasses.
*
* @ingroup colorer_handlers
* to be implemented in its subclasses.
*/
class RegionDefine
{
public:
/**
* Enumeration to distinguish different types of region mapping
* Do not use RTTI because of compatibility problems
*
* @ingroup colorer_handlers
*/
enum class RegionDefineType {
UNKNOWN_REGION = 0,
Expand All @@ -26,12 +22,12 @@ class RegionDefine
/**
* Class type identifier
*/
RegionDefineType type = RegionDefineType::UNKNOWN_REGION;
RegionDefineType type {RegionDefineType::UNKNOWN_REGION};

/**
* Completes region define values with it's parent values.
* Completes region define values with its parent values.
* If region define has some incomplete information (fe some
* transparent fields), this methods completes them with
* transparent fields), these methods completes them with
* passed parent's values.
*/
virtual void assignParent(const RegionDefine* parent) = 0;
Expand All @@ -56,14 +52,16 @@ class RegionDefine
}

RegionDefine(const RegionDefine& rd) = delete;

/**
* Clones current region and creates it's duplicate.
* To be implemented in subclasses.
*/
[[nodiscard]] virtual RegionDefine* clone() const = 0;
[[nodiscard]]
virtual RegionDefine* clone() const = 0;

/** Default Destructor */
virtual ~RegionDefine() = default;

RegionDefine(RegionDefine&&) = delete;
RegionDefine& operator=(RegionDefine&&) = delete;

Expand Down
32 changes: 16 additions & 16 deletions src/colorer/handlers/RegionMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@

std::vector<const RegionDefine*> RegionMapper::enumerateRegionDefines() const
{
std::vector<const RegionDefine*> r;
r.reserve(regionDefines.size());
std::vector<const RegionDefine*> result(regionDefines.size());
for (const auto& regionDefine : regionDefines) {
r.push_back(regionDefine.second.get());
result.push_back(regionDefine.second.get());
}
return r;
return result;
}

const RegionDefine* RegionMapper::getRegionDefine(const Region* region) const
{
if (!region)
if (!region) {
return nullptr;
}

// search in cache
const RegionDefine* rd = nullptr;
const RegionDefine* result = nullptr;
if (region->getID() < regionDefinesCache.size()) {
rd = regionDefinesCache.at(region->getID());
}
// RegionDefine maybe not yet in cache
if (rd != nullptr) {
return rd;
result = regionDefinesCache.at(region->getID());
// RegionDefine maybe not yet in cache
if (result != nullptr) {
return result;
}
}

if (regionDefinesCache.size() < region->getID() + 1) {
regionDefinesCache.resize(region->getID() * 2);
}

auto rd_new = regionDefines.find(region->getName());
const auto rd_new = regionDefines.find(region->getName());
if (rd_new != regionDefines.end()) {
regionDefinesCache.at(region->getID()) = rd_new->second.get();
return rd_new->second.get();
}

if (region->getParent()) {
rd = getRegionDefine(region->getParent());
regionDefinesCache.at(region->getID()) = rd;
result = getRegionDefine(region->getParent());
regionDefinesCache.at(region->getID()) = result;
}
return rd;
return result;
}

const RegionDefine* RegionMapper::getRegionDefine(const UnicodeString& name) const
{
auto tp = regionDefines.find(name);
const auto tp = regionDefines.find(name);
if (tp != regionDefines.end()) {
return tp->second.get();
}
Expand Down
33 changes: 15 additions & 18 deletions src/colorer/handlers/RegionMapper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _COLORER_REGIONMAPPERIMPL_H_
#define _COLORER_REGIONMAPPERIMPL_H_
#ifndef COLORER_REGIONMAPPER_H
#define COLORER_REGIONMAPPER_H

#include <unordered_map>
#include <vector>
Expand All @@ -9,10 +9,8 @@
#include "colorer/xml/XmlInputSource.h"

/** Abstract RegionMapper.
Stores all region mappings in hashtable and sequental vector for Region -> RegionDefine
mappings.
@ingroup colorer_handlers
*/
* Stores all region mappings in hashtable and sequential vector for Region -> RegionDefine mappings.
*/
class RegionMapper
{
public:
Expand All @@ -24,33 +22,32 @@ class RegionMapper
virtual void loadRegionMappings(XmlInputSource& is) = 0;

/** Saves all loaded region defines into @c writer.
Note, that result document would not be equal
to input one, because there could be multiple input
documents.
*/
* Note, that result document would not be equal
* to input one, because there could be multiple input documents.
*/
virtual void saveRegionMappings(Writer* writer) const = 0;

/** Changes specified region definition to @c rdnew
@param region Region full qualified name.
@param rdnew New region definition to replace old one
*/
* @param region Region full qualified name.
* @param rdnew New region definition to replace old one
*/

virtual void setRegionDefine(const UnicodeString& region, const RegionDefine* rdnew) = 0;

/** Enumerates all loaded region defines.
@return RegionDefine with specified internal index, or null if @c idx is too big
*/
* @return std::vector of RegionDefine
*/
std::vector<const RegionDefine*> enumerateRegionDefines() const;

/**
* Searches mapped region define value.
* @return Region define, associated with passed @c region
* parameter, or null if nothing found
* @return Region define, associated with passed @c region parameter, or null if nothing found
*/
const RegionDefine* getRegionDefine(const Region* region) const;

/**
* Searches mapped region define value with qualified name @c name.
* Searches mapped region define value with qualified name, or null if nothing found
* @param name
*/
const RegionDefine* getRegionDefine(const UnicodeString& name) const;

Expand Down
Loading

0 comments on commit d482973

Please sign in to comment.