Skip to content

Commit

Permalink
No more CopyString when Interviews is not used (#2483)
Browse files Browse the repository at this point in the history
* Remove CopyString and replace it with std::string
* Simplify xmenu.cpp
  • Loading branch information
Nicolas Cornu authored Oct 9, 2023
1 parent 90a4244 commit 846634f
Show file tree
Hide file tree
Showing 33 changed files with 214 additions and 371 deletions.
2 changes: 1 addition & 1 deletion src/ivoc/apwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ void PrintableWindow::type(const char* s) {
type_ = s;
}
const char* PrintableWindow::type() const {
return type_.string();
return type_.c_str();
}

// StandardWindow
Expand Down
4 changes: 3 additions & 1 deletion src/ivoc/apwindow.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef dismiswin_h
#define dismiswin_h

#include <string>

#include <InterViews/window.h>

#include <InterViews/action.h>
Expand Down Expand Up @@ -103,7 +105,7 @@ class PrintableWindow: public DismissableWindow, public Observable {
virtual void default_geometry();

private:
CopyString type_;
std::string type_;
static OcGlyphContainer* intercept_;
bool mappable_;
bool xplace_;
Expand Down
55 changes: 23 additions & 32 deletions src/ivoc/datapath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,16 @@ extern Objectdata* hoc_top_level_data;
/*static*/ class PathValue {
public:
PathValue();
~PathValue();
CopyString* path;
~PathValue() = default;
std::string path{};
Symbol* sym;
double original;
char* str;
};
PathValue::PathValue() {
path = NULL;
str = NULL;
sym = NULL;
}
PathValue::~PathValue() {
if (path) {
delete path;
}
}

using StringList = std::vector<char*>;

class HocDataPathImpl {
private:
Expand All @@ -62,7 +54,7 @@ class HocDataPathImpl {

private:
std::map<void*, PathValue*> table_;
StringList strlist_;
std::vector<std::string> strlist_;
int size_, count_, found_so_far_;
int pathstyle_;
};
Expand Down Expand Up @@ -110,14 +102,14 @@ void HocDataPaths::search() {
}
}

String* HocDataPaths::retrieve(double* pd) const {
std::string HocDataPaths::retrieve(double* pd) const {
assert(impl_->pathstyle_ != 2);
// printf("HocDataPaths::retrieve\n");
auto const it = impl_->table_.find(pd);
if (it != impl_->table_.end()) {
return it->second->path;
}
return nullptr;
return {};
}

Symbol* HocDataPaths::retrieve_sym(double* pd) const {
Expand All @@ -139,13 +131,13 @@ void HocDataPaths::append(char** pd) {
}
}

String* HocDataPaths::retrieve(char** pd) const {
std::string HocDataPaths::retrieve(char** pd) const {
// printf("HocDataPaths::retrieve\n");
auto const it = impl_->table_.find(pd);
if (it != impl_->table_.end()) {
return it->second->path;
}
return nullptr;
return {};
}

/*------------------------------*/
Expand Down Expand Up @@ -205,20 +197,20 @@ PathValue* HocDataPathImpl::found_v(void* v, const char* buf, Symbol* sym) {
PathValue* pv;
if (pathstyle_ != 2) {
char path[500];
CopyString cs("");
std::string cs{};
for (const auto& str: strlist_) {
Sprintf(path, "%s%s.", cs.string(), str);
Sprintf(path, "%s%s.", cs.c_str(), str.c_str());
cs = path;
}
Sprintf(path, "%s%s", cs.string(), buf);
Sprintf(path, "%s%s", cs.c_str(), buf);
const auto& it = table_.find(v);
if (it == table_.end()) {
hoc_warning("table lookup failed for pointer for-", path);
return nullptr;
}
pv = it->second;
if (!pv->path) {
pv->path = new CopyString(path);
if (pv->path.empty()) {
pv->path = path;
pv->sym = sym;
++found_so_far_;
}
Expand Down Expand Up @@ -258,7 +250,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
Symbol* sym;
int i, total;
char buf[200];
CopyString cs("");
std::string cs{};
if (sl)
for (sym = sl->first; sym; sym = sym->next) {
if (sym->cpublic != 2) {
Expand All @@ -278,7 +270,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
if (pd[i] == sentinal) {
Sprintf(buf, "%s%s", sym->name, hoc_araystr(sym, i, od));
cs = buf;
found(pd + i, cs.string(), sym);
found(pd + i, cs.c_str(), sym);
}
}
} break;
Expand All @@ -287,7 +279,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
if (*pstr == NULL) {
Sprintf(buf, "%s", sym->name);
cs = buf;
found(pstr, cs.string(), sym);
found(pstr, cs.c_str(), sym);
}
} break;
case OBJECTVAR: {
Expand All @@ -304,7 +296,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
if (obp[i]->u.dataspace != od) {
Sprintf(buf, "%s%s", sym->name, hoc_araystr(sym, i, od));
cs = buf;
strlist_.push_back((char*) cs.string());
strlist_.push_back(cs);
obp[i]->recurse = 1;
search(obp[i]->u.dataspace, obp[i]->ctemplate->symtable);
obp[i]->recurse = 0;
Expand All @@ -315,7 +307,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
if (t->is_point_) {
Sprintf(buf, "%s%s", sym->name, hoc_araystr(sym, i, od));
cs = buf;
strlist_.push_back((char*) cs.string());
strlist_.push_back(cs);
search((Point_process*) obp[i]->u.this_pointer, sym);
strlist_.pop_back();
}
Expand All @@ -330,7 +322,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
if (pitm[i]) {
Sprintf(buf, "%s%s", sym->name, hoc_araystr(sym, i, od));
cs = buf;
strlist_.push_back((char*) cs.string());
strlist_.push_back(cs);
search(hocSEC(pitm[i]));
strlist_.pop_back();
}
Expand All @@ -343,7 +335,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
Object* obj = OBJ(q);
Sprintf(buf, "%s[%d]", sym->name, obj->index);
cs = buf;
strlist_.push_back((char*) cs.string());
strlist_.push_back(cs);
if (!t->constructor) {
search(obj->u.dataspace, t->symtable);
} else {
Expand All @@ -361,14 +353,14 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {

void HocDataPathImpl::search_vectors() {
char buf[200];
CopyString cs("");
std::string cs{};
cTemplate* t = sym_vec->u.ctemplate;
hoc_Item* q;
ITERATE(q, t->olist) {
Object* obj = OBJ(q);
Sprintf(buf, "%s[%d]", sym_vec->name, obj->index);
cs = buf;
strlist_.push_back((char*) cs.string());
strlist_.push_back(cs);
Vect* vec = (Vect*) obj->u.this_pointer;
int size = vec->size();
double* pd = vector_vec(vec);
Expand All @@ -384,14 +376,14 @@ void HocDataPathImpl::search_vectors() {

void HocDataPathImpl::search_pysec() {
#if USE_PYTHON
CopyString cs("");
std::string cs{};
hoc_Item* qsec;
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
if (sec->prop && sec->prop->dparam[PROP_PY_INDEX].get<void*>()) {
cs = secname(sec);
strlist_.push_back((char*) cs.string());
strlist_.push_back(cs);
search(sec);
strlist_.pop_back();
}
Expand All @@ -418,7 +410,6 @@ void HocDataPathImpl::search(Section* sec) {
}
void HocDataPathImpl::search(Node* nd, double x) {
char buf[100];
CopyString cs("");
if (NODEV(nd) == sentinal) {
Sprintf(buf, "v(%g)", x);
// the conversion below yields a pointer that is potentially invalidated
Expand Down
4 changes: 2 additions & 2 deletions src/ivoc/datapath.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class HocDataPaths {
void append(double*);
void append(char**);
void search();
String* retrieve(double*) const;
String* retrieve(char**) const;
std::string retrieve(double*) const;
std::string retrieve(char**) const;
Symbol* retrieve_sym(double*) const;
int style();

Expand Down
50 changes: 21 additions & 29 deletions src/ivoc/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,6 @@ Graph::Graph(bool b)
loc_ = 0;
x_expr_ = NULL;
x_pval_ = {};
var_name_ = NULL;
rvp_ = NULL;
cross_action_ = NULL;
vector_copy_ = false;
Expand Down Expand Up @@ -1463,20 +1462,13 @@ Graph::~Graph() {
Resource::unref(sc_);
Resource::unref(current_polyline_);
Resource::unref(family_label_);
if (var_name_) {
delete var_name_;
}
if (cross_action_) {
delete cross_action_;
}
}

void Graph::name(char* s) {
if (var_name_) {
*var_name_ = s;
} else {
var_name_ = new CopyString(s);
}
var_name_ = s;
}

void Graph::help() {
Expand Down Expand Up @@ -2363,14 +2355,14 @@ void Graph::save_phase2(std::ostream& o) {
Sprintf(buf, "save_window_.family(\"%s\")", family_label_->text());
o << buf << std::endl;
}
if (var_name_) {
if ((var_name_->string())[var_name_->length() - 1] == '.') {
Sprintf(buf, "%sappend(save_window_)", var_name_->string());
if (!var_name_.empty()) {
if (var_name_.back() == '.') {
Sprintf(buf, "%sappend(save_window_)", var_name_.c_str());
} else {
Sprintf(buf, "%s = save_window_", var_name_->string());
Sprintf(buf, "%s = save_window_", var_name_.c_str());
}
o << buf << std::endl;
Sprintf(buf, "save_window_.save_name(\"%s\")", var_name_->string());
Sprintf(buf, "save_window_.save_name(\"%s\")", var_name_.c_str());
o << buf << std::endl;
}
if (x_expr_) {
Expand Down Expand Up @@ -2429,7 +2421,7 @@ void Graph::choose_sym() {
neuron::container::data_handle<double> pd_handle{pd};

if (sc_->selected_vector_count()) {
Sprintf(buf, "%s", sc_->selected()->string());
Sprintf(buf, "%s", sc_->selected().c_str());
GraphVector* gv = new GraphVector(buf);
gv->color(color());
gv->brush(brush());
Expand All @@ -2444,19 +2436,19 @@ void Graph::choose_sym() {
flush();
break;
} else if (pd) {
add_var(sc_->selected()->string(), color(), brush(), 1, 2);
add_var(sc_->selected().c_str(), color(), brush(), 1, 2);
break;
} else {
CopyString s(*sc_->selected());
auto s = sc_->selected();
// above required due to bug in mswindows version in which
// sc_->selected seems volatile under some kinds of hoc
// executions.
Sprintf(buf, "hoc_ac_ = %s\n", s.string());
Sprintf(buf, "hoc_ac_ = %s\n", s.c_str());
if (oc.run(buf) == 0) {
add_var(s.string(), color(), brush(), 0, 2);
add_var(s.c_str(), color(), brush(), 0, 2);
break;
}
hoc_warning(s.string(), "is not an expression.");
hoc_warning(s.c_str(), "is not an expression.");
}
}
// sc_->unref();
Expand All @@ -2475,12 +2467,12 @@ void Graph::family_label_chooser() {
}
while (fsc_->post_for_aligned(XYView::current_pick_view()->canvas()->window(), .5, 1.)) {
char buf[256];
Sprintf(buf, "hoc_ac_ = %s\n", fsc_->selected()->string());
Sprintf(buf, "hoc_ac_ = %s\n", fsc_->selected().c_str());
if (oc.run(buf) == 0) {
family(fsc_->selected()->string());
family(fsc_->selected().c_str());
break;
}
hoc_warning(sc_->selected()->string(), "is not an expression.");
hoc_warning(sc_->selected().c_str(), "is not an expression.");
}
}

Expand Down Expand Up @@ -3010,7 +3002,7 @@ GLabel::~GLabel() {
}

Glyph* GLabel::clone() const {
return new GLabel(text_.string(), color_, fixtype_, scale_, x_align_, y_align_);
return new GLabel(text_.c_str(), color_, fixtype_, scale_, x_align_, y_align_);
}

void GLabel::save(std::ostream& o, Coord x, Coord y) {
Expand All @@ -3022,7 +3014,7 @@ void GLabel::save(std::ostream& o, Coord x, Coord y) {
"save_window_.label(%g, %g, \"%s\", %d, %g, %g, %g, %d)",
x,
y,
text_.string(),
text_.c_str(),
fixtype_,
scale_,
x_align_,
Expand Down Expand Up @@ -3050,7 +3042,7 @@ void GLabel::align(float x, float y) {
void GLabel::color(const Color* c) {
Resource::unref(label_);
WidgetKit& kit = *WidgetKit::instance();
label_ = new Label(text_, kit.font(), c);
label_ = new Label(text_.c_str(), kit.font(), c);
label_->ref();
Resource::ref(c);
Resource::unref(color_);
Expand All @@ -3064,7 +3056,7 @@ void GLabel::text(const char* t) {
Resource::unref(label_);
WidgetKit& kit = *WidgetKit::instance();
text_ = t;
label_ = new Label(text_, kit.font(), color_);
label_ = new Label(text_.c_str(), kit.font(), color_);
label_->ref();
}

Expand Down Expand Up @@ -3106,7 +3098,7 @@ void GLabel::draw(Canvas* c, const Allocation& a1) const {
// printf("transformer %g %g %g %g %g %g\n", a00, a01, a10, a11, a20, a21);
label_->draw(c, a2);
c->pop_transform();
IfIdraw(text(c, text_.string(), t, NULL, color()));
IfIdraw(text(c, text_.c_str(), t, NULL, color()));
}

// DataVec------------------
Expand Down Expand Up @@ -3288,7 +3280,7 @@ GraphVector::~GraphVector() {
}

const char* GraphVector::name() const {
return name_.string();
return name_.c_str();
}

void GraphVector::save(std::ostream&) {}
Expand Down
Loading

0 comments on commit 846634f

Please sign in to comment.