Make PropertyType an enum class

This commit is contained in:
Thomas Goyne 2016-01-21 10:45:41 -08:00
parent 4cf5d5db4c
commit 03108713ee
9 changed files with 91 additions and 91 deletions

View File

@ -172,31 +172,31 @@ namespace realm {
} }
switch (property.type) { switch (property.type) {
case PropertyTypeBool: case PropertyType::Bool:
m_row.set_bool(column, Accessor::to_bool(ctx, value)); m_row.set_bool(column, Accessor::to_bool(ctx, value));
break; break;
case PropertyTypeInt: case PropertyType::Int:
m_row.set_int(column, Accessor::to_long(ctx, value)); m_row.set_int(column, Accessor::to_long(ctx, value));
break; break;
case PropertyTypeFloat: case PropertyType::Float:
m_row.set_float(column, Accessor::to_float(ctx, value)); m_row.set_float(column, Accessor::to_float(ctx, value));
break; break;
case PropertyTypeDouble: case PropertyType::Double:
m_row.set_double(column, Accessor::to_double(ctx, value)); m_row.set_double(column, Accessor::to_double(ctx, value));
break; break;
case PropertyTypeString: case PropertyType::String:
m_row.set_string(column, Accessor::to_string(ctx, value)); m_row.set_string(column, Accessor::to_string(ctx, value));
break; break;
case PropertyTypeData: case PropertyType::Data:
m_row.set_binary(column, BinaryData(Accessor::to_binary(ctx, value))); m_row.set_binary(column, BinaryData(Accessor::to_binary(ctx, value)));
break; break;
case PropertyTypeAny: case PropertyType::Any:
m_row.set_mixed(column, Accessor::to_mixed(ctx, value)); m_row.set_mixed(column, Accessor::to_mixed(ctx, value));
break; break;
case PropertyTypeDate: case PropertyType::Date:
m_row.set_timestamp(column, Accessor::to_timestamp(ctx, value)); m_row.set_timestamp(column, Accessor::to_timestamp(ctx, value));
break; break;
case PropertyTypeObject: { case PropertyType::Object: {
if (Accessor::is_null(ctx, value)) { if (Accessor::is_null(ctx, value)) {
m_row.nullify_link(column); m_row.nullify_link(column);
} }
@ -205,7 +205,7 @@ namespace realm {
} }
break; break;
} }
case PropertyTypeArray: { case PropertyType::Array: {
realm::LinkViewRef link_view = m_row.get_linklist(column); realm::LinkViewRef link_view = m_row.get_linklist(column);
link_view->clear(); link_view->clear();
if (!Accessor::is_null(ctx, value)) { if (!Accessor::is_null(ctx, value)) {
@ -231,23 +231,23 @@ namespace realm {
} }
switch (property.type) { switch (property.type) {
case PropertyTypeBool: case PropertyType::Bool:
return Accessor::from_bool(ctx, m_row.get_bool(column)); return Accessor::from_bool(ctx, m_row.get_bool(column));
case PropertyTypeInt: case PropertyType::Int:
return Accessor::from_long(ctx, m_row.get_int(column)); return Accessor::from_long(ctx, m_row.get_int(column));
case PropertyTypeFloat: case PropertyType::Float:
return Accessor::from_float(ctx, m_row.get_float(column)); return Accessor::from_float(ctx, m_row.get_float(column));
case PropertyTypeDouble: case PropertyType::Double:
return Accessor::from_double(ctx, m_row.get_double(column)); return Accessor::from_double(ctx, m_row.get_double(column));
case PropertyTypeString: case PropertyType::String:
return Accessor::from_string(ctx, m_row.get_string(column)); return Accessor::from_string(ctx, m_row.get_string(column));
case PropertyTypeData: case PropertyType::Data:
return Accessor::from_binary(ctx, m_row.get_binary(column)); return Accessor::from_binary(ctx, m_row.get_binary(column));
case PropertyTypeAny: case PropertyType::Any:
throw "Any not supported"; throw "Any not supported";
case PropertyTypeDate: case PropertyType::Date:
return Accessor::from_timestamp(ctx, m_row.get_timestamp(column)); return Accessor::from_timestamp(ctx, m_row.get_timestamp(column));
case PropertyTypeObject: { case PropertyType::Object: {
auto linkObjectSchema = m_realm->config().schema->find(property.object_type); auto linkObjectSchema = m_realm->config().schema->find(property.object_type);
TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), linkObjectSchema->name); TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), linkObjectSchema->name);
if (m_row.is_null_link(property.table_column)) { if (m_row.is_null_link(property.table_column)) {
@ -255,7 +255,7 @@ namespace realm {
} }
return Accessor::from_object(ctx, std::move(Object(m_realm, *linkObjectSchema, table->get(m_row.get_link(column))))); return Accessor::from_object(ctx, std::move(Object(m_realm, *linkObjectSchema, table->get(m_row.get_link(column)))));
} }
case PropertyTypeArray: { case PropertyType::Array: {
auto arrayObjectSchema = m_realm->config().schema->find(property.object_type); auto arrayObjectSchema = m_realm->config().schema->find(property.object_type);
return Accessor::from_list(ctx, std::move(List(m_realm, *arrayObjectSchema, static_cast<LinkViewRef>(m_row.get_linklist(column))))); return Accessor::from_list(ctx, std::move(List(m_realm, *arrayObjectSchema, static_cast<LinkViewRef>(m_row.get_linklist(column)))));
} }
@ -281,7 +281,7 @@ namespace realm {
if (primary_prop) { if (primary_prop) {
// search for existing object based on primary key type // search for existing object based on primary key type
ValueType primary_value = Accessor::dict_value_for_key(ctx, value, object_schema.primary_key); ValueType primary_value = Accessor::dict_value_for_key(ctx, value, object_schema.primary_key);
if (primary_prop->type == PropertyTypeString) { if (primary_prop->type == PropertyType::String) {
row_index = table->find_first_string(primary_prop->table_column, Accessor::to_string(ctx, primary_value)); row_index = table->find_first_string(primary_prop->table_column, Accessor::to_string(ctx, primary_value));
} }
else { else {
@ -312,7 +312,7 @@ namespace realm {
if (Accessor::has_default_value_for_property(ctx, realm.get(), object_schema, prop.name)) { if (Accessor::has_default_value_for_property(ctx, realm.get(), object_schema, prop.name)) {
object.set_property_value_impl(ctx, prop, Accessor::default_value_for_property(ctx, realm.get(), object_schema, prop.name), try_update); object.set_property_value_impl(ctx, prop, Accessor::default_value_for_property(ctx, realm.get(), object_schema, prop.name), try_update);
} }
else if (prop.is_nullable || prop.type == PropertyTypeArray) { else if (prop.is_nullable || prop.type == PropertyType::Array) {
object.set_property_value_impl(ctx, prop, Accessor::null_value(ctx), try_update); object.set_property_value_impl(ctx, prop, Accessor::null_value(ctx), try_update);
} }
else { else {

View File

@ -27,7 +27,7 @@
using namespace realm; using namespace realm;
#define ASSERT_PROPERTY_TYPE_VALUE(property, type) \ #define ASSERT_PROPERTY_TYPE_VALUE(property, type) \
static_assert(static_cast<int>(PropertyType##property) == type_##type, \ static_assert(static_cast<int>(PropertyType::property) == type_##type, \
"PropertyType and DataType must have the same values") "PropertyType and DataType must have the same values")
ASSERT_PROPERTY_TYPE_VALUE(Int, Int); ASSERT_PROPERTY_TYPE_VALUE(Int, Int);
@ -62,9 +62,9 @@ ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(n
property.type = (PropertyType)table->get_column_type(col); property.type = (PropertyType)table->get_column_type(col);
property.is_indexed = table->has_search_index(col); property.is_indexed = table->has_search_index(col);
property.is_primary = false; property.is_primary = false;
property.is_nullable = table->is_nullable(col) || property.type == PropertyTypeObject; property.is_nullable = table->is_nullable(col) || property.type == PropertyType::Object;
property.table_column = col; property.table_column = col;
if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) { if (property.type == PropertyType::Object || property.type == PropertyType::Array) {
// set link type for objects and arrays // set link type for objects and arrays
ConstTableRef linkTable = table->get_link_target(col); ConstTableRef linkTable = table->get_link_target(col);
property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data()); property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data());

View File

@ -224,25 +224,25 @@ static void copy_property_values(const Property& old_property, const Property& n
static void copy_property_values(const Property& source, const Property& destination, Table& table) { static void copy_property_values(const Property& source, const Property& destination, Table& table) {
switch (destination.type) { switch (destination.type) {
case PropertyTypeInt: case PropertyType::Int:
copy_property_values(source, destination, table, &Table::get_int, &Table::set_int); copy_property_values(source, destination, table, &Table::get_int, &Table::set_int);
break; break;
case PropertyTypeBool: case PropertyType::Bool:
copy_property_values(source, destination, table, &Table::get_bool, &Table::set_bool); copy_property_values(source, destination, table, &Table::get_bool, &Table::set_bool);
break; break;
case PropertyTypeFloat: case PropertyType::Float:
copy_property_values(source, destination, table, &Table::get_float, &Table::set_float); copy_property_values(source, destination, table, &Table::get_float, &Table::set_float);
break; break;
case PropertyTypeDouble: case PropertyType::Double:
copy_property_values(source, destination, table, &Table::get_double, &Table::set_double); copy_property_values(source, destination, table, &Table::get_double, &Table::set_double);
break; break;
case PropertyTypeString: case PropertyType::String:
copy_property_values(source, destination, table, &Table::get_string, &Table::set_string); copy_property_values(source, destination, table, &Table::get_string, &Table::set_string);
break; break;
case PropertyTypeData: case PropertyType::Data:
copy_property_values(source, destination, table, &Table::get_binary, &Table::set_binary); copy_property_values(source, destination, table, &Table::get_binary, &Table::set_binary);
break; break;
case PropertyTypeDate: case PropertyType::Date:
copy_property_values(source, destination, table, &Table::get_timestamp, &Table::set_timestamp); copy_property_values(source, destination, table, &Table::get_timestamp, &Table::set_timestamp);
break; break;
default: default:
@ -319,8 +319,8 @@ void ObjectStore::create_tables(Group *group, Schema &target_schema, bool update
if (!current_prop || current_prop->table_column == npos) { if (!current_prop || current_prop->table_column == npos) {
switch (target_prop.type) { switch (target_prop.type) {
// for objects and arrays, we have to specify target table // for objects and arrays, we have to specify target table
case PropertyTypeObject: case PropertyType::Object:
case PropertyTypeArray: { case PropertyType::Array: {
TableRef link_table = ObjectStore::table_for_object_type(group, target_prop.object_type); TableRef link_table = ObjectStore::table_for_object_type(group, target_prop.object_type);
REALM_ASSERT(link_table); REALM_ASSERT(link_table);
target_prop.table_column = table->add_column_link(DataType(target_prop.type), target_prop.name, *link_table); target_prop.table_column = table->add_column_link(DataType(target_prop.type), target_prop.name, *link_table);
@ -560,7 +560,7 @@ MissingPropertyException::MissingPropertyException(std::string const& object_typ
InvalidNullabilityException::InvalidNullabilityException(std::string const& object_type, Property const& property) : InvalidNullabilityException::InvalidNullabilityException(std::string const& object_type, Property const& property) :
ObjectSchemaPropertyException(object_type, property) ObjectSchemaPropertyException(object_type, property)
{ {
if (property.type == PropertyTypeObject) { if (property.type == PropertyType::Object) {
m_what = "'Object' property '" + property.name + "' must be nullable."; m_what = "'Object' property '" + property.name + "' must be nullable.";
} }
else { else {

View File

@ -94,7 +94,7 @@ struct PropertyExpression
KeyPath key_path = key_path_from_string(key_path_string); KeyPath key_path = key_path_from_string(key_path_string);
for (size_t index = 0; index < key_path.size(); index++) { for (size_t index = 0; index < key_path.size(); index++) {
if (prop) { if (prop) {
precondition(prop->type == PropertyTypeObject || prop->type == PropertyTypeArray, precondition(prop->type == PropertyType::Object || prop->type == PropertyType::Array,
(std::string)"Property '" + key_path[index] + "' is not a link in object of type '" + desc->name + "'"); (std::string)"Property '" + key_path[index] + "' is not a link in object of type '" + desc->name + "'");
indexes.push_back(prop->table_column); indexes.push_back(prop->table_column);
@ -400,36 +400,36 @@ void do_add_comparison_to_query(Query &query, const Schema &schema, const Object
{ {
auto type = expr.prop->type; auto type = expr.prop->type;
switch (type) { switch (type) {
case PropertyTypeBool: case PropertyType::Bool:
add_bool_constraint_to_query(query, cmp.op, value_of_type_for_query<bool>(expr.table_getter, lhs, args), add_bool_constraint_to_query(query, cmp.op, value_of_type_for_query<bool>(expr.table_getter, lhs, args),
value_of_type_for_query<bool>(expr.table_getter, rhs, args)); value_of_type_for_query<bool>(expr.table_getter, rhs, args));
break; break;
case PropertyTypeDate: case PropertyType::Date:
add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Timestamp>(expr.table_getter, lhs, args), add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Timestamp>(expr.table_getter, lhs, args),
value_of_type_for_query<Timestamp>(expr.table_getter, rhs, args)); value_of_type_for_query<Timestamp>(expr.table_getter, rhs, args));
break; break;
case PropertyTypeDouble: case PropertyType::Double:
add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Double>(expr.table_getter, lhs, args), add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Double>(expr.table_getter, lhs, args),
value_of_type_for_query<Double>(expr.table_getter, rhs, args)); value_of_type_for_query<Double>(expr.table_getter, rhs, args));
break; break;
case PropertyTypeFloat: case PropertyType::Float:
add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Float>(expr.table_getter, lhs, args), add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Float>(expr.table_getter, lhs, args),
value_of_type_for_query<Float>(expr.table_getter, rhs, args)); value_of_type_for_query<Float>(expr.table_getter, rhs, args));
break; break;
case PropertyTypeInt: case PropertyType::Int:
add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Int>(expr.table_getter, lhs, args), add_numeric_constraint_to_query(query, cmp.op, value_of_type_for_query<Int>(expr.table_getter, lhs, args),
value_of_type_for_query<Int>(expr.table_getter, rhs, args)); value_of_type_for_query<Int>(expr.table_getter, rhs, args));
break; break;
case PropertyTypeString: case PropertyType::String:
add_string_constraint_to_query(query, cmp, value_of_type_for_query<String>(expr.table_getter, lhs, args), add_string_constraint_to_query(query, cmp, value_of_type_for_query<String>(expr.table_getter, lhs, args),
value_of_type_for_query<String>(expr.table_getter, rhs, args)); value_of_type_for_query<String>(expr.table_getter, rhs, args));
break; break;
case PropertyTypeData: case PropertyType::Data:
add_binary_constraint_to_query(query, cmp.op, value_of_type_for_query<Binary>(expr.table_getter, lhs, args), add_binary_constraint_to_query(query, cmp.op, value_of_type_for_query<Binary>(expr.table_getter, lhs, args),
value_of_type_for_query<Binary>(expr.table_getter, rhs, args)); value_of_type_for_query<Binary>(expr.table_getter, rhs, args));
break; break;
case PropertyTypeObject: case PropertyType::Object:
case PropertyTypeArray: case PropertyType::Array:
add_link_constraint_to_query(query, cmp.op, expr, link_argument(lhs, rhs, args)); add_link_constraint_to_query(query, cmp.op, expr, link_argument(lhs, rhs, args));
break; break;
default: { default: {

View File

@ -22,17 +22,17 @@
#include <string> #include <string>
namespace realm { namespace realm {
enum PropertyType { enum class PropertyType {
PropertyTypeInt = 0, Int = 0,
PropertyTypeBool = 1, Bool = 1,
PropertyTypeFloat = 9, Float = 9,
PropertyTypeDouble = 10, Double = 10,
PropertyTypeString = 2, String = 2,
PropertyTypeData = 4, Data = 4,
PropertyTypeAny = 6, // deprecated and will be removed in the future Any = 6, // deprecated and will be removed in the future
PropertyTypeDate = 8, Date = 8,
PropertyTypeObject = 12, Object = 12,
PropertyTypeArray = 13, Array = 13,
}; };
struct Property { struct Property {
@ -47,15 +47,15 @@ namespace realm {
bool requires_index() const { return is_primary || is_indexed; } bool requires_index() const { return is_primary || is_indexed; }
bool is_indexable() const bool is_indexable() const
{ {
return type == PropertyTypeInt return type == PropertyType::Int
|| type == PropertyTypeBool || type == PropertyType::Bool
|| type == PropertyTypeDate || type == PropertyType::Date
|| type == PropertyTypeString; || type == PropertyType::String;
} }
#if __GNUC__ < 5 #if __GNUC__ < 5
// GCC 4.9 does not support C++14 braced-init with NSDMIs // GCC 4.9 does not support C++14 braced-init with NSDMIs
Property(std::string name="", PropertyType type=PropertyTypeInt, std::string object_type="", Property(std::string name="", PropertyType type=PropertyType::Int, std::string object_type="",
bool is_primary=false, bool is_indexed=false, bool is_nullable=false) bool is_primary=false, bool is_indexed=false, bool is_nullable=false)
: name(std::move(name)) : name(std::move(name))
, type(type) , type(type)
@ -70,25 +70,25 @@ namespace realm {
static inline const char *string_for_property_type(PropertyType type) { static inline const char *string_for_property_type(PropertyType type) {
switch (type) { switch (type) {
case PropertyTypeString: case PropertyType::String:
return "string"; return "string";
case PropertyTypeInt: case PropertyType::Int:
return "int"; return "int";
case PropertyTypeBool: case PropertyType::Bool:
return "bool"; return "bool";
case PropertyTypeDate: case PropertyType::Date:
return "date"; return "date";
case PropertyTypeData: case PropertyType::Data:
return "data"; return "data";
case PropertyTypeDouble: case PropertyType::Double:
return "double"; return "double";
case PropertyTypeFloat: case PropertyType::Float:
return "float"; return "float";
case PropertyTypeAny: case PropertyType::Any:
return "any"; return "any";
case PropertyTypeObject: case PropertyType::Object:
return "object"; return "object";
case PropertyTypeArray: case PropertyType::Array:
return "array"; return "array";
#if __GNUC__ #if __GNUC__
default: default:

View File

@ -74,11 +74,11 @@ void Schema::validate() const
// check nullablity // check nullablity
if (prop.is_nullable) { if (prop.is_nullable) {
if (prop.type == PropertyTypeArray || prop.type == PropertyTypeAny) { if (prop.type == PropertyType::Array || prop.type == PropertyType::Any) {
exceptions.emplace_back(InvalidNullabilityException(object.name, prop)); exceptions.emplace_back(InvalidNullabilityException(object.name, prop));
} }
} }
else if (prop.type == PropertyTypeObject) { else if (prop.type == PropertyType::Object) {
exceptions.emplace_back(InvalidNullabilityException(object.name, prop)); exceptions.emplace_back(InvalidNullabilityException(object.name, prop));
} }

View File

@ -24,16 +24,16 @@ TEST_CASE("list") {
config.cache = false; config.cache = false;
config.schema = std::make_unique<Schema>(Schema{ config.schema = std::make_unique<Schema>(Schema{
{"origin", "", { {"origin", "", {
{"array", PropertyTypeArray, "target"} {"array", PropertyType::Array, "target"}
}}, }},
{"target", "", { {"target", "", {
{"value", PropertyTypeInt} {"value", PropertyType::Int}
}}, }},
{"other_origin", "", { {"other_origin", "", {
{"array", PropertyTypeArray, "other_target"} {"array", PropertyType::Array, "other_target"}
}}, }},
{"other_target", "", { {"other_target", "", {
{"value", PropertyTypeInt} {"value", PropertyType::Int}
}}, }},
}); });

View File

@ -23,17 +23,17 @@ TEST_CASE("Results") {
config.automatic_change_notifications = false; config.automatic_change_notifications = false;
config.schema = std::make_unique<Schema>(Schema{ config.schema = std::make_unique<Schema>(Schema{
{"object", "", { {"object", "", {
{"value", PropertyTypeInt}, {"value", PropertyType::Int},
{"link", PropertyTypeObject, "linked to object", false, false, true} {"link", PropertyType::Object, "linked to object", false, false, true}
}}, }},
{"other object", "", { {"other object", "", {
{"value", PropertyTypeInt} {"value", PropertyType::Int}
}}, }},
{"linking object", "", { {"linking object", "", {
{"link", PropertyTypeObject, "object", false, false, true} {"link", PropertyType::Object, "object", false, false, true}
}}, }},
{"linked to object", "", { {"linked to object", "", {
{"value", PropertyTypeInt} {"value", PropertyType::Int}
}} }}
}); });
@ -426,7 +426,7 @@ TEST_CASE("Async Results error handling") {
config.automatic_change_notifications = false; config.automatic_change_notifications = false;
config.schema = std::make_unique<Schema>(Schema{ config.schema = std::make_unique<Schema>(Schema{
{"object", "", { {"object", "", {
{"value", PropertyTypeInt}, {"value", PropertyType::Int},
}}, }},
}); });

View File

@ -113,8 +113,8 @@ TEST_CASE("Transaction log parsing") {
SECTION("schema change validation") { SECTION("schema change validation") {
config.schema = std::make_unique<Schema>(Schema{ config.schema = std::make_unique<Schema>(Schema{
{"table", "", { {"table", "", {
{"unindexed", PropertyTypeInt}, {"unindexed", PropertyType::Int},
{"indexed", PropertyTypeInt, "", false, true} {"indexed", PropertyType::Int, "", false, true}
}}, }},
}); });
auto r = Realm::get_shared_realm(config); auto r = Realm::get_shared_realm(config);
@ -180,7 +180,7 @@ TEST_CASE("Transaction log parsing") {
SECTION("table change information") { SECTION("table change information") {
config.schema = std::make_unique<Schema>(Schema{ config.schema = std::make_unique<Schema>(Schema{
{"table", "", { {"table", "", {
{"value", PropertyTypeInt} {"value", PropertyType::Int}
}}, }},
}); });
@ -269,10 +269,10 @@ TEST_CASE("Transaction log parsing") {
SECTION("LinkView change information") { SECTION("LinkView change information") {
config.schema = std::make_unique<Schema>(Schema{ config.schema = std::make_unique<Schema>(Schema{
{"origin", "", { {"origin", "", {
{"array", PropertyTypeArray, "target"} {"array", PropertyType::Array, "target"}
}}, }},
{"target", "", { {"target", "", {
{"value", PropertyTypeInt} {"value", PropertyType::Int}
}}, }},
}); });
@ -806,9 +806,9 @@ TEST_CASE("DeepChangeChecker") {
config.schema = std::make_unique<Schema>(Schema{ config.schema = std::make_unique<Schema>(Schema{
{"table", "", { {"table", "", {
{"int", PropertyTypeInt}, {"int", PropertyType::Int},
{"link", PropertyTypeObject, "table", false, false, true}, {"link", PropertyType::Object, "table", false, false, true},
{"array", PropertyTypeArray, "table"} {"array", PropertyType::Array, "table"}
}}, }},
}); });