minor refactor

This commit is contained in:
MihailRis 2024-03-02 13:29:45 +03:00
parent 75d0e917cc
commit e0daa4d1b9
3 changed files with 66 additions and 63 deletions

View File

@ -7,11 +7,9 @@
#include "../files/files.h"
#include "../files/engine_paths.h"
using std::string;
using std::filesystem::path;
namespace fs = std::filesystem;
void GLSLExtension::setVersion(string version) {
void GLSLExtension::setVersion(std::string version) {
this->version = version;
}
@ -19,21 +17,21 @@ void GLSLExtension::setPaths(const ResPaths* paths) {
this->paths = paths;
}
void GLSLExtension::loadHeader(string name) {
path file = paths->find("shaders/lib/"+name+".glsl");
string source = files::read_string(file);
void GLSLExtension::loadHeader(std::string name) {
fs::path file = paths->find("shaders/lib/"+name+".glsl");
std::string source = files::read_string(file);
addHeader(name, source);
}
void GLSLExtension::addHeader(string name, string source) {
void GLSLExtension::addHeader(std::string name, std::string source) {
headers[name] = source;
}
void GLSLExtension::define(string name, string value) {
void GLSLExtension::define(std::string name, std::string value) {
defines[name] = value;
}
const string& GLSLExtension::getHeader(const string name) const {
const std::string& GLSLExtension::getHeader(const std::string& name) const {
auto found = headers.find(name);
if (found == headers.end()) {
throw std::runtime_error("no header '"+name+"' loaded");
@ -41,7 +39,7 @@ const string& GLSLExtension::getHeader(const string name) const {
return found->second;
}
const string GLSLExtension::getDefine(const string name) const {
const std::string GLSLExtension::getDefine(const std::string& name) const {
auto found = defines.find(name);
if (found == defines.end()) {
return "";
@ -49,41 +47,41 @@ const string GLSLExtension::getDefine(const string name) const {
return found->second;
}
bool GLSLExtension::hasDefine(const string name) const {
bool GLSLExtension::hasDefine(const std::string& name) const {
return defines.find(name) != defines.end();
}
bool GLSLExtension::hasHeader(const string name) const {
bool GLSLExtension::hasHeader(const std::string& name) const {
return headers.find(name) != headers.end();
}
void GLSLExtension::undefine(string name) {
void GLSLExtension::undefine(std::string name) {
if (hasDefine(name)) {
defines.erase(name);
}
}
inline std::runtime_error parsing_error(
const path& file,
const fs::path& file,
uint linenum,
const string message) {
const std::string message) {
return std::runtime_error("file "+file.string()+": "+message+
" at line "+std::to_string(linenum));
}
inline void parsing_warning(
const path& file,
const fs::path& file,
uint linenum, const
string message) {
std::string message) {
std::cerr << "file "+file.string()+": warning: "+message+
" at line "+std::to_string(linenum) << std::endl;
" at line "+std::to_string(linenum) << std::endl;
}
inline void source_line(std::stringstream& ss, uint linenum) {
ss << "#line " << linenum << "\n";
}
const string GLSLExtension::process(const path file, const string& source) {
const std::string GLSLExtension::process(const fs::path file, const std::string& source) {
std::stringstream ss;
size_t pos = 0;
uint linenum = 1;
@ -94,15 +92,15 @@ const string GLSLExtension::process(const path file, const string& source) {
source_line(ss, linenum);
while (pos < source.length()) {
size_t endline = source.find('\n', pos);
if (endline == string::npos) {
if (endline == std::string::npos) {
endline = source.length();
}
// parsing preprocessor directives
if (source[pos] == '#') {
string line = source.substr(pos+1, endline-pos);
std::string line = source.substr(pos+1, endline-pos);
util::trim(line);
// parsing 'include' directive
if (line.find("include") != string::npos) {
if (line.find("include") != std::string::npos) {
line = line.substr(7);
util::trim(line);
if (line.length() < 3) {
@ -113,7 +111,7 @@ const string GLSLExtension::process(const path file, const string& source) {
throw parsing_error(file, linenum,
"expected '#include <filename>' syntax");
}
string name = line.substr(1, line.length()-2);
std::string name = line.substr(1, line.length()-2);
if (!hasHeader(name)) {
loadHeader(name);
}
@ -125,7 +123,7 @@ const string GLSLExtension::process(const path file, const string& source) {
continue;
}
// removing extra 'include' directives
else if (line.find("version") != string::npos) {
else if (line.find("version") != std::string::npos) {
parsing_warning(file, linenum, "removed #version directive");
pos = endline+1;
linenum++;
@ -138,4 +136,4 @@ const string GLSLExtension::process(const path file, const string& source) {
pos = endline+1;
}
return ss.str();
}
}

View File

@ -23,11 +23,11 @@ public:
void undefine(std::string name);
void addHeader(std::string name, std::string source);
const std::string& getHeader(const std::string name) const;
const std::string getDefine(const std::string name) const;
const std::string& getHeader(const std::string& name) const;
const std::string getDefine(const std::string& name) const;
bool hasHeader(const std::string name) const;
bool hasDefine(const std::string name) const;
bool hasHeader(const std::string& name) const;
bool hasDefine(const std::string& name) const;
const std::string process(const std::filesystem::path file, const std::string& source);
};

View File

@ -14,10 +14,10 @@ namespace xml {
class Attribute;
class Document;
typedef Attribute xmlattribute;
typedef std::shared_ptr<Node> xmlelement;
typedef std::shared_ptr<Document> xmldocument;
typedef std::unordered_map<std::string, xmlattribute> xmlelements_map;
using xmlattribute = Attribute;
using xmlelement = std::shared_ptr<Node>;
using xmldocument = std::shared_ptr<Document>;
using xmlelements_map = std::unordered_map<std::string, xmlattribute>;
class Attribute {
std::string name;
@ -37,7 +37,7 @@ namespace xml {
glm::vec4 asColor() const;
};
/* XML element class. Text element has tag 'text' and attribute 'text' */
/// @brief XML element class. Text element has tag 'text' and attribute 'text'
class Node {
std::string tag;
std::unordered_map<std::string, xmlattribute> attrs;
@ -45,13 +45,15 @@ namespace xml {
public:
Node(std::string tag);
/* Add sub-element */
/// @brief Add sub-element
void add(xmlelement element);
/* Set attribute value. Creates attribute if does not exists */
void set(std::string name, std::string text);
/* Get element tag */
/// @brief Set attribute value. Creates attribute if does not exists
/// @param name attribute name
/// @param text attribute value
void set(std::string name, std::string text);
/// @brief Get element tag
const std::string& getTag() const;
inline bool isText() const {
@ -62,27 +64,30 @@ namespace xml {
return attr("#").getText();
}
/* Get attribute by name
@param name attribute name
@throws std::runtime_error if element has no attribute
@return xmlattribute - {name, value} */
/// @brief Get attribute by name
/// @param name attribute name
/// @throws std::runtime_error if element has no attribute
/// @return xmlattribute - {name, value}
const xmlattribute attr(const std::string& name) const;
/* Get attribute by name
@param name name
@param def default value will be returned wrapped in xmlattribute
if element has no attribute
@return xmlattribute - {name, value} or {name, def} if not found*/
/// @brief Get attribute by name
/// @param name attribute name
/// @param def default value will be returned wrapped in xmlattribute
/// if element has no attribute
/// @return xmlattribute - {name, value} or {name, def} if not found*/
const xmlattribute attr(const std::string& name, const std::string& def) const;
/* Check if element has attribute
@param name attribute name */
/// @brief Check if element has attribute
/// @param name attribute name
bool has(const std::string& name) const;
/* Get sub-element by index
@throws std::out_of_range if an invalid index given */
/// @brief Get sub-element by index
/// @param index sub-element index
/// @throws std::out_of_range if an invalid index given
/// @return sub-element
xmlelement sub(size_t index);
/* Get number of sub-elements */
/// @brief Get number of sub-elements
size_t size() const;
const std::vector<xmlelement>& getElements() const;
@ -118,21 +123,21 @@ namespace xml {
xmldocument parse();
};
/* Serialize XML Document to string
@param document serializing document
@param nice use human readable format
(with indents and line-separators)
@param indentStr indentation characters sequence
(default - 4 spaces)*/
/// @brief Serialize XML Document to string
/// @param document serializing document
/// @param nice use human readable format (with indents and line-separators)
/// @param indentStr indentation characters sequence (default - 4 spaces)
/// @return XML string
extern std::string stringify(
const xmldocument document,
bool nice=true,
const std::string& indentStr=" "
);
/* Read XML Document from string
@param filename file name will be shown in error messages
@param source xml source code string */
/// @brief Read XML Document from string
/// @param filename file name will be shown in error messages
/// @param source xml source code string
/// @return xml document
extern xmldocument parse(std::string filename, std::string source);
}