Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove boost::lexical_cast #1205

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/cmdstan/arguments/singleton_argument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@
#define CMDSTAN_ARGUMENTS_SINGLETON_ARGUMENT_HPP

#include <cmdstan/arguments/valued_argument.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#include <vector>

namespace cmdstan {

namespace internal {
void from_string(std::string &src, double &dest) { dest = std::stod(src); }
void from_string(std::string &src, int &dest) { dest = std::stoi(src); }
void from_string(std::string &src, long long int &dest) {
dest = std::stoll(src);
}
void from_string(std::string &src, unsigned int &dest) {
dest = std::stoul(src);
}
void from_string(std::string &src, bool &dest) {
if (src == "true" || src == "1") {
dest = true;
} else if (src == "false" || src == "0") {
dest = false;
} else {
throw std::invalid_argument(std::string("invalid boolean value ") + src);
}
}
void from_string(std::string &src, std::string &dest) { dest = src; }

std::string to_string(std::string &src) { return src; }
template <typename T>
std::string to_string(T &src) {
return std::to_string(src);
}
} // namespace internal

template <typename T>
struct type_name {
static std::string name() { return typeid(T).name(); }
Expand Down Expand Up @@ -73,7 +99,8 @@ class singleton_argument : public valued_argument {
args.pop_back();

try {
T proposed_value = boost::lexical_cast<T>(value);
T proposed_value;
internal::from_string(value, proposed_value);
if (set_value(proposed_value)) {
return true;
}
Expand Down Expand Up @@ -109,7 +136,7 @@ class singleton_argument : public valued_argument {
return false;
}

std::string print_value() { return boost::lexical_cast<std::string>(_value); }
std::string print_value() { return internal::to_string(_value); }

std::string print_valid() { return " " + _validity; }

Expand Down
1 change: 1 addition & 0 deletions src/test/interface/arguments/singleton_argument_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cmdstan/arguments/singleton_argument.hpp>
#include <stan/callbacks/stream_writer.hpp>
#include <stan/callbacks/writer.hpp>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>

template <typename T>
Expand Down