It is currently Sat May 27, 2017 8:43 am

All times are UTC - 5 hours




 Page 1 of 1 [ 1 post ] 
Author Message
 Post subject: A useful Utility class that I implemented.
PostPosted: Wed Oct 07, 2015 10:27 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
You can use this class for your own purposes. It is a light weight class that simply creates a smart pointer of any class type.

Allocator.h
#ifndef ALLOCATOR_H
#define ALLOCATOR_H

#include <memory>

class Allocator {
public:      
   template<class Type>
   inline static void allocate( std::shared_ptr<Type>& pShared, const Type& type = Type() );
   
   template<class Type>
   inline static void allocate( std::unique_ptr<Type>& pUnique, const Type& type = Type() );

private:
   Allocator();
   Allocator( const Allocator& c ); // Not Implemented
   Allocator& operator=( const Allocator& c ); // Not Implemented

}; // Allocator

#include "Allocator.inl"

#endif // ALLOCATOR_H


Allocator.inl
template<class Type>
inline void Allocator::allocate( std::shared_ptr<Type>& pShared, const Type& type ) {   
   pShared.reset( new Type( type ) );   
} // allocate( shared_ptr )

template<class Type>
inline void Allocator::allocate( std::unique_ptr<Type>& pUnique, const Type& type ) {
   pUnique.reset( new Type( type ) );
} // allocate( unique_ptr )


Allocator.cpp
#include "Allocator.h"


The only stipulation is that if you use this to create a smart pointer from an object of a class type, I think this will require that class to have a copy constructor.

To see this in use here is a working code snippet.

Main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <conio.h>

#include "Allocator.h"

class A {
private:
   int m_a;
public:
   explicit A( int a = 0 ) : m_a( a ) {}
   A( const A& a ) { this->m_a = a.m_a; }

   int   getA() const { return m_a; }
   void  setA( int a ) { m_a = a; }
};

class B {
private:
   float m_b;
public:
   explicit B( float b = 0.0f ) : m_b( b ) {}
   B( const B& b ) { this->m_b = b.m_b; }

   float getB() const { return m_b; }
   void  setB( float b ) { m_b = b; }   
};

int main(void) {

   std::shared_ptr<A> pSharedA;
   std::unique_ptr<A> pUniqueA;

   Allocator::allocate( pSharedA  );
   Allocator::allocate( pUniqueA );      

   std::cout << "Shared: " << pSharedA->getA() << std::endl;
   std::cout << "Unique: " << pUniqueA->getA() << std::endl;   

   pSharedA->setA( 4 );
   std::cout << "Shared: " << pSharedA->getA() << std::endl;
   pUniqueA->setA( 5 );
   std::cout << "Unique: " << pUniqueA->getA() << std::endl;
   std::cout << "Shared After Setting Unique: " << pSharedA->getA() << std::endl;
   std::cout << "Unique: " << pUniqueA->getA() << std::endl;

   pSharedA.reset();
   pUniqueA.release();
   pUniqueA.reset();

   A a1( 3 );
   std::shared_ptr<A> sharedA;
   std::unique_ptr<A> uniqueA;
   Allocator::allocate( sharedA, a1 );
   Allocator::allocate( uniqueA, a1 );

   std::cout << "Shared: " << sharedA->getA() << std::endl;
   std::cout << "Unique: " << uniqueA->getA() << std::endl;

   sharedA->setA( 6 );
   uniqueA->setA( 7 );
   std::cout << "Shared: " << sharedA->getA() << std::endl;
   std::cout << "Unique: " << uniqueA->getA() << std::endl;

   sharedA.reset();
   uniqueA.release();
   uniqueA.reset();

   std::cout << std::endl << "Press Any Key To Quit" << std::endl;
   _getch();

   return 0;
}


Enjoy!


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Baidu [Spider] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron