[Debian-astro-commits] [gyoto] 138/221: New Property type: vector<unsigned long> Documentation in Worldline

Thibaut Jean-Claude Paumard thibaut at moszumanska.debian.org
Fri May 22 20:52:40 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 478139baf50b295eeb6265673b2c3496c82ef725
Author: Thibaut Paumard <paumard at users.sourceforge.net>
Date:   Wed Dec 10 15:33:10 2014 +0100

    New Property type: vector<unsigned long>
    Documentation in Worldline
---
 include/GyotoFactory.h          |  4 ++++
 include/GyotoFactoryMessenger.h | 10 ++++++++
 include/GyotoObject.h           | 13 +++++++++++
 include/GyotoProperty.h         | 34 ++++++++++++++++++++++++++-
 include/GyotoStar.h             |  4 +++-
 include/GyotoValue.h            |  7 ++++++
 include/GyotoWorldline.h        | 31 ++++++++++++++-----------
 lib/Factory.C                   | 51 +++++++++++++++++++++++++++++++++++++++++
 lib/FixedStar.C                 |  1 +
 lib/Object.C                    | 21 +++++++++++++++++
 lib/Property.C                  |  8 +++++++
 lib/Star.C                      |  3 ---
 lib/Value.C                     |  4 ++++
 yorick/gyoto_utils.C            | 16 +++++++++++++
 yorick/ygyoto_private.h         |  3 ++-
 15 files changed, 191 insertions(+), 19 deletions(-)

diff --git a/include/GyotoFactory.h b/include/GyotoFactory.h
index aa0d018..b694815 100644
--- a/include/GyotoFactory.h
+++ b/include/GyotoFactory.h
@@ -397,6 +397,10 @@ class Gyoto::Factory
 		    xercesc::DOMElement* pel,
 		    FactoryMessenger **child = NULL);
 
+  void setParameter(std::string name, std::vector<unsigned long> const &val,
+		    xercesc::DOMElement* pel,
+		    FactoryMessenger **child = NULL);
+
   /// Transform relative path into absolute path.
   /**
    * relpath is interpreted as follows:
diff --git a/include/GyotoFactoryMessenger.h b/include/GyotoFactoryMessenger.h
index c9826c6..8955150 100644
--- a/include/GyotoFactoryMessenger.h
+++ b/include/GyotoFactoryMessenger.h
@@ -395,6 +395,8 @@ class Gyoto::FactoryMessenger {
 
   void setParameter(std::string name, std::vector<double> const &val,
 		    FactoryMessenger** child= NULL);
+  void setParameter(std::string name, std::vector<unsigned long> const &val,
+		    FactoryMessenger** child= NULL);
   ///< Output a vector of parameters
 
   /**
@@ -477,6 +479,14 @@ class Gyoto::FactoryMessenger {
    * std::vector<double>.
    */
   static std::vector<double> parseArray(std::string src);
+
+  /**
+   * \brief Parse string into array
+   *
+   * Parse tokens from string src, returns them into a
+   * std::vector<unsigned long>.
+   */
+  static std::vector<unsigned long> parseArrayULong(std::string src);
 };
 
 #endif
diff --git a/include/GyotoObject.h b/include/GyotoObject.h
index dc7faeb..e327b5a 100644
--- a/include/GyotoObject.h
+++ b/include/GyotoObject.h
@@ -37,6 +37,19 @@ namespace Gyoto {
   class FactoryMessenger;
 }
 
