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

hpx/util/safe_lexical_cast.hpp is added. #1270

Merged
merged 4 commits into from Oct 13, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 7 additions & 11 deletions hpx/runtime/parcelset/parcelport_impl.hpp
Expand Up @@ -17,6 +17,7 @@
#include <hpx/util/io_service_pool.hpp>
#include <hpx/util/connection_cache.hpp>
#include <hpx/util/runtime_configuration.hpp>
#include <hpx/util/safe_lexical_cast.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace hpx
Expand Down Expand Up @@ -48,9 +49,8 @@ namespace hpx { namespace parcelset
std::string key("hpx.parcel.");
key += connection_handler_name();

std::string thread_pool_size =
ini.get_entry(key + ".io_pool_size", "2");
return boost::lexical_cast<std::size_t>(thread_pool_size);
return hpx::util::get_entry_as<std::size_t>(
ini, key + ".io_pool_size", "2");
}

static const char *pool_name()
Expand All @@ -68,21 +68,17 @@ namespace hpx { namespace parcelset
std::string key("hpx.parcel.");
key += connection_handler_name();

std::string max_connections =
ini.get_entry(key + ".max_connections",
HPX_PARCEL_MAX_CONNECTIONS);
return boost::lexical_cast<std::size_t>(max_connections);
return hpx::util::get_entry_as<std::size_t>(
ini, key + ".max_connections", HPX_PARCEL_MAX_CONNECTIONS);
}

static std::size_t max_connections_per_loc(util::runtime_configuration const& ini)
{
std::string key("hpx.parcel.");
key += connection_handler_name();

std::string max_connections_per_locality =
ini.get_entry(key + ".max_connections_per_locality",
HPX_PARCEL_MAX_CONNECTIONS_PER_LOCALITY);
return boost::lexical_cast<std::size_t>(max_connections_per_locality);
return hpx::util::get_entry_as<std::size_t>(
ini, key + ".max_connections_per_locality", HPX_PARCEL_MAX_CONNECTIONS_PER_LOCALITY);
}

public:
Expand Down
45 changes: 45 additions & 0 deletions hpx/util/safe_lexical_cast.hpp
@@ -0,0 +1,45 @@
/*=============================================================================
Copyright (c) 2014 Anton Bikineev

Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(HPX_UTIL_SEP_21_2014_0840PM)
#define HPX_UTIL_SEP_21_2014_0840PM

#include <boost/lexical_cast.hpp>
#include <boost/type_traits.hpp>
#include <boost/utility/enable_if.hpp>

namespace hpx { namespace util
{
template <class DestType, class SrcType>
DestType safe_lexical_cast(const SrcType& value, const DestType& dflt = DestType())
{
try
{
return boost::lexical_cast<DestType>(value);
}
catch (const boost::bad_lexical_cast& )
{
;
}
return dflt;
}

template <class DestType, class Config>
typename boost::enable_if<boost::is_integral<DestType>, DestType>::type
get_entry_as(const Config& config, const std::string& key, const DestType& dflt)
{
return safe_lexical_cast(config.get_entry(key, dflt), dflt);
}

template <class DestType, class Config>
DestType get_entry_as(const Config& config, const std::string& key, const std::string& dflt)
{
return safe_lexical_cast(config.get_entry(key, dflt), safe_lexical_cast<DestType>(dflt));
}

}}

#endif //HPX_UTIL_SEP_21_2014_0840PM
5 changes: 3 additions & 2 deletions src/performance_counters/counters.cpp
Expand Up @@ -16,6 +16,7 @@
#include <hpx/util/portable_binary_iarchive.hpp>
#include <hpx/util/portable_binary_oarchive.hpp>
#include <hpx/util/bind.hpp>
#include <hpx/util/safe_lexical_cast.hpp>
#include <hpx/lcos/local/packaged_continuation.hpp>

#include <boost/format.hpp>
Expand Down Expand Up @@ -333,15 +334,15 @@ namespace hpx { namespace performance_counters
}
else if (!elements.instance_.parent_.index_.empty()) {
path.parentinstanceindex_ =
boost::lexical_cast<boost::uint64_t>(elements.instance_.parent_.index_);
hpx::util::safe_lexical_cast<boost::uint64_t>(elements.instance_.parent_.index_);
}

if (elements.instance_.child_.index_ == "#*") {
path.instancename_ += "#*";
}
else if (!elements.instance_.child_.index_.empty()) {
path.instanceindex_ =
boost::lexical_cast<boost::uint64_t>(elements.instance_.child_.index_);
hpx::util::safe_lexical_cast<boost::uint64_t>(elements.instance_.child_.index_);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/runtime/agas/addressing_service.cpp
Expand Up @@ -21,6 +21,7 @@
#include <hpx/runtime/agas/server/symbol_namespace.hpp>
#include <hpx/util/logging.hpp>
#include <hpx/util/runtime_configuration.hpp>
#include <hpx/util/safe_lexical_cast.hpp>
#include <hpx/include/performance_counters.hpp>
#include <hpx/performance_counters/counter_creators.hpp>
#include <hpx/lcos/wait_all.hpp>
Expand Down Expand Up @@ -363,8 +364,8 @@ void addressing_service::launch_bootstrap(
boost::str(boost::format("hpx.locality!=%1%")
% naming::get_locality_id_from_gid(here)));

boost::uint32_t num_threads = boost::lexical_cast<boost::uint32_t>(
ini_.get_entry("hpx.os_threads", boost::uint32_t(1)));
boost::uint32_t num_threads = hpx::util::get_entry_as<boost::uint32_t>(
ini_, "hpx.os_threads", 1u);
request locality_req(locality_ns_allocate, ep, 4, num_threads); //-V112
bootstrap->locality_ns_server_.remote_service(locality_req);

Expand Down
13 changes: 7 additions & 6 deletions src/runtime/parcelset/parcelhandler.cpp
Expand Up @@ -11,6 +11,7 @@
#include <hpx/exception.hpp>
#include <hpx/util/portable_binary_iarchive.hpp>
#include <hpx/util/io_service_pool.hpp>
#include <hpx/util/safe_lexical_cast.hpp>
#include <hpx/runtime/naming/resolver_client.hpp>
#include <hpx/runtime/parcelset/parcelhandler.hpp>
#include <hpx/runtime/threads/threadmanager.hpp>
Expand Down Expand Up @@ -304,7 +305,7 @@ namespace hpx { namespace parcelset
std::string enable_ipc =
get_config_entry("hpx.parcel.ipc.enable", "0");

if (boost::lexical_cast<int>(enable_ipc))
if (hpx::util::safe_lexical_cast<int>(enable_ipc, 0))
{
attach_parcelport(parcelport::create(
connection_ipc, hpx::get_config(),
Expand All @@ -315,7 +316,7 @@ namespace hpx { namespace parcelset
std::string enable_ibverbs =
get_config_entry("hpx.parcel.ibverbs.enable", "0");

if (boost::lexical_cast<int>(enable_ibverbs))
if (hpx::util::safe_lexical_cast<int>(enable_ibverbs, 0))
{
attach_parcelport(parcelport::create(
connection_ibverbs, hpx::get_config(),
Expand All @@ -335,7 +336,7 @@ namespace hpx { namespace parcelset
{
std::string enable_tcp =
get_config_entry("hpx.parcel.tcp.enable", "1");
if (boost::lexical_cast<int>(enable_tcp)) {
if (hpx::util::safe_lexical_cast<int>(enable_tcp, 1)) {
attach_parcelport(parcelport::create(
connection_tcp, hpx::get_config(),
pool->get_on_start_thread(), pool->get_on_stop_thread()));
Expand All @@ -355,7 +356,7 @@ namespace hpx { namespace parcelset
{
std::string cfgkey("hpx.parcel." + ppname + ".enable");
std::string enabled = get_config_entry(cfgkey, "0");
strm << ", " << (boost::lexical_cast<int>(enabled) ? "" : "not ")
strm << ", " << (hpx::util::safe_lexical_cast<int>(enabled, 0) ? "" : "not ")
<< "enabled";

std::string bootstrap = get_config_entry("hpx.parcel.bootstrap", "tcp");
Expand Down Expand Up @@ -518,7 +519,7 @@ namespace hpx { namespace parcelset
// otherwise fall back to tcp
if (use_alternative_parcelports_ &&
dest.get_address() == here().get_address() &&
boost::lexical_cast<int>(enable_ipc))
hpx::util::safe_lexical_cast<int>(enable_ipc, 0))
{
if (pports_[connection_ipc])
return connection_ipc;
Expand All @@ -532,7 +533,7 @@ namespace hpx { namespace parcelset
std::string enable_ibverbs =
get_config_entry("hpx.parcel.ibverbs.enable", "0");
if (use_alternative_parcelports_ &&
boost::lexical_cast<int>(enable_ibverbs))
hpx::util::safe_lexical_cast<int>(enable_ibverbs, 0))
{
if (pports_[connection_ibverbs])
return connection_ibverbs;
Expand Down
38 changes: 9 additions & 29 deletions src/runtime/parcelset/parcelport.cpp
Expand Up @@ -36,6 +36,7 @@
#include <hpx/runtime/parcelset/parcelport_impl.hpp>
#include <hpx/util/io_service_pool.hpp>
#include <hpx/util/runtime_configuration.hpp>
#include <hpx/util/safe_lexical_cast.hpp>
#include <hpx/exception.hpp>

#include <boost/shared_ptr.hpp>
Expand Down Expand Up @@ -110,10 +111,7 @@ namespace hpx { namespace parcelset
case connection_tcp:
{
#if defined(HPX_PARCELPORT_TCP)
std::string enable_tcp =
cfg.get_entry("hpx.parcel.tcp.enable", "1");

if (boost::lexical_cast<int>(enable_tcp))
if (hpx::util::get_entry_as<int>(cfg, "hpx.parcel.tcp.enable", "1"))
{
return boost::make_shared<policies::tcp::connection_handler>(
cfg, on_start_thread, on_stop_thread);
Expand All @@ -129,10 +127,7 @@ namespace hpx { namespace parcelset
#if defined(HPX_PARCELPORT_IPC)
// Create ipc based parcelport only if allowed by the
// configuration info.
std::string enable_ipc =
cfg.get_entry("hpx.parcel.ipc.enable", "0");

if (boost::lexical_cast<int>(enable_ipc))
if (hpx::util::get_entry_as<int>(cfg, "hpx.parcel.ipc.enable", "0"))
{
return boost::make_shared<policies::ipc::connection_handler>(
cfg, on_start_thread, on_stop_thread);
Expand All @@ -148,10 +143,7 @@ namespace hpx { namespace parcelset
{
// Create ibverbs based parcelport only if allowed by the
// configuration info.
std::string enable_ibverbs =
cfg.get_entry("hpx.parcel.ibverbs.enable", "0");

if (boost::lexical_cast<int>(enable_ibverbs))
if (hpx::util::get_entry_as<int>(cfg, "hpx.parcel.ibverbs.enable", "0"))
{
return boost::make_shared<policies::ibverbs::connection_handler>(
cfg, on_start_thread, on_stop_thread);
Expand All @@ -172,10 +164,7 @@ namespace hpx { namespace parcelset
{
// Create MPI based parcelport only if allowed by the
// configuration info.
std::string enable_mpi =
cfg.get_entry("hpx.parcel.mpi.enable", "0");

if (boost::lexical_cast<int>(enable_mpi))
if (hpx::util::get_entry_as<int>(cfg, "hpx.parcel.mpi.enable", "0"))
{
return boost::make_shared<policies::mpi::connection_handler>(
cfg, on_start_thread, on_stop_thread);
Expand Down Expand Up @@ -211,30 +200,21 @@ namespace hpx { namespace parcelset
std::string key("hpx.parcel.");
key += type;

std::string array_optimization =
ini.get_entry(key + ".array_optimization", "1");

if (boost::lexical_cast<int>(array_optimization) == 0) {
if (hpx::util::get_entry_as<int>(ini, key + ".array_optimization", "1") == 0) {
allow_array_optimizations_ = false;
allow_zero_copy_optimizations_ = false;
}
else {
std::string zero_copy_optimization =
ini.get_entry(key + ".zero_copy_optimization", "1");
if (boost::lexical_cast<int>(zero_copy_optimization) == 0)
if (hpx::util::get_entry_as<int>(ini, key + ".zero_copy_optimization", "1") == 0)
allow_zero_copy_optimizations_ = false;
}

std::string enable_security =
ini.get_entry(key + ".enable_security", "0");
if(boost::lexical_cast<int>(enable_security) != 0)
if(hpx::util::get_entry_as<int>(ini, key + ".enable_security", "0") != 0)
{
enable_security_ = true;
}

std::string async_serialization =
ini.get_entry(key + ".async_serialization", "0");
if(boost::lexical_cast<int>(async_serialization) != 0)
if(hpx::util::get_entry_as<int>(ini, key + ".async_serialization", "0") != 0)
{
async_serialization_ = true;
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
#include <hpx/runtime/parcelset/policies/ibverbs/receiver.hpp>
#include <hpx/lcos/local/spinlock.hpp>
#include <hpx/util/runtime_configuration.hpp>
#include <hpx/util/safe_lexical_cast.hpp>

#include <boost/assign/std/vector.hpp>
#include <boost/shared_ptr.hpp>
Expand Down Expand Up @@ -49,16 +50,14 @@ namespace hpx { namespace parcelset { namespace policies { namespace ibverbs

std::size_t connection_handler::memory_chunk_size(util::runtime_configuration const& ini)
{
std::string memory_chunk_size =
ini.get_entry("hpx.parcel.ibverbs.memory_chunk_size", HPX_PARCELPORT_IBVERBS_MEMORY_CHUNK_SIZE);
return boost::lexical_cast<std::size_t>(memory_chunk_size);
return hpx::util::get_entry_as<std::size_t>(
ini, "hpx.parcel.ibverbs.memory_chunk_size", HPX_PARCELPORT_IBVERBS_MEMORY_CHUNK_SIZE);
}

std::size_t connection_handler::max_memory_chunks(util::runtime_configuration const& ini)
{
std::string max_memory_chunks =
ini.get_entry("hpx.parcel.ibverbs.max_memory_chunks", HPX_PARCELPORT_IBVERBS_MAX_MEMORY_CHUNKS);
return boost::lexical_cast<std::size_t>(max_memory_chunks);
return hpx::util::get_entry_as<std::size_t>(
ini, "hpx.parcel.ibverbs.max_memory_chunks", HPX_PARCELPORT_IBVERBS_MAX_MEMORY_CHUNKS);
}

connection_handler::connection_handler(util::runtime_configuration const& ini,
Expand All @@ -74,9 +73,7 @@ namespace hpx { namespace parcelset { namespace policies { namespace ibverbs
// we never do zero copy optimization for this parcelport
allow_zero_copy_optimizations_ = false;

std::string use_io_pool =
ini.get_entry("hpx.parcel.ibverbs.use_io_pool", "1");
if(boost::lexical_cast<int>(use_io_pool) == 0)
if(hpx::util::get_entry_as<int>(ini, "hpx.parcel.ibverbs.use_io_pool", "1") == 0)
{
use_io_pool_ = false;
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
#include <hpx/lcos/local/spinlock.hpp>
#include <hpx/util/mpi_environment.hpp>
#include <hpx/util/runtime_configuration.hpp>
#include <hpx/util/safe_lexical_cast.hpp>

#include <boost/assign/std/vector.hpp>
#include <boost/shared_ptr.hpp>
Expand Down Expand Up @@ -61,9 +62,7 @@ namespace hpx { namespace parcelset { namespace policies { namespace mpi
"this parcelport was instantiated to represent an unexpected "
"locality type: " + get_connection_type_name(here_.get_type()));
}
std::string use_io_pool =
ini.get_entry("hpx.parcel.mpi.use_io_pool", "1");
if(boost::lexical_cast<int>(use_io_pool) == 0)
if(hpx::util::get_entry_as<int>(ini, "hpx.parcel.mpi.use_io_pool", "1") == 0)
{
use_io_pool_ = false;
}
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/threads/policies/affinity_data.cpp
Expand Up @@ -9,6 +9,7 @@
#include <hpx/runtime/threads/topology.hpp>

#include <hpx/util/mpi_environment.hpp>
#include <hpx/util/safe_lexical_cast.hpp>

#include <algorithm>

Expand Down Expand Up @@ -62,9 +63,10 @@ namespace hpx { namespace threads { namespace policies { namespace detail
pu_nums_.clear();

#if defined(HPX_HAVE_HWLOC)
const std::size_t used_cores = data.used_cores_;
std::size_t max_cores =
boost::lexical_cast<std::size_t>(
get_runtime().get_config().get_entry("hpx.cores", data.used_cores_));
hpx::util::safe_lexical_cast<std::size_t>(
get_runtime().get_config().get_entry("hpx.cores", used_cores), used_cores);

if (!data.affinity_desc_.empty())
{
Expand Down
5 changes: 3 additions & 2 deletions src/util/batch_environment.cpp
Expand Up @@ -7,6 +7,7 @@
#include <hpx/hpx_fwd.hpp>
#include <hpx/exception.hpp>
#include <hpx/util/batch_environment.hpp>
#include <hpx/util/safe_lexical_cast.hpp>

#include <iostream>
#include <fstream>
Expand Down Expand Up @@ -87,8 +88,8 @@ namespace hpx { namespace util { namespace detail
}
else
{
std::size_t begin = boost::lexical_cast<std::size_t>(range[0]);
std::size_t end = boost::lexical_cast<std::size_t>(range[1]);
std::size_t begin = hpx::util::safe_lexical_cast<std::size_t>(range[0]);
std::size_t end = hpx::util::safe_lexical_cast<std::size_t>(range[1]);
if(begin > end) std::swap(begin, end);

std::vector<std::string> vs;
Expand Down