PageRenderTime 88ms CodeModel.GetById 60ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/doc/src/docutils/tools/implementation_status/implementation_status.cpp

http://hadesmem.googlecode.com/
C++ | 402 lines | 314 code | 65 blank | 23 comment | 24 complexity | f3212849103b05cbb6e54bec46f2f4b6 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Tool reporting Implementation Status in QBK format
  3
  4// Copyright (c) 2011 Barend Gehrels, Amsterdam, the Netherlands.
  5// Copyright (c) 2011 Bruno Lalande, Paris, France.
  6
  7// Use, modification and distribution is subject to the Boost Software License,
  8// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9// http://www.boost.org/LICENSE_1_0.txt)
 10
 11#include <iostream>
 12#include <fstream>
 13#include <sstream>
 14#include <string>
 15#include <vector>
 16
 17#include <stdlib.h>
 18
 19#include <boost/timer.hpp>
 20#include <boost/algorithm/string/predicate.hpp>
 21#include <boost/algorithm/string/replace.hpp>
 22#include <boost/algorithm/string/trim.hpp>
 23
 24static const int point = 0;
 25static const int segment = 1;
 26static const int box = 2;
 27static const int linestring = 3;
 28static const int ring = 4;
 29static const int polygon = 5;
 30static const int multi_point = 6;
 31static const int multi_linestring = 7;
 32static const int multi_polygon = 8;
 33static const int geometry_count = 9;
 34
 35
 36struct compile_bjam
 37{
 38    static inline bool apply(int type1, int type2)
 39    {
 40        std::ostringstream command;
 41        // For debugging: 
 42        command << "bjam -a tmp > tmp/t" << type1 << "_" << type2 << ".out";
 43        //command << "bjam -a tmp > tmp/t.out";
 44        int failed = system(command.str().c_str());
 45        return failed == 0;
 46    }
 47};
 48
 49struct compile_msvc
 50{
 51    bool first;
 52    int count;
 53
 54    compile_msvc()
 55        : first(true)
 56        , count(0)
 57    {}
 58
 59    inline bool apply(int type1, int type2)
 60    {
 61        std::ostringstream command;
 62        command << "cl /nologo -I. -I/_svn/boost/trunk /EHsc /Y";
 63        if (first)
 64        {
 65            std::cout << " (creating PCH)";
 66            command << "c";
 67            first = false;
 68        }
 69        else
 70        {
 71            command <<  "u";
 72        }
 73
 74        command << "implementation_status.hpp tmp/t.cpp > tmp/t" //.out";
 75            // For debugging: 
 76            << type1 << "_" << type2 << ".out"; 
 77
 78        int failed = system(command.str().c_str());
 79        return failed == 0;
 80    }
 81};
 82
 83struct algorithm
 84{
 85    std::string name;
 86    int arity;
 87
 88    explicit algorithm(std::string const& n, int a = 1)
 89        : name(n)
 90        , arity(a)
 91    {}
 92};
 93
 94
 95inline std::string bool_string(bool v)
 96{
 97    return v ? "true" : "false";
 98}
 99
