Exemple #1
0
        } // iv()

        public override string ToString()
        {
            // TODO:
            // Policy control over how ID values are serialized,
            // defaulting to preferring (tag, auto_tag, integer ID value) in order.
            IHaximaSerializeable obj = Object_Registrar.All.object_for_ID(val);
            string tag_value;

            if (val == 0 || obj == null)
            {
                tag_value = val.ToString();
            }
            else if (obj.tag != null)
            {
                tag_value = obj.tag;
            }
            else if (obj.autotag != null)
            {
                tag_value = obj.autotag;
            }
            else
            {
                tag_value = val.ToString();
            }
            return(String.Format("{0}", tag_value));
        }
        } // ID_for_object()

        public IHaximaSerializeable object_for_ID(int ID)
        {
            IHaximaSerializeable obj = null;
            bool found = objects_by_ID.TryGetValue(ID, out obj);

            if (!found)
            {
                //Console.WriteLine("ObjectRegistrar.obj_for_ID() ID '{0}' not found\n", ID);
            }
            return(obj);
        } // object_for_ID()
        } // register_object()

        public void unregister_object(IHaximaSerializeable obj)
        {
            int ID = ID_for_object(obj);

            if (ID == 0)
            {
                throw new ArgumentException("Called for not-previously-registered object");
            }
            objects_by_ID.Remove(ID);
            IDs_by_object.Remove(obj);
            num_objs--;
            //Form1.stdout.print("un-registered 0x{0:X} from ID {1}, now {2} objects registered.\n", obj.GetHashCode(), ID, num_objs);
        } // unregister_object()
        } // ID_for_tag()

        public int register_object(IHaximaSerializeable obj, int new_ID)
        {
            // The new_ID arg is either 0 (meaning auto-assign an ID),
            // or non-zero (meaning use the given ID).
            if (obj == null)
            {
                throw new ArgumentException("Got null object");
            }
            int ID;

            if (new_ID == 0)
            {
                // Passing a zero ID means to auto-assign a new one.
                //
                // This is the case for all new objects created at run-time,
                // as well as some new objects hand-edited in the script.
                ID = ++highest_ID;
            }
            else
            {
                // Passing a non-zero ID means to use the given ID.
                //
                // We must check for a collision, and perhaps update the highest_ID
                // to prevent a future collision on auto-assign.
                //
                // This is the case for previously-saved objects (which serialized their ID into the script),
                // as well as some new objects hand-edited in the script,
                // or in the case or hand-editing re-ordering of object constructors.

                if (objects_by_ID.ContainsKey(new_ID))
                {
                    // There is a collision with an already-assigned ID.
                    // This can happen if a hand-edited script contains an object constructor call which specifies an already-used ID.
                    //
                    // TODO: Less-lethal error handling, or a more precise diagnostic (cooperating with the parser)...
                    Error.BadArg("register_obj() Got an already-in-use ID: {0}", new_ID);
                }
                ID = new_ID;
                if (ID >= highest_ID)
                {
                    highest_ID = ID;
                }
            }
            objects_by_ID.Add(ID, obj);
            IDs_by_object.Add(obj, ID);
            // Caller will call register_tag() for .autotag and .tag after this returns...
            num_objs++;
            //Form1.stdout.print("registered 0x{0:X} as ID {1}, now {2} objects registered.\n", obj.GetHashCode(), ID, num_objs);
            return(ID);
        } // register_object()
        /*****************************************/


        public int ID_for_object(IHaximaSerializeable obj)
        {
            // This method is redundant, since registered objects will have an ID() method,
            // but we define it for the sake of completeness.
            int ID = 0;

            if (obj == null)
            {
                throw new ArgumentException("Got null object");
            }
            bool found = IDs_by_object.TryGetValue(obj, out ID);

            if (!found)
            {
                //Console.WriteLine("ObjectRegistrar.ID_for_obj() obj '{0}' not found\n", obj);
            }
            return(ID);
        } // ID_for_object()
