LOOS  v2.3.2
KernelValue.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 
25 #include "KernelValue.hpp"
26 
27 namespace loos {
28 
29  namespace internal {
30 
31  int compare(const Value& x, const Value& y) {
32  float d;
33  int e;
34 
35  if (x.type != y.type)
36  throw(LOOSError("Comparing values with different types."));
37 
38  switch(x.type) {
39  case Value::STRING:
40  e = (*(x.str) == *(y.str));
41  if (!e) {
42  if (*(x.str) < *(y.str))
43  return(-1);
44  else
45  return(1);
46  }
47  return(0);
48 
49  case Value::FLOAT:
50  d = x.flt - y.flt;
51  if (fabs(d) <= FLT_THRESHOLD)
52  return(0);
53  if (d < 0.0)
54  return(-1);
55  return(1);
56 
57  case Value::INT:
58  e = x.itg - y.itg;
59  return(e);
60 
61  case Value::NONE:
62  default:
63  throw(LOOSError("Invalid comparison"));
64 
65  }
66 
67  }
68  }
69 }
Generic LOOS exception.
Definition: exceptions.hpp:40
int compare(const Value &x, const Value &y)
Compare two Value objects, depending on their types.
Definition: KernelValue.cpp:31
Namespace for most things not already encapsulated within a class.
Value class for the LOOS Kernel (virtual machine)
Definition: KernelValue.hpp:51