Mobile API Reference  MicroStrategy 2019
StrongObjects.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : StrongObjects.h
3 // AUTHOR : vovechkin
4 // CREATION : 2002-08-28
5 // Copyright (C) MicroStrategy Incorporated 2002
6 // All Rights Reserved
7 //==============================================================================================
8 #ifndef MFileSystem_StrongObjects_h
9 #define MFileSystem_StrongObjects_h
10 
11 #include <string>
12 #include <stdio.h>
13 #include <errno.h>
14 
15 #include <unistd.h> // for ::close
16 
17 #include "Base/Defines/StrongObj.h"
18 #include "../FileSystem/File.h"
19 #include "../FileSystem/Path.h"
20 
21 namespace MFileSystem
22 {
24  {
25  public:
26  void operator()( FILE* iFileStream ) throw()
27  {
28  if( iFileStream != NULL )
29  {
30  const int lResult = ::fclose(iFileStream);
31 
32  if (lResult != 0)
33  {
34  // 2004-01-02 vovechkin
35  //
36  // WHY:
37  // If closing the file failed, it could be because
38  // the file was not explicitly flushed prior to closing,
39  // and an implicit flush encountered an error.
40  //
41  // HOW TO FIX:
42  // If you get this assertion, make sure that your
43  // last file operation is MFileSystem::File::Flush(),
44  // which will throw an exception in case of an error.
45  //
46  // 2004-08-18 vovechkin
47  // Removing assertions because they fail in the
48  // course of stack unwinding when an exception is
49  // thrown. Until we have a file object, we cannot
50  // know at this point whether we should assert or not.
51 // const int lErrorCode = errno;
52 // _ASSERTE("::fclose() failed" && false);
53  }
54  }
55  }
56  };
57 
59  {
60  public:
61  void operator()( int iFileDescriptor ) throw()
62  {
63  // valid descriptors are >= 0
64  if (iFileDescriptor >= 0)
65  {
66 #ifdef WIN32
67  const int lResult = ::_close(iFileDescriptor);
68 #else
69  const int lResult = ::close(iFileDescriptor);
70 #endif
71 
72  if (lResult != 0)
73  {
74  // 2004-01-02 vovechkin
75  //
76  // WHY:
77  // If closing the file failed, it could be because
78  // the file was not explicitly flushed prior to closing,
79  // and an implicit flush encountered an error.
80  //
81  // HOW TO FIX:
82  // If you get this assertion, make sure that your
83  // last file operation is MFileSystem::File::Flush(),
84  // which will throw an exception in case of an error.
85  //
86  // 2004-08-18 vovechkin
87  // Removing assertions because they fail in the
88  // course of stack unwinding when an exception is
89  // thrown. Until we have a file object, we cannot
90  // know at this point whether we should assert or not.
91 // const int lErrorCode = errno;
92 // _ASSERTE("::close() failed" && false);
93  }
94  }
95  }
96  };
97 
99 
104  protected MBase::StrongObj<int, CloseFileDescriptor>
105  {
106  public:
107 
110  {
111  }
112 
113  StrongFileDescriptor(int iDescriptor):
114  MBase::StrongObj<int, CloseFileDescriptor>(iDescriptor)
115  {
116  }
117 
118  int Get() const throw()
119  {
120  return static_cast<const MBase::StrongObj<int, CloseFileDescriptor>*>(this)->Get();
121  }
122 
123  bool IsNull()
124  {
125  return (Get() < 0);
126  }
127 
128  void Acquire(StrongFileDescriptor& iStrongObj) throw()
129  {
130  int iData = iStrongObj.GiveUp();
131  Acquire(iData);
132  }
133 
134  void Acquire(int& irData) throw()
135  {
136  static_cast<MBase::StrongObj<int, CloseFileDescriptor>*>(this)->Acquire(irData);
137  }
138 
139  void Reset() throw()
140  {
141  static_cast<MBase::StrongObj<int, CloseFileDescriptor>*>(this)->Reset(-1);
142  }
143  private:
144 
145  int GiveUp() throw()
146  {
147  int lTemp = mData;
148  mData = -1;
149  return lTemp;
150  }
151  };
152 
158  {
159  public:
160  explicit StrongFilename( const char* ipPath ):
161  mPath( ipPath )
162  {
163  }
164 
166  {
167  mPath.swap( iorOther.mPath );
168  }
169 
170  ~StrongFilename() throw()
171  {
172  Reset();
173  }
174 
176  {
177  mPath.swap( iorOther.mPath );
178  return *this;
179  }
180 
181  const char* c_str() const
182  {
183  return mPath.c_str();
184  }
185 
186  void Dismiss() throw()
187  {
188  mPath.erase();
189  }
190 
191  void Attach( const char* ipPath ) throw()
192  {
193  Reset();
194  mPath = ipPath;
195  }
196 
197  private:
198  std::string mPath;
199 
200  void Reset() throw()
201  {
202  if( mPath.length() > 0 )
203  {
204  bool lDeleted = File::Delete( mPath.c_str() );
205 
206  // don't assert if there is no file
207 // _ASSERT(lDeleted || !Path::IsFile(mPath.c_str()));
208 
209  mPath.erase();
210  }
211  }
212  };
213 }
214 
215 #endif // MFileSystem_StrongObjects_h
Definition: StrongObj.h:20
static bool Delete(const char *ipPath)
StrongObj(int ipData=NULL)
Definition: StrongObj.h:23
StrongFileDescriptor()
Definition: StrongObjects.h:108
Definition: StrongObjects.h:58
StrongFilename(const char *ipPath)
Definition: StrongObjects.h:160
void Acquire(StrongFileDescriptor &iStrongObj)
Definition: StrongObjects.h:128
int mData
Definition: StrongBase.h:80
Definition: StrongObjects.h:23
void operator()(FILE *iFileStream)
Definition: StrongObjects.h:26
int Get() const
Definition: StrongObjects.h:118
void Dismiss()
Definition: StrongObjects.h:186
StrongFilename(StrongFilename &iorOther)
Definition: StrongObjects.h:165
Definition: Allocator.h:47
Definition: StrongObjects.h:103
void Attach(const char *ipPath)
Definition: StrongObjects.h:191
void Acquire(int &irData)
Definition: StrongObjects.h:134
void Reset()
Definition: StrongObjects.h:139
StrongFilename & operator=(StrongFilename &iorOther)
Definition: StrongObjects.h:175
bool IsNull()
Definition: StrongObjects.h:123
Definition: StrongObjects.h:157
~StrongFilename()
Definition: StrongObjects.h:170
const char * c_str() const
Definition: StrongObjects.h:181
MBase::StrongObj< FILE *, CloseFileStream > StrongFileStream
Definition: StrongObjects.h:98
#define NULL
Definition: Null.h:10
int close(int fildes)
Definition: PDCunistd.h:162
Definition: StrongObjects.h:21
void operator()(int iFileDescriptor)
Definition: StrongObjects.h:61
StrongFileDescriptor(int iDescriptor)
Definition: StrongObjects.h:113