Skip to content

Commit

Permalink
Image pixel iterator implementation; Improved Runtime cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Epixu committed Mar 14, 2024
1 parent a1e6607 commit 05a2bd9
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 24 deletions.
2 changes: 1 addition & 1 deletion include/Entity/Thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
/// See LICENSE file, or https://www.gnu.org/licenses
///
#pragma once
#include "Unit.hpp"
#include "../../source/Thing.hpp"
#include "../../source/Thing.inl"
#include "../../source/Thing-Gather.inl"
#include "../../source/Thing-Seek.inl"
#include "Unit.hpp"
#include "Pin.hpp"


Expand Down
4 changes: 2 additions & 2 deletions include/Langulus/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ namespace Langulus::A

NOD() constexpr Iterator<true> begin() noexcept;
NOD() constexpr Iterator<false> begin() const noexcept;
NOD() constexpr Iterator<true> last() noexcept;
NOD() constexpr Iterator<false> last() const noexcept;
constexpr A::IteratorEnd end() const noexcept { return {}; }
};

Expand All @@ -93,6 +91,8 @@ namespace Langulus::A
LANGULUS(ABSTRACT) false;

protected:
friend struct Image;

// Current iterator position pointer
Byte const* mValue;
// Iterator position which is considered the 'end' iterator
Expand Down
80 changes: 80 additions & 0 deletions include/Langulus/Image.inl
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,84 @@ namespace Langulus::A
Commit<Traits::Color>(data.Forward());
}

constexpr Image::Iterator<true> Image::begin() noexcept {
return {
this,
reinterpret_cast<const Byte*>(GetData<Traits::Color>()->GetRaw()),
reinterpret_cast<const Byte*>(GetData<Traits::Color>()->GetRawEnd())
};
}

constexpr Image::Iterator<false> Image::begin() const noexcept {
return {
const_cast<Image*>(this),
reinterpret_cast<const Byte*>(GetData<Traits::Color>()->GetRaw()),
reinterpret_cast<const Byte*>(GetData<Traits::Color>()->GetRawEnd())
};
}

/// Construct an iterator
/// @param start - the current iterator position
/// @param end - the ending marker
template<bool M> LANGULUS(INLINED)
constexpr Image::Iterator<M>::Iterator(Image* image, Byte const* it, Byte const* end) noexcept
: mValue {it}
, mEnd {end}
, mImage {image} {}

/// Construct an end iterator
template<bool M> LANGULUS(INLINED)
constexpr Image::Iterator<M>::Iterator(const A::IteratorEnd&) noexcept
: mValue {nullptr}
, mEnd {nullptr}
, mImage {nullptr} {}

/// Compare two iterators
/// @param rhs - the other iterator
/// @return true if iterators point to the same element
template<bool M> LANGULUS(INLINED)
constexpr bool Image::Iterator<M>::operator == (const Iterator& rhs) const noexcept {
return mValue == rhs.mValue;
}

/// Compare iterator with an end marker
/// @param rhs - the end iterator
/// @return true element is at or beyond the end marker
template<bool M> LANGULUS(INLINED)
constexpr bool Image::Iterator<M>::operator == (const A::IteratorEnd&) const noexcept {
return mValue >= mEnd;
}

/// Prefix increment operator
/// @attention assumes iterator points to a valid element
/// @return the modified iterator
template<bool M> LANGULUS(INLINED)
constexpr Image::Iterator<M>& Image::Iterator<M>::operator ++ () noexcept {
mValue += mImage->GetView().GetPixelBytesize();
return *this;
}

/// Suffix increment operator
/// @attention assumes iterator points to a valid element
/// @return the previous value of the iterator
template<bool M> LANGULUS(INLINED)
constexpr Image::Iterator<M> Image::Iterator<M>::operator ++ (int) noexcept {
const auto backup = *this;
operator ++ ();
return backup;
}

/// Check if iterator is valid
template<bool M> LANGULUS(INLINED)
constexpr Image::Iterator<M>::operator bool() const noexcept {
return *this != A::IteratorEnd {};
}

/// Interpret the pixel as a desired color format
template<bool M> template<CT::ColorBased T> LANGULUS(INLINED)
T Image::Iterator<M>::As() const {
TODO();
return {};
}

} // namespace Langulus::A
16 changes: 8 additions & 8 deletions include/Langulus/Mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ namespace Langulus::A
return {};

Any soughtDecayed;
if (soughtt->template CastsTo<Triangle3>())
soughtDecayed = soughtt->template ReinterpretAs<Point3>();
else if (soughtt->template CastsTo<Triangle2>())
soughtDecayed = soughtt->template ReinterpretAs<Point2>();
if (soughtt->template CastsTo<Math::Triangle3>())
soughtDecayed = soughtt->template ReinterpretAs<Math::Point3>();
else if (soughtt->template CastsTo<Math::Triangle2>())
soughtDecayed = soughtt->template ReinterpretAs<Math::Point2>();
else
soughtDecayed = *soughtt;

Expand Down Expand Up @@ -389,10 +389,10 @@ namespace Langulus::A
return {};

Any soughtDecayed;
if (soughtt->template CastsTo<Triangle3>())
soughtDecayed = soughtt->template ReinterpretAs<Point3>();
else if (soughtt->template CastsTo<Triangle2>())
soughtDecayed = soughtt->template ReinterpretAs<Point2>();
if (soughtt->template CastsTo<Math::Triangle3>())
soughtDecayed = soughtt->template ReinterpretAs<Math::Point3>();
else if (soughtt->template CastsTo<Math::Triangle2>())
soughtDecayed = soughtt->template ReinterpretAs<Math::Point2>();
else
soughtDecayed = *soughtt;

