Skip to content

Commit

Permalink
improve tinyconfig
Browse files Browse the repository at this point in the history
- uses a proper state-machine-like token extractor. Still needs a lot of cleanup to replace any string manipulation to use string views.
- Reduced header dependency in vlm_types
- Created new forward declaration file for the tiny headers.
- Set policy to merge static libs in the main libvlm which should fix CI
  • Loading branch information
samayala22 committed Mar 12, 2024
1 parent 80a41f8 commit 7b5f46a
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 196 deletions.
1 change: 0 additions & 1 deletion config/elliptic.vlm
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ backend = cpu
s_ref = 3.92699 # half wing area
c_ref = 0.85
ref_pt = [0.25, 0.0, 0.0]
test = [ [ 123, 5, 12], [ 98, 234, 53, 12, 531]]
wake_included = false
83 changes: 50 additions & 33 deletions headeronly/tinyconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SOFTWARE.

#pragma once

#include <string_view>
#include <type_traits> // std::is_same, std::is_integral, std::is_floating_point, std::disjunction
#include <unordered_map> // std::unordered_map
#include <vector> // std::vector
Expand All @@ -37,7 +38,7 @@ SOFTWARE.
#include <filesystem> // std::filesystem
#include <stdexcept> // std::runtime_error
#include <initializer_list> // std::initializer_list
#include <iostream> // dbg
// #include <iostream> // dbg

namespace tiny {

Expand Down Expand Up @@ -119,34 +120,51 @@ inline static std::string clean_str(const std::string& str) {
return str.substr(start, end - start + 1);
}

inline bool extract_depth0_token(std::string_view& stream, std::string_view& token, const char delimiter, const char marker_open, const char marker_close) {
int depth = 0;
// Loop through each character in the stream
for(auto it = stream.begin(); it != stream.end(); ++it) {
if (*it == marker_open) {
depth++;
} else if (*it == marker_close) {
if (depth > 0) {
depth--;
if (depth == 0) {
// Extract last possible token
token = stream.substr(0, it - stream.begin() + 1);
stream.remove_prefix(it - stream.begin() + 1);
return true;
inline static std::string_view trim_space(std::string_view str) {
while (!str.empty() && std::isspace(str.front())) str.remove_prefix(1);
while (!str.empty() && std::isspace(str.back())) str.remove_suffix(1);
return str;
}

class Extractor {
public:
bool extract(std::string_view& token) {
// Loop through each character in the stream
token = stream;
for(auto it = stream.begin(); it != stream.end(); ++it) {
if (*it == '[' || *it == '{') {
depth++;
if (depth == 1) {
token.remove_prefix(it - stream.begin() + 1);
}
} else {
// Mismatched delimiters
return false;
} else if (*it == ']' || *it == '}') {
if (depth > 0) {
depth--;
if (depth == 0) {
// Extract last possible token
token = token.substr(0, it - token.begin());
stream.remove_prefix(it - stream.begin() + 1);
return true;
}
} else {
// Mismatched delimiters
throw std::runtime_error("Mismatched delimiters");
}
} else if (*it == ',' && depth == 1) {
// Extract the token from the stream
token = token.substr(0, it - token.begin());
stream.remove_prefix(it - stream.begin() + 1);
return true;
}
} else if (*it == delimiter && depth == 0) {
// Extract the token from the stream
token = stream.substr(0, it - stream.begin());
stream.remove_prefix(it - stream.begin() + 1);
return true;
}
return false; // no match
}
return false; // no match
}
Extractor(const std::string& s) : stream{s} {}

public:
std::string_view stream;
int depth = 0;
};

// Base template for Converter
template<typename T, typename Enable = void>
Expand All @@ -168,9 +186,9 @@ struct Converter<std::vector<T>, std::enable_if_t<is_vector_of_base<std::vector<
if (s.front() == '[' && s.back() == ']') {
if (s.size() > 2) {
std::vector<T> result;
std::istringstream stream(s.substr(1, s.size() - 2));
std::string token;
while (std::getline(stream, token, ',')) result.push_back(Converter<T>::convert(clean_str(token)));
std::string_view token;
Extractor ex{s};
while (ex.extract(token)) result.push_back(Converter<T>::convert(std::string{trim_space(token)}));
return result;
} else {
return {};
Expand All @@ -187,12 +205,11 @@ struct Converter<std::array<T, N>, std::enable_if_t<is_array_of_base<std::array<
if (s.front() == '[' && s.back() == ']') {
if (s.size() > 2) {
std::array<T, N> result;
std::istringstream stream(s.substr(1, s.size() - 2));
std::string token;
std::string_view token;
Extractor ex{s};
for (std::size_t i = 0; i < N; i++) {
if (std::getline(stream, token, ',')) {
std::cout << s << " " << token << std::endl;
result[i] = Converter<T>::convert(clean_str(token));
if (ex.extract(token)) {
result[i] = Converter<T>::convert(std::string{trim_space(token)});
}
else throw std::runtime_error("Array size mismatch. Expected: " + std::to_string(N) + " elements. String: " + s);
}
Expand Down
5 changes: 5 additions & 0 deletions headeronly/tinyfwd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace tiny {
class Config;
}
2 changes: 2 additions & 0 deletions vlm/dev/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "vlm_types.hpp"
#include "vlm_utils.hpp"

#include "tinyconfig.hpp"

#include <iostream>
#include <cstdio>
#include <algorithm>
Expand Down
154 changes: 0 additions & 154 deletions vlm/dev/tests.cpp

This file was deleted.

1 change: 1 addition & 0 deletions vlm/include/vlm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "vlm_data.hpp"
#include "vlm_types.hpp"
#include "vlm_backend.hpp"
#include "tinyfwd.hpp"

namespace vlm {

Expand Down
1 change: 1 addition & 0 deletions vlm/include/vlm_mesh.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "vlm_types.hpp"
#include "tinyfwd.hpp"

namespace vlm {

Expand Down
2 changes: 1 addition & 1 deletion vlm/include/vlm_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include <string>
#include <cmath>
#include <limits>
#include <memory>

#include "linalg.h"
#include "tinyconfig.hpp"

namespace vlm {

Expand Down
1 change: 1 addition & 0 deletions vlm/src/vlm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "vlm_backend.hpp"
#include "tinytimer.hpp"
#include "tinyconfig.hpp"

#include "vlm_data.hpp"
#include <utility>
Expand Down
8 changes: 1 addition & 7 deletions vlm/src/vlm_mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "vlm_mesh.hpp"
#include "linalg.h"
#include "vlm_types.hpp"
#include "tinyconfig.hpp"

#include <cassert>
#include <iostream>
Expand All @@ -19,13 +20,6 @@ Mesh::Mesh(const tiny::Config& cfg) :
cfg().section("mesh").get<f32>("c_ref", 0.0f),
linalg::alias::float3{cfg().section("mesh").get<std::array<f32, 3>>("ref_pt").data()}
) {
// auto res = cfg().section("mesh").get<std::array<std::vector<u32>, 2>>("test");
// for (auto& v : res) {
// for (auto& e : v) {
// std::cout << e << " ";
// }
// std::cout << "\n";
// }
}

Mesh::Mesh(
Expand Down
1 change: 1 addition & 0 deletions vlm/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_requires("taskflow_custom")

target("libvlm")
set_kind("static")
set_policy("build.merge_archive", true)
add_packages("taskflow_custom", {public = true})

for _,name in ipairs(backends) do
Expand Down

0 comments on commit 7b5f46a

Please sign in to comment.