-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdvalue.hpp
More file actions
160 lines (146 loc) · 4.57 KB
/
dvalue.hpp
File metadata and controls
160 lines (146 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef DSTRUCT_DVALUE_HPP_
#define DSTRUCT_DVALUE_HPP_
#include "destruct.hpp"
#include "dobject.hpp"
#include "dfunction.hpp"
#include "dunicodestring.hpp"
#include "dbuffer.hpp"
//#include "protocol/dstreambase.hpp"
namespace Destruct
{
//class DValue;
//spliter les base value ds un autre fichier
/*! \brief This an interface, bae class for value,
*
* all value implementation must inherit this class
* this let manipulate value , value implemetation are in a separe class called DValue
* objet can store this basevalue class to get access to derived class by inheritance
*/
class BaseValue
{
public:
EXPORT virtual BaseValue* clone(DObject *) const = 0;
EXPORT virtual ~BaseValue() {};
EXPORT virtual DValue getFinal() const = 0;
EXPORT virtual void set(DValue const & v) = 0;
protected:
EXPORT BaseValue() {}
EXPORT BaseValue(BaseValue const &) {}
EXPORT BaseValue& operator=(BaseValue const &)
{
return (*this);
}
};
/*! \brief inherit base value to create a new kind of value that call an internal function
*/
class FinalValue : public BaseValue
{
public:
EXPORT virtual BaseValue * clone(DObject *) const;
EXPORT virtual FinalValue * clone() const = 0;
EXPORT virtual DUnicodeString asUnicodeString() const = 0;
EXPORT virtual DBuffer asDBuffer() const = 0;
//EXPORT virtual DStreamBase& serialize(DStreamBase& os) const = 0;//XXX Better use an object protocol ?
//EXPORT virtual DStreamBase& unserialize(DStreamBase& is) = 0; //XXX Better use an object protocol ?
EXPORT virtual DValue getFinal() const;
protected:
EXPORT FinalValue();
EXPORT FinalValue(FinalValue const &);
EXPORT FinalValue& operator=(FinalValue const &);
};
/*! \brief provide copy and plain type conversion operator
*/
template <typename PlainType>
class TypedValue : public FinalValue
{
public:
virtual operator PlainType() const = 0;
protected:
TypedValue() {}
TypedValue(TypedValue const & rhs) : FinalValue(rhs) {}
TypedValue& operator=(TypedValue const& rhs)
{
FinalValue::operator =(rhs);
return (*this);
}
};
/*! \brief Value handle
*
* this is the value implementation to manipulate basevalue object allocate/delete embeded acess
* through final value converter
* it use a template class to specialize the get method
*/
class DValue
{
public:
//template <typename PlainType>
//EXPORT DValue(RealValue<PlainType> v) : __value(v) {}
EXPORT DValue(FinalValue const &);
EXPORT DValue(DValue const &);
EXPORT explicit DValue (FinalValue* = NULL);
EXPORT ~DValue();
EXPORT DValue& operator=(const DValue&);
EXPORT DValue& replace(const DValue&);
template <typename PlainType>// ok but could be ambigous in some case
operator PlainType() const
{
return (this->get<PlainType>());
}
template <typename PlainType>
EXPORT PlainType get() const
{
if (this->__value)
{
TypedValue<PlainType> const &tv = dynamic_cast<TypedValue<PlainType> const &>(*this->__value);
//try catch bad_cast and rather return DEXception (cast error) so no need to check for dexception and bad cast everywhere ?
//this can happen for exemple when object as a default constructor that take an argument and the argument is not of the right type
//also need to type constructor argument
return (tv);
}
else
return PlainType();
}
//operator std::string();
//EXPORT friend DStreamBase& operator<<(DStreamBase& os, DValue& value);
//EXPORT friend DStreamBase& operator>>(DStreamBase& is, DValue& value);
//std::ostream& serialize (std::ostream& os) const;
EXPORT DUnicodeString asUnicodeString() const;
EXPORT DBuffer asDBuffer() const;
private:
FinalValue* __value;
};
/*
* We specialize DValue because of refCount this shouldn't happen in normal case ...
*/
/*
template <>
inline DObject* DValue::get<DObject* >() const
{
if (this->__value)
{
TypedValue<DObject* > const &tv = dynamic_cast<TypedValue<DObject* > const &>(*this->__value);
DObject* dobject = tv;
if (dobject)
dobject->addRef(); //add ref for getter so he doesn't have do to it but must call destroy
return (dobject);
}
else
return (NULL); // ret DNone !
}
template <>
inline DFunctionObject* DValue::get<DFunctionObject* >() const
{
if (this->__value)
{
TypedValue<DFunctionObject* > const &tv = dynamic_cast<TypedValue<DFunctionObject* > const &>(*this->__value);
DFunctionObject* dobject = tv;
if (dobject)
dobject->addRef(); //add ref for getter so he doesn't have do to it but must call destroy
return (dobject);
}
else
return (NULL); // ret DNone !
}
*/
}
#endif