Commits

Carl Friedrich Bolz committed deb3f72

support changing of classes

Comments (0)

Files changed (2)

     def get_terminator(self):
         raise NotImplementedError("abstract base class")
 
+    def set_terminator(self, obj, terminator):
+        raise NotImplementedError("abstract base class")
+
     def add_attr(self, obj, attrname, w_value):
         cache = self.cache_attrs
         if cache is None:
     def get_terminator(self):
         return self
 
+    def set_terminator(self, obj, terminator):
+        return Object(terminator, [])
 
 class PlainAttribute(AbstractAttribute):
     def __init__(self, attrname, back):
         self.position = back.length()
         self.back = back
 
+    def _copy_attr(self, obj, new_obj):
+        w_value = self.read(obj, self.attrname)
+        new_obj.map.add_attr(new_obj, self.attrname, w_value)
+
     def read(self, obj, attrname):
         if attrname == self.attrname:
             return obj.storage[self.position]
     def delete(self, obj, attrname):
         new_obj = self.back.delete(obj, attrname)
         if self.attrname != attrname:
-            w_value = self.read(obj, self.attrname)
-            new_obj.map.add_attr(new_obj, self.attrname, w_value)
+            self._copy_attr(obj, new_obj)
+        return new_obj
+
+    def set_terminator(self, obj, terminator):
+        new_obj = self.back.set_terminator(obj, terminator)
+        self._copy_attr(obj, new_obj)
         return new_obj
 
     def length(self):
         self.map = map
         self.storage = storage
 
+    def _become(self, new_obj):
+        self.map = new_obj.map
+        self.storage = new_obj.storage
+
     def read(self, attrname):
         return self.map.read(self, attrname)
 
 
     def delete(self, attrname):
         new_obj = self.map.delete(self, attrname)
-        self.map = new_obj.map
-        self.storage = new_obj.storage
+        self._become(new_obj)
 
     def get_class(self):
         return self.map.get_terminator().cls
 
+    def set_class(self, cls):
+        new_obj = self.map.set_terminator(self, cls.terminator)
+        self._become(new_obj)
+
 class Class(object):
     def __init__(self):
         self.terminator = Terminator(self)
     assert obj.get_class() is c
     obj.write("c", 70)
     assert obj.get_class() is c
+
+    c2 = Class()
+    obj.set_class(c2)
+    assert obj.get_class() is c2
+    assert obj.storage == [50, 60, 70]