On 4 September 2013 15:25, Cedric BAIL <[email protected]> wrote:
> On Wed, Sep 4, 2013 at 5:24 AM, Christophe Sadoine <[email protected]> wrote:
>> On 3 September 2013 15:26, Cedric BAIL <[email protected]> wrote:
>>> On Tue, Sep 3, 2013 at 7:04 AM, Christophe Sadoine <[email protected]> 
>>> wrote:
>>>> I have some questions concerning eet :)
>>>>
>>>> 1) I have a struct like this:
>>>>
>>>> struct Vec3{
>>>> double x;
>>>> double y;
>>>> double ;
>>>> }
>>>>
>>>> struct Object {
>>>> Vec3 position;
>>>> Vec3 rotation;
>>>> }
>>>>
>>>> I want to serialize Object.
>>>>
>>>> I have an eet descriptor for Vec3 that I want to use but
>>>> EET_DATA_DESCRIPTOR_ADD_SUB takes a pointer, right?
>>>> I want to avoid using EET_DATA_DESCRIPTOR_ADD_BASIC with position.x,
>>>> position.y, position.z and use the eet descriptor for vec3.
>>>> Is it possible to handle this use case?
>>>
>>> Nop, use macro. I agree it would be nice to have such a feature.
>>
>> Then is it ok to introduce something like:
>> EET_DATA_DESCRIPTOR_ADD_SUB_NESTED and EET_G_UNKNOWN_NESTED?
>
> Could be indeed ! A patch would be welcome :-)

Here it is.
This patch is only for this feature.
There is also an example.
I don't know eet so I may have missed something.

I added EET_G_UNKNOWN_NESTED.
I considered just adding a bool in Eet_Data_Element to say nested or
pointed... but then I would have to change/make a new
eet_data_descriptor_element_add function...

-- 
http://indefini.org

Attachment: 0001-Added-EET_DATA_DESCRIPTOR_ADD_SUB_NESTED.patch
Description: Binary data

//Compile with:
// gcc -o eet-nested eet-nested.c `pkg-config --cflags --libs eet eina`

#include <Eina.h>
#include <Eet.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

typedef struct _Vec3 Vec3;

struct _Vec3
{
  double x;
  double y;
  double z;
};

typedef struct
{
  Vec3 position;
  Vec3 *rotation;
} Object;

static const char OBJECT_ENTRY[] = "object";

static Eet_Data_Descriptor *_vec3_descriptor;
static Eet_Data_Descriptor *_object_descriptor;

static void
_vec3_descriptor_init(void)
{
   Eet_Data_Descriptor_Class eddc;

   EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, Vec3);
   _vec3_descriptor = eet_data_descriptor_stream_new(&eddc);

#define ADD_BASIC(member, eet_type) \
  EET_DATA_DESCRIPTOR_ADD_BASIC             \
    (_vec3_descriptor, Vec3, # member, member, eet_type)

   ADD_BASIC(x, EET_T_DOUBLE);
   ADD_BASIC(y, EET_T_DOUBLE);
   ADD_BASIC(z, EET_T_DOUBLE);

#undef ADD_BASIC
}

static Vec3 *
_vec3_new(void)
{
   Vec3 *v = calloc(1, sizeof *v);
   v->x = 4;
   v->y = 5;
   v->z = 6;

   return v;
} 

/////////////////////////////////////////////

static void
_object_descriptor_init(void)
{
  Eet_Data_Descriptor_Class eddc;

  EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, Object);
  _object_descriptor = eet_data_descriptor_stream_new(&eddc);

  EET_DATA_DESCRIPTOR_ADD_SUB(_object_descriptor, Object, "rotation", rotation, _vec3_descriptor);
  EET_DATA_DESCRIPTOR_ADD_SUB_NESTED(_object_descriptor, Object, "position", position, _vec3_descriptor);

}

static Object*
_object_new()
{
  Object* o = calloc(1, sizeof *o);

  o->rotation = _vec3_new();

  o->position.x = 1;
  o->position.y = 2;
  o->position.z = 3;

  return o;
}

void output(void *data, const char *string)
{
  printf("%s", string);
}


static Object *
_object_load(const char *filename)
{
   Object *o;
   Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
   if (!ef)
    {
     fprintf(stderr, "Error: could not open '%s' for read.\n", filename);
     return NULL;
    }
   else {
     printf("dump start\n");
     eet_data_dump(ef, OBJECT_ENTRY, output, NULL);
     printf("dump \n");
   }

   o = eet_data_read(ef, _object_descriptor, OBJECT_ENTRY);

   eet_close(ef);
   return o;
} 

static Eina_Bool
_object_save(
      const Object *o,
      const char *filename)
{
  Eet_File *ef;
  Eina_Bool ret;

  ef = eet_open(filename, EET_FILE_MODE_WRITE);
  if (!ef)
   {
    fprintf(stderr, "ERROR: could not open '%s' for write\n", filename);
    return EINA_FALSE;
   }

  ret = eet_data_write(ef, _object_descriptor, OBJECT_ENTRY, o, EINA_TRUE);
  eet_close(ef);

  return ret;
} 

int
main(
      int   argc,
      char *argv[])
{
  Object* o;
  int ret = 0;

  if (argc != 3)
   {
    fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
    return -1;
   }

  eina_init();
  eet_init();
  _vec3_descriptor_init();
  _object_descriptor_init();

  o = _object_load(argv[1]);
  if (!o)
   {
    printf("Creating new object.\n");
    o = _object_new();
    if (!o)
     {
      ret = -2;
      goto end;
     }
   }

  printf("position : %f, %f, %f \nrotation: %f, %f, %f\n", 
        o->position.x,
        o->position.y,
        o->position.z,
        o->rotation->x,
        o->rotation->y,
        o->rotation->z);

   if (!_object_save(o, argv[2]))
   ret = -3;

   free(o->rotation);
   free(o);

end:
   eet_data_descriptor_free(_vec3_descriptor);
   eet_data_descriptor_free(_object_descriptor);
   eet_shutdown();
   eina_shutdown();

   return ret;
} /* main */

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58041391&iu=/4140/ostg.clktrk
_______________________________________________
enlightenment-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to