Exemple #6
0
        } // ilist()

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("[");
            string sep = "";  // We _pre_pend a separator before all but the _first_ element

            foreach (int ii in val)
            {
                // TODO:
                // Policy control over how ID values are serialized,
                // defaulting to preferring (tag, auto_tag, integer ID value) in order.
                IHaximaSerializeable obj = Object_Registrar.All.object_for_ID(ii);
                string tag_value;
                if (ii == 0 || obj == null)
                {
                    tag_value = ii.ToString();
                }
                else if (obj.tag != null)
                {
                    tag_value = obj.tag;
                }
                else if (obj.autotag != null)
                {
                    tag_value = obj.autotag;
                }
                else
                {
                    tag_value = val.ToString();
                }

                sb.AppendFormat("{0}{1}", sep, tag_value);
                sep = ", ";
            }
            sb.Append("]");
            return(sb.ToString());
        }
 public void unregister_object(IHaximaSerializeable obj)
 {
     int ID = ID_for_object(obj);
     if (ID == 0)
     {
         throw new ArgumentException("Called for not-previously-registered object");
     }
     objects_by_ID.Remove(ID);
     IDs_by_object.Remove(obj);
     num_objs--;
     //Form1.stdout.print("un-registered 0x{0:X} from ID {1}, now {2} objects registered.\n", obj.GetHashCode(), ID, num_objs);
 }
        public int register_object(IHaximaSerializeable obj, int new_ID)
        {
            // The new_ID arg is either 0 (meaning auto-assign an ID),
            // or non-zero (meaning use the given ID).
            if (obj == null)
            {
                throw new ArgumentException("Got null object");
            }
            int ID;
            if (new_ID == 0)
            {
                // Passing a zero ID means to auto-assign a new one.
                //
                // This is the case for all new objects created at run-time,
                // as well as some new objects hand-edited in the script.
                ID = ++highest_ID;
            }
            else
            {
                // Passing a non-zero ID means to use the given ID.
                //
                // We must check for a collision, and perhaps update the highest_ID
                // to prevent a future collision on auto-assign.
                //
                // This is the case for previously-saved objects (which serialized their ID into the script),
                // as well as some new objects hand-edited in the script,
                // or in the case or hand-editing re-ordering of object constructors.

                if (objects_by_ID.ContainsKey(new_ID))
                {
                    // There is a collision with an already-assigned ID.
                    // This can happen if a hand-edited script contains an object constructor call which specifies an already-used ID.
                    //
                    // TODO: Less-lethal error handling, or a more precise diagnostic (cooperating with the parser)...
                    Error.BadArg("register_obj() Got an already-in-use ID: {0}", new_ID);
                }
                ID = new_ID;
                if (ID >= highest_ID)
                {
                    highest_ID = ID;
                }
            }
            objects_by_ID.Add(ID, obj);
            IDs_by_object.Add(obj, ID);
            // Caller will call register_tag() for .autotag and .tag after this returns...
            num_objs++;
            //Form1.stdout.print("registered 0x{0:X} as ID {1}, now {2} objects registered.\n", obj.GetHashCode(), ID, num_objs);
            return ID;
        }
 /*****************************************/
 public int ID_for_object(IHaximaSerializeable obj)
 {
     // This method is redundant, since registered objects will have an ID() method,
     // but we define it for the sake of completeness.
     int ID = 0;
     if (obj == null)
     {
         throw new ArgumentException("Got null object");
     }
     bool found = IDs_by_object.TryGetValue(obj, out ID);
     if (!found)
     {
         //Console.WriteLine("ObjectRegistrar.ID_for_obj() obj '{0}' not found\n", obj);
     }
     return ID;
 }
Exemple #10
0
 public void unregister_object(IHaximaSerializeable obj)
 {
     // NOTE: There are no callers ATM, as test/demo code has not exercised object cleanup yet.
     //
     // Any object which we wish to dispose of needs to have unregister_object() called on it first,
     // otherwise the references in the ObjectRegistrar would keep the garbage collector from reclaiming it.
     //
     // Among other callers would be code for
     // teardown and cleanup/reset of the "session" / "world state".
     if (obj == null) { Error.BadArg("Got null obj"); }  // Or perhaps just return?
     int ID = obj.ID;
     if (ID == 0) {
         Error.BadArg("Called for not-previously-registered object");
     }
     objs_by_ID.Remove(ID);
     num_objs--;
     //Console.WriteLine("un-registered 0x{0:X} from ID {1}, now {2} objects registered", obj.GetHashCode(), ID, num_objs);
 }
Exemple #11
0
    /*****************************************/
    public int register_object_as(IHaximaSerializeable obj, Type tt, int new_ID)
    {
        // In the constructor of any IHaximaSerializeable,
        // a call must be made to this method, to register the new object
        // into 'All' and into the class-specific registrar.
        //
        // This scheme may be extended at some point to allow adding
        // more ObjectRegistrar instances at runtime (when some script syntax specifies thus),
        // indicating that all Obj of some Archetype should be
        // indexed in such an ObjectRegistrar.
        //
        // To do so would probably require re-arranging the current
        // named static ObjectRegistrar instances into a Dictionary<string, ObjectRegistrar>
        // so that all such could be accessed in the same way.
        if (obj == null ||
            tt != registered_type) {
            throw new ArgumentException("Got incorrect object type");
        }
        int ID;
        if (new_ID == 0) {
            // Passing a zero ID means to auto-assign a new one.
            //
            // This is the case for all new objects created at run-time,
            // as well as some new objects hand-edited in the script.
            ID = ++highest_ID;
        }
        else {
            // Passing a non-zero ID means to use the given ID.
            //
            // We must check for a collision, and perhaps update the highest_ID
            // to prevent a future collision on auto-assign.
            //
            // This is the case for previously-saved objects (which serialized their ID into the script),
            // as well as some new objects hand-edited in the script,
            // or in the case or hand-editing re-ordering of object constructors.

            if (ObjectRegistrar.All.objs_by_ID.ContainsKey(new_ID)) {
                // There is a collision with an already-assigned ID.
                // This can happen if a hand-edited script contains an object constructor call which specifies an already-used ID.
                //
                // TODO: Less-lethal error handling, or a more precise diagnostic (cooperating with the parser)...
                Error.BadArg("register_obj() Got an already-in-use ID: {0}", new_ID);
            }
            ID = new_ID;
            if (ID >= highest_ID) {
                highest_ID = ID;
            }
        }
        // At this point, ID is definitely non-zero
        if (tt != typeof(object)) {
            // Registration in a type-specific registrar also causes registration in 'All' as object.
            ObjectRegistrar.All.register_object_as(obj, typeof(object), ID);
        }
        this.objs_by_ID.Add(ID, obj);
        this.num_objs++;
        //Console.WriteLine("registered 0x{0:X} as ID {1}, now {2} objects registered", obj.GetHashCode(), ID, num_objs);
        return ID;
    }
Exemple #12
0
 /*****************************************/
 public int ID_for_object(IHaximaSerializeable obj)
 {
     // This method is redundant, (normally one just says 'obj.ID')
     // It exists for the sake of symmetry, I suppose.
     if (obj == null) { return 0; }
     int ID = obj.ID;
     return ID;
 }