Git init
[pkgs/e/elektra.git] / src / bindings / cpp / include / key
1 #ifndef CPP_KEY_H
2 #define CPP_KEY_H
3
4 #include <sstream>
5 #include <string>
6 #include <kdb.h>
7
8 namespace kdb {
9
10 class KeyException : public std::exception
11 {
12         const char* what() {return "Key Exception";}
13 };
14
15 class KeyInvalidName : public KeyException
16 {
17         const char* what() {return "Invalid Keyname";}
18 };
19
20 /**Key represents an elektra key.*/
21 class Key
22 {
23 public:
24         Key () :key (ckdb::keyNew (0)) { operator++(); }
25         Key (ckdb::Key *k) :key(k) { operator++(); }
26         Key (Key &k) :key (k.key) { operator++(); }
27         Key (const Key &k) :key (k.key) { operator++(); }
28         Key (const char * name, va_list ap);
29         Key (const char * name, ...);
30         Key (const std::string name, ...);
31
32         void del () { operator --(); ckdb::keyDel(key); }
33         void copy (const Key &other) { ckdb::keyCopy(key,other.key); }
34         void clear () { ckdb::keyCopy(key,0); }
35         ~Key () { del(); }
36
37         void operator ++(int) { operator++(); }
38         void operator ++() { ckdb::keyIncRef(key); }
39
40         void operator --(int) { operator--(); }
41         void operator --() { ckdb::keyDecRef(key); }
42
43         bool operator ==(const Key &k) const { return key == k.key; }
44
45         template <class T>
46         T get()
47         {
48                 T x;
49                 std::string str;
50                 str = getString();
51                 std::istringstream ist(str);
52                 ist >> x;       // convert string to type
53                 return x;
54         }
55
56         template <class T>
57         void set(T x)
58         {
59                 std::string str;
60                 std::ostringstream ost;
61                 ost << x;       // convert type to string
62                 setString (ost.str());
63         }
64
65         /*Passes out the pointer, maybe directly changing this object*/
66         ckdb::Key* getKey () const { return key; }
67         ckdb::Key* operator* () const { return key; }
68         ckdb::Key* dup () const { return ckdb::keyDup(getKey()); }
69
70         Key& operator= (ckdb::Key *k);
71         Key& operator= (const Key &k);
72
73         Key& operator=  (const std::string &name);
74         Key& operator+= (const std::string &name);
75         Key& operator-= (const std::string &name);
76
77         Key& operator=  (const char *name);
78         Key& operator+= (const char *name);
79         Key& operator-= (const char *name);
80
81         type_t getType() const;
82         void setType(type_t type = KEY_TYPE_STRING);
83
84         std::string getName() const;
85         const char* name() const;
86         size_t getNameSize() const;
87
88         std::string getBaseName() const;
89         const char* baseName() const;
90         size_t getBaseNameSize() const;
91
92         void setName (const std::string &name);
93         void setBaseName (const std::string &basename);
94
95         size_t getFullNameSize() const;
96         std::string getFullName() const;
97
98         std::string getComment() const;
99         const char* comment() const;
100         size_t getCommentSize() const;
101         void setComment(const std::string &comment);
102
103         uid_t getUID() const;
104         void setUID(uid_t uid);
105
106         gid_t getGID() const;
107         void setGID(gid_t gid);
108
109         mode_t getMode() const;
110         void setMode(mode_t mode);
111
112         std::string getOwner() const;
113         const char* owner() const;
114         size_t getOwnerSize() const;
115         void setOwner(const std::string &owner);
116
117         const void* value() const;
118         size_t getValueSize() const;
119
120         std::string getString() const;
121         void setString(std::string newString);
122
123         size_t getBinary(void *returnedBinary, size_t maxSize) const;
124         size_t setBinary(const void *newBinary, size_t dataSize);
125
126         void setDir ();
127
128         void setMTime(time_t time);
129         void setATime(time_t time);
130         void setCTime(time_t time);
131
132         time_t getMTime() const;
133         time_t getATime() const;
134         time_t getCTime() const;
135
136         bool isSystem() const;
137         bool isUser() const;
138
139         int getNamespace() const;
140         size_t getReference() const;
141
142         bool isDir() const;
143         bool isString() const;
144         bool isBinary() const;
145
146         bool isInactive() const;
147         bool isBelow(const Key &k) const;
148         bool isDirectBelow(const Key &k) const;
149
150         bool needSync() const;
151         bool needRemove() const;
152         bool needStat() const;
153
154         void remove();
155         void stat();
156
157         /*
158         ssize_t toStream(FILE* stream = stdout, unsigned long options = 0) const;
159         ssize_t output(FILE* stream = stdout, unsigned long options = 0) const;
160         ssize_t generate(FILE* stream = stdout, unsigned long options = 0) const;
161         */
162
163         friend std::ostream & operator << (std::ostream & os, const Key &d);
164         friend std::istream & operator >> (std::istream & os, Key &d);
165
166 private:
167         ckdb::Key * key; // holds elektra key struct
168 };
169
170 } // end of namespace kdb
171
172 #endif
173