Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - PropertiesToString.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 38 47 80.9 %
Date: 2012-04-09 Functions: 10 13 76.9 %
Branches: 13 22 59.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the PropertiesToString class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_UTILITY_PROPERTIES_TO_STRING_H
      23                 :            : #define __SSRC_WSPR_UTILITY_PROPERTIES_TO_STRING_H
      24                 :            : 
      25                 :            : #include <iosfwd>
      26                 :            : 
      27                 :            : #include <ssrc/wispers/utility/Properties.h>
      28                 :            : #include <ssrc/wispers/utility/ToString.h>
      29                 :            : 
      30                 :            : __BEGIN_NS_SSRC_WSPR_UTILITY
      31                 :            : 
      32                 :            : void escape_lua(string & result,
      33                 :            :                 const char *text, const unsigned int text_size);
      34                 :            : 
      35                 :         10 : inline void escape_lua(string & result, const string & text) {
      36                 :         10 :   escape_lua(result, text.c_str(), text.size());
      37                 :         10 : }
      38                 :            : 
      39                 :         10 : inline string escape_lua(const string & text) {
      40                 :         10 :   string result;
      41         [ +  - ]:         10 :   escape_lua(result, text);
      42                 :         10 :   return result;
      43                 :            : }
      44                 :            : 
      45                 :            : // Converts Properties to a string representation that can be read by a
      46                 :            : // Lua interpreter.  Probably should be moved to the lua package because
      47                 :            : // we could also have JSON and other string representations.
      48                 :          4 : class PropertiesToString : public boost::static_visitor<void> {
      49                 :            : public:
      50                 :            :   enum ToFormat { ToDefault = 0, ToLua = 0, ToJSON = 1 };
      51                 :            : 
      52                 :            : private:
      53                 :            :   const char * const * const _token;
      54                 :            :   std::ostream & _out;
      55                 :            : 
      56                 :            : public:
      57                 :            : 
      58                 :            :   explicit PropertiesToString(std::ostream & out,
      59                 :            :                               const ToFormat to_format = ToDefault);
      60                 :            : 
      61                 :            :   void operator()(const primitive_property_vector & v) const;
      62                 :            :   void operator()(const property_vector & v) const;
      63                 :            : 
      64                 :          2 :   void operator()(const bool v) const {
      65         [ +  - ]:          2 :     _out << (v ? "true" : "false");
      66                 :          2 :   }
      67                 :            : 
      68                 :            :   // Convert 64-bit integers to string representation because Lua/JavaScript
      69                 :            :   // usually support only 32-bit integers.
      70                 :          0 :   void operator()(const std::int64_t v) const {
      71                 :          0 :     _out << "\"" << v << "\"";
      72                 :          0 :   }
      73                 :            : 
      74                 :          0 :   void operator()(const std::uint64_t v) const {
      75                 :          0 :     _out << "\"" << v << "\"";
      76                 :          0 :   }
      77                 :            : 
      78                 :            :   // Convert 32-bit unsigned integers to equivalent 2's complement
      79                 :            :   // signed integer, allowing the original value to be recovered via
      80                 :            :   // a cast when read back.
      81                 :          2 :   void operator()(const std::uint32_t v) const {
      82                 :          2 :     _out << static_cast<std::int32_t>(v);
      83                 :          2 :   }
      84                 :            : 
      85                 :         10 :   void operator()(const string & s) const {
      86   [ +  -  +  -  :         10 :     _out << "\"" << escape_lua(s) << "\"";
                   +  - ]
      87                 :         10 :   }
      88                 :            : 
      89                 :         26 :   void operator()(const Properties & properties) const {
      90         [ -  + ]:         26 :     if(properties.is_leaf()) {
      91                 :          0 :       const property_type *v = properties.value();
      92         [ #  # ]:          0 :       if(v != 0) {
      93                 :          0 :         boost::apply_visitor(*this, *v);
      94                 :            :       }
      95                 :            :     } else {
      96                 :         26 :       _out << "{";
      97                 :         26 :       properties.visit(*this);
      98                 :         26 :       _out << "}";
      99                 :            :     }
     100                 :         26 :   }
     101                 :            : 
     102                 :            :   template<typename T>
     103                 :         44 :   void operator()(const T & v) const {
     104                 :         44 :     _out << v;
     105                 :         44 :   }
     106                 :            : 
     107                 :            :   void enter(const std::string & key, const Properties *props,
     108                 :            :              const bool is_last_child) const;
     109                 :            : 
     110                 :         54 :   void leave(const std::string & key, const Properties *props,
     111                 :            :              const bool is_last_child) const
     112                 :            :   {
     113         [ +  + ]:         54 :     if(props->is_leaf()) {
     114         [ +  + ]:         43 :       if(!is_last_child) {
     115                 :         10 :         _out << ",";
     116                 :            :       }
     117                 :            :     } else {
     118         [ +  + ]:         11 :       if(is_last_child) {
     119                 :          4 :         _out << "}";
     120                 :            :       } else {
     121                 :          7 :         _out << "},";
     122                 :            :       }
     123                 :            :     }
     124                 :         54 :   }
     125                 :            : };
     126                 :            : 
     127                 :            : inline
     128                 :          3 : std::ostream & operator<<(std::ostream & out, const Properties & properties) {
     129                 :          6 :   PropertiesToString tostring(out);
     130         [ +  - ]:          3 :   tostring(properties);
     131                 :          6 :   return out;
     132                 :            : }
     133                 :            : 
     134                 :            : __END_NS_SSRC_WSPR_UTILITY
     135                 :            : 
     136                 :            : #endif