Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / 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 class Node;
30 typedef DPL::SharedPtr<Node> NodePtr;
31
32 class Stream :
33     public Api::IStream,
34     public DPL::EnableSharedFromThis<Stream>
35 {
36     friend class Node;
37
38   public:
39     ~Stream();
40
41     Api::IStreamPtr write(bool arg);
42     Api::IStreamPtr write(unsigned char arg);
43     Api::IStreamPtr write(char arg);
44     Api::IStreamPtr write(int arg);
45     Api::IStreamPtr write(double arg);
46     Api::IStreamPtr write(const std::string& arg);
47
48     Api::IStreamPtr read(bool& arg);
49     Api::IStreamPtr read(unsigned char& arg);
50     Api::IStreamPtr read(char& arg);
51     Api::IStreamPtr read(int& arg);
52     Api::IStreamPtr read(double& arg);
53     Api::IStreamPtr read(std::string& arg);
54
55     /**
56      * @throw PlatformException If unable to read from the stream.
57      */
58     unsigned char* getBytes(std::size_t num);
59
60     std::size_t getCount() const;
61
62     /**
63      * @throw PlatformException If unable to read from the stream.
64      */
65     char* getChars(std::size_t num);
66
67     std::string getLine();
68
69     bool isOpen() const;
70     bool isEof() const;
71
72     void close();
73
74     long getPosition() const;
75     void setPosition(long position);
76
77     int getMode() const;
78
79     long getSize() const;
80
81   private:
82     template<typename T>
83     Api::IStreamPtr read_(T& arg);
84     template<typename T>
85     Api::IStreamPtr write_(T arg);
86
87     inline bool isReadable() const;
88     inline bool isWriteable() const;
89     void checkForReading() const;
90     void checkForWriting() const;
91
92   private:
93     Stream(const NodePtr& parent,
94            int mode);
95
96   private:
97     NodePtr m_parent;
98     int m_mode;
99     mutable std::fstream m_stream;
100 };
101
102 typedef DPL::SharedPtr<Stream> StreamPtr;
103 } // Filesystem
104 } // WrtDeviceApis
105
106 #endif /* WRTDEVICEAPIS_FILESYSTEM_STREAM_H_ */