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) {
case PropertyTypeBool:
case PropertyType::Bool:
m_row.set_bool(column, Accessor::to_bool(ctx, value));
break;
case PropertyTypeInt:
case PropertyType::Int:
m_row.set_int(column, Accessor::to_long(ctx, value));
break;
case PropertyTypeFloat:
case PropertyType::Float:
m_row.set_float(column, Accessor::to_float(ctx, value));
break;
case PropertyTypeDouble:
case PropertyType::Double:
m_row.set_double(column, Accessor::to_double(ctx, value));
break;
case PropertyTypeString:
case PropertyType::String:
m_row.set_string(column, Accessor::to_string(ctx, value));
break;
case PropertyTypeData:
case PropertyType::Data:
m_row.set_binary(column, BinaryData(Accessor::to_binary(ctx, value)));
break;
case PropertyTypeAny:
case PropertyType::Any:
m_row.set_mixed(column, Accessor::to_mixed(ctx, value));
break;
case PropertyTypeDate:
case PropertyType::Date:
m_row.set_timestamp(column, Accessor::to_timestamp(ctx, value));
break;
case PropertyTypeObject: {
case PropertyType::Object: {
if (Accessor::is_null(ctx, value)) {
m_row.nullify_link(column);
}
@ -205,7 +205,7 @@ namespace realm {
}
break;
}
case PropertyTypeArray: {
case PropertyType::Array: {
realm::LinkViewRef link_view = m_row.get_linklist(column);
link_view->clear();
if (!Accessor::is_null(ctx, value)) {
@ -231,23 +231,23 @@ namespace realm {
}
switch (property.type) {
case PropertyTypeBool:
case PropertyType::Bool:
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));
case PropertyTypeFloat:
case PropertyType::Float:
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));
case PropertyTypeString:
case PropertyType::String:
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));
case PropertyTypeAny:
case PropertyType::Any:
throw "Any not supported";
case PropertyTypeDate:
case PropertyType::Date:
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);
TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), linkObjectSchema->name);
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)))));
}
case PropertyTypeArray: {
case PropertyType::Array: {
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)))));
}
@ -281,7 +281,7 @@ namespace realm {
if (primary_prop) {
// search for existing object based on primary key type
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));
}
else {
@ -312,7 +312,7 @@ namespace realm {
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);
}
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);
}
else {

View File

@ -27,7 +27,7 @@
using namespace realm;
#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")
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.is_indexed = table->has_search_index(col);
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;
if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) {
if (property.type == PropertyType::Object || property.type == PropertyType::Array) {
// set link type for objects and arrays
ConstTableRef linkTable = table->get_link_target(col);
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) {
switch (destination.type) {
case PropertyTypeInt:
case PropertyType::Int:
copy_property_values(source, destination, table, &Table::get_int, &Table::set_int);
break;
case PropertyTypeBool:
case PropertyType::Bool:
copy_property_values(source, destination, table, &Table::get_bool, &Table::set_bool);
break;
case PropertyTypeFloat:
case PropertyType::Float:
copy_property_values(source, destination, table, &Table::get_float, &Table::set_float);
break;
case PropertyTypeDouble:
case PropertyType::Double:
copy_property_values(source, destination, table, &Table::get_double, &Table::set_double);
break;
case PropertyTypeString:
case PropertyType::String:
copy_property_values(source, destination, table, &Table::get_string, &Table::set_string);
break;
case PropertyTypeData:
case PropertyType::Data:
copy_property_values(source, destination, table, &Table::get_binary, &Table::set_binary);
break;
case PropertyTypeDate:
case PropertyType::Date:
copy_property_values(source, destination, table, &Table::get_timestamp, &Table::set_timestamp);
break;
default:
@ -319,8 +319,8 @@ void ObjectStore::create_tables(Group *group, Schema &target_schema, bool update
if (!current_prop || current_prop->table_column == npos) {
switch (target_prop.type) {
// for objects and arrays, we have to specify target table
case PropertyTypeObject:
case PropertyTypeArray: {
case PropertyType::Object:
case PropertyType::Array: {
TableRef link_table = ObjectStore::table_for_object_type(group, target_prop.object_type);
REALM_ASSERT(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) :
ObjectSchemaPropertyException(object_type, property)
{
if (property.type == PropertyTypeObject) {
if (property.type == PropertyType::Object) {
m_what = "'Object' property '" + property.name + "' must be nullable.";
}
else {

View File

@ -94,7 +94,7 @@ struct PropertyExpression
KeyPath key_path = key_path_from_string(key_path_string);
for (size_t index = 0; index < key_path.size(); index++) {
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 + "'");
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;
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),
value_of_type_for_query<bool>(expr.table_getter, rhs, args));
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),
value_of_type_for_query<Timestamp>(expr.table_getter, rhs, args));
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),
value_of_type_for_query<Double>(expr.table_getter, rhs, args));
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),
value_of_type_for_query<Float>(expr.table_getter, rhs, args));
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),
value_of_type_for_query<Int>(expr.table_getter, rhs, args));
break;
case PropertyTypeString:
case PropertyType::String:
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));
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),
value_of_type_for_query<Binary>(expr.table_getter, rhs, args));
break;
case PropertyTypeObject:
case PropertyTypeArray:
case PropertyType::Object:
case PropertyType::Array:
add_link_constraint_to_query(query, cmp.op, expr, link_argument(lhs, rhs, args));
break;
default: {

View File

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

View File

@ -74,11 +74,11 @@ void Schema::validate() const
// check nullablity
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));
}
}
else if (prop.type == PropertyTypeObject) {
else if (prop.type == PropertyType::Object) {
exceptions.emplace_back(InvalidNullabilityException(object.name, prop));
}

View File

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

View File

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

View File

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