getfem-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Getfem-commits] r5167 - in /trunk/getfem/interface/src: getfemint.cc ge


From: Yves . Renard
Subject: [Getfem-commits] r5167 - in /trunk/getfem/interface/src: getfemint.cc getfemint.h getfemint_workspace.cc getfemint_workspace.h
Date: Tue, 08 Dec 2015 09:55:41 -0000

Author: renard
Date: Tue Dec  8 10:55:39 2015
New Revision: 5167

URL: http://svn.gna.org/viewcvs/getfem?rev=5167&view=rev
Log:
work in progress

Modified:
    trunk/getfem/interface/src/getfemint.cc
    trunk/getfem/interface/src/getfemint.h
    trunk/getfem/interface/src/getfemint_workspace.cc
    trunk/getfem/interface/src/getfemint_workspace.h

Modified: trunk/getfem/interface/src/getfemint.cc
URL: 
http://svn.gna.org/viewcvs/getfem/trunk/getfem/interface/src/getfemint.cc?rev=5167&r1=5166&r2=5167&view=diff
==============================================================================
--- trunk/getfem/interface/src/getfemint.cc     (original)
+++ trunk/getfem/interface/src/getfemint.cc     Tue Dec  8 10:55:39 2015
@@ -55,7 +55,7 @@
 #  include <signal.h>
 # endif
 //#endif
-#include <exception> // NE PAS METTRE CE FICHIER EN PREMIER !!!! ou bien 
ennuis avec dec cxx 6.3 garantis
+#include <exception>
 
 namespace getfemint {
 

Modified: trunk/getfem/interface/src/getfemint.h
URL: 
http://svn.gna.org/viewcvs/getfem/trunk/getfem/interface/src/getfemint.h?rev=5167&r1=5166&r2=5167&view=diff
==============================================================================
--- trunk/getfem/interface/src/getfemint.h      (original)
+++ trunk/getfem/interface/src/getfemint.h      Tue Dec  8 10:55:39 2015
@@ -118,22 +118,9 @@
     default :                       return "not_a_getfem_class";
     }
   }
-
-
-  // à gerer avec un singleton ...
-
-
-  // void gfi_add_stored_object(pstatic_stored_object o, void *struct_pointer);
-  // void gfi_add_dependency(void *ptr1, void *ptr2);
-  // void gfi_del_dependency(void *ptr1, void *ptr2);
-  // void del_stored_object(void *ptr);
-
-
   
 
- 
-
-  
+
 
 
 

Modified: trunk/getfem/interface/src/getfemint_workspace.cc
URL: 
http://svn.gna.org/viewcvs/getfem/trunk/getfem/interface/src/getfemint_workspace.cc?rev=5167&r1=5166&r2=5167&view=diff
==============================================================================
--- trunk/getfem/interface/src/getfemint_workspace.cc   (original)
+++ trunk/getfem/interface/src/getfemint_workspace.cc   Tue Dec  8 10:55:39 2015
@@ -25,8 +25,8 @@
 #include <getfem/bgeot_config.h>
 #include <getfemint_workspace.h>
 
