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

C++ code cleanup and modernization #1452

Merged
merged 13 commits into from
Mar 9, 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
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
* For any new code, do *not* use the `doublereal` and `integer` typedefs for the
basic types `double` and `int`, but also do not go out of your way to change
uses of these in otherwise unmodified code.
* Initialize member variables with their declarations, when possible, rather than using
constructor-based initialization.

## Python

Expand Down
36 changes: 13 additions & 23 deletions include/cantera/base/AnyMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
#include "cantera/base/Units.h"

#include <unordered_map>
#include <functional>
#include <set>

namespace boost
{
class any;
}
#include <filesystem>
#include <any>

namespace YAML
{
Expand All @@ -33,8 +28,8 @@ namespace Cantera
//! objects.
class AnyBase {
public:
AnyBase();
virtual ~AnyBase() {};
AnyBase() = default;
virtual ~AnyBase() = default;

//! For values which are derived from an input file, set the line and column
//! of this value in that file. Used for providing context for some error
Expand All @@ -48,11 +43,11 @@ class AnyBase {
protected:
//! The line where this value occurs in the input file. Set to -1 for values
//! that weren't created from an input file.
int m_line;
int m_line = -1;

//! If m_line >= 0, the column where this value occurs in the input file.
//! If m_line == -1, a value used for determining output ordering
int m_column;
int m_column = 0;

//! Metadata relevant to an entire AnyMap tree, such as information about
// the input file used to create it
Expand Down Expand Up @@ -86,10 +81,6 @@ class AnyValue : public AnyBase
public:
AnyValue();
~AnyValue();
AnyValue(AnyValue const& other);
AnyValue(AnyValue&& other);
AnyValue& operator=(AnyValue const& other);
AnyValue& operator=(AnyValue&& other);

bool operator==(const AnyValue& other) const;
bool operator!=(const AnyValue& other) const;
Expand Down Expand Up @@ -276,24 +267,24 @@ class AnyValue : public AnyBase
std::string m_key;

//! The held value
std::unique_ptr<boost::any> m_value;
std::any m_value;

typedef bool (*Comparer)(const boost::any&, const boost::any&);
typedef bool (*Comparer)(const std::any&, const std::any&);

//! Equality comparison function used when *lhs* is of type *T*
template <typename T>
static bool eq_comparer(const boost::any& lhs, const boost::any& rhs);
static bool eq_comparer(const std::any& lhs, const std::any& rhs);

//! Helper function for comparing vectors of different (but comparable)
//! types, for example `vector<double>` and `vector<long int>`
template<class T, class U>
static bool vector_eq(const boost::any& lhs, const boost::any& rhs);
static bool vector_eq(const std::any& lhs, const std::any& rhs);

//! Helper function for comparing nested vectors of different (but
//! comparable) types, for example `vector<vector<double>>` and
//! `vector<vector<long int>>`
template<class T, class U>
static bool vector2_eq(const boost::any& lhs, const boost::any& rhs);
static bool vector2_eq(const std::any& lhs, const std::any& rhs);

mutable Comparer m_equals;

Expand Down Expand Up @@ -676,7 +667,8 @@ class AnyMap : public AnyBase
//! Cache for previously-parsed input (YAML) files. The key is the full path
//! to the file, and the second element of the value is the last-modified
//! time for the file, which is used to enable change detection.
static std::unordered_map<std::string, std::pair<AnyMap, int>> s_cache;
static std::unordered_map<string,
pair<AnyMap, std::filesystem::file_time_type>> s_cache;

//! Information about fields that should appear first when outputting to
//! YAML. Keys in this map are matched to `__type__` keys in AnyMap
Expand Down Expand Up @@ -754,8 +746,6 @@ void warn_deprecated(const std::string& source, const AnyBase& node,

}

#ifndef CANTERA_API_NO_BOOST
#include "cantera/base/AnyMap.inl.h"
#endif

#endif
62 changes: 29 additions & 33 deletions include/cantera/base/AnyMap.inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

#include "cantera/base/AnyMap.h"

#include <boost/any.hpp>
#include <boost/algorithm/string.hpp>

namespace Cantera
{
// re-declared to avoid needing to include global.h here
Expand All @@ -18,31 +15,31 @@ std::string demangle(const std::type_info& type);
template<class T>
const T &AnyValue::as() const {
try {
if (typeid(T) == typeid(double) && m_value->type() == typeid(long int)) {
if (typeid(T) == typeid(double) && m_value.type() == typeid(long int)) {
// Implicit conversion of long int to double
*m_value = static_cast<double>(as<long int>());
const_cast<AnyValue*>(this)->m_value = static_cast<double>(as<long int>());
m_equals = eq_comparer<double>;
} else if (typeid(T) == typeid(std::vector<double>)
&& m_value->type() == typeid(std::vector<AnyValue>)) {
&& m_value.type() == typeid(std::vector<AnyValue>)) {
// Implicit conversion of vector<AnyValue> to vector<double>
auto& asAny = as<std::vector<AnyValue>>();
vector_fp asDouble(asAny.size());
for (size_t i = 0; i < asAny.size(); i++) {
asDouble[i] = asAny[i].as<double>();
}
*m_value = std::move(asDouble);
const_cast<AnyValue*>(this)->m_value = std::move(asDouble);
m_equals = eq_comparer<std::vector<double>>;
}
return boost::any_cast<const T&>(*m_value);
} catch (boost::bad_any_cast&) {
if (m_value->type() == typeid(void)) {
return std::any_cast<const T&>(m_value);
} catch (std::bad_any_cast&) {
if (m_value.type() == typeid(void)) {
// Values that have not been set are of type 'void'
throw InputFileError("AnyValue::as", *this,
"Key '{}' not found or contains no value", m_key);
} else {
throw InputFileError("AnyValue::as", *this,
"Key '{}' contains a '{}',\nnot a '{}'",
m_key, demangle(m_value->type()), demangle(typeid(T)));
m_key, demangle(m_value.type()), demangle(typeid(T)));
}
}
}
Expand All @@ -56,14 +53,14 @@ T &AnyValue::as() {

template<class T>
bool AnyValue::is() const {
return m_value->type() == typeid(T);
return m_value.type() == typeid(T);
}

template<> bool AnyValue::is<std::vector<double>>() const;

template<class T>
AnyValue &AnyValue::operator=(const std::vector<T> &value) {
*m_value = value;
m_value = value;
m_equals = eq_comparer<std::vector<T>>;
return *this;
}
Expand All @@ -84,22 +81,22 @@ std::vector<T> &AnyValue::asVector(size_t nMin, size_t nMax) {

template<class T>
AnyValue& AnyValue::operator=(const std::unordered_map<std::string, T> items) {
*m_value = AnyMap();
m_value = AnyMap();
m_equals = eq_comparer<AnyMap>;
AnyMap& dest = as<AnyMap>();
for (const auto& item : items) {
dest[item.first] = item.second;
for (const auto& [key, value] : items) {
dest[key] = value;
}
return *this;
}

template<class T>
AnyValue& AnyValue::operator=(const std::map<std::string, T> items) {
*m_value = AnyMap();
m_value = AnyMap();
m_equals = eq_comparer<AnyMap>;
AnyMap& dest = as<AnyMap>();
for (const auto& item : items) {
dest[item.first] = item.second;
for (const auto& [key, value] : items) {
dest[key] = value;
}
return *this;
}
Expand All @@ -109,15 +106,15 @@ inline AnyMap& AnyValue::as<AnyMap>() {
try {
// This is where nested AnyMaps are created when the syntax
// m[key1][key2] is used.
if (m_value->type() == typeid(void)) {
*m_value = AnyMap();
if (m_value.type() == typeid(void)) {
m_value = AnyMap();
m_equals = eq_comparer<AnyMap>;
}
return boost::any_cast<AnyMap&>(*m_value);
} catch (boost::bad_any_cast&) {
return std::any_cast<AnyMap&>(m_value);
} catch (std::bad_any_cast&) {
throw InputFileError("AnyValue::as", *this,
"value of key '{}' is a '{}',\nnot an 'AnyMap'.",
m_key, demangle(m_value->type()));
m_key, demangle(m_value.type()));
}
}

Expand Down Expand Up @@ -147,10 +144,10 @@ void AnyValue::checkSize(const std::vector<T>& v, size_t nMin, size_t nMax) cons
}

template<class T, class U>
bool AnyValue::vector_eq(const boost::any& lhs, const boost::any& rhs)
bool AnyValue::vector_eq(const std::any& lhs, const std::any& rhs)
{
const auto& lvec = boost::any_cast<T>(lhs);
const auto& rvec = boost::any_cast<U>(rhs);
const auto& lvec = std::any_cast<T>(lhs);
const auto& rvec = std::any_cast<U>(rhs);
if (lvec.size() != rvec.size()) {
return false;
} else {
Expand All @@ -159,10 +156,10 @@ bool AnyValue::vector_eq(const boost::any& lhs, const boost::any& rhs)
}

template<class T, class U>
bool AnyValue::vector2_eq(const boost::any& lhs, const boost::any& rhs)
bool AnyValue::vector2_eq(const std::any& lhs, const std::any& rhs)
{
const auto& lvec = boost::any_cast<std::vector<T>>(lhs);
const auto& rvec = boost::any_cast<std::vector<U>>(rhs);
const auto& lvec = std::any_cast<std::vector<T>>(lhs);
const auto& rvec = std::any_cast<std::vector<U>>(rhs);
if (lvec.size() != rvec.size()) {
return false;
} else {
Expand All @@ -176,10 +173,9 @@ bool AnyValue::vector2_eq(const boost::any& lhs, const boost::any& rhs)
}

template<class T>
bool AnyValue::eq_comparer(const boost::any& lhs, const boost::any& rhs)
bool AnyValue::eq_comparer(const std::any& lhs, const std::any& rhs)
{
using boost::any_cast;
using std::vector;
using std::any_cast;
typedef vector<double> vd;
typedef vector<long int> vi;
typedef vector<AnyValue> va;
Expand Down
8 changes: 4 additions & 4 deletions include/cantera/base/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Array2D
/**
* Default constructor. Create an empty array.
*/
Array2D();
Array2D() = default;

//! Constructor.
/*!
Expand All @@ -72,7 +72,7 @@ class Array2D

Array2D(const Array2D& y);

virtual ~Array2D() {}
virtual ~Array2D() = default;

Array2D& operator=(const Array2D& y);

Expand Down Expand Up @@ -249,10 +249,10 @@ class Array2D
vector_fp m_data;

//! Number of rows
size_t m_nrows;
size_t m_nrows = 0;

//! Number of columns
size_t m_ncols;
size_t m_ncols = 0;
};

//! Output the current contents of the Array2D object
Expand Down
1 change: 0 additions & 1 deletion include/cantera/base/ExtensionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// at https://cantera.org/license.txt for license and copyright information.

#include "cantera/base/ctexceptions.h"
#include <functional>

namespace Cantera
{
Expand Down
2 changes: 1 addition & 1 deletion include/cantera/base/ExtensionManagerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ExtensionManagerFactory : public Factory<ExtensionManager>
static ExtensionManagerFactory* s_factory;

//! Private constructor prevents direct usage
ExtensionManagerFactory();
ExtensionManagerFactory() = default;

//! Decl for locking mutex for thermo factory singleton
static std::mutex s_mutex;
Expand Down
1 change: 0 additions & 1 deletion include/cantera/base/FactoryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <mutex>
#include <unordered_map>
#include <functional>
#include "cantera/base/ctexceptions.h"
#include "cantera/base/global.h"

Expand Down
2 changes: 1 addition & 1 deletion include/cantera/base/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Cantera
class Interface : public Solution
{
private:
Interface();
Interface() = default;

public:
~Interface() {}
Expand Down
1 change: 0 additions & 1 deletion include/cantera/base/NoExitLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#ifndef NOEXITLOGGER_H
#define NOEXITLOGGER_H

#include <string>
#include "cantera/base/logger.h"

namespace Cantera {
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/base/Solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class ExternalHandle;
class Solution : public std::enable_shared_from_this<Solution>
{
protected:
Solution();
Solution() = default;

public:
virtual ~Solution() {}
virtual ~Solution() = default;
Solution(const Solution&) = delete;
Solution& operator=(const Solution&) = delete;

Expand Down
1 change: 0 additions & 1 deletion include/cantera/base/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "cantera/base/ct_defs.h"
#include "cantera/base/stringUtils.h"
#include <set>

#if CT_USE_HDF5

Expand Down
Loading