Skip to content

Commit

Permalink
Improve the rest args retrieve.
Browse files Browse the repository at this point in the history
* argparser.hpp (get_rest) : Now returns a vector of std::strings.
Make the C++ binding test more C++
Remove printf and use the new function
  • Loading branch information
Ergus committed May 5, 2024
1 parent 8fe6582 commit 11d9b90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
21 changes: 16 additions & 5 deletions argparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <string>
#include <type_traits>
#include "argparser.h"

#include <vector>

namespace {

Expand Down Expand Up @@ -108,19 +108,30 @@ namespace argparser {
}

template<bool json = false>
void report() {
void report()
{
if constexpr(json)
report_args_json();
else
report_args();
}

void free() {
void free()
{
free_args();
}

int get_rest(char **rest) {
return get_rest_args(&rest);
std::vector<std::string> get_rest()
{
char **rest;
int nrest = get_rest_args(&rest);

std::vector<std::string> result(nrest);

for(int i = 0; i < nrest; ++i)
result[i] = std::string(rest[i]);

return result;
}

class time {
Expand Down
21 changes: 10 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,16 @@ int main(int argc, char *argv[])

argparser::time t1("timer_1");
int r_int_1 = argparser::reportable<int>("r_int_1", argc);
printf("# Report: r_int_1 %d\n", r_int_1);
std::cout << "# Report: r_int_1 " << r_int_1 << std::endl;
assert(r_int_1 == argc);

char **rest = NULL;
int nrest = get_rest_args(&rest);
if (nrest > 0){
printf("# ");
for (int i = 0; i < nrest; ++i) {
printf("rest[%d]: \"%s\", ", i, rest[i]);
std::vector<std::string> rest = argparser::get_rest();
if (rest.size() > 0){
std::cout << "# ";
for (int i = 0; i < rest.size(); ++i) {
std::cout << "rest[" << i << "]: \"" << rest[i] << "\", ";
}
printf("\n");
std::cout << std::endl;
}

double r_double_1 = argparser::reportable<double>("r_double_1", v_double_1 / o_double_1);
Expand All @@ -107,16 +106,16 @@ int main(int argc, char *argv[])
t2.stop();

const double diff_time1 = t1.getNS();
printf("# Timer: T1 %lf\n", diff_time1);
std::cout << "# Timer: T1 " << diff_time1 << std::endl;
assert(diff_time1 == 0.);

const double diff_time2 = t2.getNS();
printf("# Times: T2 %lf\n", diff_time2);
std::cout << "# Timer: T2 " << diff_time2 << std::endl;
assert(diff_time2 >= 1E9);

t2.reset();
const double diff_time_reset = t2.getNS();
printf("# Times: T2 (reset) %lf\n", diff_time_reset);
std::cout << "# Times: T2 (reset) " << diff_time_reset << std::endl;
assert(diff_time_reset == 0.);

std::cout << "# Reporting args" << std::endl;
Expand Down

0 comments on commit 11d9b90

Please sign in to comment.