Commit 2e1201df authored by Dominik Charousset's avatar Dominik Charousset

Make c_args_remainder const

parent d7653648
......@@ -9,26 +9,25 @@
* - qt_group_chat -g remote:chatroom@localhost:4242 -n bob *
\******************************************************************************/
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <time.h>
#include <cstdlib>
#include <vector>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
CAF_PUSH_WARNINGS
#include <QMainWindow>
#include <QApplication>
#include "ui_chatwindow.h" // auto generated from chatwindow.ui
#include <QApplication>
#include <QMainWindow>
CAF_POP_WARNINGS
#include "chatwidget.hpp"
using namespace std;
using namespace caf;
class config : public actor_system_config {
......@@ -43,7 +42,7 @@ public:
int caf_main(actor_system& sys, const config& cfg) {
std::string name;
if (auto config_name = get_if<std::string>(&cfg, "name"))
name = *config_name;
name = std::move(*config_name);
while (name.empty()) {
std::cout << "please enter your name: " << std::flush;
if (!std::getline(std::cin, name)) {
......@@ -62,8 +61,7 @@ int caf_main(actor_system& sys, const config& cfg) {
<< std::endl;
}
}
auto [argc, argv] = const_cast<config&>(cfg).c_args_remainder();
auto [argc, argv] = cfg.c_args_remainder();
QApplication app{argc, argv};
app.setQuitOnLastWindowClosed(true);
QMainWindow mw;
......@@ -71,10 +69,8 @@ int caf_main(actor_system& sys, const config& cfg) {
helper.setupUi(&mw);
helper.chatwidget->init(sys);
auto client = helper.chatwidget->as_actor();
if (! name.empty())
send_as(client, client, set_name_atom_v, move(name));
if (grp)
send_as(client, client, join_atom_v, std::move(grp));
anon_send(client, set_name_atom_v, move(name));
anon_send(client, join_atom_v, std::move(grp));
mw.show();
return app.exec();
}
......
......@@ -197,7 +197,7 @@ public:
/// necessary buffers lazily on first call.
/// @note The returned pointer remains valid only as long as the
/// `actor_system_config` object exists.
std::pair<int, char**> c_args_remainder();
std::pair<int, char**> c_args_remainder() const noexcept;
// -- caf-run parameters -----------------------------------------------------
......@@ -328,7 +328,9 @@ protected:
config_option_set custom_options_;
private:
std::vector<char*> c_args_remainder_;
void set_remainder(string_list args);
mutable std::vector<char*> c_args_remainder_;
std::vector<char> c_args_remainder_buf_;
actor_system_config& set_impl(string_view name, config_value value);
......
......@@ -206,25 +206,27 @@ error actor_system_config::parse(int argc, char** argv, std::istream& conf) {
return parse(std::move(args), conf);
}
std::pair<int, char**> actor_system_config::c_args_remainder() {
if (c_args_remainder_.empty()) {
c_args_remainder_buf_.assign(program_name.begin(), program_name.end());
std::pair<int, char**> actor_system_config::c_args_remainder() const noexcept {
return {static_cast<int>(c_args_remainder_.size()), c_args_remainder_.data()};
}
void actor_system_config::set_remainder(string_list args) {
remainder.swap(args);
c_args_remainder_buf_.assign(program_name.begin(), program_name.end());
c_args_remainder_buf_.emplace_back('\0');
for (const auto& arg : remainder) {
c_args_remainder_buf_.insert(c_args_remainder_buf_.end(), //
arg.begin(), arg.end());
c_args_remainder_buf_.emplace_back('\0');
for (const auto& arg : remainder) {
c_args_remainder_buf_.insert(c_args_remainder_buf_.end(), //
arg.begin(), arg.end());
c_args_remainder_buf_.emplace_back('\0');
}
auto ptr = c_args_remainder_buf_.data();
auto end = ptr + c_args_remainder_buf_.size();
auto advance_ptr = [&ptr] {
while (*ptr++ != '\0')
; // nop
};
for (; ptr != end; advance_ptr())
c_args_remainder_.emplace_back(ptr);
}
return {static_cast<int>(c_args_remainder_.size()), c_args_remainder_.data()};
auto ptr = c_args_remainder_buf_.data();
auto end = ptr + c_args_remainder_buf_.size();
auto advance_ptr = [&ptr] {
while (*ptr++ != '\0')
; // nop
};
for (; ptr != end; advance_ptr())
c_args_remainder_.emplace_back(ptr);
}
namespace {
......@@ -316,15 +318,16 @@ error actor_system_config::parse(string_list args, std::istream& config) {
using std::make_move_iterator;
auto res = custom_options_.parse(content, args);
if (res.second != args.end()) {
if (res.first != pec::success && starts_with(*res.second, "-"))
if (res.first != pec::success && starts_with(*res.second, "-")) {
return make_error(res.first, *res.second);
auto first = args.begin();
first += std::distance(args.cbegin(), res.second);
remainder.insert(remainder.end(), make_move_iterator(first),
make_move_iterator(args.end()));
} else {
args.erase(args.begin(), res.second);
set_remainder(std::move(args));
}
} else {
cli_helptext_printed = get_or(content, "help", false)
|| get_or(content, "long-help", false);
set_remainder(string_list{});
}
// Generate help text if needed.
if (cli_helptext_printed) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment