Skip to content

Added support for int32_t and float #122

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 50 additions & 3 deletions include/cpptoml.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ class value;

template <class T>
struct valid_value
: is_one_of<T, std::string, int64_t, double, bool, local_date, local_time,
local_datetime, offset_datetime>
: is_one_of<T, std::string, int64_t, int32_t, double, float, bool,
local_date, local_time, local_datetime, offset_datetime>
{
};

Expand Down Expand Up @@ -455,12 +455,24 @@ struct base_type_traits<offset_datetime>
static const base_type type = base_type::OFFSET_DATETIME;
};

template <>
struct base_type_traits<int32_t>
{
static const base_type type = base_type::INT;
};

template <>
struct base_type_traits<int64_t>
{
static const base_type type = base_type::INT;
};

template <>
struct base_type_traits<float>
{
static const base_type type = base_type::FLOAT;
};

template <>
struct base_type_traits<double>
{
Expand Down Expand Up @@ -714,6 +726,24 @@ inline std::shared_ptr<value<double>> base::as()
return nullptr;
}

// specialization for <float>
template <>
inline std::shared_ptr<value<float>> base::as()
{
auto v1 = as<double>();
auto v2 = make_value<float>(static_cast<float>(v1->get()));
return v2;
}

// specialization for <int32_t>
template <>
inline std::shared_ptr<value<int32_t>> base::as()
{
auto v1 = as<int64_t>();
auto v2 = make_value<int32_t>(static_cast<int32_t>(v1->get()));
return v2;
}

template <class T>
inline std::shared_ptr<const value<T>> base::as() const
{
Expand Down Expand Up @@ -760,6 +790,23 @@ inline std::shared_ptr<const value<double>> base::as() const
return nullptr;
}

template <>
inline std::shared_ptr<const value<float>> base::as() const
{
auto v1 = as<double>();
auto v2 = make_value<const float>(static_cast<float>(v1->get()));
return v2;
}

// specialization for <int32_t>
template <>
inline std::shared_ptr<const value<int32_t>> base::as() const
{
auto v1 = as<int64_t>();
auto v2 = make_value<const int32_t>(static_cast<int32_t>(v1->get()));
return v2;
}

/**
* Exception class for array insertion errors.
*/
Expand Down Expand Up @@ -3665,4 +3712,4 @@ inline std::ostream& operator<<(std::ostream& stream, const array& a)
return stream;
}
} // namespace cpptoml
#endif // CPPTOML_H
#endif // CPPTOML_H