[Debian-astro-commits] [gyoto] 171/221: * Value: make sure only the member that was set can be retrieved. * Python: rename Properties to AstrobjProperties
Thibaut Jean-Claude Paumard
thibaut at moszumanska.debian.org
Fri May 22 20:52:44 UTC 2015
This is an automated email from the git hooks/post-receive script.
thibaut pushed a commit to branch master
in repository gyoto.
commit 8557a8fa22832152f0e70629bfada1945f38d440
Author: Thibaut Paumard <paumard at users.sourceforge.net>
Date: Mon Dec 29 12:39:44 2014 +0100
* Value: make sure only the member that was set can be retrieved.
* Python: rename Properties to AstrobjProperties
---
include/GyotoValue.h | 41 ++++++++++++++++++++++++++------
lib/Object.C | 26 ++++++++++++--------
lib/Value.C | 67 +++++++++++++++++-----------------------------------
python/example.py | 12 +++++-----
python/gyoto.i | 20 ++++++++++++++++
yorick/gyoto_utils.C | 20 +++++++++-------
6 files changed, 109 insertions(+), 77 deletions(-)
diff --git a/include/GyotoValue.h b/include/GyotoValue.h
index c8757a1..9a66a6e 100644
--- a/include/GyotoValue.h
+++ b/include/GyotoValue.h
@@ -68,89 +68,116 @@ class Gyoto::Value {
/// Assignement operator
Value& operator=(Value const&);
+ /// Type of this instance
+ int type;
+
+ private:
/// A double value
double Double;
+ public:
/// Construct/cast from double
Value(double);
/// Cast to double
operator double() const;
+ private:
/// A boolean value
bool Bool;
+ public:
/// Construct/cast from boolean
Value(bool);
/// Cast to bool
operator bool() const;
+ private:
/// A long value
long Long;
+ public:
/// Construct/cast from long
Value(long);
/// Cast to long
operator long() const;
+ private:
/// An unsigned long (a.k.a. size_t)
unsigned long ULong;
+ public:
/// Construct/cast from unsigned long
Value(unsigned long);
/// Cast to unsigned long
operator unsigned long() const;
+ private:
/// A string value
std::string String;
+ public:
/// Construct/cast from string
Value(std::string);
/// Cast to string
operator std::string() const;
+ private:
/// A vector of double values
std::vector<double> VDouble;
+ public:
/// Construct/cast from vector of doubles
Value(std::vector<double>);
/// Cast to vector of doubles
operator std::vector<double>() const;
+ private:
/// A vector of unsigned long values
std::vector<unsigned long> VULong;
+ public:
/// Construct/cast from vector of unsigned long values
Value(std::vector<unsigned long>);
/// Cast to vector of unsigned long values
operator std::vector<unsigned long>() const;
+ private:
/// A Metric object
Gyoto::SmartPointer<Gyoto::Metric::Generic> Metric;
- /// Cast from Metric object
+ public:
+ /// Cast from Metric object
Value(Gyoto::SmartPointer<Gyoto::Metric::Generic>);
/// Cast to Metric object
- operator Gyoto::SmartPointer<Gyoto::Metric::Generic>();
+ operator Gyoto::SmartPointer<Gyoto::Metric::Generic>() const;
+ private:
/// An Astrobj Object
- Gyoto::SmartPointer<Gyoto::Astrobj::Generic> Astrobj;
+ Gyoto::SmartPointer<Gyoto::Astrobj::Generic> Astrobj;
+ public:
/// Cast from Astrobj
Value(Gyoto::SmartPointer<Gyoto::Astrobj::Generic>);
/// Cast to Astrobj
- operator Gyoto::SmartPointer<Gyoto::Astrobj::Generic>();
+ operator Gyoto::SmartPointer<Gyoto::Astrobj::Generic>() const;
+ private:
/// A Spectrum object
Gyoto::SmartPointer<Gyoto::Spectrum::Generic> Spectrum;
+ public:
/// Cast from Spectrum
Value(Gyoto::SmartPointer<Gyoto::Spectrum::Generic>);
/// Cast to Spectrum
- operator Gyoto::SmartPointer<Gyoto::Spectrum::Generic>();
+ operator Gyoto::SmartPointer<Gyoto::Spectrum::Generic>() const;
+ private:
/// A Spectrometer object
Gyoto::SmartPointer<Gyoto::Spectrometer::Generic> Spectrometer;
+ public:
/// Cast from Spectrometer
Value(Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>);
/// Cast to Spectrometer
- operator Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>();
+ operator Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>() const;
+ private:
/// A Screen object
Gyoto::SmartPointer<Gyoto::Screen> Screen;
+ public:
/// Cast from Screen
Value(Gyoto::SmartPointer<Gyoto::Screen>);
/// Cast to Screen
- operator Gyoto::SmartPointer<Gyoto::Screen>();
+ operator Gyoto::SmartPointer<Gyoto::Screen>() const;
};
#endif
diff --git a/lib/Object.C b/lib/Object.C
index 8539fdf..bb2dc2f 100644
--- a/lib/Object.C
+++ b/lib/Object.C
@@ -227,10 +227,10 @@ void Object::fillProperty(Gyoto::FactoryMessenger *fmp, Property const &p) const
fmp->setParameter(name, std::string(get(p)));
break;
case Property::vector_double_t:
- fmp->setParameter(name, get(p).VDouble);
+ fmp->setParameter(name, get(p).operator std::vector<double>());
break;
case Property::vector_unsigned_long_t:
- fmp->setParameter(name, get(p).VULong);
+ fmp->setParameter(name, get(p).operator std::vector<unsigned long>());
break;
case Property::metric_t:
fmp->metric(get(p));
@@ -242,16 +242,22 @@ void Object::fillProperty(Gyoto::FactoryMessenger *fmp, Property const &p) const
fmp->screen(get(p));
break;
case Property::spectrum_t:
- if (!get(p).Spectrum) return;
- childfmp = fmp -> makeChild ( name );
- get(p).Spectrum -> fillElement(childfmp);
- delete childfmp;
+ {
+ SmartPointer<Spectrum::Generic> sp=get(p);
+ if (!sp) return;
+ childfmp = fmp -> makeChild ( name );
+ sp -> fillElement(childfmp);
+ delete childfmp;
+ }
break;
case Property::spectrometer_t:
- if (!get(p).Spectrometer) return;
- childfmp = fmp -> makeChild ( name );
- get(p).Spectrometer -> fillElement(childfmp);
- delete childfmp;
+ {
+ SmartPointer<Spectrometer::Generic> spr=get(p);
+ if (!spr) return;
+ childfmp = fmp -> makeChild ( name );
+ spr -> fillElement(childfmp);
+ delete childfmp;
+ }
break;
default:
throwError("Property type unimplemented in Object::fillProperty()");
diff --git a/lib/Value.C b/lib/Value.C
index 0f487b8..d3fbea4 100644
--- a/lib/Value.C
+++ b/lib/Value.C
@@ -4,6 +4,7 @@
#include "GyotoSpectrum.h"
#include "GyotoSpectrometer.h"
#include "GyotoScreen.h"
+#include "GyotoProperty.h"
#include <iostream>
using namespace Gyoto ;
using namespace std ;
@@ -13,54 +14,30 @@ using namespace std ;
Value::Value() {}
Value::~Value() {}
-Value::Value(double val) : Double(val) {}
-Value::operator double() const {return Double;}
-
-Value::Value(bool val) : Bool(val) {}
-Value::operator bool() const {return Bool;}
-
-Value::Value(long val) : Long(val) {}
-Value::operator long() const {return Long;}
-
-Value::Value(unsigned long val) : ULong(val) {}
-Value::operator unsigned long() const {return ULong;}
-
-Value::Value(std::string val) : String(val) {}
-Value::operator std::string() const {return String;}
-
-Value::Value(std::vector<double> val) : VDouble(val) {}
-Value::operator std::vector<double>() const {return VDouble;}
-
-Value::Value(std::vector<unsigned long> val) : VULong(val) {}
-Value::operator std::vector<unsigned long>() const {return VULong;}
-
-Value::Value(Gyoto::SmartPointer<Gyoto::Metric::Generic> p)
- : Metric(p) {}
-Value::operator Gyoto::SmartPointer<Gyoto::Metric::Generic>()
-{ return Metric; }
-
-Value::Value(Gyoto::SmartPointer<Gyoto::Astrobj::Generic> p)
- : Astrobj(p) {}
-Value::operator Gyoto::SmartPointer<Gyoto::Astrobj::Generic>()
-{ return Astrobj; }
-
-Value::Value(Gyoto::SmartPointer<Gyoto::Spectrum::Generic> p)
- : Spectrum(p) {}
-Value::operator Gyoto::SmartPointer<Gyoto::Spectrum::Generic>()
-{ return Spectrum; }
-
-Value::Value(Gyoto::SmartPointer<Gyoto::Spectrometer::Generic> p)
- : Spectrometer(p) {}
-Value::operator Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>()
-{ return Spectrometer; }
-
-Value::Value(Gyoto::SmartPointer<Gyoto::Screen> p)
- : Screen(p) {}
-Value::operator Gyoto::SmartPointer<Gyoto::Screen>()
-{ return Screen; }
+#define ___local_stuff(t, t_t, T) \
+ Value::Value(t val) : type(Property::t_t), T(val){} \
+ Value::operator t() const { \
+ if (type!=Property::t_t) \
+ throwError("This Value does not hold a " #t); \
+ return T; \
+ }
+
+___local_stuff(double, double_t, Double)
+___local_stuff(bool, bool_t, Bool)
+___local_stuff(long, long_t, Long)
+___local_stuff(unsigned long, unsigned_long_t, ULong)
+___local_stuff(std::string, string_t, String)
+___local_stuff(std::vector<double>, vector_double_t, VDouble)
+___local_stuff(std::vector<unsigned long>, vector_unsigned_long_t, VULong)
+___local_stuff(Gyoto::SmartPointer<Gyoto::Metric::Generic>, metric_t, Metric)
+___local_stuff(Gyoto::SmartPointer<Gyoto::Astrobj::Generic>, astrobj_t, Astrobj)
+___local_stuff(Gyoto::SmartPointer<Gyoto::Spectrum::Generic>, spectrum_t, Spectrum)
+___local_stuff(Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>, spectrometer_t, Spectrometer)
+___local_stuff(Gyoto::SmartPointer<Gyoto::Screen>, screen_t, Screen)
Value& Value::operator=(Value const &right) {
# define ___local_case(member) member = right.member
+ ___local_case(type);
___local_case(Double);
___local_case(Bool);
___local_case(Long);
diff --git a/python/example.py b/python/example.py
index bf03fb1..417b72a 100644
--- a/python/example.py
+++ b/python/example.py
@@ -75,7 +75,7 @@ res=sc.screen().resolution()
intensity=numpy.zeros((res, res), dtype=float)
time=numpy.zeros((res, res), dtype=float)
distance=numpy.zeros((res, res), dtype=float)
-aop=gyoto.Properties()
+aop=gyoto.AstrobjProperties()
aop.Intensity(intensity)
aop.EmissionTime(time)
aop.MinDistance(distance)
@@ -104,7 +104,7 @@ ii=gyoto.Range(1, res, 1)
jj=gyoto.Range(1, res, 1)
grid=gyoto.Grid(ii, jj, "\rj = ")
-aop=gyoto.Properties()
+aop=gyoto.AstrobjProperties()
aop.Spectrum(spectrum)
aop.offset=res*res
@@ -145,7 +145,7 @@ grid=gyoto.Grid(ii, jj, "\rj = ")
ipct=numpy.zeros((res, res, 16), dtype=float)
-aop=gyoto.Properties()
+aop=gyoto.AstrobjProperties()
aop.ImpactCoords(ipct)
aop.offset=res*res
@@ -165,7 +165,7 @@ bucket=gyoto.Bucket(a, d)
ipct=numpy.zeros((N, 1, 16), dtype=float)
-aop=gyoto.Properties()
+aop=gyoto.AstrobjProperties()
aop.ImpactCoords(ipct)
aop.offset=N
@@ -185,7 +185,7 @@ bucket=gyoto.Bucket(ii, jj)
ipct=numpy.zeros((res, 1, 16), dtype=float)
-aop=gyoto.Properties()
+aop=gyoto.AstrobjProperties()
aop.ImpactCoords(ipct)
aop.offset=res
@@ -217,7 +217,7 @@ tt=gyoto.AstrobjPtr(tr.__deref__())
# from XML:
p=tt.property("SmallRadius")
tt.set(p, gyoto.Value(0.2))
-tt.get(p).Double == tr.smallRadius()
+tt.get(p).toDouble() == tr.smallRadius()
# Etc:
cplx=gyoto_std.ComplexAstrobj()
diff --git a/python/gyoto.i b/python/gyoto.i
index 32d31f3..d9775b4 100644
--- a/python/gyoto.i
+++ b/python/gyoto.i
@@ -133,6 +133,7 @@ using namespace Gyoto;
%rename(assign) Gyoto::SmartPointer::operator=;
%include "GyotoSmartPointer.h"
+%immutable Gyoto::Value::type;
%rename(assign) Gyoto::Value::operator=;
%rename(toDouble) Gyoto::Value::operator double;
%rename(toLong) Gyoto::Value::operator long;
@@ -358,3 +359,22 @@ public:
Coord1dSet& operator++();
double angle() const ;
};
+
+// Can't work out how to get swig to do this automatically
+%inline {
+enum {
+ Property_double_t=Gyoto::Property::double_t,
+ Property_long_t=Gyoto::Property::long_t,
+ Property_unsigned_long_t=Gyoto::Property::unsigned_long_t,
+ Property_bool_t=Gyoto::Property::bool_t,
+ Property_string_t=Gyoto::Property::string_t,
+ Property_filename_t=Gyoto::Property::filename_t,
+ Property_vector_double_t=Gyoto::Property::vector_double_t,
+ Property_vector_unsigned_long_t=Gyoto::Property::vector_unsigned_long_t,
+ Property_metric_t=Gyoto::Property::metric_t,
+ Property_screen_t=Gyoto::Property::screen_t,
+ Property_astrobj_t=Gyoto::Property::astrobj_t,
+ Property_spectrum_t=Gyoto::Property::spectrum_t,
+ Property_spectrometer_t=Gyoto::Property::spectrometer_t,
+ Property_empty_t=Gyoto::Property::empty_t};
+}
diff --git a/yorick/gyoto_utils.C b/yorick/gyoto_utils.C
index fcd07df..d6b5198 100644
--- a/yorick/gyoto_utils.C
+++ b/yorick/gyoto_utils.C
@@ -415,26 +415,27 @@ void ypush_property(Gyoto::SmartPointer<Gyoto::SmartPointee> ptr,
break;
case Gyoto::Property::vector_unsigned_long_t:
{
- size_t n = val.VULong.size();
+ std::vector<unsigned long> vval = val;
+ size_t n = vval.size();
long dims[]={1, long(n)};
long * buf = ypush_l(dims);
- for (size_t i=0; i<n; ++i) buf[i]=val.VULong[i];
+ for (size_t i=0; i<n; ++i) buf[i]=vval[i];
}
break;
case Gyoto::Property::metric_t:
- *ypush_Metric() = val.Metric;
+ *ypush_Metric() = Gyoto::SmartPointer<Gyoto::Metric::Generic>(val);
break;
case Gyoto::Property::astrobj_t:
- *ypush_Astrobj() = val.Astrobj;
+ *ypush_Astrobj() = Gyoto::SmartPointer<Gyoto::Astrobj::Generic>(val);
break;
case Gyoto::Property::spectrum_t:
- *ypush_Spectrum() = val.Spectrum;
+ *ypush_Spectrum() = Gyoto::SmartPointer<Gyoto::Spectrum::Generic>(val);
break;
case Gyoto::Property::spectrometer_t:
- *ypush_Spectrometer() = val.Spectrometer;
+ *ypush_Spectrometer() = Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>(val);
break;
case Gyoto::Property::screen_t:
- *ypush_Screen() = val.Screen;
+ *ypush_Screen() = Gyoto::SmartPointer<Gyoto::Screen>(val);
break;
default:
y_error("Property type unimplemented in ypush_property()");
@@ -492,8 +493,9 @@ void yget_property(Gyoto::SmartPointer<Gyoto::SmartPointee> ptr,
{
long n;
long *buf = ygeta_l(iarg, &n, NULL);
- val.VULong.resize(n);
- for (size_t i=0; i<n; ++i) val.VULong[i]=buf[i];
+ std::vector<unsigned long> vval=val;
+ vval.resize(n);
+ for (size_t i=0; i<n; ++i) vval[i]=buf[i];
}
break;
case Gyoto::Property::metric_t:
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-astro/packages/gyoto.git
More information about the Debian-astro-commits
mailing list