100inline std::string typedef_string(int type, bool clockwise, bool open)
101{
102    std::ostringstream out;
103    switch(type)
104    {
105        case point : return "P";
106        case linestring : return "bg::model::linestring<P>";
107        case box : return "bg::model::box<P>";
108        case segment : return "bg::model::segment<P>";
109        case ring :
110            out << "bg::model::ring<P, "
111                << bool_string(clockwise) << ", " << bool_string(open) << ">";
112            break;
113        case polygon : 
114            out << "bg::model::polygon<P, "
115                << bool_string(clockwise) << ", " << bool_string(open) << ">";
116            break;
117        case multi_point : return "bg::model::multi_point<P>";
118        case multi_linestring : 
119            out << "bg::model::multi_linestring<bg::model::linestring<P> >";
120            break;
121        case multi_polygon : 
122            out << "bg::model::multi_polygon<bg::model::polygon<P, "
123                << bool_string(clockwise) << ", " << bool_string(open) << "> >";
124            break;
125    }
126    return out.str();
127}
128
129inline std::string wkt_string(int type)
130{
131    switch(type)
132    {
133        case point : return "POINT(1 1)";
134        case linestring : return "LINESTRING(1 1,2 2)";
135        case segment : return "LINESTRING(1 1,2 2)";
136        case box : return "POLYGON((1 1,2 2)";
137        case polygon : 
138        case ring :
139            return "POLYGON((0 0,0 1,1 1,0 0))";
140        case multi_point : return "MULTIPOINT((1 1),(2 2))";
141        case multi_linestring : return "MULTILINESTRING((1 1,2 2))";
142        case multi_polygon : return "MULTIPOLYGON(((0 0,0 1,1 1,0 0)))";
143    }
144    return "";
145}
146
147inline std::string geometry_string(int type)
148{
149    switch(type)
150    {
151        case point : return "Point";
152        case linestring : return "Linestring";
153        case box : return "Box";
154        case polygon : return "Polygon"; 
155        case ring : return "Ring";
156        case segment : return "Segment";
157        case multi_point : return "MultiPoint";
158        case multi_linestring : return "MultiLinestring";
159        case multi_polygon : return "MultiPolygon";
160    }
161    return "";
162}
163
164
165template <typename CompilePolicy>
166int report_library(CompilePolicy& compile_policy,
167                   int type, algorithm const& algo, bool clockwise,
168                   bool open, int dimensions, std::string const& cs,
169                   int type2 = -1)
170{
171    std::string lit;
172    {
173        std::ostringstream out;
174        out << geometry_string(type);
175        if (type2 != -1)
176        {
177            out << "_" << geometry_string(type2);
178        }
179        out 
180            << "_" << algo.name
181            << "_" << bool_string(clockwise) 
182            << "_" << bool_string(open)
183            << "_" << boost::replace_all_copy
184                        (
185                            boost::replace_all_copy
186                                (
187                                    boost::replace_all_copy(cs, "bg::", "")
188                                , "<", "_"
189                                )
190                            , ">", "_"
191                        );
192        lit = out.str();
193    }
194
195    std::cout << lit;
196
197    {
198        std::ofstream out("tmp/t.cpp");
199
200        out
201            << "#include <implementation_status.hpp>" << std::endl
202            << "template <typename P>" << std::endl
203            << "inline void test()" << std::endl
204            << "{" << std::endl
205            << "  namespace bg = boost::geometry;" << std::endl
206            << "  " << typedef_string(type, clockwise, open) << " geometry;" << std::endl
207            << "  bg::read_wkt(\"" << wkt_string(type) << "\", geometry);" << std::endl;
208
209        
210        if (algo.arity > 1)
211        {
212            out 
213                << "  " << typedef_string(type2, clockwise, open) << " geometry2;" << std::endl
214                << "  bg::read_wkt(\"" << wkt_string(type2) << "\", geometry2);" << std::endl;
215        }
216
217        switch(algo.arity)
218        {
219            case 1 : 
220                out << "  bg::" << algo.name << "(geometry);" << std::endl; 
221                break;
222            case 2 : 
223                out << "  bg::" << algo.name << "(geometry, geometry2);" << std::endl; 
224                break;
225        }
226
227        out
228            << "}" << std::endl
229            << std::endl
230            ;
231
232        out
233            << "int main()" << std::endl
234            << "{" << std::endl
235            << "  namespace bg = boost::geometry;" << std::endl
236            << "  test<bg::model::point< double, " << dimensions << ", bg::cs::" << cs << " > >();" << std::endl
237            << "  return 0;" << std::endl
238            << "}" << std::endl
239            << std::endl
240            ;
241    }
242
243    bool result = compile_policy.apply(type, type2);
244    if (! result)
245    {
246        std::cout << " ERROR";
247    }
248    std::cout << std::endl;
249    return result;
250}
251
252
253template <typename CompilePolicy>
254std::vector<int> report(CompilePolicy& compile_policy,
255                        int type, algorithm const& algo, bool clockwise,
256                        bool open, int dimensions, std::string const& cs)
257{
258    std::vector<int> result;
259
260    switch(algo.arity)
261    {
262        case 1 :
263            result.push_back(report_library(compile_policy, type, algo, clockwise, open, dimensions, cs));
264            break;
265        case 2 :
266            for (int type2 = point; type2 < geometry_count; ++type2)
267            {
268                result.push_back(report_library(compile_policy, type, algo, clockwise, open, dimensions, cs, type2));
269            }
270            break;
271    }
272
273    return result;
274}
275
276
277struct cs
278{
279    std::string name;
280
281    cs(std::string const& n)
282        : name(n)
283    {}
284};
285
286
287int main(int argc, char** argv)
288{
289#if defined(_MSC_VER)
290    compile_msvc compile_policy;
291#else
292    compile_bjam compile_policy;
293#endif
294
295
296    typedef std::vector<algorithm> v_a_type;
297    v_a_type algorithms;
298    algorithms.push_back(algorithm("area"));
299    algorithms.push_back(algorithm("length"));
300    algorithms.push_back(algorithm("perimeter"));
301    algorithms.push_back(algorithm("correct"));
302    algorithms.push_back(algorithm("distance", 2));
303    algorithms.push_back(algorithm("centroid", 2));
304    algorithms.push_back(algorithm("intersects", 2));
305    algorithms.push_back(algorithm("within", 2));
306    algorithms.push_back(algorithm("equals", 2));
307
308    typedef std::vector<cs> cs_type;
309    cs_type css;
310    css.push_back(cs("cartesian"));
311    // css.push_back(cs("spherical<bg::degree>"));
312    // css.push_back(cs("spherical<bg::radian>"));
313
314
315    boost::timer timer;
316
317    for (v_a_type::const_iterator it = algorithms.begin(); it != algorithms.end(); ++it)
318    {
319/*([heading Behavior]
320[table
321[[Case] [Behavior] ]
322[[__2dim__][All combinations of: box, ring, polygon, multi_polygon]]
323[[__other__][__nyiversion__]]
324[[__sph__][__nyiversion__]]
325[[Three dimensional][__nyiversion__]]
326]*/
327
328        std::ostringstream name;
329        name << "../../../../generated/" << it->name << "_status.qbk";
330
331        std::ofstream out(name.str().c_str());
332        out << "[heading Supported geometries]" << std::endl;
333
334        cs_type::const_iterator cit = css.begin();
335
336        {
337            // Construct the table
338
339            std::vector<std::vector<int> > table;
340
341            for (int type = point; type < geometry_count; type++)
342            {
343                table.push_back(report(compile_policy, type, *it, true, true, 2, cit->name));
344            }
345
346
347            // Detect red rows/columns
348
349            std::vector<int> lines_status(table.size(), false);
350            std::vector<int> columns_status(table[0].size(), false);
351
352            for (unsigned int i = 0; i != table.size(); ++i)
353            {
354                for (unsigned int j = 0; j != table[i].size(); ++j)
355                {
356                    lines_status[i] |= table[i][j];
357                    columns_status[j] |= table[i][j];
358                }
359            }
360
361
362            // Display the table
363
364            out << "[table" << std::endl << "[";
365
366            if (it->arity > 1)
367            {
368                out << "[ ]";
369                for (int type = point; type < geometry_count; type++)
370                {
371                    if (!columns_status[type]) continue;
372                    out << "[" << geometry_string(type) << "]";
373                }
374            }
375            else
376            {
377                out << "[Geometry][Status]";
378            }
379
380            out << "]" << std::endl;
381
382            for (unsigned int i = 0; i != table.size(); ++i)
383            {
384                if (!lines_status[i]) continue;
385                out << "[";
386                out << "[" << geometry_string(i) << "]";
387                for (unsigned int j = 0; j != table[i].size(); ++j)
388                {
389                    if (!columns_status[j]) continue;
390                    out << "[[$img/" << (table[i][j] ? "ok" : "nyi") << ".png]]";
391                }
392                out << "]" << std::endl;
393            }
394
395            out << "]" << std::endl;
396        }
397    }
398
399    std::cout << "TIME: " << timer.elapsed() << std::endl;
400
401    return 0;
402}