Jump to content

  • Log In with Google      Sign In   
  • Create Account


Allocation, Initialization & Deallocation of Objects


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Kyall   Members   -  Reputation: 287

Like
0Likes
Like

Posted 09 October 2012 - 03:00 AM

I'm building a reflection system, I've got the parser, I've got the reflection system to turn the parsed data into objects in game, I've got binary reading/writing for serialization ( that can even withstand a few code changes so it's not rigid and doesn't require rebuilding binaries every time the code is changed ) I've got a fair bit of stuff going in it. Now I come to what is probably the most important aspect to me as the programmer who's going to use it ( other important aspects such as the syntax for the language being simple enough for designers/artists)

(Note: For the time being let's think architecture design, leave any DO or DO NOT, allocate in constructor/initialization function as a secondary tip, not the main article because I don't really want to go into that argument here. I'm not even going to say where I side on that. And I don't want such considerations to be an interference in the design of my memory/initialization solution, I just want the best solution either or. So truce? )

What I want to explore is how to allocate the memory for objects, delete them effectively, as well as handle the initialization of the data for the object. There are a few ways of doing this and I'm more than happy to code a couple out to see if they're feasible before I stick with one. The techniques I know of are:



(A): Using specific reflection struct, as input to constructor for object

struct ReflectionData {
};
class ReflectionUser {
ReflectionUser( ReflectionData& );
};

Created with:

template <typename Type, typename Reflect>
void Constructor( void* object, void* reflect ) {
   if( reflect ) {
	  // inline constructor - reflection
	  new (object) Type( *(Reflect*)reflect );
   } else {
	  // inline constructor - no reflection
	  new (object) Type;
   }
}

This would pretty much be using new/delete with the factory handling the delete.

(B): By initializing the data members directly from reflection, no parsing of reflection struct. This has the advantage that the reflection system can be used for live edit without any live-edit specific support for standard types. Types that have custom behaviours, such as entities that have components will need a general purpose function for live edit to be created, but it would be just 1 function for all components.

class MyClass {
   int x, y, z;

  // Call Init from the factory once the object has had it's members loaded to load in textures etc
   void Init();
};

template <typename T>
void Construct( void* object, DataObject&amp; data ) {
   new (object) T;
   for( data ) {
	   // Get the field name
	   // find the field in T
	   // Get the Type of the field
	   // Use the type to construct the object
	   // Paraphased code:
	   Construct< field_type >( object + field_offset, data[field_index] );
   }
}


void MyClass::Init() {
   // Load texture or something.  
}

One thing I can do with this techique to load texture resources is that any object that has resources, owns a texture object, and when the texture object is created via the reflection system, it's constructor adds it to a list of textures that need to be loaded to a resource manager. Simple enough.

That's the two ways I know of doing this, if you have any others I would like to hear them because I don't feel comfortable continuing when I only have 2 options to select from; I find myself ignorant of other possible techniques.

As to Deletion:

Using new/delete means I can use the new keyword in any construction of objects and the deletion of objects is handled by whatever class owns them. This is simple enough. It is possible to delete the objects through the reflection manager, with something such as:


// Definition for a field
struct Field {
   size_t offset;
   bool is_pointer;
};

struct Type {
   Field *fields;
};

template <typename T>
void Destroy( void * object ) {
  // Get the object Type
  Type* t  = get type from database
  for all fields
	 if field is pointer
	   Destroy< field type >( object + field offset )

  free( object )
}

Any one who of any other / or best use strategies to achieve this?


EDIT:

I am a bit slow but I figured out I can use a template as well as template specialization to handle any objects that require a resolve step, such as asset loading, as most objects created are not going to need to load assets and any assets that are loaded will be from a pretty specific set of items, such as:
- Textures
- Audio
So all I need to do is create a templated Resource type

template <typename T>
class Resource {
public:
  Resource( T&amp; data );
  shared_ptr<T> resource;
};

And then partially specialize the reflection system's constructor function:


template <typename T>
void TypeHandler<Resource<T>> (void *object, Data&amp; data ) {
   // Construct type T from Data
   T tobj;
   for ( data as field ) {
        tobj[field] = data[field];
   }

   // Now create the resource from that T obj value
   new (object) Resource<T>( data );  
}

And in the constructor for resource:

template <typename T> Resource<T>::Resource( T&amp; data ) {
   resource = manager.Find( data );
}

As well as setting up some templates for the different resource managers etc, this should pretty much achieve what I'm after as far as designing the 'resolve' step in my project. Will still keep checking to see if anyone has any examples or different methods they've used in the past.

Edited by Kyall, 09 October 2012 - 11:28 PM.

I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS