~ubuntu-branches/ubuntu/trusty/mapnik/trusty

« back to all changes in this revision

Viewing changes to include/mapnik/value.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2009-05-20 15:39:58 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090520153958-cf6z1ql9zva4y4dq
Tags: 0.6.0-1ubuntu1
* Merge from debian unstable (LP: #378819), remaining changes:
  - debian/control:
    + Change bdeps from python2.5-dev to python-all-dev (>= 2.5)
    + Change XS-Python-Version from 2.5 to >= 2.5
  - debian/rules:
    + Various changes to enable python2.5 and python2.6 builds
* debian/patches/libtool2_2.diff Dropped. Included upsteam.
* Removed quilt support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
// mapnik
28
28
#include <mapnik/unicode.hpp>
 
29
#include <mapnik/config_error.hpp>
29
30
// boost
30
31
#include <boost/variant.hpp>
 
32
#include <boost/scoped_array.hpp>
 
33
#include <boost/concept_check.hpp>
31
34
// stl
32
35
#include <iostream>
33
36
#include <string>
34
37
#include <sstream>
35
38
#include <iomanip>
 
39
#include <cmath>
 
40
// uci
 
41
#include <unicode/unistr.h>
 
42
#include <unicode/ustring.h>
 
43
 
36
44
 
37
45
namespace mapnik  {
38
 
   
39
 
   typedef boost::variant<int,double,std::wstring> value_base;
 
46
 
 
47
   inline void to_utf8(UnicodeString const& input, std::string & target)
 
48
   {
 
49
      if (input.length() == 0) return;
 
50
      
 
51
      const int BUF_SIZE = 256;
 
52
      char  buf [BUF_SIZE];
 
53
      int len;
 
54
      
 
55
      UErrorCode err = U_ZERO_ERROR;
 
56
      u_strToUTF8(buf, BUF_SIZE, &len, input.getBuffer(), input.length(), &err);
 
57
      if (err == U_BUFFER_OVERFLOW_ERROR || err == U_STRING_NOT_TERMINATED_WARNING ) 
 
58
      {
 
59
         boost::scoped_array<char> buf_ptr(new char [len+1]);
 
60
         err = U_ZERO_ERROR;
 
61
         u_strToUTF8(buf_ptr.get() , len + 1, &len, input.getBuffer(), input.length(), &err);
 
62
         target.assign(buf_ptr.get() , len);
 
63
      }
 
64
      else
 
65
      {
 
66
         target.assign(buf, len);
 
67
      }
 
68
   }
 
69
   
 
70
   struct value_null
 
71
   {
 
72
   };
 
73
   
 
74
   typedef boost::variant<value_null,bool,int,double,UnicodeString> value_base;
40
75
   
41
76
   namespace impl {
42
77
      struct equals
43
78
         : public boost::static_visitor<bool>
44
79
      {
45
80
            template <typename T, typename U>
46
 
            bool operator() (const T &, const U & ) const
 
81
            bool operator() (const T &, const U &) const
47
82
            {
48
83
               return false;
49
84
            }
61
96
        
62
97
            bool operator() (double lhs, int rhs) const
63
98
            {
64
 
               return  lhs == rhs;
 
99
              return  (lhs == rhs)? true : false ;
65
100
            }
66
101
        
67
 
            bool operator() (std::wstring const& lhs, 
68
 
                             std::wstring const& rhs) const
69
 
            {
70
 
               return  lhs == rhs;
 
102
            bool operator() (UnicodeString const& lhs, 
 
103
                             UnicodeString const& rhs) const
 
104
            {
 
105
               return  (lhs == rhs) ? true: false;
 
106
            }
 
107
 
 
108
            bool operator() (value_null, value_null) const
 
109
            {
 
110
               return false;
71
111
            }
72
112
      };
73
113
      
 
114
      struct not_equals
 
115
         : public boost::static_visitor<bool>
 
116
      {
 
117
            template <typename T, typename U>
 
118
            bool operator() (const T &, const U &) const
 
119
            {
 
120
               return true;
 
121
            }
 
122
        
 
123
            template <typename T>
 
124
            bool operator() (T lhs, T rhs) const
 
125
            {
 
126
               return lhs != rhs;
 
127
            }
 
128
            
 
129
            bool operator() (int lhs, double rhs) const
 
130
            {
 
131
               return  lhs != rhs;
 
132
            }
 
133
        
 
134
            bool operator() (double lhs, int rhs) const
 
135
            {
 
136
               return  lhs != rhs;
 
137
            }
 
138
        
 
139
            bool operator() (UnicodeString const& lhs, 
 
140
                             UnicodeString const& rhs) const
 
141
            {
 
142
               return  (lhs != rhs)? true : false;
 
143
            }
 
144
 
 
145
            bool operator() (value_null, value_null) const
 
146
            {
 
147
               return false;
 
148
            }
 
149
 
 
150
            template <typename T>
 
151
            bool operator() (value_null, const T &) const
 
152
            {
 
153
               return false;
 
154
            }
 
155
 
 
156
            template <typename T>
 
157
            bool operator() (const T &, value_null) const
 
158
            {
 
159
               return false;
 
160
            }
 
161
      };
 
162
 
74
163
      struct greater_than
75
164
         : public boost::static_visitor<bool>
76
165
      {
77
166
            template <typename T, typename U>
78
 
            bool operator()( const T &, const U & ) const
 
167
            bool operator()(const T &, const U &) const
79
168
            {
80
169
               return false;
81
170
            }
82
171
        
83
172
            template <typename T>
84
 
            bool operator()( T lhs, T rhs ) const
 
173
            bool operator()(T lhs, T rhs) const
85
174
            {
86
175
               return lhs > rhs;
87
176
            }
96
185
               return  lhs > rhs;
97
186
            }
98
187
        
99
 
            bool operator() (std::wstring const& lhs, std::wstring const& rhs) const
100
 
            {
101
 
               return  lhs > rhs;
 
188
            bool operator() (UnicodeString const& lhs, UnicodeString const& rhs) const
 
189
            {
 
190
              return  (lhs > rhs) ? true : false ;
 
191
            }
 
192
 
 
193
            bool operator() (value_null, value_null) const
 
194
            {
 
195
               return false;
102
196
            }
103
197
      };
104
198
    
106
200
         : public boost::static_visitor<bool>
107
201
      { 
108
202
            template <typename T, typename U>
109
 
            bool operator()( const T &, const U & ) const
 
203
            bool operator()(const T &, const U &) const
110
204
            {
111
205
               return false;
112
206
            }
127
221
               return  lhs >= rhs;
128
222
            }
129
223
        
130
 
            bool operator() (std::wstring const& lhs, std::wstring const& rhs ) const
131
 
            {
132
 
               return lhs >= rhs;
 
224
            bool operator() (UnicodeString const& lhs, UnicodeString const& rhs) const
 
225
            {
 
226
               return ( lhs >= rhs ) ? true : false ;
 
227
            }
 
228
 
 
229
            bool operator() (value_null, value_null) const
 
230
            {
 
231
               return false;
133
232
            }
134
233
      };
135
234
    
137
236
         : public boost::static_visitor<bool>
138
237
      { 
139
238
            template <typename T, typename U>
140
 
            bool operator()( const T &, const U & ) const
 
239
            bool operator()(const T &, const U &) const
141
240
            {
142
241
               return false;
143
242
            }
144
243
        
145
244
            template <typename T>
146
 
            bool operator()( T  lhs,T  rhs) const
 
245
            bool operator()(T lhs, T rhs) const
147
246
            {
148
247
               return lhs < rhs;
149
248
            }
158
257
               return  lhs < rhs;
159
258
            }
160
259
        
161
 
            bool operator()( std::wstring const& lhs, 
162
 
                             std::wstring const& rhs ) const
163
 
            {
164
 
               return lhs < rhs;
 
260
            bool operator()(UnicodeString const& lhs, 
 
261
                            UnicodeString const& rhs ) const
 
262
            {
 
263
              return (lhs < rhs) ? true : false ;
 
264
            }
 
265
 
 
266
            bool operator() (value_null, value_null) const
 
267
            {
 
268
               return false;
165
269
            }
166
270
      };
167
271
 
169
273
         : public boost::static_visitor<bool>
170
274
      { 
171
275
            template <typename T, typename U>
172
 
            bool operator()( const T &, const U & ) const
 
276
            bool operator()(const T &, const U &) const
173
277
            {
174
278
               return false;
175
279
            }
176
280
        
177
281
            template <typename T>
178
 
            bool operator()(T lhs, T rhs ) const
 
282
            bool operator()(T lhs, T rhs) const
179
283
            {
180
284
               return lhs <= rhs;
181
285
            }
190
294
               return  lhs <= rhs;
191
295
            }
192
296
        
193
 
            template <typename T>
194
 
            bool operator()( std::wstring const& lhs, 
195
 
                             std::wstring const& rhs ) const
196
 
            {
197
 
               return lhs <= rhs;
 
297
            bool operator()(UnicodeString const& lhs, 
 
298
                            UnicodeString const& rhs ) const
 
299
            {
 
300
              return (lhs <= rhs) ? true : false ;
 
301
            }
 
302
 
 
303
            bool operator() (value_null, value_null) const
 
304
            {
 
305
               return false;
198
306
            }
199
307
      };
200
308
    
213
321
               return lhs + rhs ;
214
322
            }
215
323
        
216
 
            value_type operator() (std::wstring const& lhs , 
217
 
                                   std::wstring const& rhs ) const
 
324
            value_type operator() (UnicodeString const& lhs , 
 
325
                                   UnicodeString const& rhs ) const
218
326
            {
219
327
               return lhs + rhs;
220
328
            }
245
353
               return lhs - rhs ;
246
354
            }
247
355
 
248
 
            value_type operator() (std::wstring const& lhs,
249
 
                                   std::wstring const& ) const
 
356
            value_type operator() (UnicodeString const& lhs,
 
357
                                   UnicodeString const& ) const
250
358
            {
251
359
               return lhs;
252
360
            }
277
385
               return lhs * rhs;
278
386
            }
279
387
        
280
 
            value_type operator() (std::wstring const& lhs,
281
 
                                   std::wstring const& ) const
 
388
            value_type operator() (UnicodeString const& lhs,
 
389
                                   UnicodeString const& ) const
282
390
            {
283
391
               return lhs;
284
392
            }   
310
418
               return lhs / rhs;
311
419
            }
