[Debian-astro-commits] [gyoto] 141/221: More macros in Object a, Property to easily declare and define accessors.

Thibaut Jean-Claude Paumard thibaut at moszumanska.debian.org
Fri May 22 20:52:41 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 0f553cc2e466d78a7b2cfcb9753ef8003848b38c
Author: Thibaut Paumard <paumard at users.sourceforge.net>
Date:   Thu Dec 11 12:59:05 2014 +0100

    More macros in Object a, Property to easily declare and define accessors.
---
 include/GyotoObject.h   |  27 +++++++++--
 include/GyotoProperty.h |  20 ++++++++
 lib/Star.C              | 125 +++++++++++++++++++++++++-----------------------
 3 files changed, 110 insertions(+), 62 deletions(-)

diff --git a/include/GyotoObject.h b/include/GyotoObject.h
index e327b5a..7fba758 100644
--- a/include/GyotoObject.h
+++ b/include/GyotoObject.h
@@ -37,18 +37,39 @@ namespace Gyoto {
   class FactoryMessenger;
 }
 
+/// Declare a pair of accessors to string member in a class declaration
+/**
+ * The accessors must also be defined in the .C file
+ *
+ * \param method name of the accessors.
+ */
+#define GYOTO_OBJECT_ACCESSORS_STRING(method)				\
+  virtual void method(std::string const&);				\
+  virtual std::string method() const;
+
 /// 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
