blob: 4b7ad4d0e96df4da965241a44ade87d4028dd6f7 [file] [log] [blame]
Zonr Changd670be72012-04-05 15:09:28 +08001/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Stephen Hines2f6a4932012-05-03 12:27:13 -070017#ifndef BCC_EXECUTION_ENGINE_FILE_H
18#define BCC_EXECUTION_ENGINE_FILE_H
Zonr Changd670be72012-04-05 15:09:28 +080019
Stephen Hines2f6a4932012-05-03 12:27:13 -070020#include "FileBase.h"
Zonr Changd670be72012-04-05 15:09:28 +080021
22namespace bcc {
23
24template<enum FileBase::OpenModeEnum OpenMode>
25struct FileAttribute {
26 // The flags to the 2nd argument in ::open().
27 enum { kOpenFlags };
28
29 // Default value of LockMode.
30 enum { kDefaultLockMode };
31};
32
33// FileAttribute for accessing read-only file
34template<>
35struct FileAttribute<FileBase::kReadMode> {
36 enum { kOpenFlags = O_RDONLY };
37 enum { kDefaultLockMode = FileBase::kReadLock };
38};
39
40// FileAttribute for accessing writable file
41template<>
42struct FileAttribute<FileBase::kWriteMode> {
43 enum { kOpenFlags = O_RDWR | O_CREAT | O_TRUNC };
44 enum { kDefaultLockMode = FileBase::kWriteLock };
45};
46
47template<enum FileBase::OpenModeEnum OpenMode>
48class File : public FileBase {
49public:
50 File(const std::string &pFilename, unsigned pFlags)
51 : FileBase(pFilename, FileAttribute<OpenMode>::kOpenFlags, pFlags) { }
52
53 inline bool lock(enum LockModeEnum pMode = static_cast<enum LockModeEnum>(
54 FileAttribute<OpenMode>::kDefaultLockMode),
55 bool pNonblocking = true,
56 unsigned pMaxRetry = FileBase::kDefaultMaxRetryLock,
57 useconds_t pRetryInterval =
58 FileBase::kDefaultRetryLockInterval) {
59 return FileBase::lock(pMode, pNonblocking, pMaxRetry, pRetryInterval);
60 }
61
62 inline android::FileMap *createMap(off_t pOffset, size_t pLength,
63 bool pIsReadOnly =
64 (OpenMode == FileBase::kReadMode)) {
65 return FileBase::createMap(pOffset, pLength, pIsReadOnly);
66 }
67};
68
69
70} // end namespace bcc
71
Stephen Hines2f6a4932012-05-03 12:27:13 -070072#endif // BCC_EXECUTION_ENGINE_FILE_H