[MOAB-dev] r1526 - in MOAB/trunk: . test/h5file

kraftche at mcs.anl.gov kraftche at mcs.anl.gov
Mon Jan 14 17:35:35 CST 2008


Author: kraftche
Date: 2008-01-14 17:35:35 -0600 (Mon, 14 Jan 2008)
New Revision: 1526

Modified:
   MOAB/trunk/MBCore.cpp
   MOAB/trunk/SequenceData.cpp
   MOAB/trunk/TagInfo.cpp
   MOAB/trunk/TagInfo.hpp
   MOAB/trunk/TagServer.cpp
   MOAB/trunk/TagServer.hpp
   MOAB/trunk/VarLenTag.hpp
   MOAB/trunk/test/h5file/h5file_test.cpp
Log:
- Add low-level code to have variable-length default and mesh tag values
  
- Fix some issues w/ default and mesh values for bit tags



Modified: MOAB/trunk/MBCore.cpp
===================================================================
--- MOAB/trunk/MBCore.cpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/MBCore.cpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -1336,8 +1336,10 @@
                                     const int num_entities,
                                     void *tag_data) const
 {
-  if (NULL == entity_handles && 0 == num_entities)
-    return tagServer->get_mesh_data(tag_handle, tag_data);
+  if (NULL == entity_handles && 0 == num_entities) {
+    int size;
+    return tagServer->get_mesh_data(tag_handle, tag_data, size);
+  }
 
   else return tagServer->get_data(tag_handle, entity_handles, num_entities, tag_data);
 }
@@ -1532,18 +1534,8 @@
   //! get default value of the tag
 MBErrorCode MBCore::tag_get_default_value(const MBTag tag_handle, void *def_value) const
 {
-  const TagInfo* tag_info = tagServer->get_tag_info( tag_handle );
-  if(!tag_info)
-    return MB_TAG_NOT_FOUND;
-
-  if (NULL == def_value) return MB_FAILURE;
-  
-  if (tag_info->default_value() == NULL)
-    return MB_ENTITY_NOT_FOUND;
-  
-  memcpy(def_value, tag_info->default_value(), tag_info->get_size());
-
-  return MB_SUCCESS;
+  int size;
+  return tagServer->get_default_data( tag_handle, def_value, size );
 }
 
   //! get type of tag (sparse, dense, etc.; 0 = dense, 1 = sparse, 2 = bit, 3 = static)

Modified: MOAB/trunk/SequenceData.cpp
===================================================================
--- MOAB/trunk/SequenceData.cpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/SequenceData.cpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -155,7 +155,7 @@
 {
   assert( num_tag_sizes >= numTagData );
   for (unsigned i = 1; i <= numTagData; ++i)
-    release_tag_data( i, tag_sizes[i] );
+    release_tag_data( i, tag_sizes[i-1] );
 }
 
 void SequenceData::release_tag_data( MBTagId tag_num, int tag_size )

Modified: MOAB/trunk/TagInfo.cpp
===================================================================
--- MOAB/trunk/TagInfo.cpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/TagInfo.cpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -17,8 +17,6 @@
 {
   mTagName.clear();
   isValid = false;
-  delete [] mDefaultValue;
-  mDefaultValue = 0;
-  delete [] mMeshValue;
-  mMeshValue = 0;
+  mDefaultValue.clear();
+  mMeshValue.clear();
 }

Modified: MOAB/trunk/TagInfo.hpp
===================================================================
--- MOAB/trunk/TagInfo.hpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/TagInfo.hpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -2,9 +2,11 @@
 #define TAG_INFO_HPP
 
 #include "MBTypes.h"
+#include "VarLenTag.hpp"
 
 #include <string>
 #include <string.h>
+#include <assert.h>
 
 // ! stores information about a tag
 class TagInfo
@@ -15,22 +17,15 @@
   TagInfo() : mTagName(""), 
               mDataSize(0), 
               isValid(false),
-              mDefaultValue(NULL), 
-              mMeshValue(NULL),
               dataType(MB_TYPE_OPAQUE)
               {}
-  
-  //! destructor
-  inline ~TagInfo();
-  
-  //! copy constructor
-  inline TagInfo(const TagInfo&);
 
   //! constructor that takes all parameters
-  inline TagInfo(const char *, int, MBDataType type, const void *);
-
-  //! assignment operator
-  inline TagInfo &operator=(const TagInfo &rhs);
+  inline TagInfo( const char * name, 
+                  int size, 
+                  MBDataType type, 
+                  const void * default_value,
+                  int default_value_size);
   
   //! set the name of the tag
   void set_name( const std::string& name) { mTagName = name; }