+ * done using #GYOTO_PROPERTY_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;
+  virtual void method(type);						\
+  virtual type method() const;
+
+/// Declare a quadruplet of accessors to double member that supports unit
+/**
+ * The accessors must also be defined in the .C file.
+ *
+ * \param method name of the accessors.
+ */
+#define GYOTO_OBJECT_ACCESSORS_UNIT(method)				\
+  GYOTO_OBJECT_ACCESSORS(double, method)				\
+  virtual void method(double, std::string const &);			\
+  virtual double method(std::string const &) const;
 
 /// Declare  class::properties and class::getProperties()
 /**
diff --git a/include/GyotoProperty.h b/include/GyotoProperty.h
index bbc9835..a28719f 100644
--- a/include/GyotoProperty.h
+++ b/include/GyotoProperty.h
@@ -51,6 +51,26 @@ namespace Gyoto {
   void class::method(type v) {member=v;}				\
   type class::method() const {return member;}
 
+/// Define 4 accessors to double scalar member in geometrical units
+/**
+ * Accessors must also be declared in the class declaration, which can
+ * be done using #GYOTO_OBJECT_ACCESSORS_UNIT.
+ *
+ * \param class class name
+ * \param member member holding the value in geometrical unit
+ * \param method name for accessors
+ * \metric member or expression yielding metric (which defines the
+ * geometrical unit)
+ */
+#define GYOTO_PROPERTY_ACCESSORS_GEOMETRICAL(class, member, method, metric) \
+  GYOTO_PROPERTY_ACCESSORS(class, double, member, method)		\
+  void class::method(double v, std::string const &u) {			\
+    member=Units::ToGeometrical(v, u, metric);				\
+  }									\
+  double class::method(std::string const &u) const {			\
+    return Units::FromGeometrical(member, u, metric);			\
+  }
+
 /// Start Property list
 /**
  * \param class Class for which we are defining a Property list
diff --git a/lib/Star.C b/lib/Star.C
index 7789063..0c6934b 100644
--- a/lib/Star.C
+++ b/lib/Star.C
@@ -38,9 +38,75 @@ using namespace std;
 using namespace Gyoto;
 using namespace Gyoto::Astrobj;
 
+/// Properties
 GYOTO_PROPERTY_START(Star)
+// Star only need to implement the Worldline interface on top of the 
+// UniformSphere interface, which is trivially tone with this macro:
 GYOTO_WORLDLINE_PROPERTY_END(Star, UniformSphere::properties)
 
+// XML I/O
+// We also need to parse and write Position+Velocity in addition to
+// InitCoord, which is done by overriding setParameter(), setParameters()
+// and fillProperty()
+int Star::setParameter(std::string name,
+			    std::string content,
+			    std::string unit) {
+  double coord[8];
+  char* tc = const_cast<char*>(content.c_str());
+  if (name=="InitialCoordinate") {
+    name=="InitCoord";
+    return UniformSphere::setParameter(name, content, unit);
+  } else if (name=="Position") {
+    if (FactoryMessenger::parseArray(content, coord, 4) != 4)
+      throwError("Worldline \"Position\" requires exactly 4 tokens");
+    if (init_vel_) {
+      setInitCoord(coord, init_vel_);
+      delete[] init_vel_; init_vel_=NULL;
+    } else setPosition(coord);
+    wait_pos_ = 0;
+  } else if (name=="Velocity") {
+    if (FactoryMessenger::parseArray(content, coord, 3) != 3)
+      throwError("Worldline \"Velocity\" requires exactly 3 tokens");
+    if (wait_pos_) {
+      if (init_vel_) delete [] init_vel_;
+      init_vel_ = new double[3];
+      memcpy(init_vel_, coord, 3*sizeof(double));
+    } else setVelocity(coord);
+  }
+  else return UniformSphere::setParameter(name, content, unit);
+  return 0;
+}
+
+#ifdef GYOTO_USE_XERCES
+void Star::fillProperty(Gyoto::FactoryMessenger *fmp, Property const &p) const {
+  if (p.name == "InitCoord") {
+    if (imin_ <= imax_) {
+      double coord[8];
+      getInitialCoord(coord);
+      // For massive particule, express initial condition with 3-velocity
+      double vel[3] = {coord[5]/coord[4], coord[6]/coord[4], coord[7]/coord[4]};
+      fmp -> setParameter ("Position", coord, 4);
+      fmp -> setParameter ("Velocity", vel, 3);
+    }
+    return;
+  }
+  UniformSphere::fillProperty(fmp, p);
+}
+
+void Star::setParameters(FactoryMessenger* fmp) {
+  wait_pos_ = 1;
+  metric(fmp->metric());
+  UniformSphere::setParameters(fmp);
+  wait_pos_ = 0;
+  if (init_vel_) {
+    delete[] init_vel_; init_vel_=NULL;
+    throwError("Worldline::setParameters(): "
+	       "Velocity was found but not Position");
+  }
+}
+#endif
+///
+
 Star::Star() :
   UniformSphere("Star"),
   Worldline()
@@ -121,62 +187,3 @@ double Star::rMax() {
   }
   return rmax_;
 }
-
-int Star::setParameter(std::string name,
-			    std::string content,
-			    std::string unit) {
-  double coord[8];
-  char* tc = const_cast<char*>(content.c_str());
-  if (name=="InitialCoordinate") {
-    name=="InitCoord";
-    return UniformSphere::setParameter(name, content, unit);
-  } else if (name=="Position") {
-    if (FactoryMessenger::parseArray(content, coord, 4) != 4)
-      throwError("Worldline \"Position\" requires exactly 4 tokens");
-    if (init_vel_) {
-      setInitCoord(coord, init_vel_);
-      delete[] init_vel_; init_vel_=NULL;
-    } else setPosition(coord);
-    wait_pos_ = 0;
-  } else if (name=="Velocity") {
-    if (FactoryMessenger::parseArray(content, coord, 3) != 3)
-      throwError("Worldline \"Velocity\" requires exactly 3 tokens");
-    if (wait_pos_) {
-      if (init_vel_) delete [] init_vel_;
-      init_vel_ = new double[3];
-      memcpy(init_vel_, coord, 3*sizeof(double));
-    } else setVelocity(coord);
-  }
-  else return UniformSphere::setParameter(name, content, unit);
-  return 0;
-}
-
-#ifdef GYOTO_USE_XERCES
-
-void Star::fillProperty(Gyoto::FactoryMessenger *fmp, Property const &p) const {
-  if (p.name == "InitCoord") {
-    if (imin_ <= imax_) {
-      double coord[8];
-      getInitialCoord(coord);
-      // For massive particule, express initial condition with 3-velocity
-      double vel[3] = {coord[5]/coord[4], coord[6]/coord[4], coord[7]/coord[4]};
-      fmp -> setParameter ("Position", coord, 4);
-      fmp -> setParameter ("Velocity", vel, 3);
-    }
-    return;
-  }
-  UniformSphere::fillProperty(fmp, p);
-}
-
-void Star::setParameters(FactoryMessenger* fmp) {
-  wait_pos_ = 1;
-  metric(fmp->metric());
-  UniformSphere::setParameters(fmp);
-  wait_pos_ = 0;
-  if (init_vel_) {
-    delete[] init_vel_; init_vel_=NULL;
-    throwError("Worldline::setParameters(): "
-	       "Velocity was found but not Position");
-  }
-}
-#endif

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