Expand Down
4 changes: 2 additions & 2 deletions source/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Langulus
namespace Langulus::A
{

class Module;
/*class Module;
struct Unit;
struct PlatformModule;
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace Langulus::A
struct Text;
struct Input;
} // namespace Langulus::A::UI
}*/ // namespace Langulus::A::UI

} // namespace Langulus::A

Expand Down
2 changes: 2 additions & 0 deletions source/Hierarchy-Gather.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
///
#pragma once
#include "Hierarchy.hpp"
#include "Thing.hpp"
#include "Unit.hpp"

#define TEMPLATE() template<class THIS>
#define TME() SeekInterface<THIS>
Expand Down
2 changes: 2 additions & 0 deletions source/Hierarchy-Seek.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
///
#pragma once
#include "Hierarchy.hpp"
#include "Thing.hpp"
#include "Unit.hpp"

#define TEMPLATE() template<class THIS>
#define TME() SeekInterface<THIS>
Expand Down
5 changes: 5 additions & 0 deletions source/Hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#include "Module.hpp"


namespace Langulus::A
{
struct Unit;
}

namespace Langulus::Entity
{

Expand Down
5 changes: 5 additions & 0 deletions source/Module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ LANGULUS_EXCEPTION(Module);

namespace Langulus
{
namespace A
{
class Module;
}


using MetaList = TUnorderedSet<AMeta>;
using TraitList = TAny<Trait>;
Expand Down
26 changes: 19 additions & 7 deletions source/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ namespace Langulus::Entity
if (list.Remove(module) and not list) {
VERBOSE("Unregistering `", type, '`');
map.RemoveIt(found);
if (not map)
map.Reset();
}
}
}
Expand Down Expand Up @@ -230,6 +232,8 @@ namespace Langulus::Entity
if (not *found.mValue)
mModules.RemoveIt(found);
}
if (not mModules)
mModules.Reset();

UnregisterAllBases(mModulesByType, module, module->GetType());
delete module;
Expand All @@ -243,10 +247,10 @@ namespace Langulus::Entity
}

/// Load a shared library for a module
/// @param name - the name for the dynamic library
/// the filename will be derived from it, by prefixing
/// with `LangulusMod`, and suffixing with `.so` or `.dll`
/// the name also should correspond to the RTTI::Boundary
/// @param name - the name for the dynamic library - the filename will
/// be derived from it, by prefixing with `LangulusMod`, and
/// suffixing with `.so` or `.dll` the name also should correspond to
/// the RTTI::Boundary
/// @return the module handle (OS dependent)
LANGULUS(NOINLINE)
Runtime::SharedLibrary Runtime::LoadSharedLibrary(const Token& name) {
Expand Down Expand Up @@ -285,7 +289,8 @@ namespace Langulus::Entity
#endif

if (not dll) {
Logger::Error("Failed to load module `", path, "` - file is missing or corrupted; Error code: ");
Logger::Error("Failed to load module `", path, "` - "
"file is missing or corrupted; Error code: ");
#if LANGULUS_OS(WINDOWS)
Logger::Append(::GetLastError());
#else
Expand Down Expand Up @@ -403,6 +408,8 @@ namespace Langulus::Entity
Logger::Error("Could not enter `", path, "` due to an exception");
if (UnloadSharedLibrary(library))
mLibraries.RemoveKey(name);
if (not mLibraries)
mLibraries.Reset();
return {};
}

Expand Down Expand Up @@ -436,6 +443,12 @@ namespace Langulus::Entity
list = mModules.RemoveIt(list);
}

// Make sure memory for the maps is released
if (not mModulesByType)
mModulesByType.Reset();
if (not mModules)
mModules.Reset();

// Collect garbage, and check if library's boundary is still used
const auto wasMarked = library.mMarkedForUnload;
const auto boundary = library.mBoundary;
Expand All @@ -453,8 +466,7 @@ namespace Langulus::Entity
Logger::Warning(
"Module `", boundary, "` can't be unloaded yet, because "
"exposed data is still in use in ", poolsInUse, " memory pools. "
"Unload has been postponed to the next library unload."
);
"Unload has been postponed to the next library unload.");
const_cast<SharedLibrary&>(library).mMarkedForUnload = true;
}

Expand Down
6 changes: 6 additions & 0 deletions source/Runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "Module.hpp"


namespace Langulus::A
{
struct File;
struct Folder;
}

namespace Langulus::Entity
{

Expand Down
8 changes: 4 additions & 4 deletions source/Thing-Seek.inl
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ namespace Langulus::Entity
template<Seek SEEK> LANGULUS(INLINED)
A::Unit* Thing::SeekUnitAux(const Neat& aux, DMeta meta, Index offset) {
A::Unit* result {};
aux.ForEachDeep([&](const Unit* unit) {
aux.ForEachDeep([&](const A::Unit* unit) {
if (unit->CastsTo(meta)) {
if (offset == 0) {
// Found match
result = const_cast<Unit*>(unit);
result = const_cast<A::Unit*>(unit);
return Loop::Break;
}
else --offset;
Expand Down Expand Up @@ -143,13 +143,13 @@ namespace Langulus::Entity
A::Unit* Thing::SeekUnitAuxExt(DMeta type, const Neat& aux, const Neat& ext, Index offset) {
// Scan descriptor even if hierarchy is empty
A::Unit* result {};
aux.ForEachDeep([&](const Unit* unit) {
aux.ForEachDeep([&](const A::Unit* unit) {
if (unit->CastsTo(type)) {
//TODO check construct arguments
// Found match
if (offset == 0) {
// We're done
result = const_cast<Unit*>(unit);
result = const_cast<A::Unit*>(unit);
return Loop::Break;
}
else --offset;
Expand Down

0 comments on commit 05a2bd9

Please sign in to comment.