312
420
        
313
 
            value_type operator() (std::wstring const& lhs,
314
 
                                   std::wstring const&) const
315
 
            {
316
 
               return lhs;
317
 
            }
318
 
        
319
 
            value_type operator() (double lhs, int rhs) const
320
 
            {
321
 
               return lhs / rhs;
322
 
            }
323
 
        
324
 
            value_type operator() (int lhs, double rhs) const
325
 
            {
326
 
               return lhs / rhs;
327
 
            }
328
 
      };
329
 
    
 
421
            value_type operator() (bool lhs, bool rhs ) const
 
422
            {
 
423
               boost::ignore_unused_variable_warning(lhs);
 
424
               boost::ignore_unused_variable_warning(rhs);
 
425
               return false;
 
426
            }
 
427
   
 
428
            value_type operator() (UnicodeString const& lhs,
 
429
                                   UnicodeString const&) const
 
430
            {
 
431
               return lhs;
 
432
            }
 
433
        
 
434
            value_type operator() (double lhs, int rhs) const
 
435
            {
 
436
               return lhs / rhs;
 
437
            }
 
438
        
 
439
            value_type operator() (int lhs, double rhs) const
 
440
            {
 
441
               return lhs / rhs;
 
442
            }
 
443
      };
 
444
 
 
445
      template <typename V>
 
