Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
4 captures
06 Jun 2008 - 10 Jul 2011
AugJULAug
Previous capture10Next capture
200820112012
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20110710081734/http://www.codeproject.com:80/KB/cpp/Ref_Object_Set.aspx
Click here to Skip to main content
7,937,352 members and growing! (38,327 online)
EmailPassword Lost password?
Home
Search within:




Licence 
First Posted 8 Jun 2005
Views 14,203
Downloads 107
Bookmarked 16 times

Reference-based Object Set

ByChan-gu Lee | 8 Jun 2005
CRefObject is not a smart pointer. It is a reference manager class for value-based objects.
 
See Also
Print Article
add
Add to your CodeProject bookmarks
Discuss
Discuss this article
1
  3.67 (3 votes)
1 vote, 33.3%
1

2

3
1 vote, 33.3%
4
1 vote, 33.3%
5
3.67/5 - 3 votes
μ 3.67, σa 3.64 [?]
Sponsored Links

Introduction

A reference-based object must be managed with consideration to memory allocation and de-allocation. A garbage collector and smart-pointer helps us avoid these annoying considerations.

But, a standard C++ compiler has no garbage collector. And, in spite of some libraries implementing a smart-pointer, a C++ compiler has no reference count for an object, so the compiler can't manage the object's scope within functions.

TheCRefObject is not just a smart pointer, it supports and manages reference information of objects. So you can passCRefObject to a function and return from that function. In any scope, if you keep theCRefObject instance, its object is never destroyed.

Also, when you want to store different types of objects in one container likeClassA andClassB, what can you do? UsingCSessionObject you can do this without any additional work. Just reference the container with an Object-Key and you can store and retrieve these objects from the container.

Background

CRefObject uses a technique implemented inCString.CRefObject allocates a memory block for an object as an unknown memory block followed byCObjectData.CObjectData has information for managing the reference counter.

This method has many advantages - it prevents defragmenting memory byCObjectData.

class _TAllocator :public T {public:void* PASCALoperatornew(size_t nSize) {    LPVOID pNewObj =newBYTE[nSize +sizeof(CObjectData)] ;return ((BYTE*)pNewObj) +sizeof(CObjectData) ;}void PASCALoperatordelete(void* p) {delete [] (((CObjectData*)p) -1) ;}} ;

If you wish to know more about the principle behindCRefObject, look at theCString source code.

CSessionObject started from the question 'How can I store all ofCRefObject objects in onestd::map class?'. Because STL is based on templates, if I want to use these I must define an explicit parameterized Type. However,CRefObject is also template based, so I can't do it.

I thought about it, and had an idea!

CRefObject doesn't manage a real object. It just manages unknown memory blocks. So, I decided to store these memory blocks instd::map.

CSessionObject represents an explicit Session object usingCRefObject. You can modifyCRefObject, but I didn't want to do that. So, I wrote another class.CManagedObjData is a bridge to manage the reference count withinCObjectData andCSessionObject.CSession is the container which stores and retrieves Objects by the Key.

The download project also has a C++ Unit Test. I wrote some unit test routines based onCPPUnit Lite. The DLL must have a Test Entry Point.

// Copy this and paste to your codeextern"C" {__declspec(dllexport)void TestEntryPoint()  {    TestResult tr;    TestRegistry::runAllTests(tr);  }}

TestLoader.EXE loads the DLL and executes its Test Entry Point. You can findTestLoader.EXE in my Zip files, not in the CPPUnit Lite.TestLoader.EXE is written by me.

TestLoader.EXE xXx.DLL

If you use Visual Studio, use like this:

This is my setting, you must adapt it for your environment.

Using the code

First, you need to define a parameterized class.

typedef CRefObject<_TWorkspaceValue> CWorkspaceValue ;

CRefObject has no constructor for baseType. So, if you define it like this, no object will be created.

CWorkspaceValue WorkspaceValue ;

If you want to create a new instance for the object, you must define it like this:

CWorkspaceValue WorkspaceValue(NULL) ;

Finally, you can use this object just like a value object. No need to worry about memory management.

Do you want to assign this to a new one? Look at this:

CWorkspaceValue AnotherValue(WorkspaceValue) ;CWorkspaceValue Others ;Others = WorkspaceValue ;

AnotherValue,Others andWorkspaceValue share one object -WorkspaceValue. If you change an object's value from any one of them, it affects all of them.

To prevent this, you'd better useLock.

Others.Lock() ;Others->Value = NewValue ;Others.Unlock() ;

Lock() method creates a new object. It works like aCString::Lock().

You can create a new instance from base type.

_TWorkspaceValue BaseType ;BaseType.Value = NewValue ;CWorkspaceValue v1(&BaseType) ;Others = &BaseType ;

In function parameter and return value, you can use this like valued-object, but it works as a ref-object. No value copy, just counts a reference counter.

CWorkspaceValue GetWorkspace(LPCSTR szWorkspaceName){return m_WsList[szWorkspaceName] ;}void InsertItem(CWorkspaceValue NewValue){    m_WsList[NewValue->WorkspaceName] = NewValue ;}

If you want to reference base type, use the-> operator.

For more information aboutCSessionObject, you'd better referenceRefObject.cpp in the Zip file.

Just what I say is,CSessionObject is compatible withCRefObject :).

History

  • 06/10/2005 - Fixed 'SessionObject.h'. It reports a compile error when compiled with another project that uses 'SessionObject.h'.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be foundhere

About the Author

Chan-gu Lee

Software Developer (Senior)

Korea (Republic Of) Korea (Republic Of)

Member


Sign Up to vote for this article
Add a reason or comment to your vote:x
Votes of 3 or less require a comment

Comments and Discussions

 
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
Generalinteresting.memberChauJohnthan6:52 11 Jun '05  
Last Visit: 19:00 31 Dec '99     Last Update: 23:17 9 Jul '111

General General   News News   Suggestion Suggestion   Question Question   Bug Bug   Answer Answer   Joke Joke   Rant Rant   Admin Admin   

Permalink |Advertise |Privacy |Mobile
|Web21 |2.3.110709.1
Article Copyright 2005 by Chan-gu Lee
Everything elseCopyright ©CodeProject, 1999-2011
Terms of Use
Layout:fixed|fluid

See Also...
MFC Testing framework
A simple MFC automated testing framework for...
Code that debugs itself
A set of macros for detecting and reporting...
Unit testing with CPPUnit
CPPUnit is a unit testing framework for C++,...
Simple Reminder Application
The article presents a simple Reminder...
Agile Development
Five Steps to Continuous Integration
The Daily Insider

[8]ページ先頭

©2009-2025 Movatter.jp