- Resource management - 2 Updates
- i not understand your C++ - 2 Updates
- I am disappoint. - 1 Update
- Onwards and upwards - 1 Update
woodbrian77@gmail.com: Oct 25 03:37PM -0700 On Saturday, October 24, 2015 at 2:37:44 PM UTC-5, Paavo Helde wrote: > encapsulating pointers, but it will make two concepts (RAII and pointers) > closely intertwined which creates unnecessary strong coupling. > I would much prefer the approach you used in IntFD instead: I agree. I use unique_ptr, but not a lot. |
"Norman J. Goldstein" <normvcr@telus.net>: Oct 25 04:02PM -0700 As a follow-up to my original post, I want to thank people for their responses. The consensus seems to be to have a resource-specific struct for each resource, and to stay away from abusing unique_ptr. I think an approach like BOOST_SCOPE_EXIT has its place, but not for the type of resource management being discussed -- the resource needs to be allocated, anyway, so just code up the release code in the destructor, and, then, no program-specific BOOST_SCOPE_EXIT is needed. Attached(below) is a no-frills header file with 5 resource managers, which I have just used and tested in a small program. I am certain I will be re-using this functionality in future code. The code needs to be broken into separate header files, maybe use namespaces, .... Any other suggestions on this? I have used the "_mgr" suffix for the resource classes, like the "_ptr" suffix on the smart pointers. ==================== /* The structs in this file are resource managers. When the object goes out of scope, the resource is freed. If the struct has the method get(), possibly const, it should return the resource that is being managed. The return value can be a value, a refererence or a pointer, whichever is appropriate for the particualr resource. */ PS I was not able to send the header file as an attachment. //============ Integer file descriptors =============== #include <unistd.h> // close // Example: // IntFD fd = open( "filename", O_WRONLY ); struct IntFD_mgr { int fd_; IntFD_mgr( int fd = -1 ) : fd_( fd ) {} int get( void ) const { return fd_; } ~IntFD_mgr(){ close( get() ); } }; //============ X resources =============== #include <X11/Xlib.h> struct Display_mgr { Display* display_; Display_mgr( Display* disp = nullptr ) : display_( disp ) {} Display* get( void ) const { return display_; }; ~Display_mgr() { XCloseDisplay( get() ); } }; struct FontWithDisplay_mgr { XFontStruct* fontStruct_; Display* display_; FontWithDisplay_mgr( XFontStruct* fs = nullptr, Display* disp = nullptr ) : fontStruct_( fs ), display_( disp ) {} XFontStruct* get( void ) const { return fontStruct_; }; ~FontWithDisplay_mgr() { XFreeFont( display_, get() ); } }; //============ syslog management =============== #include <syslog.h> #include <sstream> // insertion convert to string // Only one instance allowed per process struct SystemLogger_mgr { SystemLogger_mgr( const char *ident, int option, int facility) { openlog( ident, option, facility ); } ~SystemLogger_mgr() { closelog(); } }; // Allows stream logging such as // SysLog( LOG_ERR, "value= " << 5 ); #define SysLog( P, D ) \ if( LOG_MASK(P) ) { \ std::stringstream str; str << D; \ syslog(P, str.str().c_str() ); } //============ XDG management =============== #include <basedir.h> // Example: // XDGHandle xdgh; struct XDGHandle_mgr { xdgHandle handle_; XDGHandle_mgr( void ) { xdgInitHandle( get() ); } xdgHandle* get( void ) { return &handle_; } ~XDGHandle_mgr(){ xdgWipeHandle( get() ); } }; |
asetofsymbols@gmail.com: Oct 25 12:53PM -0700 Always in the same file I have ____________ int main() { auto x=10, y=2; auto& good = min(x,y); // ok, pset(good) == {x,y} cout << good; // ok, 2 auto& bad = min(x,y+1) // A: IN: pset(arg1)=={x}, // pset(arg2)=={temp(y+1)} // min() returns temp2 // OUT: pset(temp2) = {x,temp} ; // KILL(temp) pset(temp2) = {invalid ____________ But why crate in a function a reference variable int& y;? There is someone of you know that? Reference are good only for pass (as pointer) data to function and operator (without change it) It allow to write: x + y where x and y are reference In that is useful |
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Oct 25 02:47PM -0600 On Sun, 25 Oct 2015 12:00:36 -0700 (PDT), asetofsymbols@gmail.com wrote: >if one read from above >In this link >https://github.com/isocpp/CppCoreGuidelines/tree/master/docs OK. You, the authors of C++ Core Guidelines, and the rest of the world all agree that this is an example of what not to do. Louis |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Oct 25 08:24PM I am disappoint. |
Jorgen Grahn <grahn+nntp@snipabacken.se>: Oct 25 07:24PM >> What does "support" refer to? > I should have included the link in my post. > http://stackoverflow.com/questions/33329031/size-of-unknown-container Ok, thanks. I guess the relevant part of the question is "What is the alternative to my_vector.size() that will work for all containers including normal arrays?" I /think/ what the response is trying to say is "don't worry so much about supporting all possible container-like things with one piece of code; you're likely to use it with only one or two containers". But I'm not sure. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment