File: doc_file_descriptor.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (90 lines) | stat: -rw-r--r-- 2,501 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
//////////////////////////////////////////////////////////////////////////////
//
// (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.
//
//////////////////////////////////////////////////////////////////////////////

//[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)
   {}
   //->
   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;
}
//]