tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Filesystem / Stream.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #ifndef WRTDEVICEAPIS_FILESYSTEM_STREAM_H_
17 #define WRTDEVICEAPIS_FILESYSTEM_STREAM_H_
18
19 #include <cstddef>
20 #include <fstream>
21 #include <dpl/shared_ptr.h>
22 #include <dpl/enable_shared_from_this.h>
23 #include <Filesystem/IStream.h>
24 #include <Filesystem/Enums.h>
25 #include <Filesystem/INode.h>
26
27 namespace WrtDeviceApis {
28 namespace Filesystem {
29
30 class Node;
31 typedef DPL::SharedPtr<Node> NodePtr;
32
33 class Stream :
34     public Api::IStream,
35     public DPL::EnableSharedFromThis<Stream>
36 {
37     friend class Node;
38
39   public:
40     ~Stream();
41
42     Api::IStreamPtr write(bool arg);
43     Api::IStreamPtr write(unsigned char arg);
44     Api::IStreamPtr write(char arg);
45     Api::IStreamPtr write(int arg);
46     Api::IStreamPtr write(double arg);
47     Api::IStreamPtr write(const std::string& arg);
48
49     Api::IStreamPtr read(bool& arg);
50     Api::IStreamPtr read(unsigned char& arg);
51     Api::IStreamPtr read(char& arg);
52     Api::IStreamPtr read(int& arg);
53     Api::IStreamPtr read(double& arg);
54     Api::IStreamPtr read(std::string& arg);
55
56     /**
57      * @throw PlatformException If unable to read from the stream.
58      */
59     unsigned char* getBytes(std::size_t num);
60
61     std::size_t getCount() const;
62
63     /**
64      * @throw PlatformException If unable to read from the stream.
65      */
66     char* getChars(std::size_t num);
67
68     std::string getLine();
69
70     bool isOpen() const;
71     bool isEof() const;
72
73     void close();
74
75     long getPosition() const;
76     void setPosition(long position);
77
78     int getMode() const;
79
80     long getSize() const;
81
82   private:
83     template<typename T>
84     Api::IStreamPtr read_(T& arg);
85     template<typename T>
86     Api::IStreamPtr write_(T arg);
87
88     inline bool isReadable() const;
89     inline bool isWriteable() const;
90     void checkForReading() const;
91     void checkForWriting() const;
92
93   private:
94     Stream(const NodePtr& parent,
95             int mode);
96
97   private:
98     NodePtr m_parent;
99     int m_mode;
100     mutable std::fstream m_stream;
101 };
102
103 typedef DPL::SharedPtr<Stream> StreamPtr;
104
105 } // Filesystem
106 } // WrtDeviceApis
107
108 #endif /* WRTDEVICEAPIS_FILESYSTEM_STREAM_H_ */