Template Classes to Manage Collections of Objects

If you are going to create a project in ATL or some other type, then MFC is often included.  The reason for this is often for a few collection classes and CString.  There will be some overhead in an ATL project if MFC is included and the collection classes is not that hard to rewrite.

I created some templates that I use and it is working fine for me.  The classes are designed to be fast and use very little memory.  They store pointers to classes and not objects except one.  If constructors, copy constructors, assignment and other things are added then the collection class becomes slower.  And it is not much harder to work with the class if it stores pointers to a class instead of the whole object.  Sometimes it is easier because you don't have to add functions needed for the collection class in object that is managed by it.

Usage Examples

1.   How can I create a string class?
  CBuffer<char,'0'> bufferText;
  bufferText = "This is a text.";
  bufferText += " Some more text";
  int iPosition = bufferText.Find( "Some" );
2.   How can I store some integer values but none of them has the value -1?
  CBuffer<int,-1> bufferInt;
3.   How can I store some pointers?
  CBuffer<void*,NULL> bufferPtr;
4.   I want to store some objects (CMyObject) in a array.
  int iDummy;
  CMyObject* pMyObject;
  CArrayPtr<CMyObject> arrayToMyObject;
  arrayToMyObject.Add( new CMyObject() );
  iDummy = 10;
  arrayToMyObject.Add( new CMyObject( iDummy ) );
  iDummy = 20;
  arrayToMyObject.InsertAt( 1, new CMyObject( iDummy ) );
  pMyObject = arrayToMyObject.GetAt( 0 );
  iDummy = arrayToMyObject.GetSize(); // gets the value 3
  arrayToMyObject.DeleteItems();
5.   I want to store CXxx in a hash.
  CMapPtr<ULONG,CXxx> mapptrToXxx;
  mapptrToXxx.InitHashTable( 21 );
  mapptrToXxx.SetAt( 10, new CXxx );
  CXxx* pXxx;
  if ( mapptrToXxx.Lookup( 10, &pXxx ) == true )
     { ... }
  mapptrToXxx.Empty( true );

Download

Download demo project - 16 KB    templ_coll_demo.zip
Download source - 3 KB    templ_coll_src.zip



Comments

  • string key in hash

    Posted by Legacy on 05/26/2000 12:00am

    Originally posted by: vlad

    But what will happand if I want to use string as key? I guess your hash will treat pointer as a key. Hence, the equal strings in different memory locations will give different values. In my opinion, hash class needs the third parameter - hash function.

    Reply
  • Don't forget the STL

    Posted by Legacy on 04/07/1999 12:00am

    Originally posted by: Kevin King

    Don't forget the STL (ANSI/ISO standard C++ library) provides a host of templated collection classes. The best value of the STL are all of the predefined algorithms that work on the collection classes, such as binary_search. Saves a lot of time!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds