add convenience constructors and assignment ops to any_value

master
Daniel Kolesa 2021-05-07 01:56:43 +02:00
parent 1c56ea36ff
commit 7f74602b7e
3 changed files with 98 additions and 4 deletions

View File

@ -303,6 +303,24 @@ struct LIBCUBESCRIPT_EXPORT any_value {
/** @brief Construct a value_type::NONE value. */
any_value();
/** @brief Construct a value_type::INTEGER value. */
any_value(integer_type val);
/** @brief Construct a value_type::FLOAT value. */
any_value(float_type val);
/** @brief Construct a value_type::STRING value. */
any_value(std::string_view val, state &cs);
/** @brief Construct a value_type::STRING value. */
any_value(string_ref const &val);
/** @brief Construct a value_type::CODE value. */
any_value(bcode_ref const &val);
/** @brief Construct a value_type::IDENT value. */
any_value(ident &val);
/** @brief Destroy the value.
*
* If holding a reference counted value, the refcount will be decreased
@ -328,6 +346,21 @@ struct LIBCUBESCRIPT_EXPORT any_value {
*/
any_value &operator=(any_value &&);
/** @brief Assign an integer to the value. */
any_value &operator=(integer_type val);
/** @brief Assign a float to the value. */
any_value &operator=(float_type val);
/** @brief Assign a string reference to the value. */
any_value &operator=(string_ref const &val);
/** @brief Assign a bytecode reference to the value. */
any_value &operator=(bcode_ref const &val);
/** @brief Assign an ident to the value. */
any_value &operator=(ident &val);
/** @brief Get the type of the value. */
value_type type() const;

View File

@ -83,6 +83,44 @@ any_value::any_value():
p_stor{}, p_type{value_type::NONE}
{}
any_value::any_value(integer_type val):
p_stor{}, p_type{value_type::INTEGER}
{
csv_get<integer_type>(&p_stor) = val;
}
any_value::any_value(float_type val):
p_stor{}, p_type{value_type::FLOAT}
{
csv_get<float_type>(&p_stor) = val;
}
any_value::any_value(std::string_view val, state &cs):
p_stor{}, p_type{value_type::STRING}
{
csv_get<char const *>(&p_stor) = state_p{cs}.ts().istate->strman->add(val);
}
any_value::any_value(string_ref const &val):
p_stor{}, p_type{value_type::STRING}
{
csv_get<char const *>(&p_stor) = str_managed_ref(val.p_str);
}
any_value::any_value(bcode_ref const &val):
p_stor{}, p_type{value_type::CODE}
{
bcode *p = bcode_p{val}.get();
bcode_addref(p->raw());
csv_get<bcode *>(&p_stor) = p;
}
any_value::any_value(ident &val):
p_stor{}, p_type{value_type::IDENT}
{
csv_get<ident *>(&p_stor) = &val;
}
any_value::~any_value() {
csv_cleanup(p_type, &p_stor);
}
@ -125,6 +163,31 @@ any_value &any_value::operator=(any_value &&v) {
return *this;
}
any_value &any_value::operator=(integer_type val) {
set_integer(val);
return *this;
}
any_value &any_value::operator=(float_type val) {
set_float(val);
return *this;
}
any_value &any_value::operator=(string_ref const &val) {
set_string(val);
return *this;
}
any_value &any_value::operator=(bcode_ref const &val) {
set_code(val);
return *this;
}
any_value &any_value::operator=(ident &val) {
set_ident(val);
return *this;
}
value_type any_value::type() const {
return p_type;
}

View File

@ -336,11 +336,9 @@ int main(int argc, char **argv) {
if (nargs == 2) {
nv = args[1];
} else if (nargs == 3) {
nv.set_integer(
(args[1].get_integer() << 8) | (args[2].get_integer() << 16)
);
nv = (args[1].get_integer() << 8) | (args[2].get_integer() << 16);
} else {
nv.set_integer(
nv = (
args[1].get_integer() | (args[2].get_integer() << 8) |
(args[3].get_integer() << 16)
);