Material loading base

This commit is contained in:
Jan
2022-07-15 11:21:54 +02:00
parent bc91738ee9
commit 82349d3432
7 changed files with 442 additions and 4 deletions

View File

@ -0,0 +1,82 @@
#include "AbstractGdtEntryReader.h"
#include <sstream>
GdtReadingException::GdtReadingException(std::string message)
: exception(message.c_str()),
m_message(std::move(message))
{
}
const char* GdtReadingException::what() const
{
return m_message.c_str();
}
AbstractGdtEntryReader::AbstractGdtEntryReader(const GdtEntry& entry)
: m_entry(entry)
{
}
std::string AbstractGdtEntryReader::ReadStringProperty(const std::string& propertyName, std::string defaultValue) const
{
const auto foundProperty = m_entry.m_properties.find(propertyName);
if (foundProperty == m_entry.m_properties.end())
return defaultValue;
return foundProperty->second;
}
bool AbstractGdtEntryReader::ReadBoolProperty(const std::string& propertyName, const bool defaultValue) const
{
const auto foundProperty = m_entry.m_properties.find(propertyName);
if (foundProperty == m_entry.m_properties.end())
return defaultValue;
char* endPtr = nullptr;
const auto result = std::strtol(foundProperty->second.c_str(), &endPtr, 10);
if (endPtr != &foundProperty->second[foundProperty->second.size()])
{
std::ostringstream ss;
ss << "\"" << foundProperty->second << "\" is not a valid boolean value";
throw GdtReadingException(ss.str());
}
return result != 0;
}
int AbstractGdtEntryReader::ReadIntegerProperty(const std::string& propertyName, const int defaultValue) const
{
const auto foundProperty = m_entry.m_properties.find(propertyName);
if (foundProperty == m_entry.m_properties.end())
return defaultValue;
char* endPtr = nullptr;
const auto result = std::strtol(foundProperty->second.c_str(), &endPtr, 10);
if (endPtr != &foundProperty->second[foundProperty->second.size()])
{
std::ostringstream ss;
ss << "\"" << foundProperty->second << "\" is not a valid integer value";
throw GdtReadingException(ss.str());
}
return result;
}
float AbstractGdtEntryReader::ReadFloatProperty(const std::string& propertyName, const float defaultValue) const
{
const auto foundProperty = m_entry.m_properties.find(propertyName);
if (foundProperty == m_entry.m_properties.end())
return defaultValue;
char* endPtr = nullptr;
const auto result = std::strtof(foundProperty->second.c_str(), &endPtr);
if (endPtr != &foundProperty->second[foundProperty->second.size()])
{
std::ostringstream ss;
ss << "\"" << foundProperty->second << "\" is not a valid float value";
throw GdtReadingException(ss.str());
}
return result;
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <exception>
#include "Utils/ClassUtils.h"
#include "Obj/Gdt/GdtEntry.h"
class GdtReadingException : public std::exception
{
public:
explicit GdtReadingException(std::string message);
_NODISCARD const char* what() const override;
private:
std::string m_message;
};
class AbstractGdtEntryReader
{
protected:
explicit AbstractGdtEntryReader(const GdtEntry& entry);
_NODISCARD std::string ReadStringProperty(const std::string& propertyName, std::string defaultValue = std::string()) const;
_NODISCARD bool ReadBoolProperty(const std::string& propertyName, bool defaultValue = false) const;
_NODISCARD int ReadIntegerProperty(const std::string& propertyName, int defaultValue = 0) const;
_NODISCARD float ReadFloatProperty(const std::string& propertyName, float defaultValue = 0.0f) const;
const GdtEntry& m_entry;
};