File: doc_file_descriptor.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (92 lines) | stat: -rw-r--r-- 2,624 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2008-2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/move/detail/config_begin.hpp>
#include <cassert>

//[file_descriptor_def

#include <boost/move/utility_core.hpp>
#include <stdexcept>

class file_descriptor
{
   //<-
   int operating_system_open_file(const char *)
   {
      return 1;
   }

   void operating_system_close_file(int fd)
   {  (void)fd;   assert(fd != 0); }
   //->
   int os_descr_;

   private:
   BOOST_MOVABLE_BUT_NOT_COPYABLE(file_descriptor)

   public:
   explicit file_descriptor(const char *filename)              //Constructor
      : os_descr_(operating_system_open_file(filename))
   {  if(!os_descr_) throw std::runtime_error("file not found");  }

   ~file_descriptor()                                          //Destructor
   {  if(os_descr_)  operating_system_close_file(os_descr_);  }

   file_descriptor(BOOST_RV_REF(file_descriptor) x)            // Move ctor
      :  os_descr_(x.os_descr_)
   {  x.os_descr_ = 0;  }      

   file_descriptor& operator=(BOOST_RV_REF(file_descriptor) x) // Move assign
   {
      if(os_descr_) operating_system_close_file(os_descr_);
      os_descr_   = x.os_descr_;
      x.os_descr_ = 0;
      return *this;
   }

   bool empty() const   {  return os_descr_ == 0;  }
};

//]

//[file_descriptor_example
#include <boost/container/vector.hpp>
#include <cassert>

//Remember: 'file_descriptor' is NOT copyable, but it
//can be returned from functions thanks to move semantics
file_descriptor create_file_descriptor(const char *filename)
{  return file_descriptor(filename);  }

int main()
{
   //Open a file obtaining its descriptor, the temporary
   //returned from 'create_file_descriptor' is moved to 'fd'.
   file_descriptor fd = create_file_descriptor("filename");
   assert(!fd.empty());

   //Now move fd into a vector
   boost::container::vector<file_descriptor> v;
   v.push_back(boost::move(fd));

   //Check ownership has been transferred
   assert(fd.empty());
   assert(!v[0].empty());

   //Compilation error if uncommented since file_descriptor is not copyable
   //and vector copy construction requires value_type's copy constructor:
   //boost::container::vector<file_descriptor> v2(v);
   return 0;
}
//]

#include <boost/move/detail/config_end.hpp>