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

Avoid implicit conversion #335

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion demo/epidemiology/src/evaluate.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ inline void PlotResults(const TimeSeries* analytical, const TimeSeries* mean,
allts.Add(ind_ts, Concat("i", i++));
}
LineGraph lg(&allts, "", "Time [h]", "Population Fraction", plot_legend,
style, 350, 250);
&style, 350, 250);
if (analytical) {
lg.Add("susceptible-analytical", " ", "L", kBlue, 1.0, kDashed, 2, kBlue,
1.0, 1, 1, 0, 1.0, 0);
Expand Down
9 changes: 6 additions & 3 deletions src/core/agent/agent_pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ class AgentPointer {

uint64_t GetUidAsUint64() const {
if (*this == nullptr) {
return AgentUid();
return static_cast<uint64_t>(AgentUid());
}
if (gAgentPointerMode == AgentPointerMode::kIndirect) {
return d_.uid;
return static_cast<uint64_t>(d_.uid);
} else {
return d_.agent->GetUid();
return static_cast<uint64_t>(d_.agent->GetUid());
}
}

Expand Down Expand Up @@ -211,6 +211,9 @@ class AgentPointer {

const TAgent& operator*() const { return *(this->operator->()); }

// Note: allow for implicit conversion to bool
/// Allows to use `if (agent_ptr) { ... }`. Returns true if the pointer is not
/// nullptr.
operator bool() const { return *this != nullptr; } // NOLINT

operator AgentPointer<Agent>() const {
Expand Down
2 changes: 1 addition & 1 deletion src/core/agent/agent_uid.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AgentUid {
return *this;
}

operator uint64_t() const {
explicit operator uint64_t() const {
return (static_cast<uint64_t>(reused_) << 32) |
static_cast<uint64_t>(index_);
}
Expand Down
14 changes: 11 additions & 3 deletions src/core/analysis/line_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ namespace experimental {
// -----------------------------------------------------------------------------
LineGraph::LineGraph(const TimeSeries* ts, const std::string& title,
const std::string& xaxis_title,
const std::string& yaxis_title, bool legend, TStyle* style,
int width, int height)
: ts_(ts), s_(style) {
const std::string& yaxis_title, bool legend,
const Style* style, int width, int height)
: ts_(ts) {
if (!ts_) {
Log::Fatal("LineGraph::LineGraph",
"The provided TimeSeries is a nullptr. Operation aborted.");
return;
}
if (style) {
s_ = style->GetTStyle();
}
if (s_) {
s_->cd();
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/analysis/line_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string>
#include <unordered_map>
#include <vector>
#include "core/analysis/style.h"
#include "core/real_t.h"
#include "core/util/root.h"

Expand All @@ -39,7 +40,7 @@ class LineGraph {
LineGraph(const TimeSeries* ts, const std::string& title = "",
const std::string& xaxis_title = "",
const std::string& yaxis_title = "", bool legend = true,
TStyle* style = nullptr, int width = 700, int height = 500);
const Style* style = nullptr, int width = 700, int height = 500);

~LineGraph();

Expand Down
2 changes: 1 addition & 1 deletion src/core/analysis/style.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Style : public TNamed,
public:
Style();
~Style();
operator TStyle*() const;
explicit operator TStyle*() const;
TStyle* GetTStyle() const;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/core/environment/morton_order.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace bdm {
template <typename T>
class Stack {
public:
Stack(uint64_t max_elements) { data_.resize(max_elements); }
explicit Stack(uint64_t max_elements) { data_.resize(max_elements); }

void Push(const T& el) {
data_[++top_] = el;
Expand Down
2 changes: 1 addition & 1 deletion src/core/environment/uniform_grid_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ class UniformGridEnvironment : public Environment {
private:
class LoadBalanceInfoUG : public LoadBalanceInfo {
public:
LoadBalanceInfoUG(UniformGridEnvironment* grid);
explicit LoadBalanceInfoUG(UniformGridEnvironment* grid);
virtual ~LoadBalanceInfoUG();
void Update();
void CallHandleIteratorConsumer(
Expand Down
4 changes: 2 additions & 2 deletions src/core/functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct LambdaFunctor<TReturn (TLambda::*)(TArgs...) const> final
: public Functor<TReturn, TArgs...> {
TLambda lambda;

LambdaFunctor(const TLambda& lambda) : lambda(lambda) {}
explicit LambdaFunctor(const TLambda& lambda) : lambda(lambda) {}
LambdaFunctor(const LambdaFunctor& other) : lambda(other.lambda) {}
virtual ~LambdaFunctor() = default;

Expand All @@ -57,7 +57,7 @@ struct LambdaFunctor<TReturn (TLambda::*)(TArgs...)> final
: public Functor<TReturn, TArgs...> {
TLambda lambda;

LambdaFunctor(const TLambda& lambda) : lambda(lambda) {}
explicit LambdaFunctor(const TLambda& lambda) : lambda(lambda) {}
LambdaFunctor(const LambdaFunctor& other) : lambda(other.lambda) {}
virtual ~LambdaFunctor() = default;

Expand Down
17 changes: 10 additions & 7 deletions src/core/util/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ template <typename TSample>
class DistributionRng {
public:
DistributionRng() = default;
DistributionRng(TRootIOCtor*) {}
explicit DistributionRng(TRootIOCtor*) {}
virtual ~DistributionRng() = default;
/// Draws a sample from the distribution
TSample Sample();
Expand Down Expand Up @@ -89,7 +89,7 @@ class GausRng : public DistributionRng<real_t> {
// -----------------------------------------------------------------------------
class ExpRng : public DistributionRng<real_t> {
public:
ExpRng(real_t tau);
explicit ExpRng(real_t tau);
virtual ~ExpRng();

private:
Expand All @@ -113,7 +113,7 @@ class LandauRng : public DistributionRng<real_t> {
// -----------------------------------------------------------------------------
class PoissonDRng : public DistributionRng<real_t> {
public:
PoissonDRng(real_t mean);
explicit PoissonDRng(real_t mean);
virtual ~PoissonDRng();

private:
Expand All @@ -137,7 +137,8 @@ class BreitWignerRng : public DistributionRng<real_t> {
// -----------------------------------------------------------------------------
class UserDefinedDistRng1D : public DistributionRng<real_t> {
public:
UserDefinedDistRng1D(TRootIOCtor* ioctor) : DistributionRng<real_t>(ioctor) {}
explicit UserDefinedDistRng1D(TRootIOCtor* ioctor)
: DistributionRng<real_t>(ioctor) {}
UserDefinedDistRng1D(TF1* function, const char* option);
virtual ~UserDefinedDistRng1D();
void Draw(const char* option = "");
Expand All @@ -154,7 +155,8 @@ class UserDefinedDistRng1D : public DistributionRng<real_t> {
// -----------------------------------------------------------------------------
class UserDefinedDistRng2D : public DistributionRng<real_t> {
public:
UserDefinedDistRng2D(TRootIOCtor* ioctor) : DistributionRng<real_t>(ioctor) {}
explicit UserDefinedDistRng2D(TRootIOCtor* ioctor)
: DistributionRng<real_t>(ioctor) {}
UserDefinedDistRng2D(TF2* function, const char* option);
virtual ~UserDefinedDistRng2D();
void Draw(const char* option = "");
Expand All @@ -172,7 +174,8 @@ class UserDefinedDistRng2D : public DistributionRng<real_t> {
// -----------------------------------------------------------------------------
class UserDefinedDistRng3D : public DistributionRng<real_t> {
public:
UserDefinedDistRng3D(TRootIOCtor* ioctor) : DistributionRng<real_t>(ioctor) {}
explicit UserDefinedDistRng3D(TRootIOCtor* ioctor)
: DistributionRng<real_t>(ioctor) {}
UserDefinedDistRng3D(TF3* function, const char* option);
virtual ~UserDefinedDistRng3D();
void Draw(const char* option = "");
Expand Down Expand Up @@ -246,7 +249,7 @@ class BinomialRng : public DistributionRng<int> {
// -----------------------------------------------------------------------------
class PoissonRng : public DistributionRng<int> {
public:
PoissonRng(real_t mean);
explicit PoissonRng(real_t mean);
virtual ~PoissonRng();

private:
Expand Down
2 changes: 1 addition & 1 deletion src/core/visualization/paraview/mapped_data_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct GetDataMemberForVis {
auto* casted_agent = static_cast<TClass*>(agent);
auto* data = reinterpret_cast<TDataMember*>(
reinterpret_cast<char*>(casted_agent) + dm_offset_);
uint64_t uid = *data;
uint64_t uid = static_cast<uint64_t>(*data);
auto tid = ThreadInfo::GetInstance()->GetUniversalThreadId();
assert(temp_values_.size() > tid);
temp_values_[tid][0] = uid;
Expand Down
6 changes: 4 additions & 2 deletions src/core/visualization/root/adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class RootAdaptor {

/// Adds a sphere object to the volume
void AddSphere(const Agent *agent, TGeoVolume *container) {
std::string name = agent->GetTypeName() + std::to_string(agent->GetUid());
std::string name = agent->GetTypeName() +
std::to_string(static_cast<uint64_t>(agent->GetUid()));
auto radius = agent->GetDiameter() / 2;
auto massLocation = agent->GetPosition();
auto x = massLocation[0];
Expand All @@ -154,7 +155,8 @@ class RootAdaptor {
/// Adds a cylinder object to the volume
void AddCylinder(const Agent *agent, TGeoVolume *container) {
if (auto neurite = dynamic_cast<const NeuriteElement *>(agent)) {
std::string name = agent->GetTypeName() + std::to_string(agent->GetUid());
std::string name = agent->GetTypeName() +
std::to_string(static_cast<uint64_t>(agent->GetUid()));
auto radius = neurite->GetDiameter() / 2;
auto half_length = neurite->GetLength() / 2;
auto massLocation = neurite->GetPosition();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/core/agent/agent_uid_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ TEST(AgentUidTest, MinusOperator) {

TEST(AgentUidTest, uint64_tOperator) {
AgentUid uid(123, 0);
uint64_t idx = uid;
uint64_t idx = static_cast<uint64_t>(uid);
EXPECT_EQ(idx, 123u);
}

TEST(AgentUidTest, uint64_tOperator2) {
AgentUid uid(123, 2);
uint64_t idx = uid;
uint64_t idx = static_cast<uint64_t>(uid);
EXPECT_EQ(idx, 8589934715u); // (2 << 32) | 123u);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ struct ZOrderCallback : Functor<void, const AgentHandle&> {
box_cnt++;
}
auto* agent = rm->GetAgent(ah);
zorder[box_cnt].insert(agent->GetUid() - ref_uid);
zorder[box_cnt].insert(agent->GetUid() - static_cast<uint64_t>(ref_uid));
cnt++;
}
};
Expand Down
6 changes: 4 additions & 2 deletions test/unit/core/resource_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ TEST(ResourceManagerTest, ForEachAgentFilter) {
rm->ForEachAgent(
[&](Agent* a) { // NOLINT
counter++;
switch (a->GetUid() - ref_uid) {
switch (static_cast<uint64_t>(a->GetUid()) -
static_cast<uint64_t>(ref_uid)) {
case 0:
EXPECT_EQ(0, dynamic_cast<TestAgent*>(a)->GetData());
break;
Expand All @@ -63,7 +64,8 @@ TEST(ResourceManagerTest, ForEachAgentFilter) {
rm->ForEachAgent(
[&](Agent* a) { // NOLINT
counter++;
switch (a->GetUid() - ref_uid) {
switch (static_cast<uint64_t>(a->GetUid()) -
static_cast<uint64_t>(ref_uid)) {
case 1:
EXPECT_EQ(1, dynamic_cast<TestAgent*>(a)->GetData());
break;
Expand Down
3 changes: 2 additions & 1 deletion test/unit/core/resource_manager_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ inline void RunForEachAgentTest() {
uint64_t counter = 0;
rm->ForEachAgent([&](Agent* element) { // NOLINT
counter++;
switch (element->GetUid() - ref_uid) {
switch (static_cast<uint64_t>(element->GetUid()) -
static_cast<uint64_t>(ref_uid)) {
case 0:
EXPECT_EQ(12, dynamic_cast<A*>(element)->GetData());
break;
Expand Down