Yoba Perl
scalar.cpp
1 #include "yobaperl/scalar.hpp"
2 #include "yobaperl/array.hpp"
3 #include "yobaperl/hash.hpp"
4 #include "yobaperl/code.hpp"
5 #include "yobaperl/perl.hpp"
6 
7 namespace yoba {
8 
9 
10 
11 Scalar::Scalar(Perl & perl, SV * sv, bool increase_refcount)
12  : Variable(perl, sv, increase_refcount)
13 {
14 }
15 
16 
17 
18 Scalar & Scalar::setValue(const std::string & value)
19 {
20  sv_setpvn(getSV(), value.c_str(), value.length());
21  return *this;
22 }
23 
25 {
26  sv_setiv(getSV(), value);
27  return *this;
28 }
29 
31 {
32  sv_setuv(getSV(), value);
33  return *this;
34 }
35 
37 {
38  sv_setnv(getSV(), value);
39  return *this;
40 }
41 
42 Scalar & Scalar::setValue(bool value)
43 {
44  sv_setiv(getSV(), value ? 1 : 0);
45  return *this;
46 }
47 
48 Scalar & Scalar::setValue(const char * value)
49 {
50  setValue(std::string(value));
51  return *this;
52 }
53 
54 Scalar & Scalar::setValue(int value)
55 {
56  setValue(static_cast<IV>(value));
57  return *this;
58 }
59 
60 Scalar & Scalar::setValue(unsigned value)
61 {
62  setValue(static_cast<UV>(value));
63  return *this;
64 }
65 
66 Scalar & Scalar::setValue(float value)
67 {
68  setValue(static_cast<NV>(value));
69  return *this;
70 }
71 
72 
73 
74 std::string Scalar::toString() const
75 {
76  STRLEN len;
77  const char * ptr = SvPVx(const_cast<SV *>(getSV()), len);
78 
79  return std::string(ptr, len);
80 }
81 
82 IV Scalar::toIV() const
83 {
84  return SvIVx(getSV());
85 }
86 
87 UV Scalar::toUV() const
88 {
89  return SvUVx(getSV());
90 }
91 
92 NV Scalar::toNV() const
93 {
94  return SvNVx(getSV());
95 }
96 
97 bool Scalar::toBool() const
98 {
99  return SvTRUEx(getSV());
100 }
101 
103 {
104  if(isScalarRef())
105  {
106  return Scalar(_perl, SvRVx(getSV()), true);
107  }
108  else
109  {
110  std::string error = "Not a scalar reference (" + toString().substr(0, 10) + ")";
111 
112  if(_perl.isExceptionsEnabled())
113  throw PerlException(_perl.getId(), error);
114 
115  if(_perl.isWarningsEnabled())
116  warn("%s", error.c_str());
117 
118  return _perl.newScalar();
119  }
120 }
121 
123 {
124  if(isArrayRef())
125  {
126  return Array(_perl, MUTABLE_AV(SvRVx(getSV())), true);
127  }
128  else
129  {
130  std::string error = "Not an array reference (" + toString().substr(0, 10) + ")";
131 
132  if(_perl.isExceptionsEnabled())
133  throw PerlException(_perl.getId(), error);
134 
135  if(_perl.isWarningsEnabled())
136  warn("%s", error.c_str());
137 
138  return _perl.newArray();
139  }
140 }
141 
143 {
144  if(isHashRef())
145  {
146  return Hash(_perl, MUTABLE_HV(SvRVx(getSV())), true);
147  }
148  else
149  {
150  std::string error = "Not a hash reference (" + toString().substr(0, 10) + ")";
151 
152  if(_perl.isExceptionsEnabled())
153  throw PerlException(_perl.getId(), error);
154 
155  if(_perl.isWarningsEnabled())
156  warn("%s", error.c_str());
157 
158  return _perl.newHash();
159  }
160 }
161 
163 {
164  if(isCodeRef())
165  {
166  return Code(_perl, MUTABLE_CV(SvRVx(getSV())), true);
167  }
168  else
169  {
170  std::string error = "Not a code reference (" + toString().substr(0, 10) + ")";
171 
172 // if(_perl.isExceptionsEnabled())
173  throw PerlException(_perl.getId(), error);
174 
175 // if(_perl.isWarningsEnabled())
176 // warn("%s", error.c_str());
177  }
178 }
179 
180 
181 
182 I32 Scalar::getLength() const
183 {
184  return sv_len(getSV());
185 }
186 
188 {
189  return Scalar(_perl, newSVsv(_sv), false);
190 }
191 
193 {
194  return Scalar(_perl, newRV_inc(getSV()), false);
195 }
196 
197 bool Scalar::isRef() const
198 {
199  return SvROK(getSV());
200 }
201 
203 {
204  return SvTYPE(SvRVx(getSV())) < SVt_PVAV;
205 }
206 
207 bool Scalar::isArrayRef() const
208 {
209  return SvTYPE(SvRVx(getSV())) == SVt_PVAV;
210 }
211 
212 bool Scalar::isHashRef() const
213 {
214  return SvTYPE(SvRVx(getSV())) == SVt_PVHV;
215 }
216 
217 bool Scalar::isCodeRef() const
218 {
219  return SvTYPE(SvRVx(getSV())) == SVt_PVCV;
220 }
221 
222 bool Scalar::isObject() const
223 {
224  return sv_isobject(getSV());
225 }
226 
227 bool Scalar::isObjectOf(const std::string & class_name) const
228 {
229  return sv_isa(getSV(), class_name.c_str());
230 }
231 
233 {
234  return looks_like_number(getSV());
235 }
236 
237 
238 
239 yoba::Scalar::operator bool() const
240 {
241  return getLength() != 0;
242 }
243 
244 
245 
246 } // namespace yoba
Array reference
Definition: array.hpp:26
SV * getSV() const
Get raw scalar.
Definition: variable.cpp:63
bool isObjectOf(const std::string &class_name) const
Check if scalar is a class object.
Definition: scalar.cpp:227
Definition: array.cpp:5
bool isRef() const
Check if scalar is a reference.
Definition: scalar.cpp:197
Scalar unrefScalar() const
Dereference scalar.
Definition: scalar.cpp:102
std::string toString() const
Convert scalar value to C++ string.
Definition: scalar.cpp:74
Scalar(Perl &perl, SV *sv, bool increase_refcount)
Constructor.
Definition: scalar.cpp:11
Hash unrefHash() const
Dereference hash.
Definition: scalar.cpp:142
NV toNV() const
Convert scalar value to float number.
Definition: scalar.cpp:92
Hash reference
Definition: hash.hpp:22
Scalar makeRef() const
Take reference.
Definition: scalar.cpp:192
bool isObject() const
Check if scalar is a object.
Definition: scalar.cpp:222
bool isValidNumber() const
Check if scalar is a valid number.
Definition: scalar.cpp:232
bool isCodeRef() const
Check if scalar is a subroutine reference.
Definition: scalar.cpp:217
I32 getLength() const
Value length.
Definition: scalar.cpp:182
Scalar makeCopy() const
Create copy.
Definition: scalar.cpp:187
IV toIV() const
Convert scalar value to integer.
Definition: scalar.cpp:82
Array newArray()
Create anonymous empty array.
Definition: perl.cpp:61
Base class for perl variables.
Definition: variable.hpp:20
Scalar newScalar()
Create anonymous empty scalar.
Definition: perl.cpp:51
UV toUV() const
Convert scalar value to unsigned integer.
Definition: scalar.cpp:87
Main class.
Definition: perl.hpp:32
Scalar & setValue(const std::string &value)
Replace scalar value by string.
Definition: scalar.cpp:18
int getId() const
Perl object id.
Definition: perl.cpp:298
Perl exception.
Subroutine reference
Definition: code.hpp:15
bool isScalarRef() const
Check if scalar is a scalar reference.
Definition: scalar.cpp:202
bool isHashRef() const
Check if scalar is a hash reference.
Definition: scalar.cpp:212
Code unrefCode() const
Dereference code (without call)
Definition: scalar.cpp:162
Scalar reference
Definition: scalar.hpp:24
bool toBool() const
Convert scalar value to boolean.
Definition: scalar.cpp:97
bool isArrayRef() const
Check if scalar is an array reference.
Definition: scalar.cpp:207
Array unrefArray() const
Dereference array.
Definition: scalar.cpp:122
Hash newHash()
Create anonymous empty hash.
Definition: perl.cpp:83