Savarese Software Research Corporation
NumberVisitor.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WSPR_UTILITY_NUMBER_VISITOR_H
00023 #define __SSRC_WSPR_UTILITY_NUMBER_VISITOR_H
00024 
00025 #include <ssrc/wispers/utility/Properties.h>
00026 
00027 __BEGIN_NS_SSRC_WSPR_UTILITY
00028 
00037 template<typename T>
00038 class NumberVisitor : public boost::static_visitor<void> {
00039   mutable T _value;
00040 
00041 public:
00042 
00043   NumberVisitor(T value) : _value(value) { }
00044 
00045   const T get() const {
00046     return _value;
00047   }
00048 
00049   void operator()(bool v) const {
00050     _value = v;
00051   }
00052 
00053   void operator()(std::int32_t v) const {
00054     _value = v;
00055   }
00056 
00057   void operator()(std::uint32_t v) const {
00058     _value = v;
00059   }
00060 
00061   void operator()(std::int64_t v) const {
00062     _value = v;
00063   }
00064 
00065   void operator()(std::uint64_t v) const {
00066     _value = v;
00067   }
00068 
00069   void operator()(double v) const {
00070     _value = v;
00071   }
00072 
00073   template<typename V>
00074   void operator()(V & v) const { }
00075 };
00076 
00077 template<typename T, typename V>
00078 const T get_number(const V & variant,
00079                    const T & default_value = T())
00080 {
00081   NumberVisitor<T> nv(default_value);
00082   boost::apply_visitor(nv, variant);
00083   return nv.get();
00084 }
00085 
00086 template<typename T>
00087 const T get_number(const Properties & properties,
00088                    const T & default_value = T())
00089 {
00090   if(properties.is_null())
00091     return default_value;
00092   return get_number(*properties.value(), default_value);
00093 }
00094 
00095 template<typename T, typename... P>
00096 const T get_number(const Properties & properties,
00097                    const T & default_value,
00098                    const P & ...p)
00099 {
00100   const Properties *node = properties.find(p...);
00101 
00102   if(node != 0)
00103     return get_number(*node, default_value);
00104 
00105   return default_value;
00106 }
00107 
00108 __END_NS_SSRC_WSPR_UTILITY
00109 
00110 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.