[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libcvd-members] gvars3 gvars3/GStringUtil.h gvars3/gv3_implemen...
From: |
Edward Rosten |
Subject: |
[libcvd-members] gvars3 gvars3/GStringUtil.h gvars3/gv3_implemen... |
Date: |
Mon, 20 Apr 2009 17:05:28 +0000 |
CVSROOT: /cvsroot/libcvd
Module name: gvars3
Changes by: Edward Rosten <edrosten> 09/04/20 17:05:28
Modified files:
gvars3 : GStringUtil.h gv3_implementation.hh gvars3.h
serialize.h
src : serialize.cc
Added files:
gvars3 : default.h
Log message:
It seems to compile cleanly with TooN2 and work with Vector<-1>.
All instances of default initialization have been deferred to the
DefaultValue class.
Textual assignment (set_var, etc) is performed with a new function
which does
does an erase, followed by an insert. If you're using unreliable thread
unsafe
code, this will probably make it much worse.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gvars3/gvars3/GStringUtil.h?cvsroot=libcvd&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/gvars3/gvars3/gv3_implementation.hh?cvsroot=libcvd&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/gvars3/gvars3/gvars3.h?cvsroot=libcvd&r1=1.22&r2=1.23
http://cvs.savannah.gnu.org/viewcvs/gvars3/gvars3/serialize.h?cvsroot=libcvd&r1=1.12&r2=1.13
http://cvs.savannah.gnu.org/viewcvs/gvars3/gvars3/default.h?cvsroot=libcvd&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gvars3/src/serialize.cc?cvsroot=libcvd&r1=1.14&r2=1.15
Patches:
Index: gvars3/GStringUtil.h
===================================================================
RCS file: /cvsroot/libcvd/gvars3/gvars3/GStringUtil.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- gvars3/GStringUtil.h 22 Nov 2005 18:33:27 -0000 1.2
+++ gvars3/GStringUtil.h 20 Apr 2009 17:05:27 -0000 1.3
@@ -24,6 +24,7 @@
#include <vector>
#include <string>
+#include <sstream>
#include <gvars3/serialize.h>
namespace GVars3
@@ -33,8 +34,8 @@
std::vector<std::string> ChopAndUnquoteString(std::string s);
template <class T> T* ParseAndAllocate(std::string s)
{
- T* n = new T;
- serialize::from_string(s, *n);
+ std::istringstream is(s);
+ T* n = new T(serialize::from_stream<T>(is));
return n;
}
Index: gvars3/gv3_implementation.hh
===================================================================
RCS file: /cvsroot/libcvd/gvars3/gvars3/gv3_implementation.hh,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- gvars3/gv3_implementation.hh 20 Apr 2009 14:59:39 -0000 1.13
+++ gvars3/gv3_implementation.hh 20 Apr 2009 17:05:28 -0000 1.14
@@ -1,31 +1,18 @@
-// Replacement for operator= which doesn't crash for Vector<-1>; general case
-template<class T> inline void robust_assignment(T& lvalue, T rhs)
+template<class T> inline void robust_set_var(std::string& name, const T& value)
{
- lvalue =rhs;
+ GV3::safe_replace<T>(name,value);
};
-#if 0 && !defined GVARS3_HAVE_TOON
-// Replacement for operator= which doesn't crash for Vector<-1>; specialisation
-void inline robust_assignment(TooN::Vector<> &lvalue, TooN::Vector<> rhs)
-{
- if(lvalue.size()!=rhs.size())
- lvalue.resize(rhs.size());
- lvalue = rhs;
-}
-#endif
-
// TODO: Make the specialisation for matrices as well.
-
-
template<class T> T* GV3::register_new_gvar(const std::string& name, const T&
default_val, int flags)
{
- T* d ;
std::map<std::string, std::string>::iterator i;
i = unmatched_tags.find(name);
- d = TypedMap<T>::instance().create(name);
+ T* d;
+
registered_type_and_trait[name] = std::pair<BaseMap*,
int>(&TypedMap<T>::instance(), flags);
//Look to see if ``name'' has not already been set
@@ -38,15 +25,16 @@
};
if(!(flags & SILENT))
- std::cerr << "? GV3::Register: " << type_name<T>() << " " << name << "
undefined. Defaults to "
- << serialize::to_string(default_val) << std::endl;
+ std::cerr << "? GV3::Register: " << type_name<T>() << "
" << name << " undefined. Defaults to " << serialize::to_string(default_val) <<
std::endl;
- // *d = default_val; // This crashes with vector<-1> if sizes
don't match, which they don't. Replace with:
- robust_assignment(*d, default_val);
+ d = &safe_replace(name, default_val);
}
else
{
- int e = serialize::from_string(i->second, *d);
+ std::istringstream is(i->second);
+ T value = serialize::from_stream<T>(is);
+ int e = serialize::check_stream(is);
+
parse_warning(e, type_name<T>(), name, i->second);
if(e > 0 && flags & FATAL_IF_NOT_DEFINED)
{
@@ -54,8 +42,11 @@
throw gvar_was_not_defined();
}
+ d = &safe_replace(name, value);
+
unmatched_tags.erase(i);
}
+
return d;
}
@@ -73,8 +64,10 @@
T* d = attempt_get<T>(name);
if(d!=NULL) return d;
- T def=T();
- int e = serialize::from_string(default_val, def);
+ std::istringstream is(default_val);
+ T def = serialize::from_stream<T>(is);
+ int e = serialize::check_stream(is);
+
parse_warning(e, type_name<T>(), name, default_val);
return register_new_gvar(name, def, flags);
Index: gvars3/gvars3.h
===================================================================
RCS file: /cvsroot/libcvd/gvars3/gvars3/gvars3.h,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -b -r1.22 -r1.23
--- gvars3/gvars3.h 20 Apr 2009 14:59:39 -0000 1.22
+++ gvars3/gvars3.h 20 Apr 2009 17:05:28 -0000 1.23
@@ -29,6 +29,7 @@
#include <iostream>
#include <gvars3/config.h>
+#include <gvars3/default.h>
#include <gvars3/type_name.h>
#include <gvars3/serialize.h>
@@ -113,8 +114,6 @@
inline gvar3(const std::string& name, const std::string& default_val =
"", int flags=0);
inline gvar3(){};
};
-
-
class GV3
{
private:
@@ -152,21 +151,51 @@
return &(i->second);
}
+ //Replace a member using erase then reinsert
+ T& safe_replace(const std::string& n, const T&
t)
+ {
+ typename std::map<std::string,
T>::iterator i, j;
+ //Keep track of the neighboring point
+ //to pass as a hint to insert.
+ i = data.find(n);
+ j=i;
+
+ if(i != data.end())
+ {
+ j++;
+ data.erase(i);
+ }
+
+ data.insert(j, make_pair(n, t));
+
+ return data.insert(j, make_pair(n,
t))->second;
+ }
+
//Create a data member
T* create(const std::string& n)
{
- data[n] = T();
- return &data[n];
+ return data.insert(make_pair(n,
DefaultValue<T>::val()))->second;
}
virtual int set_from_string(const std::string&
name, const std::string& val)
{
- return serialize::from_string(val,
data[name]);
+ std::istringstream is(val);
+ T tmp = serialize::from_stream<T>(is);
+ int e = serialize::check_stream(is);
+
+ if(e == 0)
+ safe_replace(name, tmp);
+ return e;
}
virtual std::string get_as_string(const
std::string& name)
{
- return serialize::to_string(data[name]);
+ typename std::map<std::string,
T>::iterator i = data.find(name);
+
+ if(i == data.end())
+ i = data.insert(make_pair(name,
DefaultValue<T>::val())).first;
+
+ return serialize::to_string(i->second);
}
virtual std::string name()
@@ -208,6 +237,11 @@
return d;
}
+ template<class T> static T& safe_replace(const std::string&
name, const T& t)
+ {
+ return TypedMap<T>::instance().safe_replace(name, t);
+ }
+
static void add_typemap(BaseMap* m);
static std::map<std::string, std::string>
unmatched_tags;
@@ -221,11 +255,11 @@
public:
//Get references by name
- template<class T> static T& get(const std::string& name, const
T& default_val=T(), int flags=0);
+ template<class T> static T& get(const std::string& name, const
T& default_val=DefaultValue<T>::val(), int flags=0);
template<class T> static T& get(const std::string& name,
std::string default_val, int flags=0);
//Register GVars
- template<class T> static void Register(gvar2<T>& gv, const
std::string& name, const T& default_val=T(), int flags=0);
+ template<class T> static void Register(gvar2<T>& gv, const
std::string& name, const T& default_val=DefaultValue<T>::val(), int flags=0);
template<class T> static void Register(gvar2<T>& gv, const
std::string& name, const std::string& default_val, int flags=0);
static inline void Register(gvar2<std::string>& gv, const
std::string& name, const std::string& default_val="", int flags=0);
Index: gvars3/serialize.h
===================================================================
RCS file: /cvsroot/libcvd/gvars3/gvars3/serialize.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- gvars3/serialize.h 20 Apr 2009 14:59:39 -0000 1.12
+++ gvars3/serialize.h 20 Apr 2009 17:05:28 -0000 1.13
@@ -22,14 +22,11 @@
#ifndef GV3_INC_SERIALIZE_H
#define GV3_INC_SERIALIZE_H
#include <gvars3/config.h>
+#include <gvars3/default.h>
#include <string>
#include <vector>
#include <sstream>
-#ifdef GVARS3_HAVE_TOON
- #include <TooN/TooN.h>
-#endif
-
namespace GVars3
{
namespace serialize
@@ -50,19 +47,27 @@
std::string to_string(const std::string& val);
-
-
- template<class T> std::istream& from_stream(std::istream& i, T&
result)
+ template<class T> struct FromStream
+ {
+ static T from(std::istream& i)
{
+ T result = DefaultValue<T>::val();
i >> result;
- return i;
+ return result;
}
+ };
//Special reading of strings
- std::istream& from_stream(std::istream& in, std::string& s);
+ template<> struct FromStream<std::string>
+ {
+ static std::string from(std::istream& in);
+ };
- template<class T> std::istream& from_stream(std::istream& in,
std::vector<T>& v)
+ template<class T> struct FromStream<std::vector<T> >
{
+ static std::vector<T> from(std::istream& in)
+ {
+ std::vector<T> v;
using std::ws;
using std::ios;
v.clear();
@@ -70,12 +75,12 @@
int c;
if((c = in.get()) == EOF)
- return in;
+ return v;
if(c != '[')
{
in.setstate(ios::failbit);
- return in;
+ return v;
}
for(;;)
@@ -85,22 +90,25 @@
c = in.get();
if(c == EOF || c == ']')
- return in;
+ return v;
in.unget();
- T val;
- from_stream(in, val);
+ T val = FromStream<T>::from(in);
if(!in.fail() && !in.bad())
v.push_back(val);
else
- return in;
+ return v;
}
}
+ };
- template<class T> std::istream& from_stream(std::istream& in,
std::vector<std::vector<T> >& v)
+ template<class T> struct FromStream<std::vector<std::vector<T>
> >
+ {
+ static std::vector<std::vector<T> > from(std::istream&
in)
{
+ std::vector<std::vector<T> > v;
using std::ws;
using std::ios;
v.clear();
@@ -113,7 +121,7 @@
if(c != '[')
{
in.setstate(ios::failbit);
- return in;
+ return v;
}
std::vector<T> current;
@@ -126,7 +134,7 @@
{
if(!current.empty())
v.push_back(current);
- return in;
+ return v;
}
else if(c == ';')
{
@@ -136,27 +144,18 @@
else
in.unget();
- T val;
- from_stream(in, val);
+ T val = FromStream<T>::from(in);
if(!in.fail() && !in.bad())
current.push_back(val);
else
- return in;
+ return v;
}
}
+ };
-
- template<class T> int from_string(std::string s, T& result)
- {
- using GVars3::serialize::from_stream;
- std::istringstream i(s);
- from_stream(i, result);
- return check_stream(i);
- }
-
#ifdef GVARS3_HAVE_TOON
template<int N> std::string to_string(const
TooN::Vector<N>& m)
{
@@ -188,26 +187,26 @@
return o.str();
}
- template<int N> std::istream& from_stream(std::istream&
i, TooN::Vector<N>& m)
+
+ template<int N> struct FromStream<TooN::Vector<N> >
{
- std::vector<double> v;
- from_stream(i, v);
+ static TooN::Vector<N> from(std::istream& i)
+ {
+ std::vector<double> v =
FromStream<std::vector<double> >::from(i);
- if(v.size() != m.size())
+ if(i.fail() || i.bad() || (N != -1 &&
v.size() != N) || v.size() == 0)
{
i.setstate(std::ios::failbit);
- return i;
+ return
DefaultValue<TooN::Vector<N> >::val();
}
else
{
- for(int j=0; j < v.size(); j++)
- m[j] = v[j];
- return i;
+ return TooN::wrapVector(&v[0],
v.size());
}
}
+ };
-
- template<int N> std::istream& from_stream(std::istream&
i, TooN::Matrix<N>& m)
+ /*template<int N> std::istream&
from_stream(std::istream& i, TooN::Matrix<N>& m)
{
std::vector<std::vector<double> > v;
from_stream(i, v);
@@ -234,8 +233,21 @@
m[r][c] = v[r][c];
}
return i;
- }
+ }*/
#endif
+
+ template<class T> T from_stream(std::istream& i)
+ {
+ return FromStream<T>::from(i);
+ }
+
+ template<class T> int from_string(std::string& s, T& t)
+ {
+ std::istringstream is(s);
+ t = from_stream<T>(is);
+ return check_stream(is);
+ }
+
}
}
Index: src/serialize.cc
===================================================================
RCS file: /cvsroot/libcvd/gvars3/src/serialize.cc,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- src/serialize.cc 20 Apr 2009 14:59:39 -0000 1.14
+++ src/serialize.cc 20 Apr 2009 17:05:28 -0000 1.15
@@ -48,9 +48,9 @@
return os.str();
}
- istream& from_stream(istream& in, string& s)
+ string FromStream<string>::from(istream& in)
{
- s.clear();
+ string s;
bool quoted=0;
int c;
@@ -59,7 +59,7 @@
in >> ws;
if((c=in.get()) == EOF)
- return in;
+ return s;
if(c == '"')
quoted=1;
@@ -96,25 +96,21 @@
//Append any trailing parts of an escape sequence
s += escape;
- return in;
+ return s;
}
int check_stream(std::istream& i)
{
+ if(i.good())
+ return 0;
+
if(i.bad())
return 1;
- if(i.good())
+ if(i.fail())
{
- i >> ws;
- char c = i.get();
-
- if(!i.eof())
- {
- i.putback(c);
return -i.tellg();
}
- }
return 0;
}
Index: gvars3/default.h
===================================================================
RCS file: gvars3/default.h
diff -N gvars3/default.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gvars3/default.h 20 Apr 2009 17:05:28 -0000 1.1
@@ -0,0 +1,52 @@
+/*
+ This file is part of the GVars3 Library.
+
+ Copyright (C) 2009 The Authors
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef GV3_INC_DEFUALT_H
+#define GV3_INC_DEFUALT_H
+#include <gvars3/config.h>
+
+#ifdef GVARS3_HAVE_TOON
+#include <TooN/TooN.h>
+#endif
+
+namespace GVars3
+{
+
+template<class C> struct DefaultValue
+{
+ static inline const C val()
+ {
+ return C();
+ }
+};
+
+#ifdef GVARS3_HAVE_TOON
+template<> struct DefaultValue<TooN::Vector<-1> >
+{
+ inline static const TooN::Vector<-1> val()
+ {
+ return TooN::makeVector(0);
+ }
+};
+#endif
+}
+
+#endif
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [libcvd-members] gvars3 gvars3/GStringUtil.h gvars3/gv3_implemen...,
Edward Rosten <=