[svn] commit: r2345 - in /branches/trac172/src/lib/cc: data.cc data.h data_unittests.cc session.cc
BIND 10 source code commits
bind10-changes at lists.isc.org
Wed Jun 30 11:49:10 UTC 2010
Author: jelte
Date: Wed Jun 30 11:49:10 2010
New Revision: 2345
Log:
updated doxy of merge()
made number parser use strol/d
added 'support' for numbers of the form '.1'
IntElement now contains long int instead of int
Modified:
branches/trac172/src/lib/cc/data.cc
branches/trac172/src/lib/cc/data.h
branches/trac172/src/lib/cc/data_unittests.cc
branches/trac172/src/lib/cc/session.cc
Modified: branches/trac172/src/lib/cc/data.cc
==============================================================================
--- branches/trac172/src/lib/cc/data.cc (original)
+++ branches/trac172/src/lib/cc/data.cc Wed Jun 30 11:49:10 2010
@@ -63,7 +63,7 @@
// installed files we define the methods here.
//
bool
-Element::getValue(int& t UNUSED_PARAM) {
+Element::getValue(long int& t UNUSED_PARAM) {
return false;
}
@@ -93,7 +93,7 @@
}
bool
-Element::setValue(const int v UNUSED_PARAM) {
+Element::setValue(const long int v UNUSED_PARAM) {
return false;
}
@@ -209,7 +209,7 @@
}
ElementPtr
-Element::create(const int i) {
+Element::create(const long int i) {
return ElementPtr(new IntElement(i));
}
@@ -344,19 +344,15 @@
return ss.str();
}
-inline int
-count_chars_i(int i) {
- int result = 1;
- if (i < 0) {
- i = -i;
- // account for the '-' symbol
- result += 1;
- }
- while (i > 10) {
- ++result;
- i = i / 10;
- }
- return result;
+static std::string
+number_from_stringstream(std::istream &in, int& pos) {
+ std::stringstream ss;
+ while (isdigit(in.peek()) || in.peek() == '+' || in.peek() == '-' ||
+ in.peek() == '.' || in.peek() == 'e' || in.peek() == 'E') {
+ ss << (char) in.get();
+ }
+ pos += ss.str().size();
+ return ss.str();
}
// Should we change from IntElement and DoubleElement to NumberElement
@@ -364,61 +360,30 @@
// value is larger than an int can handle)
ElementPtr
from_stringstream_number(std::istream &in, int &pos) {
- int i = 0;
+ long int i = 0;
double d = 0.0;
bool is_double = false;
-
- in >> i;
- pos += count_chars_i(i);
- if (in.fail()) {
- isc_throw(JSONError, "Bad integer or overflow");
- }
- if (in.peek() == '.') {
- int d_i = 0;
+ char *endptr;
+
+ std::string number = number_from_stringstream(in, pos);
+
+ i = strtol(number.c_str(), &endptr, 10);
+ if (*endptr != '\0') {
+ d = strtod(number.c_str(), &endptr);
is_double = true;
- in.get();
- pos++;
- in >> d_i;
- if (in.fail()) {
- isc_throw(JSONError, "Bad real or overflow");
- }
- d = (double)d_i / 10;
- while (d > 1.0) {
- d = d / 10;
- }
- if (i < 0) {
- d = - d;
- }
- d += i;
- pos += count_chars_i(d_i);
- }
- if (in.peek() == 'e' || in.peek() == 'E') {
- int e;
- double p;
- in.get();
- pos++;
- in >> e;
- if (in.fail()) {
- isc_throw(JSONError, "Bad exponent or overflow");
- }
- pos += count_chars_i(e);
- p = pow(10, e);
- if (p == HUGE_VAL) {
- isc_throw(JSONError, "Bad exponent or overflow");
- }
- if (is_double) {
- d = d * p;
+ if (*endptr != '\0') {
+ isc_throw(JSONError, std::string("Bad number: ") + number);
} else {
- if (p > 1.0) {
- i = i * p;
- } else {
- // negative exponent, so type becomes a double
- is_double = true;
- d = i * p;
+ if (d == HUGE_VAL) {
+ isc_throw(JSONError, std::string("Number overflow: ") + number);
}
}
- }
-
+ } else {
+ if (i == LONG_MAX || i == LONG_MIN) {
+ isc_throw(JSONError, std::string("Number overflow: ") + number);
+ }
+ }
+
if (is_double) {
return Element::create(d);
} else {
@@ -595,11 +560,9 @@
case '9':
case '0':
case '-':
+ case '+':
+ case '.':
in.putback(c);
- element = from_stringstream_number(in, pos);
- el_read = true;
- break;
- case '+':
element = from_stringstream_number(in, pos);
el_read = true;
break;
Modified: branches/trac172/src/lib/cc/data.h
==============================================================================
--- branches/trac172/src/lib/cc/data.h (original)
+++ branches/trac172/src/lib/cc/data.h Wed Jun 30 11:49:10 2010
@@ -126,7 +126,7 @@
/// If you want an exception-safe getter method, use
/// getValue() below
//@{
- virtual int intValue() { isc_throw(TypeError, "intValue() called on non-integer Element"); };
+ virtual long int intValue() { isc_throw(TypeError, "intValue() called on non-integer Element"); };
virtual double doubleValue() { isc_throw(TypeError, "doubleValue() called on non-double Element"); };
virtual bool boolValue() { isc_throw(TypeError, "boolValue() called on non-Bool Element"); };
virtual std::string stringValue() { isc_throw(TypeError, "stringValue() called on non-string Element"); };
@@ -143,7 +143,7 @@
/// data to the given reference and returning true
///
//@{
- virtual bool getValue(int& t);
+ virtual bool getValue(long int& t);
virtual bool getValue(double& t);
virtual bool getValue(bool& t);
virtual bool getValue(std::string& t);
@@ -159,7 +159,7 @@
/// is of the correct type
///
//@{
- virtual bool setValue(const int v);
+ virtual bool setValue(const long int v);
virtual bool setValue(const double v);
virtual bool setValue(const bool t);
virtual bool setValue(const std::string& v);
@@ -264,7 +264,8 @@
/// represents an empty value, and is created with Element::create())
//@{
static ElementPtr create();
- static ElementPtr create(const int i);
+ static ElementPtr create(const long int i);
+ static ElementPtr create(const int i) { return create(static_cast<long int>(i)); };
static ElementPtr create(const double d);
static ElementPtr create(const bool b);
static ElementPtr create(const std::string& s);
@@ -360,15 +361,15 @@
};
class IntElement : public Element {
- int i;
-
-public:
- IntElement(int v) : Element(integer), i(v) { };
- int intValue() { return i; }
+ long int i;
+
+public:
+ IntElement(long int v) : Element(integer), i(v) { };
+ long int intValue() { return i; }
using Element::getValue;
- bool getValue(int& t) { t = i; return true; };
+ bool getValue(long int& t) { t = i; return true; };
using Element::setValue;
- bool setValue(const int v) { i = v; return true; };
+ bool setValue(const long int v) { i = v; return true; };
void toJSON(std::ostream& ss);
bool equals(ElementPtr other);
};
@@ -499,8 +500,12 @@
/// MapElements.
/// Every string,value pair in other is copied into element
/// (the ElementPtr of value is copied, this is not a new object)
-/// Unless the value is an empty ElementPtr, in which case the
-/// whole key is removed from element.
+/// Unless the value is a NullElement, in which case the
+/// key is removed from element, rather than setting the value to
+/// the given NullElement.
+/// This way, we can remove values from for instance maps with
+/// configuration data (which would then result in reverting back
+/// to the default).
/// Raises a TypeError if either ElementPtr is not a MapElement
void merge(ElementPtr element, const ElementPtr other);
Modified: branches/trac172/src/lib/cc/data_unittests.cc
==============================================================================
--- branches/trac172/src/lib/cc/data_unittests.cc (original)
+++ branches/trac172/src/lib/cc/data_unittests.cc Wed Jun 30 11:49:10 2010
@@ -135,6 +135,7 @@
EXPECT_EQ("100", Element::fromJSON("+1e2")->str());
EXPECT_EQ("-100", Element::fromJSON("-1e2")->str());
EXPECT_EQ("0.01", Element::fromJSON("1e-2")->str());
+ EXPECT_EQ("0.01", Element::fromJSON(".01")->str());
EXPECT_EQ("-0.01", Element::fromJSON("-1e-2")->str());
EXPECT_EQ("1.2", Element::fromJSON("1.2")->str());
EXPECT_EQ("1", Element::fromJSON("1.0")->str());
@@ -153,7 +154,6 @@
// number overflows
EXPECT_THROW(Element::fromJSON("12345678901234567890")->str(), JSONError);
- EXPECT_THROW(Element::fromJSON("1.12345678901234567890")->str(), JSONError);
EXPECT_THROW(Element::fromJSON("1.1e12345678901234567890")->str(), JSONError);
EXPECT_THROW(Element::fromJSON("1e12345678901234567890")->str(), JSONError);
EXPECT_THROW(Element::fromJSON("1e50000")->str(), JSONError);
@@ -164,7 +164,7 @@
// this test checks whether elements throw exceptions if the
// incorrect type is requested
ElementPtr el;
- int i;
+ long int i;
double d;
bool b;
std::string s("asdf");
Modified: branches/trac172/src/lib/cc/session.cc
==============================================================================
--- branches/trac172/src/lib/cc/session.cc (original)
+++ branches/trac172/src/lib/cc/session.cc Wed Jun 30 11:49:10 2010
@@ -66,7 +66,7 @@
virtual void readData(void* data, size_t datalen) = 0;
virtual void startRead(boost::function<void()> user_handler) = 0;
- int sequence_; // the next sequence number to use
+ long int sequence_; // the next sequence number to use
std::string lname_;
ElementPtr queue_;
};
@@ -458,7 +458,7 @@
std::string instance, std::string to)
{
ElementPtr env = Element::createMap();
- int nseq = ++impl_->sequence_;
+ long int nseq = ++impl_->sequence_;
env->set("type", Element::create("send"));
env->set("from", Element::create(impl_->lname_));
@@ -482,7 +482,7 @@
int
Session::reply(ElementPtr& envelope, ElementPtr& newmsg) {
ElementPtr env = Element::createMap();
- int nseq = ++impl_->sequence_;
+ long int nseq = ++impl_->sequence_;
env->set("type", Element::create("send"));
env->set("from", Element::create(impl_->lname_));
More information about the bind10-changes
mailing list