446
      struct mod: public boost::static_visitor<V>
 
447
      { 
 
448
            typedef V value_type;
 
449
            template <typename T1, typename T2>
 
450
            value_type operator() (T1 const& lhs, T2 const&) const
 
451
            {
 
452
               return lhs;
 
453
            }
 
454
            
 
455
            template <typename T>
 
456
            value_type operator() (T lhs, T rhs) const
 
457
            {
 
458
               return lhs % rhs;
 
459
            }
 
460
        
 
461
            value_type operator() (UnicodeString const& lhs,
 
462
                                   UnicodeString const&) const
 
463
            {
 
464
               return lhs;
 
465
            }
 
466
        
 
467
            value_type operator() (bool lhs,
 
468
                                   bool rhs) const
 
469
           {
 
470
               boost::ignore_unused_variable_warning(lhs);
 
471
               boost::ignore_unused_variable_warning(rhs);
 
472
               return false;
 
473
            }
 
474
        
 
475
            value_type operator() (double lhs, int rhs) const
 
476
            {
 
477
               return fmod(lhs, rhs);
 
478
            }
 
479
        
 
480
            value_type operator() (int lhs, double rhs) const
 
481
            {
 
482
               return fmod(lhs, rhs);
 
483
            }
 
484
        
 
485
            value_type operator() (double lhs, double rhs) const
 
486
            {
 
487
               return fmod(lhs, rhs);
 
488
            }
 
489
      };
 