@@ -44,17 +39,25 @@
   //! get the size of the data
   int get_size() const { return mDataSize; }
 
+    //! get length of default value
+  int default_value_size() const { return mDefaultValue.size(); }
+
     //! get the default data
-  const void *default_value() const  { return mDefaultValue;}
+  const void *default_value() const  
+    { return mDefaultValue.size() ? mDefaultValue.data() : 0;}
   
     //! set mesh value
-  void set_mesh_value( const void* data );
+  void set_mesh_value( const void* data, int size );
   
     //! get mesh value
-  const void* get_mesh_value() const { return mMeshValue; }
+  int get_mesh_value_size() const { return mMeshValue.size(); }
   
+    //! get mesh value
+  const void* get_mesh_value() const 
+    { return mMeshValue.size() ? mMeshValue.data() : 0; }
+  
     //! remove mesh value
-  void remove_mesh_value();
+  void remove_mesh_value() { mMeshValue.clear(); }
   
   inline MBDataType get_data_type() const     { return dataType; }
   
@@ -79,96 +82,38 @@
   bool isValid;
 
   //! stores the default data, if any
-  unsigned char *mDefaultValue;
+  VarLenTag mDefaultValue;
   
   //! store the mesh value, if any
-  unsigned char *mMeshValue;
+  VarLenTag mMeshValue;
   
   //! type of tag data
   MBDataType dataType;
 
 };
 
-
-inline TagInfo::TagInfo(const TagInfo& copy)
-  : mTagName( copy.mTagName ),
-    mDataSize( copy.mDataSize ),
-    isValid( copy.isValid ),
-    mDefaultValue( 0 ),
-    mMeshValue( 0 ),
-    dataType( copy.dataType )
-{
-  if (copy.mDefaultValue) {
-    mDefaultValue = new unsigned char[mDataSize];
-    memcpy(mDefaultValue, copy.mDefaultValue, mDataSize);
-  }
-  
-  if (copy.mMeshValue) {
-    mMeshValue = new unsigned char[mDataSize];
-    memcpy(mMeshValue, copy.mMeshValue, mDataSize);
-  }
-}
-
 inline TagInfo::TagInfo( const char* name, 
                          int size, 
                          MBDataType type,
-                         const void* default_value)
+                         const void* default_value,
+                         int default_value_size)
  : mTagName( name ),
    mDataSize( size ),
    isValid( true ),
-   mDefaultValue( 0 ),
-   mMeshValue( 0 ),
+   mDefaultValue( default_value_size, default_value ),
    dataType( type )
 {
-  if (default_value) {
-    mDefaultValue = new unsigned char[size];
-    memcpy(mDefaultValue, default_value, size);
-  }
+    // if tag is not variable-length and default_value_size is not zero,
+    // then size and default_value_size must be the same.
+  assert( size == MB_VARIABLE_LENGTH || default_value_size == 0 || default_value_size == size );
 }
 
-inline TagInfo &TagInfo::operator=(const TagInfo &rhs)
-{
-  mTagName = rhs.mTagName;
-  mDataSize = rhs.mDataSize;
-  isValid = rhs.isValid;
-  
-  delete [] mDefaultValue;
-  delete [] mMeshValue;
-  mDefaultValue = 0;
-  mMeshValue = 0;
-  
-  if (rhs.mDefaultValue) {
-    mDefaultValue = new unsigned char[mDataSize];
-    memcpy( mDefaultValue, rhs.mDefaultValue, mDataSize );
-  }
-  
-  if (rhs.mMeshValue) {
-    mMeshValue = new unsigned char[mDataSize];
-    memcpy( mMeshValue, rhs.mMeshValue, mDataSize );
-  }
-  
-  return *this;
-}
 
-inline TagInfo::~TagInfo() 
+inline void TagInfo::set_mesh_value( const void* data, int size )
 {
-  // clean up default value
-  delete [] mDefaultValue;
-  delete [] mMeshValue;
+    // if tag is not variable-length, then size must be tag size
+  assert( get_size() == MB_VARIABLE_LENGTH || get_size() == size );
+  mMeshValue.set( data, size );
 }
 
-
-inline void TagInfo::set_mesh_value( const void* data )
-{
-  if (!mMeshValue)
-    mMeshValue = new unsigned char[mDataSize];
-  memcpy( mMeshValue, data, mDataSize );
-}
-
-inline void TagInfo::remove_mesh_value()
-{ 
-  delete [] mMeshValue;
-  mMeshValue = 0;
-}
-
 #endif

Modified: MOAB/trunk/TagServer.cpp
===================================================================
--- MOAB/trunk/TagServer.cpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/TagServer.cpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -82,8 +82,20 @@
                                 const MBTagType storage,
                                 const MBDataType data_type,
                                 MBTag &tag_handle,
-                                const void *default_value)
+                                const void *default_value,
+                                int default_value_size)
 {
+  if (!default_value) {
+    default_value_size = 0;
+  }
+  else if (storage == MB_TAG_BIT) {
+    if (data_size == MB_VARIABLE_LENGTH)
+      return MB_INVALID_SIZE;
+    default_value_size = (data_size+7)/8; // convert from bits to bytes
+  }
+  else if (data_size != MB_VARIABLE_LENGTH) 
+    default_value_size = data_size;
+
     // Check if name is already in use
     // if so, pass back the existing tag handle.
     // NOTE: If name is NULL or empty (tag is unnamed),
@@ -111,9 +123,9 @@
 
     // add TagInfo entry for new tag
   if (i == list.end()) 
-    i = list.insert( i, TagInfo( tag_name, data_size, data_type, default_value ) );
+    i = list.insert( i, TagInfo( tag_name, data_size, data_type, default_value, default_value_size ) );
   else
-    *i = TagInfo( tag_name, data_size, data_type, default_value );
+    *i = TagInfo( tag_name, data_size, data_type, default_value, default_value_size );
 
 
   MBTagId tag_id = i - list.begin() + 1;
@@ -214,13 +226,23 @@
 }
 
 MBErrorCode TagServer::set_mesh_data( const MBTag tag_handle,
-                                      const void* data )
+                                      const void* data,
+                                      int size )
 {
   TagInfo* info = get_tag_info( tag_handle );
   if (!info)
     return MB_TAG_NOT_FOUND;
   
-  info->set_mesh_value( data );
+  if (info->get_size() == MB_VARIABLE_LENGTH) {
+    if (!size)
+      return MB_INVALID_SIZE;
+  }
+  else if (PROP_FROM_TAG_HANDLE(tag_handle) == MB_TAG_BIT)
+    size = (size+7)/8; // convert from bits to bytes
+  else
+    size = info->get_size();
+  
+  info->set_mesh_value( data, size );
   return MB_SUCCESS;
 }
 
@@ -362,13 +384,19 @@
 
 
 MBErrorCode TagServer::get_mesh_data( const MBTag tag_handle,
-                                      void* data ) const
+                                      void* data,
+                                      int& size ) const
 {
   const TagInfo* info = get_tag_info( tag_handle );
-  if (!info || !info->get_mesh_value())
+  if (!info || !info->get_mesh_value_size())
     return MB_TAG_NOT_FOUND;
   
-  memcpy( data, info->get_mesh_value(), info->get_size() );
+  if (PROP_FROM_TAG_HANDLE(tag_handle) == MB_TAG_BIT)
+    size = info->get_size();
+  else 
+    size = info->get_mesh_value_size();
+  if (data) 
+    memcpy( data, info->get_mesh_value(), info->get_mesh_value_size() );
   return MB_SUCCESS;
 }
 
@@ -412,10 +440,11 @@
   // try to get a default value
   if(result == MB_TAG_NOT_FOUND)
   {
-    result = get_default_data(tag_handle, data);
+    int size;
+    result = get_default_data(tag_handle, data, size);
       // if failure is returned, change it back to tag not found, since it's
       // ok to look for data and not find any
-    if (result == MB_FAILURE) result = MB_TAG_NOT_FOUND;
+    if (result == MB_ENTITY_NOT_FOUND) result = MB_TAG_NOT_FOUND;
   }
 
   return result;
@@ -467,10 +496,11 @@
       result = mSparseData->get_data(tag_id, *iter, mydata);
       if(result == MB_TAG_NOT_FOUND)
       {
-        result = get_default_data(tag_handle, mydata);
+        int size;
+        result = get_default_data(tag_handle, mydata, size);
           // if failure is returned, change it back to tag not found, since it's
           // ok to look for data and not find any
-        if (result == MB_FAILURE) result = MB_TAG_NOT_FOUND;
+        if (result == MB_ENTITY_NOT_FOUND) result = MB_TAG_NOT_FOUND;
       }
       if(result != MB_SUCCESS)
         return result;
