[glom] Field: Avoid some copy/pasted code.



commit ee0ae539f3c4ae4d938f28087dc0ac9d56dd2031
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Nov 19 10:25:45 2013 +0100

    Field: Avoid some copy/pasted code.

 glom/libglom/data_structure/field.cc |   47 +++++++++++++---------------------
 glom/libglom/data_structure/field.h  |    2 +
 2 files changed, 20 insertions(+), 29 deletions(-)
---
diff --git a/glom/libglom/data_structure/field.cc b/glom/libglom/data_structure/field.cc
index b6722a0..58003c1 100644
--- a/glom/libglom/data_structure/field.cc
+++ b/glom/libglom/data_structure/field.cc
@@ -176,23 +176,11 @@ void Field::set_field_info(const Glib::RefPtr<Gnome::Gda::Column>& fieldinfo)
   if(cur_type == G_TYPE_NONE)
     set_glom_type( get_glom_type_for_gda_type(fieldinfo->get_g_type()) );
 
-  Gnome::Gda::Value value = get_default_value();
+  const Gnome::Gda::Value value = get_default_value();
   if(!value.is_null())
   {
     // Check that the default value is consistent with the field's type
-    // TODO: Basically copied from set_default_value(). Maybe this check should
-    // be moved into an extra function.
-    GType cur_type = get_gda_type_for_glom_type(get_glom_type());
-    const FieldTypes* pFieldTypes = get_field_types();
-
-    // Take into account that value might be one of the fallback types
-    if(pFieldTypes)
-    {
-      while(cur_type != value.get_value_type() && cur_type != G_TYPE_NONE)
-        cur_type = pFieldTypes->get_fallback_type_for_gdavaluetype(cur_type);
-    }
-
-    if(!value.is_null() && value.get_value_type() != cur_type)
+    if(!value.is_null() && value.get_value_type() != get_gda_data_type_with_fallback(value))
     {
       std::cerr << G_STRFUNC << ": New field's default value type (" << g_type_name(value.get_value_type()) 
<< " does not match field type (" << g_type_name(get_gda_type_for_glom_type(get_glom_type())) << "). 
Resetting default value." << std::endl;
       m_field_info->set_default_value(Gnome::Gda::Value());
@@ -200,6 +188,21 @@ void Field::set_field_info(const Glib::RefPtr<Gnome::Gda::Column>& fieldinfo)
   }
 }
 
+GType Field::get_gda_data_type_with_fallback(const Gnome::Gda::Value& value)
+{
+  GType cur_type = get_gda_type_for_glom_type(get_glom_type());
+  const FieldTypes* pFieldTypes = get_field_types();
+
+  // Take into account that value might be one of the fallback types
+  if(pFieldTypes)
+  {
+    while(cur_type != value.get_value_type() && cur_type != G_TYPE_NONE)
+      cur_type = pFieldTypes->get_fallback_type_for_gdavaluetype(cur_type);
+  }
+
+  return cur_type;
+}
+
 sharedptr<Relationship> Field::get_lookup_relationship() const
 {
   return m_lookup_relationship;
@@ -523,21 +526,7 @@ void Field::set_default_value(const Gnome::Gda::Value& value)
   // TODO: Allow setting a NULL default value when glom type is invalid?
 
   // Verify that the value matches the type of the field.
-  GType cur_type = get_gda_type_for_glom_type(get_glom_type());
-  const FieldTypes* pFieldTypes = 0;
-
-  ConnectionPool* pConnectionPool = ConnectionPool::get_instance();
-  if(pConnectionPool)
-    pFieldTypes = pConnectionPool->get_field_types();
-
-  // Take into account that value might be one of the fallback types
-  if(pFieldTypes)
-  {
-    while(cur_type != value.get_value_type() && cur_type != G_TYPE_NONE)
-      cur_type = pFieldTypes->get_fallback_type_for_gdavaluetype(cur_type);
-  }
-
-  if(value.is_null() || value.get_value_type() == cur_type)
+  if(value.is_null() || value.get_value_type() == get_gda_data_type_with_fallback(value))
     m_field_info->set_default_value(value);
   else
     std::cerr << G_STRFUNC << ": Cannot set incompatible default value: Default value has type " << 
g_type_name(value.get_value_type()) << ", but field has type " << 
g_type_name(get_gda_type_for_glom_type(get_glom_type())) << std::endl;
diff --git a/glom/libglom/data_structure/field.h b/glom/libglom/data_structure/field.h
index e734a2c..12bc603 100644
--- a/glom/libglom/data_structure/field.h
+++ b/glom/libglom/data_structure/field.h
@@ -232,6 +232,8 @@ private:
 
   static void init_map();
 
+  GType get_gda_data_type_with_fallback(const Gnome::Gda::Value& value);
+
   //The glom type to be used for the gda type:
   typedef std::map<GType, glom_field_type> type_map_gda_type_to_glom_type;
   static type_map_gda_type_to_glom_type m_map_gda_type_to_glom_type;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]