+/// Declare a pair of accessors to scalar member in a class declaration
+/**
+ * The accessors must also be defined in the .C file, which can be
+ * done using #GYOTO_PROPERTY_SCALAR_ACCESSORS
+ *
+ * \param type data type of the memebr beeing accessed. Any scalar
+ *        type (double, long, size_t, SmartPointer<Metric::Generic> etc).
+ * \param method name of the accessors.
+ */
+#define GYOTO_OBJECT_ACCESSORS(type, method)				\
+  void method(type);							\
+  type method() const;
+
 /// Declare  class::properties and class::getProperties()
 /**
  * Any derived class that does define Properties (i.e. the macro
diff --git a/include/GyotoProperty.h b/include/GyotoProperty.h
index cfba481..bbc9835 100644
--- a/include/GyotoProperty.h
+++ b/include/GyotoProperty.h
@@ -41,6 +41,16 @@ namespace Gyoto {
   template <class T> class SmartPointer;
 }
 
+
+/// Define a pair of accessors to scalar member (double, long, size_t)
+/**
+ * Accessors must also be declared in the class declaration, which can
+ * be done using #GYOTO_OBJECT_SCALAR_ACCESSORS.
+ */
+#define GYOTO_PROPERTY_ACCESSORS(class, type, member, method)	\
+  void class::method(type v) {member=v;}				\
+  type class::method() const {return member;}
+
 /// Start Property list
 /**
  * \param class Class for which we are defining a Property list
@@ -130,6 +140,13 @@ namespace Gyoto {
    (Gyoto::Property::set_vector_double_unit_t)&class::fname,		\
    (Gyoto::Property::get_vector_double_unit_t)&class::fname),
 
+/// Define a Property of type vector<unsigned long>
+#define GYOTO_PROPERTY_VECTOR_UNSIGNED_LONG(class, name, fname)		\
+  Gyoto::Property							\
+  (#name,								\
+   (Gyoto::Property::set_vector_unsigned_long_t)&class::fname,		\
+   (Gyoto::Property::get_vector_unsigned_long_t)&class::fname),
+
 /// Define a Property of type Gyoto::Metric::Generic
 #define GYOTO_PROPERTY_METRIC(class, name, fname)			\
   Gyoto::Property							\
@@ -212,6 +229,8 @@ namespace Gyoto {
  *   - bool: see #GYOTO_PROPERTY_BOOL;
  *   - std::vector<double>: see #GYOTO_PROPERTY_VECTOR_DOUBLE
  *     and #GYOTO_PROPERTY_VECTOR_DOUBLE_UNIT;
+ *   - std::vector<unsigned long>:
+ *     see #GYOTO_PROPERTY_VECTOR_UNSIGNED_LONG;
  *   - Gyoto::SmartPointers to various base classes: Screen,
  *     Metric::Generic, Astrobj::Generic, Spectrum::Generic and
  *     Spectrometer::Generic. See #GYOTO_PROPERTY_METRIC,
@@ -226,7 +245,7 @@ namespace Gyoto {
  *
  * The type used in these accessors may not be the same as the type of
  * the underlying class member. For instance, to read an array, it was
- * chosen to use the std::vector<double> type because it is easy to
+ * chosen to use the std::vector<type> type because it is easy to
  * read such a vector from XML and to thus determine dynamically the
  * number of elements provided. But this type is slow, so it is
  * expected that the class member will rather be a C-style array
@@ -295,6 +314,8 @@ class Gyoto::Property
     filename_t,
     /// Type is std::vector<double>
     vector_double_t,
+    /// Type is std::vector<unsigned long>
+    vector_unsigned_long_t,
     /// Type is Gyoto::SmartPointer<Gyoto::Metric::Generic>
     metric_t,
     /// Type is Gyoto::SmartPointer<Gyoto::Screen::Generic>
@@ -360,6 +381,10 @@ class Gyoto::Property
   typedef void (Object::* set_vector_double_unit_t)(std::vector<double> const&, std::string const &);
   /// Prototype for an accessor to get a std::vector<double>, with unit
   typedef std::vector<double> (Object::* get_vector_double_unit_t)(std::string const &) const;
+  /// Prototype for an accessor to set a std::vector<unsigned long>
+  typedef void (Object::* set_vector_unsigned_long_t)(std::vector<unsigned long> const&);
+  /// Prototype for an accessor to get a std::vector<unsigned long>
+  typedef std::vector<unsigned long> (Object::* get_vector_unsigned_long_t)() const;
 
   /// Prototype for an accessor to set a Gyoto::SmartPointer<Gyoto::Metric::Generic>
   typedef void (Object::* set_metric_t)
@@ -407,6 +432,7 @@ class Gyoto::Property
     set_bool_t set_bool;
     set_string_t set_string;
     set_vector_double_t set_vdouble;
+    set_vector_unsigned_long_t set_vulong;
     set_metric_t set_metric;
     set_screen_t set_screen;
     set_astrobj_t set_astrobj;
@@ -424,6 +450,7 @@ class Gyoto::Property
     get_bool_t get_bool;
     get_string_t get_string;
     get_vector_double_t get_vdouble;
+    get_vector_unsigned_long_t get_vulong;
     get_metric_t get_metric;
     get_screen_t get_screen;
     get_astrobj_t get_astrobj;
@@ -526,6 +553,11 @@ class Gyoto::Property
 	   set_vector_double_unit_t set_vdouble_unit,
 	   get_vector_double_unit_t get_vdouble_unit);
 
+  /// Constructor for #type==#vector_unsigned_long_t
+  Property(std::string name,
+	   set_vector_unsigned_long_t set_vulong,
+	   get_vector_unsigned_long_t get_vulong);
+
   /// Constructor for #type==#metric_t
   Property(std::string name,
 	   set_metric_t set_metric,
diff --git a/include/GyotoStar.h b/include/GyotoStar.h
index d33433a..6686d62 100644
--- a/include/GyotoStar.h
+++ b/include/GyotoStar.h
@@ -162,9 +162,11 @@ class Gyoto::Astrobj::Star :
   // Overload to dispatch InitCoord into Position and Velocity
   // for massive particle
   virtual void fillProperty(Gyoto::FactoryMessenger *fmp, Property const &p) const ;
-  virtual void fillElement(Gyoto::FactoryMessenger *fmp) const ;
 #endif
+  // Declare the wrappers around the Worldline accessors
   GYOTO_WORLDLINE;
+  // Both base classes have deltaMax methods: we need to explicitly
+  // use both
   using Gyoto::Worldline::deltaMax;
   using Gyoto::Astrobj::UniformSphere::deltaMax;
 
diff --git a/include/GyotoValue.h b/include/GyotoValue.h
index 46e0447..c8757a1 100644
--- a/include/GyotoValue.h
+++ b/include/GyotoValue.h
@@ -110,6 +110,13 @@ class Gyoto::Value {
   /// Cast to vector of doubles
   operator std::vector<double>() const;
 
+  /// A vector of unsigned long values
+  std::vector<unsigned long> VULong;
+  /// 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;
+
   /// A Metric object
   Gyoto::SmartPointer<Gyoto::Metric::Generic> Metric;
   /// Cast from Metric object
diff --git a/include/GyotoWorldline.h b/include/GyotoWorldline.h
index c41141a..5776dac 100644
--- a/include/GyotoWorldline.h
+++ b/include/GyotoWorldline.h
@@ -175,19 +175,24 @@ namespace Gyoto {
  * extended as needed using xExpand(). These coordinates can be
  * retrieved using get_t(), get_xyz(), getCartesian(), getCoord() etc.
  *
- * Supported XML parameters:
- *  - InitialCoordinate or InitCoord: 8-element vector yielding the initial
- *    4-position and 4-velocity;
- *  - Only for massive particle (Gyoto::Astrobj::Star): Position
- *    (yielding initial 4-position) and Velocity (yielding initial
- *    3-velocity);
- *  - Delta: integration step, initial in case or adaptive step;
- *  - Adaptive or NonAdaptive: sets whether integration step should be
- *    adaptive; default: Adaptive.;
- *  - MaxIter: maximum number of iterations for the integration;
- *    default: 100000.
- *  - DeltaMin, DeltaMax, DeltaMaxOverR, AbsTol, RelTol: tuning
- *    parameters used by some of the integrators.
+ * Worldline does not derive from Object, and does not instanciate a
+ * Property list. This is because this would lead to multiple
+ * inheritance of the Object base in derived classes. Instead,
+ * #GyotoWorldline.h provides a few macros that can be used to include
+ * the Worldline properties in a derived classe's Property list:
+ *   - #GYOTO_WORLDLINE is to be used in a public section of the
+ *     derived class declaration (.h file); it declares wrappers
+ *     around the Worldline property accessors;
+ *   - #GYOTO_WORLDLINE_ACCESSORS is to be used with the class
+ *     definition (.C file; it defines the accessors declared by
+ *     #GYOTO_WORLDLINE;
+ *   - #GYOTO_WORLDLINE_PROPERTIES declares the Properties that use
+ *     these accessors. It must be used like
+ *     e.g. #GYOTO_PROPERTY_DOUBLE, between #GYOTO_PROPERTY_START andf
+ *     #GYOTO_PROPERTY_END.
+ *   - Finally, #GYOTO_WORLDLINE_PROPERTY_END is a drop-in replacement
+ *     for #GYOTO_PROPERTY_END that calls #GYOTO_WORLDLINE_PROPERTIES
+ *     and #GYOTO_WORLDLINE_ACCESSORS.
  * 
  */
 class Gyoto::Worldline
diff --git a/lib/Factory.C b/lib/Factory.C
index f3d3117..e15c37c 100644
--- a/lib/Factory.C
+++ b/lib/Factory.C
@@ -27,6 +27,7 @@
 #include <string>
 #include <libgen.h>
 #include <unistd.h>
+#include <cstdlib>
 
 #include "GyotoMetric.h"
 #include "GyotoAstrobj.h"
@@ -785,6 +786,27 @@ void Factory::setParameter(std::string name,
 
 }
 
+void Factory::setParameter(std::string name,
+			   std::vector<unsigned long> const &val,
+			   DOMElement *pel, FactoryMessenger **child){
+
+  DOMElement*  el = doc_->createElement(X(name.c_str()));
+  pel -> appendChild(el);
+
+  size_t n=val.size();
+
+  if (n) {
+    ostringstream ss;
+    ss << val[0];
+    for (size_t i=1; i<n; ++i) {
+      ss << " " << val[i];
+    }
+    el->appendChild( doc_->createTextNode(X(ss.str().c_str())) );
+  }
+  if (child) *child = new FactoryMessenger(this, el);
+
+}
+
 void Factory::setContent(std::string content, DOMElement *el) {
   el -> appendChild( doc_->createTextNode( X( content.c_str() ) ) );
 }
@@ -955,6 +977,12 @@ void FactoryMessenger::setParameter(std::string name,
   employer_ -> setParameter(name, val, element_, child);
 }
 
+void FactoryMessenger::setParameter(std::string name,
+				    std::vector<unsigned long> const &val,
+				    FactoryMessenger **child){
+  employer_ -> setParameter(name, val, element_, child);
+}
+
 size_t FactoryMessenger::parseArray(std::string content, double val[], size_t max_tokens)
 {
   char const * const delim = " \t\n" ;
@@ -1002,6 +1030,29 @@ std::vector<double> FactoryMessenger::parseArray(std::string content)
   return result;
 }
 
+std::vector<unsigned long> FactoryMessenger::parseArrayULong(std::string content)
+{
+  std::vector<unsigned long> result;
+  char const * const delim = " \t\n" ;
+  char const * const c_content=content.c_str();
+  size_t len=strlen(c_content);
+  if (len==0) return result;
+
+  size_t n=0;
+  char * const tc = new char[len+1];
+  memcpy(tc, c_content, len+1);
+  char * sub = strtok(tc, delim);
+
+  while (sub) {
+    result.push_back(atol(sub));
+    sub = strtok(NULL, delim);
+  }
+  
+  delete [] tc;
+
+  return result;
+}
+
 std::string FactoryMessenger::fullPath(std::string fname) {
   return employer_ -> fullPath(fname);
 }