@@ -520,10 +550,11 @@
       result = mSparseData->get_data(tag_id, *iter, mydata);
       if(result == MB_TAG_NOT_FOUND)
       {
-        result = get_default_data(tag_handle, mydata);
+        int size;
+        result = get_default_data(tag_handle, mydata, size);
           // if failure is returned, change it back to tag not found, since it's
           // ok to look for data and not find any
-        if (result == MB_FAILURE) result = MB_TAG_NOT_FOUND;
+        if (result == MB_ENTITY_NOT_FOUND) result = MB_TAG_NOT_FOUND;
       }
       if(result != MB_SUCCESS)
         return result;
@@ -594,51 +625,53 @@
   return MB_SUCCESS;
 }
 
-MBErrorCode TagServer::get_default_data_ref(const MBTag tag_handle, const void *& data) 
+MBErrorCode TagServer::get_default_data_ref( const MBTag tag_handle, 
+                                             const void *& data,
+                                             int& size) 
 {
 
   const TagInfo* tag_info = get_tag_info(tag_handle);
   if(!tag_info)
     return MB_TAG_NOT_FOUND;
 
-  // get the default value
-  const void *def_data = tag_info->default_value();
-  // if we have a default value, copy it
-  if(def_data != NULL)
-  {
-    data = def_data; 
+  size = tag_info->default_value_size();
+  if (size) {
+    data = tag_info->default_value();
+    if (PROP_FROM_TAG_HANDLE(tag_handle) == MB_TAG_BIT)
+      size = tag_info->get_size(); // get number of bits rather than bytes
     return MB_SUCCESS;
   }
-
-  return MB_FAILURE;
-
+  else {
+    data = 0;
+    return MB_ENTITY_NOT_FOUND;
+  }
 }
 
 
-MBErrorCode TagServer::get_default_data(const MBTag tag_handle, void *data) 
+MBErrorCode TagServer::get_default_data( const MBTag tag_handle, 
+                                         void *data,
+                                         int& size) 
 {
 
   const TagInfo* tag_info = get_tag_info(tag_handle);
   if(!tag_info)
     return MB_TAG_NOT_FOUND;
 
-  // get the default value
-  const void *def_data = tag_info->default_value();
-  // if we have a default value, copy it
-  if(def_data != NULL)
-  {
-    memcpy(data, def_data, tag_info->get_size());
-    return MB_SUCCESS;
-  }
-  return MB_FAILURE;
-
+  size = tag_info->default_value_size();
+  if (!size)
+    return MB_ENTITY_NOT_FOUND;
+  if (data)
+    memcpy( data, tag_info->default_value(), size );
+  if (PROP_FROM_TAG_HANDLE(tag_handle) == MB_TAG_BIT)
+    size = tag_info->get_size(); // get bits rather than bytes
+  return MB_SUCCESS;
 }
 
 
 MBErrorCode TagServer::remove_mesh_data( const MBTag tag_handle )
 {
   TagInfo* info = get_tag_info( tag_handle );
-  if (!info || !info->get_mesh_value())
+  if (!info || !info->get_mesh_value_size())
     return MB_TAG_NOT_FOUND;
   info->remove_mesh_value();
   return MB_SUCCESS;
@@ -932,7 +965,8 @@
 
 unsigned long TagServer::get_memory_use( MBTag tag_handle ) const
 {
-  if (!get_tag_info(tag_handle))
+  const TagInfo* tag_info = get_tag_info(tag_handle);
+  if (!tag_info)
     return 0;
 
   unsigned long result = 0, tmp;
@@ -952,7 +986,11 @@
   }
 
     // add in size of entry in mTagTable
-  return result + sizeof(MBTag) + sizeof(TagInfo) + 3*sizeof(void*);
+  result += sizeof(MBTag) + sizeof(TagInfo) + 3*sizeof(void*);
+  result += tag_info->default_value_size();
+  result += tag_info->get_mesh_value_size();
+  result += tag_info->get_name().size();
+  return result;
 }
 
 MBErrorCode TagServer::get_memory_use( MBTag tag_handle,
@@ -980,10 +1018,8 @@
   
     // size of entry in mTagTable map
   total += sizeof(MBTag) + sizeof(TagInfo) + 3*sizeof(void*);
-  if (tag_info->default_value())
-    total += tag_info->get_size();
-  if (tag_info->get_mesh_value())
-    total += tag_info->get_size();
+  total += tag_info->default_value_size();
+  total += tag_info->get_mesh_value_size();
   total += tag_info->get_name().size();
   
   return MB_SUCCESS;

Modified: MOAB/trunk/TagServer.hpp
===================================================================
--- MOAB/trunk/TagServer.hpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/TagServer.hpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -62,7 +62,8 @@
                        const MBTagType storage,
                        const MBDataType data_type,
                        MBTag &tag_handle,
-                       const void *default_data = NULL);
+                       const void *default_data = NULL,
+                       int default_value_size = 0);
 
   // tag name is the name of the tag
   // entity_type is the type of entity (in implementation - it means
@@ -82,7 +83,7 @@
   MBErrorCode reset_all_data();
 
   //! set global/mesh value of tag
-  MBErrorCode set_mesh_data( const MBTag tag_handle, const void* data );
+  MBErrorCode set_mesh_data( const MBTag tag_handle, const void* data, int size = 0);
 
   //! set the value of a tag
   MBErrorCode set_data(const MBTag tag_handle, const MBEntityHandle entity_handle, const void* data );
@@ -92,7 +93,7 @@
   MBErrorCode set_data(const MBTag tag_handle, const MBRange& entity_handles, const void* data );
 
   //! get global/mesh value of tag
-  MBErrorCode get_mesh_data( const MBTag tag_handle, void* data ) const;
+  MBErrorCode get_mesh_data( const MBTag tag_handle, void* data, int& size ) const;
 
   //! get the value of a tag
   MBErrorCode get_data(const MBTag tag_handle, const MBEntityHandle entity_handle, void* data );
@@ -176,10 +177,10 @@
   MBErrorCode get_tags(std::vector<MBTag> &all_tags);
   
     //! get the default value for a given tag
-  MBErrorCode get_default_data(const MBTag tag_handle, void *data);
+  MBErrorCode get_default_data(const MBTag tag_handle, void *data, int& size);
 
     //! get the default value for a given tag
-  MBErrorCode get_default_data_ref(const MBTag tag_handle, const void *& data);
+  MBErrorCode get_default_data_ref(const MBTag tag_handle, const void *& data, int& size);
 
   //! get information about a tag
   const TagInfo* get_tag_info(const char *tag_name ) const;

Modified: MOAB/trunk/VarLenTag.hpp
===================================================================
--- MOAB/trunk/VarLenTag.hpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/VarLenTag.hpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -129,7 +129,7 @@
   inline VarLenTag( unsigned size );
   inline ~VarLenTag() { clear(); }
   inline VarLenTag( const VarLenTag& copy );
-  inline VarLenTag( unsigned size, void* data );
+  inline VarLenTag( unsigned size, const void* data );
   
   inline unsigned size() const { return mData.mPointer.size; }
 
@@ -149,6 +149,9 @@
   
   inline void set( const void* data, unsigned size )
     { memcpy( resize(size), data, size ); }
+    
+  inline VarLenTag& operator=( const VarLenTag& other )
+    { set( other.data(), other.size() ); return *this; }
   
 };
   
@@ -207,11 +210,11 @@
 #endif
   {
     mData.mPointer.array = reinterpret_cast<unsigned char*>(malloc(size()));
-    memcpy( copy.mData.mPointer.array, mData.mPointer.array, size() );
+    memcpy( mData.mPointer.array, copy.mData.mPointer.array, size() );
   }
 }
 
-inline VarLenTag::VarLenTag( unsigned size, void* data )
+inline VarLenTag::VarLenTag( unsigned size, const void* data )
 {
   mData.mPointer.size = 0;
   if (size) 

Modified: MOAB/trunk/test/h5file/h5file_test.cpp
===================================================================
--- MOAB/trunk/test/h5file/h5file_test.cpp	2008-01-14 21:46:39 UTC (rev 1525)
+++ MOAB/trunk/test/h5file/h5file_test.cpp	2008-01-14 23:35:35 UTC (rev 1526)
@@ -759,7 +759,7 @@
   if (iface->type_from_handle(hdata[0]) != MBHEX ||
       hdata[2] != 0 ||
       iface->type_from_handle(hdata[1]) != MBPOLYHEDRON) {
-    fprintf( stderr, "incorrect default value for handle tag.\n");
+    fprintf( stderr, "incorrect default value for handle tag '%s'\n",handlename);
     return false;
   }
   




More information about the moab-dev mailing list