-namespace getfemint
-{
+namespace getfemint {
+
   workspace_stack& workspace() {
     return dal::singleton<workspace_stack>::instance();
   }
@@ -83,11 +83,6 @@
     if (o->is_static() && o->ikey == 0) { THROW_ERROR("o->is_static() && 
o->ikey == 0"); }
     if (o->is_anonymous()) {
       o->set_workspace(current_workspace);
-      //if (o->ikey) kmap[o->ikey] = o;// would it be necessary??
-      //std::cout << "o->ikey       : " << o->ikey << std::endl;
-      //std::cout << "o             : " << o << std::endl;
-      //std::cout << "kmap[o->ikey] : " << kmap[o->ikey] << std::endl;
-      //newly_created_objects.push_back(id); // would it be necessary??
     } // what do you do if o is not anonymous?
   }
 
@@ -247,4 +242,140 @@
     }
   }
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+  workspace2_stack& workspace2() {
+    return dal::singleton<workspace2_stack>::instance();
+  }
+
+  /* deletes the current workspace2 and returns to the parent workspace2 */
+  void workspace2_stack::pop_workspace2(bool keep_all) {
+    if (wrk.size() == 1) THROW_ERROR("You cannot pop the main workspace\n");
+    if (keep_all) send_all_objects_to_parent_workspace2();
+    else clear_workspace2();
+    wrk.pop_back();
+  }
+
+  /* inserts a new object (and gives it an id) */
+  id_type workspace2_stack::push_object(const dal::pstatic_stored_object &p,
+                                       void *raw_pointer,
+                                       getfemint_class_id class_id) {
+    id_type obj_id = id_type(valid_objects.first_false());
+    valid_objects.add(obj_id);
+    if (obj_id >= obj.size()) obj.push_back(object_info());
+    
+    object_info &o = obj[obj_id];
+    o.p = p;
+    o.raw_pointer = raw_pointer;
+    o.workspace = get_current_workspace2();
+    o.class_id = class_id;
+    o.used_by.clear();
+
+    kmap[raw_pointer] = obj_id;
+    newly_created_objects.push_back(obj_id);
+    return obj_id;
+  }
+
+  void workspace2_stack::set_dependance(id_type user, id_type used) {
+    if (!(valid_objects.is_in(user)) || !(valid_objects.is_in(used)))
+      THROW_ERROR("Invalid objects\n");
+    auto &u = obj[used].used_by;
+    auto &p = obj[user].p;
+    for (auto it = u.begin(); it != u.end(); ++it)
+      if (it->get() == p.get()) return;
+    u.push_back(p);
+  }
+
+  void workspace2_stack::sup_dependance(id_type user, id_type used) {
+    if (!(valid_objects.is_in(user)) || !(valid_objects.is_in(used)))
+      THROW_ERROR("Invalid objects\n");
+    auto &u = obj[used].used_by;
+    auto &p = obj[user].p;
+    size_type i = 0, j = 0;
+    for ( ; i < u.size(); ++i)
+      { u[j] = u[i]; if (u[i].get() != p.get()) ++j; }
+    u.resize(j);
+  }
+
+  void workspace2_stack::delete_object(id_type id) {
+    if (valid_objects[id]) {
+      valid_objects.sup(id);
+      kmap.erase(obj[id].raw_pointer);
+      obj[id] = object_info();
+    } else {
+      std::stringstream s;
+      s << "Object number " << id << " no longer exists : can't delete it";
+      throw getfemint_error(s.str());
+    }
+  }
+
+  void workspace2_stack::send_object_to_parent_workspace2(id_type id) {
+    if (get_current_workspace2() == 0) THROW_ERROR("Invalid operation\n");
+    if (!(valid_objects.is_in(id))) THROW_ERROR("Invalid objects\n");
+    auto &o = obj[id];
+    o.workspace = id_type(get_current_workspace2() - 1);
+  }
+
+  void workspace2_stack::send_all_objects_to_parent_workspace2() {
+    id_type cw = get_current_workspace2();
+    for (dal::bv_visitor_c id(valid_objects); !id.finished(); ++id)
+      if ((obj[id]).workspace == cw) obj[id].workspace = id_type(cw-1);
+  }
+
+  void workspace2_stack::clear_workspace2(id_type wid) {
+    if (wid > get_current_workspace2()) THROW_INTERNAL_ERROR;
+    dal::bit_vector bv = valid_objects;
+    for (dal::bv_visitor_c id(bv); !id.finished(); ++id) {
+      if (valid_objects.is_in(id)) {
+       id_type owid = obj[id].workspace;
+       if (owid > get_current_workspace2()) THROW_INTERNAL_ERROR;
+       if (owid == wid) delete_object(id_type(id));
+      }
+    }
+  }
+
+  void* workspace2_stack::object(id_type id, const char *expected_type) {
+    if (valid_objects[id] &&
+        std::find(newly_created_objects.begin(),newly_created_objects.end(),id)
+       == newly_created_objects.end()) {
+      return obj[id].raw_pointer;
+    } else {
+      THROW_ERROR("object " << expected_type << " [id=" << id << "] not 
found");
+    }
+    return 0;
+  }
+
+  id_type workspace2_stack::object(void *raw_pointer) {
+    auto it = kmap.find(raw_pointer);
+    if (it != kmap.end()) return it->second; else return id_type(-1);
+  }
+
+  void workspace2_stack::commit_newly_created_objects() {
+    newly_created_objects.resize(0);
+  }
+
+  void workspace2_stack::destroy_newly_created_objects() {
+    while (newly_created_objects.size()) {
+      delete_object(newly_created_objects.back());
+      newly_created_objects.pop_back();
+    }
+  }
+
+
+
 }

Modified: trunk/getfem/interface/src/getfemint_workspace.h
URL: 
http://svn.gna.org/viewcvs/getfem/trunk/getfem/interface/src/getfemint_workspace.h?rev=5167&r1=5166&r2=5167&view=diff
==============================================================================
--- trunk/getfem/interface/src/getfemint_workspace.h    (original)
+++ trunk/getfem/interface/src/getfemint_workspace.h    Tue Dec  8 10:55:39 2015
@@ -32,8 +32,10 @@
 #ifndef GETFEMINT_WORKSPACE_H__
 #define GETFEMINT_WORKSPACE_H__
 
-#include <getfem/dal_tas.h>
+#include <getfemint.h>
 #include <getfemint_object.h>
+#include <getfem/dal_bit_vector.h>
+
 namespace getfemint {
 
   class workspace_data {
@@ -125,5 +127,102 @@
   };
 
   workspace_stack& workspace();
+
+
+  // workspace_stack stores the various object assigned to the script
+  // languages variables (Python, Scilab or Matlab).
+  // This objects are organised in workspaces in Matlab and Scilab in
+  // order to be able to delete all the variables created locally in a
+  // sub-program or a loop (however, this has to be managed by the user).
+  // Additionnally, a variable may have dependances with respect to some
+  // other variables, in the sense that is a deletion of a variable occurs
+  // it will be delayed untill all the dependant variables are deleted
+  // (implemented with shared pointers).
+
+
+  // Cacher une partie de l'implementation ?
+
+  class workspace2_stack {
+    
+    struct object_info {
+      dal::pstatic_stored_object p;
+      void *raw_pointer;
+      id_type workspace;
+      getfemint_class_id class_id;
+      std::vector<dal::pstatic_stored_object> used_by;
+
+      object_info() : raw_pointer(0), class_id(GETFEMINT_NB_CLASS) {}
+    };
+
+    typedef std::vector<object_info>  obj_ct;
+    typedef std::vector<std::string>  wrk_ct;
+    static const id_type invalid_id = id_type(-1);
+
+    obj_ct obj;                      // Array of getfem object.
+    dal::bit_vector valid_objects;   // Indices of valid objects.
+    wrk_ct wrk;                      // Stack of used workspaces.
+
+    std::map<void *, id_type> kmap;
+    std::vector<id_type> newly_created_objects;
+
+  public:
+
+    // Creates a new workspace2 on top of the stack
+    void push_workspace2(const std::string &n = "Unnamed") { wrk.push_back(n); 
}
+
+    // Deletes the current workspace2 and returns to the parent workspace2
+    void pop_workspace2(bool keep_all = false);
+
+    // Inserts a new object (and gives it an id)
+    id_type push_object(const dal::pstatic_stored_object &p,
+                       void *raw_pointer, getfemint_class_id class_id);
+
+    // Sets the dependance of an object with respect to another
+    void set_dependance(id_type user, id_type used);
+    void sup_dependance(id_type user, id_type used);
+
+    /** At least mark the objet for future deletion (object becomes anonymous)
+       and if possible, destroy the object (and all the objects which use
+       this one if they are all anonymous).
+    */
+    void delete_object(id_type id);
+
+    /* Move the object in the parent workspace2, in order to prevent
+       the object from being deleted when the current workspace2 will
+       be 'poped' */
+    void send_object_to_parent_workspace2(id_type obj_id);
+    void send_all_objects_to_parent_workspace2();
+
+    id_type get_current_workspace2() const { return id_type(wrk.size()-1); }
+    id_type get_base_workspace2() const { return id_type(0); }
+    /* Delete every object in the workspace2, but *does not* delete the
+       workspace2 itself */
+    void clear_workspace2(id_type w);
+    /* clears the current workspace2 */
+    void clear_workspace2() { clear_workspace2(get_current_workspace2()); }
+
+    
+    /* Throw an error if not found */
+    void *object(id_type id, const char *expected_type="");
+
+    /* Return id_type(-1) if not found */
+    id_type object(void *raw_pointer);
+    
+    workspace2_stack() { push_workspace2("main"); }
+
+    void commit_newly_created_objects();
+    void destroy_newly_created_objects();
+  };
+
+  workspace2_stack& workspace2();
+
+
+
+
+
+
+
+
+
 }
 #endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]