490
        
 
491
      struct to_bool : public boost::static_visitor<bool>
 
492
      {
 
493
                
 
494
            template <typename T>
 
495
            bool operator() (T val) const
 
496
            {
 
497
               boost::ignore_unused_variable_warning(val);
 
498
               throw config_error("Boolean value expected");
 
499
            }
 
500
 
 
501
            bool operator() (bool val) const
 
502
            {
 
503
               return val;
 
504
            }
 
505
      };
 
506
 
330
507
      struct to_string : public boost::static_visitor<std::string>
331
508
      {
332
509
                
338
515
               return ss.str();
339
516
            }
340
517
            // specializations 
341
 
            std::string operator() (std::wstring const& val) const
 
518
            std::string operator() (UnicodeString const& val) const
342
519
            {
343
 
               std::stringstream ss;
344
 
               std::wstring::const_iterator pos = val.begin();
345
 
               ss << std::hex ;
346
 
               for (;pos!=val.end();++pos)
347
 
               {
348
 
                  wchar_t c = *pos;
349
 
                  if (c < 0x80) 
350
 
                  {
351
 
                     ss << char(c);
352
 
                  }
353
 
                  else
354
 
                  {
355
 
                     ss << "\\x";
356
 
                     unsigned c0 = (c >> 8) & 0xff;
357
 
                     if (c0) ss << c0;
358
 
                     ss << (c & 0xff);
359
 
                  }
360
 
               }
361
 
               return ss.str();
 
520
               std::string utf8;
 
521
               to_utf8(val,utf8);
 
522
               return utf8;
362
523
            }
363
524
            
364
525
            std::string operator() (double val) const
367
528
               ss << std::setprecision(16) << val;
368
529
               return ss.str();
369
530
            }
370
 
      };
 
531
            
 
532
            std::string operator() (value_null const& val) const
 
533
            {
 
534
               boost::ignore_unused_variable_warning(val);
 
535
               return "";
 
536
            }
 
537
       };
371
538
 
372
 
      struct to_unicode : public boost::static_visitor<std::wstring>
 
539
      struct to_unicode : public boost::static_visitor<UnicodeString>
373
540
      {
374
541
                
375
542
            template <typename T>
376
 
            std::wstring operator() (T val) const
 
543
            UnicodeString operator() (T val) const
377
544
            {
378
 
               std::basic_ostringstream<wchar_t> out;
 
545
               std::basic_ostringstream<char> out;
379
546
               out << val;
380
 
               return out.str();
 
547
               return UnicodeString(out.str().c_str());
381
548
            }
382
549
 
383
550
            // specializations 
384
 
            std::wstring const& operator() (std::wstring const& val) const
 
551
            UnicodeString const& operator() (UnicodeString const& val) const
385
552
            {
386
553
               return val;
387
554
            }
388
555
 
389
 
            std::wstring operator() (double val) const
 
556
            UnicodeString operator() (double val) const
390
557
            {
391
 
               std::basic_ostringstream<wchar_t> out;
 
558
               std::basic_ostringstream<char> out;
392
559
               out << std::setprecision(16) << val;
393
 
               return out.str();
 
560
               return UnicodeString(out.str().c_str());
 
561
            }
 
562
            
 
563
            UnicodeString operator() (value_null const& val) const
 
564
            {
 
565
               boost::ignore_unused_variable_warning(val);
 
566
               return UnicodeString("");
394
567
            }
395
568
      };
