Home » U++ Library support » U++ Callbacks and Timers » About storing references and pointers to callbacks.  
	
		
		
			| About storing references and pointers to callbacks. [message #48350] | 
			Sun, 25 June 2017 21:13   | 
		 
		
			
				
				
				
					
						  
						Oblivion
						 Messages: 1240 Registered: August 2007 
						
					 | 
					Senior Contributor  | 
					 | 
		 
		 
	 | 
 
	
		Hello, 
 
I'd like to ask you a question. Here is the problem I need to solve: 
 
I need to store pointers of complex objects, say, Streams, to callbacks (e.g. for deferred/async file reads and writes) so that I can access them only when I need them. 
The culprit is that I don't want the caller function (or callback) to own those objects. Knowing their current state -whether they are destroyed or existing- to proceed or to halt is sufficient.  
 
I know that simply passing pointers is dangerous, since the life time of objects can vary and not be strictly determined especially on complex applications. 
Now, I know C++11 and above versions of C++ standard have std::shared_ptr and std::weak_ptr suitable for this purpose.  
Also U++ has something similar: Ptr and Pte. 
 
What would be the U++ way to handle these situations? 
 
MyClass::MyAsyncReadMethod(Stream& s) 
{
        Stream *p = &s; // This is a bad, very bad practice!
        vector_containing_callbacks.Add() << [=] {
                       p->GetLine() // !! might eat cats!
                             ?? // How should I proceed? 
                                // Should I use shared_ptr && weak_ptr?
                                // Or A Stream derivative with Ptr && Pte? 
                                // or another alternative?
        };
}
 
 
 
Any suggestions or ideas will be much appreciated. Thanks. 
 
Bestt regards, 
Oblivion
		
		
  Github page: https://github.com/ismail-yilmaz 
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
		[Updated on: Sun, 25 June 2017 21:29] Report message to a moderator  
 |  
	| 
		
	 | 
 
 
 |  
  
 
Goto Forum:
 
 Current Time: Tue Nov 04 06:55:22 CET 2025 
 Total time taken to generate the page: 0.06341 seconds 
 |