From 81ad287c0621e3655782696b2ce1369ef6ae8e8e Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Sat, 24 Dec 2011 16:22:10 +0400 Subject: [PATCH] Generate very simple static functions to find objects in global vectors. --- library/DataStatics.cpp | 12 +++++++++ library/include/dfhack/MiscUtils.h | 20 +++++++++++++- library/xml/codegen.pl | 39 +++++++++++++++++++++++++++ library/xml/df.buildings.xml | 4 +-- library/xml/df.history.xml | 12 ++++++--- library/xml/df.items.xml | 5 ++-- library/xml/df.jobs.xml | 4 +-- library/xml/df.language.xml | 42 ++++++++++++++---------------- library/xml/df.legends.xml | 24 +++-------------- library/xml/df.machines.xml | 5 ++-- library/xml/df.materials.xml | 8 +++--- library/xml/df.military.xml | 4 +-- library/xml/df.ui.xml | 3 +-- library/xml/df.units.xml | 4 +-- 14 files changed, 117 insertions(+), 69 deletions(-) diff --git a/library/DataStatics.cpp b/library/DataStatics.cpp index 57aeb779c..e3a448eac 100644 --- a/library/DataStatics.cpp +++ b/library/DataStatics.cpp @@ -1,5 +1,17 @@ #include "Internal.h" #include "dfhack/DataDefs.h" +#include "dfhack/MiscUtils.h" + +#include "dfhack/df/world.h" +#include "dfhack/df/world_data.h" +#include "dfhack/df/ui.h" + +namespace { + template + inline T &_toref(T &r) { return r; } + template + inline T &_toref(T *&p) { return *p; } +} // Instantiate all the static objects #include "dfhack/df/static.inc" diff --git a/library/include/dfhack/MiscUtils.h b/library/include/dfhack/MiscUtils.h index 2bdab10c6..dfac76665 100644 --- a/library/include/dfhack/MiscUtils.h +++ b/library/include/dfhack/MiscUtils.h @@ -187,6 +187,7 @@ void print_bits ( T val, DFHack::Console& out ) strs << endl; out.print(strs.str().c_str()); } + /* // this is probably completely bogus std::string PrintSplatterType (int16_t mat1, int32_t mat2, vector &creature_types) @@ -247,4 +248,21 @@ std::string PrintSplatterType (int16_t mat1, int32_t mat2, vector +CT *binsearch_in_vector(std::vector &vec, FT CT::*field, AT value) { + int min = -1, max = (int)vec.size(); + CT **p = vec.data(); + FT key = (FT)value; + for (;;) { + int mid = (min + max)>>1; + if (mid == min) + return NULL; + FT midv = p[mid]->*field; + if (midv == key) + return p[mid]; + else if (midv < key) + min = mid; + else + max = mid; + } +} diff --git a/library/xml/codegen.pl b/library/xml/codegen.pl index 33210f616..05684732b 100755 --- a/library/xml/codegen.pl +++ b/library/xml/codegen.pl @@ -56,6 +56,17 @@ sub type_header_def($) { return uc($main_namespace).'_'.uc($name).'_H'; } +sub translate_lookup($) { + my ($str) = @_; + return undef unless $str && $str =~ /^\$global((\.[_a-zA-Z0-9]+)+)$/; + my @fields = split /\./, substr($1,1); + my $expr = "df::global::".shift(@fields); + for my $fn (@fields) { + $expr = "_toref($expr).$fn"; + } + return $expr; +} + # Text generation with indentation our @lines; @@ -730,6 +741,32 @@ sub get_df_flagarray_type($) { ); $struct_field_handlers{$_} ||= \&get_primitive_field_type for @primitive_type_list; +sub emit_find_instance { + my ($tag) = @_; + + my $instance_vector = translate_lookup $tag->getAttribute('instance-vector'); + if ($instance_vector) { + emit "static std::vector<$typename*> &get_vector();"; + emit "static $typename *find(int id);"; + + with_emit_static { + emit_block { + emit "return ", $instance_vector, ";"; + } "std::vector<$typename*>& ${typename}::get_vector() "; + + emit_block { + emit "std::vector<$typename*> &vec_ = get_vector();"; + + if (my $id = $tag->getAttribute('key-field')) { + emit "return binsearch_in_vector(vec_, &${typename}::$id, id_);"; + } else { + emit "return (id_ >= 0 && id_ < vec_.size()) ? vec_[id_] : NULL;"; + } + } "$typename *${typename}::find(int id_) "; + } + } +} + sub render_struct_type { my ($tag) = @_; @@ -750,6 +787,8 @@ sub render_struct_type { with_struct_block { render_struct_field($_) for get_struct_fields($tag); + emit_find_instance($tag); + if ($has_methods) { if ($is_class) { emit "static class_virtual_identity<$typename> _identity;"; diff --git a/library/xml/df.buildings.xml b/library/xml/df.buildings.xml index f54c76eb8..74df09eb7 100644 --- a/library/xml/df.buildings.xml +++ b/library/xml/df.buildings.xml @@ -7,8 +7,8 @@ - - (find-by-id $global.world.buildings.all $id $) + diff --git a/library/xml/df.history.xml b/library/xml/df.history.xml index 78dd33a3b..700329ea8 100644 --- a/library/xml/df.history.xml +++ b/library/xml/df.history.xml @@ -1,5 +1,5 @@ - + @@ -17,8 +17,6 @@ - (find-by-id $global.world.history.figures $id $) - (describe-obj $.name) (awhen (find-creature $.race) @@ -44,6 +42,14 @@ + + + + + + + diff --git a/library/xml/df.items.xml b/library/xml/df.items.xml index a49c35ff5..c1b989b23 100644 --- a/library/xml/df.items.xml +++ b/library/xml/df.items.xml @@ -66,9 +66,8 @@ -- CORE ITEM - - (find-by-id $global.world.items.all $id $) - + diff --git a/library/xml/df.jobs.xml b/library/xml/df.jobs.xml index 48163fc48..8c6c6d849 100644 --- a/library/xml/df.jobs.xml +++ b/library/xml/df.jobs.xml @@ -190,9 +190,7 @@ - - (find-by-id $global.world.activities.all $id $) - + diff --git a/library/xml/df.language.xml b/library/xml/df.language.xml index 347946602..961057f2d 100644 --- a/library/xml/df.language.xml +++ b/library/xml/df.language.xml @@ -18,28 +18,24 @@ - - - - $global.world.raws.language_words[$] - - - - - - + + + + + + + + + + + - + + - + $.word - - - - - - - + @@ -48,10 +44,10 @@ - + - $global.world.raws.translations[$] + $.name @@ -78,7 +74,9 @@ - + + + diff --git a/library/xml/df.legends.xml b/library/xml/df.legends.xml index 0b8b8c926..a9d2e4a65 100644 --- a/library/xml/df.legends.xml +++ b/library/xml/df.legends.xml @@ -1,5 +1,5 @@ - + @@ -9,8 +9,6 @@ - (find-by-id $global.world.entities.all $id $) - (describe-obj $.name) (describe-obj (find-creature $.race)) @@ -225,10 +223,9 @@ - + - (find-by-id $global.world.entity_populations $id $) (describe-obj $.name) @@ -258,11 +255,9 @@ - + - (find-by-id $global.world.nemesis.all $id $) - @@ -284,11 +279,9 @@ - + - (find-by-id $global.world.artifacts.all $id $) - (describe-obj $.name) @@ -298,15 +291,6 @@ - - - - - - - - (find-by-id $global.world.history.events $id $) -