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

Make c_args_remainder const

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