initial
[prop.git] / lib-src / persist / ptype.cc
blob0235c887c57d4c736be81748d5d339e1b444d0ec
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung (leunga@cs.nyu.edu)
22 // 1994-1995
23 //////////////////////////////////////////////////////////////////////////////
25 #define PSTREAM_IMPLEMENTATION
26 #include <string.h>
27 #include <iostream.h>
28 #include <strstream.h>
29 #include <AD/persist/pconfig.h>
30 #include <AD/persist/ptype.h>
31 #include <AD/persist/ptypeentry.h>
32 #include <AD/persist/pstream.h>
33 #include <AD/hash/lhash.h>
34 #include <AD/contain/vararray.h>
36 //////////////////////////////////////////////////////////////////////////////
38 // Persistent id table
40 //////////////////////////////////////////////////////////////////////////////
41 static P_OBJECT_TYPEID next_unique_id = 1;
42 typedef LHashTable<const char *, PObjectType::Entry *> PersistTypeMap;
43 static PersistTypeMap * persist_type_map;
44 VarArray <PObjectType::Entry *> * persist_type_table;
45 //////////////////////////////////////////////////////////////////////////////
47 // Persistent object id constructor
49 //////////////////////////////////////////////////////////////////////////////
50 PObjectType::PObjectType(const char * name)
51 { if (persist_type_map == 0)
52 { persist_type_map = new PersistTypeMap(257);
53 persist_type_table = new VarArray<PObjectType::Entry *>(0,257);
55 Ix e = persist_type_map->lookup(name);
56 if (e)
57 { // found type entry
58 entry = persist_type_map->value(e);
59 } else
60 { // create new type entry
61 entry = new PObjectType::Entry;
62 entry->unique_id = next_unique_id;
63 entry->type_name = name;
64 entry->type_name_len = strlen(name);
65 entry->factory = 0;
66 persist_type_map->insert(name,entry);
67 (*persist_type_table)[next_unique_id] = entry;
68 next_unique_id++;
72 //////////////////////////////////////////////////////////////////////////////
74 // Persistent object type destructor
76 //////////////////////////////////////////////////////////////////////////////
77 PObjectType::~PObjectType() {}
79 //////////////////////////////////////////////////////////////////////////////
81 // Retrieve the name of a persistent type
83 //////////////////////////////////////////////////////////////////////////////
84 const char * PObjectType::name() const
85 { return entry ? entry->type_name : "<none>"; }
87 //////////////////////////////////////////////////////////////////////////////
89 // Lookup an entry
91 //////////////////////////////////////////////////////////////////////////////
92 PObjectType::Entry * PObjectType::lookup_entry (const char * name)
93 { Ix e = persist_type_map->lookup(name);
94 return e ? persist_type_map->value(e) : 0;
97 //////////////////////////////////////////////////////////////////////////////
99 // Return the number of entries in table
101 //////////////////////////////////////////////////////////////////////////////
102 int PObjectType::number_of_types () { return next_unique_id - 1; }
104 //////////////////////////////////////////////////////////////////////////////
106 // Write an persistent type to an iostream
108 //////////////////////////////////////////////////////////////////////////////
109 ostream& operator << (ostream& f, const PObjectType& id)
110 { if (id.entry)
111 { f << "[name=" << id.entry->type_name
112 << ", id=" << id.entry->unique_id;
113 if (id.entry->factory) f << " (with factory)";
114 f << " ]";
115 return f;
116 } else
117 { return f << "[empty persistent object type]";