/*----------------------------------------------------------------------------*/
/*!
Copyright (c) 2018, 2019 by Christian Nowak a.k.a. Creestee a.k.a. chn
Contact: chnowak@web.de or creestee@creestee.com
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. This application may be used and redistributed in source code and binary
form free of charge for personal and for educational purposes.
4. Redistribution and usage of this application in source code or binary form
for commercial purposes requires explicit written permission from the
author.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the RadarVolumeExplorer project.
*/
/*----------------------------------------------------------------------------*/

#include <iostream>

#include "RveHDF5Group.h"
#include "RveHDF5Attribute.h"
#include "RveHDF5Dataset.h"

/*----------------------------------------------------------------------------*/
/*!
   Create a new Group
   \param name name of the new group
   \param parent pointer to the parent object; can be omitted
*/
/*----------------------------------------------------------------------------*/
RveHDF5Group::RveHDF5Group( QString name, RveHDF5Object *parent )
{
   m_Name = name;
   m_pParent = parent;
}


/*----------------------------------------------------------------------------*/
/*!
   Create a new group. The name will be empty
*/
/*----------------------------------------------------------------------------*/
RveHDF5Group::RveHDF5Group()
{
   m_Name = "";
   m_pParent = 0;
}


/*----------------------------------------------------------------------------*/
/*!
   Destructor
*/
/*----------------------------------------------------------------------------*/
RveHDF5Group::~RveHDF5Group()
{
   QMap<QString, RveHDF5Object *>::iterator iter;

   for( iter = m_Objects.begin(); iter != m_Objects.end(); iter++ )
   {
      delete iter.value();
   }

   m_Objects.clear();
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the hdf5 object type
   \return always TYPE_GROUP
*/
/*----------------------------------------------------------------------------*/
RveHDF5Object::OBJ_TYPE RveHDF5Group::type()
{
   return( RveHDF5Object::TYPE_GROUP );
}


/*----------------------------------------------------------------------------*/
/*!
   Add an object to the group as a direct child
   \param obj pointer to the object to be added
   \return true on success
*/
/*----------------------------------------------------------------------------*/
bool RveHDF5Group::add( RveHDF5Object *obj )
{
   return( RveHDF5Object::add( obj ) );
}


bool RveHDF5Group::remove( QString name )
{
   return( RveHDF5Object::remove( name ) );
}

/*----------------------------------------------------------------------------*/
/*!
   Retrieve a direct child object by its name
   \param name name of the object to retrieve
   \return pointer to the retrieved object or 0 on failure
*/
/*----------------------------------------------------------------------------*/
RveHDF5Object *RveHDF5Group::get( QString name ) const
{
   return( RveHDF5Object::get( name ) );
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the names of all direct child objects
   \return vector of the names of all direct children
*/
/*----------------------------------------------------------------------------*/
QStringList RveHDF5Group::names()
{
   return( RveHDF5Object::names() );
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve pointers to all direct child objects
   \return vector of pointers to all child objects
*/
/*----------------------------------------------------------------------------*/
QVector<RveHDF5Object *> RveHDF5Group::objects()
{
   return( RveHDF5Object::objects() );
}


/*----------------------------------------------------------------------------*/
/*!
   Print information about the group
*/
/*----------------------------------------------------------------------------*/
void RveHDF5Group::print()
{
   if( path() != "" )
      std::cout << "(GROUP) " << path().toStdString() << std::endl;

   RveHDF5Object::print();
}


/*----------------------------------------------------------------------------*/
/*!
   Save the group to an hdf5 file
   \param f hdf5 object to which the current group will be added
   \return true on success
*/
/*----------------------------------------------------------------------------*/
bool RveHDF5Group::save( hid_t f )
{
   if( name() != "" )
   {
      hid_t grp = H5Gcreate(f, name().toLatin1(), 32);
      RveHDF5Object::save( grp );
      H5Gclose( grp );
   } else
   {
      RveHDF5Object::save( f );
   }


   return( true );
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the object denoted by its path
   \param path the path where the object shall be retrieved from
   \return pointer to the object
*/
/*----------------------------------------------------------------------------*/
RveHDF5Object *RveHDF5Group::getByPath( QString path ) const
{
   return( RveHDF5Object::getByPath( path ) );
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the object denoted by the first valid path from a list of paths
   \param paths list of paths to check for an object
   \return pointer to the first found object
*/
/*----------------------------------------------------------------------------*/
RveHDF5Object *RveHDF5Group::getByPath( const QStringList &paths ) const
{
   return( RveHDF5Object::getByPath( paths ) );
}