396
569
      
397
570
      struct to_expression_string : public boost::static_visitor<std::string>
398
571
      {
399
 
            std::string operator() (std::wstring const& val) const
 
572
            std::string operator() (UnicodeString const& val) const
400
573
            {
401
 
               std::stringstream ss;
402
 
               std::wstring::const_iterator pos = val.begin();
403
 
               ss << std::hex ;
404
 
               for (;pos!=val.end();++pos)
405
 
               {
406
 
                  wchar_t c = *pos;
407
 
                  if (c < 0x80) 
408
 
                  {
409
 
                     ss << char(c);
410
 
                  }
411
 
                  else
412
 
                  {
413
 
                     ss << "\\x";
414
 
                     unsigned c0 = (c >> 8) & 0xff;
415
 
                     if (c0) ss << c0;
416
 
                     ss << (c & 0xff);
417
 
                  }
418
 
               }
419
 
               return "\'" + ss.str() + "\'";
 
574
               std::string utf8;
 
575
               to_utf8(val,utf8);
 
576
               return "'" + utf8 + "'";
420
577
            } 
421
578
            
422
579
            template <typename T>
433
590
               ss << std::setprecision(16) << val;
434
591
               return ss.str();
435
592
            }
 
593
 
 
594
            std::string operator() (value_null const& val) const
 
595
            {
 
596
               boost::ignore_unused_variable_warning(val);
 
597
               return "null";
 
598
            }
436
599
      };
437
600
   }
438
 
    
 
601
 
439
602
   class value
440
603
   {
441
604
         value_base base_;
443
606
         friend const value operator-(value const&,value const&);
444
607
         friend const value operator*(value const&,value const&);
445
608
         friend const value operator/(value const&,value const&);
 
609
         friend const value operator%(value const&,value const&);
446
610
        
447
611
      public:
448
612
         value ()
449
 
            : base_(0) {}
 
613
            : base_(value_null()) {}
450
614
        
451
615
         template <typename T> value(T _val_)
452
616
            : base_(_val_) {}
458
622
 
459
623
         bool operator!=(value const& other) const
460
624
         {
461
 
            return !(boost::apply_visitor(impl::equals(),base_,other.base_));
 
625
            return boost::apply_visitor(impl::not_equals(),base_,other.base_);
462
626
         }
463
627
        
464
628
         bool operator>(value const& other) const
486
650
            return base_;
487
651
         }
488
652
 
 
653
         bool to_bool() const
 
654
         {
 
655
            return boost::apply_visitor(impl::to_bool(),base_);
 
656
         }
 
657
 
489
658
         std::string to_expression_string() const
490
659
         {
491
660
            return boost::apply_visitor(impl::to_expression_string(),base_);
496
665
            return boost::apply_visitor(impl::to_string(),base_);
497
666
         }
498
667
         
499
 
         std::wstring to_unicode() const
 
668
         UnicodeString to_unicode() const
500
669
         {
501
670
            return boost::apply_visitor(impl::to_unicode(),base_);
502
671
         }
526
695
      return value(boost::apply_visitor(impl::div<value>(),p1.base_, p2.base_));
527
696
   }
528
697
 
 
698
   inline const value operator%(value const& p1,value const& p2)
 
699
   {
 
700
 
 
701
      return value(boost::apply_visitor(impl::mod<value>(),p1.base_, p2.base_));
 
702
   }
 
703
 
529
704
  template <typename charT, typename traits>
530
705
  inline std::basic_ostream<charT,traits>& 
531
706
  operator << (std::basic_ostream<charT,traits>& out,