diff --git a/lib/FixedStar.C b/lib/FixedStar.C
index a4c3829..3483f2e 100644
--- a/lib/FixedStar.C
+++ b/lib/FixedStar.C
@@ -174,6 +174,7 @@ void FixedStar::setPos(const double p[3])
 { for (int i=0; i<3; ++i) pos_[i]=p[i]; radius(radius_);}
 
 void FixedStar::position(std::vector<double> const &v) {
+  GYOTO_DEBUG_EXPR(v.size());
   if (v.size() !=3)
     throwError("FixedStar position needs exactly 3 tokens"); 
   for (int i=0; i<3; ++i) pos_[i]=v[i];
diff --git a/lib/Object.C b/lib/Object.C
index 661fd34..8539fdf 100644
--- a/lib/Object.C
+++ b/lib/Object.C
@@ -102,6 +102,13 @@ void Object::set(Property const &p, Value val) {
       (this->*set)(val);
     }
     break;
+  case Property::vector_unsigned_long_t:
+    {
+      Property::set_vector_unsigned_long_t set = p.setter.set_vulong;
+      if (!set) throwError("Can't set this Property");
+      (this->*set)(val);
+    }
+    break;
     ___local_case(metric);
     ___local_case(astrobj);
     ___local_case(spectrum);
@@ -163,6 +170,13 @@ Value Object::get(Property const &p) const {
       val = (this->*get)();
     }
     break;
+  case Property::vector_unsigned_long_t:
+    {
+      Property::get_vector_unsigned_long_t get = p.getter.get_vulong;
+      if (!get) throwError("Can't get this Property");
+      val = (this->*get)();
+    }
+    break;
     ___local_case(metric);
     ___local_case(astrobj);
     ___local_case(spectrum);
@@ -179,6 +193,7 @@ Property const * Object::property(std::string const pname) const {
   Property const * prop = getProperties(); 
   while (prop) {
     if (*prop) {
+      GYOTO_DEBUG_EXPR(prop->name);
       if (prop->name == pname ||
 	  (prop->type==Property::bool_t && prop->name_false == pname))
 	return prop;
@@ -214,6 +229,9 @@ void Object::fillProperty(Gyoto::FactoryMessenger *fmp, Property const &p) const
   case Property::vector_double_t:
     fmp->setParameter(name, get(p).VDouble);
     break;
+  case Property::vector_unsigned_long_t:
+    fmp->setParameter(name, get(p).VULong);
+    break;
   case Property::metric_t:
     fmp->metric(get(p));
     break;
@@ -327,6 +345,9 @@ void Object::setParameter(Property const &p, string const &name,
     val = FactoryMessenger::parseArray(content);
     set(p, val, unit);
     return;
+  case Property::vector_unsigned_long_t:
+    val = FactoryMessenger::parseArrayULong(content);
+    break;
   case Property::metric_t:
     throwError("Metric can't be set using setParameter()");
   default:
diff --git a/lib/Property.C b/lib/Property.C
index 720f5b1..0e91cf1 100644
--- a/lib/Property.C
+++ b/lib/Property.C
@@ -75,4 +75,12 @@ Property::Property(string n,
   getter_unit.get_vdouble=getu;
 }
 
+Property::Property(string n,
+		   set_vector_unsigned_long_t set,
+		   get_vector_unsigned_long_t get)
+  : name(n), type(vector_unsigned_long_t), parent(NULL) {
+  setter.set_vulong=set;
+  getter.get_vulong=get;
+}
+
 Property::operator bool() const { return type != empty_t; }
diff --git a/lib/Star.C b/lib/Star.C
index 14b0105..7789063 100644
--- a/lib/Star.C
+++ b/lib/Star.C
@@ -179,7 +179,4 @@ void Star::setParameters(FactoryMessenger* fmp) {
 	       "Velocity was found but not Position");
   }
 }
-void Star::fillElement(Gyoto::FactoryMessenger *fmp) const {
-  UniformSphere::fillElement(fmp);
-}
 #endif
diff --git a/lib/Value.C b/lib/Value.C
index ecd421c..0f487b8 100644
--- a/lib/Value.C
+++ b/lib/Value.C
@@ -31,6 +31,9 @@ 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>()
@@ -64,6 +67,7 @@ Value& Value::operator=(Value const &right) {
   ___local_case(ULong);
   ___local_case(String);
   ___local_case(VDouble);
+  ___local_case(VULong);
   ___local_case(Metric);
   ___local_case(Astrobj);
   ___local_case(Spectrum);
diff --git a/yorick/gyoto_utils.C b/yorick/gyoto_utils.C
index 2e24ce8..15beec0 100644
--- a/yorick/gyoto_utils.C
+++ b/yorick/gyoto_utils.C
@@ -409,6 +409,14 @@ void ypush_property(Gyoto::SmartPointer<Gyoto::SmartPointee> ptr,
       for (size_t i=0; i<n; ++i) buf[i]=vval[i];
     }
     break;
+  case Gyoto::Property::vector_unsigned_long_t:
+    {
+      size_t n = val.VULong.size();
+      long dims[]={1, long(n)};
+      long * buf = ypush_l(dims);
+      for (size_t i=0; i<n; ++i) buf[i]=val.VULong[i];
+    }
+    break;
   case Gyoto::Property::metric_t:
     *ypush_Metric() = val.Metric;
     break;
@@ -476,6 +484,14 @@ void yget_property(Gyoto::SmartPointer<Gyoto::SmartPointee> ptr,
       object->set(p, vval, unit);
     }
     return;
+  case Gyoto::Property::vector_unsigned_long_t:
+    {
+      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];
+    }
+    break;
   case Gyoto::Property::metric_t:
     val = *yget_Metric(iarg);
     break;
diff --git a/yorick/ygyoto_private.h b/yorick/ygyoto_private.h
index 6c9da3f..713bb10 100644
--- a/yorick/ygyoto_private.h
+++ b/yorick/ygyoto_private.h
@@ -94,9 +94,10 @@ long int __ygyoto_var_idx(long id);
     }									\
     void gyoto_##NAME##_eval(void *obj, int argc);			\
     void gyoto_##NAME##_closure_eval(void *obj, int argc) {		\
-      Property const * prop = \
+      Property const * prop =						\
 	((Gyoto::Object*)((gyoto_##NAME##_closure*)obj)->smptr())	\
 	->property(((gyoto_##NAME##_closure*)obj)->member);		\
+      GYOTO_DEBUG_EXPR(prop);						\
 	if (prop) {							\
 	  std::string unit="";						\
 	  std::string kwd="";						\

-- 
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