Remove dynamic exceptions
[anna.git] / example / comm / codec / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <iostream>
10
11 #include <anna/core/util/CommandLine.hpp>
12 #include <anna/core/DataBlock.hpp>
13 #include <anna/core/tracing/TraceWriter.hpp>
14 #include <anna/core/functions.hpp>
15
16 #include <anna/app/Application.hpp>
17
18 #include <anna/comm/comm.hpp>
19
20 using namespace anna;
21
22 class MyCodec : public Codec {
23 public:
24    MyCodec () : Codec (0xaa) ,
25       a_dataBlock (true)
26    {
27       attach ("string", a_string);
28       attach ("integer", a_integer);
29       attach ("block", a_dataBlock);
30       attach ("string", a_string2);
31       attach ("float", a_float);
32       attach ("double", a_double);
33    }
34    
35    void set (const std::string& value) { a_string = a_string2 = value; a_string2 += value; }
36    void set (const int value) { a_integer = value; }
37    void set (const anna::DataBlock& value) { a_dataBlock = value; }
38    void set (const float value) { a_float = value; }
39    void set (const double value) { a_double = value; }
40    
41    const std::string& getString () const { return a_string; }
42    const int getInteger () const { return a_integer; }
43    const anna::DataBlock& getDataBlock () const noexcept(false) { return a_dataBlock; }
44    const std::string& getString2 () const { return a_string2; }
45    float getFloat () const { return a_float; }
46    float getDouble () const { return a_double; }
47    
48 private:
49    std::string a_string;
50    int a_integer;
51    anna::DataBlock a_dataBlock;
52    std::string a_string2;
53    float a_float;
54    double a_double;
55 };
56
57 class Test : public anna::app::Application {
58 public:
59    Test ();
60    
61 private:
62    MyCodec a_input;
63    MyCodec a_output;
64
65    void run () noexcept(false);      
66 };
67
68 using namespace std;
69 using namespace anna;
70
71 int main (int argc, const char** argv)
72 {
73    CommandLine& commandLine (CommandLine::instantiate ());
74    Test test;
75    TraceWriter* traceWriter = new TraceWriter;
76
77    try {
78       commandLine.initialize (argv, argc);
79       commandLine.verify ();
80
81       Logger::initialize ("testfunctions", traceWriter);
82       Logger::setLevel (Logger::Debug); 
83  
84       test.start ();
85    }
86    catch (Exception& ex) {
87       cout << ex.asString () << endl;
88    }
89    
90    delete traceWriter;
91    
92    return 0;
93 }
94
95 Test::Test () : 
96    anna::app::Application ("testfunctions", "Comprobacion de la clase comm::Codec", "1.0.0") 
97 {
98    CommandLine& commandLine (CommandLine::instantiate ());
99       
100    commandLine.add ("string", CommandLine::Argument::Mandatory, "Cadena a enviar");
101    commandLine.add ("i", CommandLine::Argument::Mandatory, "Enterno a enviar");
102 }
103
104 void Test::run () 
105    noexcept(false)
106 {
107    CommandLine& cm (CommandLine::instantiate ());
108    
109    const char* string = cm.getValue ("string");
110
111    float ff;
112    double dd;
113    
114    a_input.set (string);
115    a_input.set (cm.getIntegerValue ("i"));
116    a_input.set (ff = 1.12345);
117    a_input.set (dd = 2.12345);
118    
119    DataBlock dataBlock (true);
120    
121    for (int i = strlen (string) - 1; i > 0; i --)
122       dataBlock += string [i];
123       
124    a_input.set (dataBlock);
125
126    const DataBlock& result (a_input.code ());
127
128    a_output.decode (result);
129    
130    cout << "String: " << a_output.getString () << endl;
131    cout << "Integer: " << a_output.getInteger () << endl;
132    cout << "Data block: " << anna::functions::asString (a_output.getDataBlock ()) << endl;
133    cout << "String2: " << a_output.getString2 () << endl << endl;
134    cout << "Float: " << a_output.getFloat () << endl << endl;
135    cout << "Double: " << a_output.getDouble () << endl << endl;
136
137    /*
138     * Para comprobar que el modo de codificar los bytes son igual que en la versiĆ³n de Java
139     */
140    char buffer [sizeof (S64)];
141
142    short ss = 23456;
143    comm::functions::codeShort (buffer, ss);
144    dataBlock.clear ();
145    dataBlock.append (buffer, sizeof (ss));
146    cout << anna::functions::asString ("Short: %d - 0x%x", ss, ss);
147    cout << anna::functions::asString (dataBlock) << endl << endl;
148    ss = comm::functions::decodeShort (buffer);
149    cout << anna::functions::asString ("Short2: %d - 0x%x\n\n", ss, ss);
150
151    int xx = 123456;
152    comm::functions::codeInteger (buffer, xx);
153    dataBlock.clear ();
154    dataBlock.append (buffer, sizeof (xx));
155    cout << anna::functions::asString ("Integer: %d - 0x%x", xx, xx);
156    cout << anna::functions::asString (dataBlock) << endl << endl;
157    xx = comm::functions::decodeInteger (buffer);
158    cout << anna::functions::asString ("Integer2: %d - 0x%x\n\n", xx, xx);
159
160 //   S64 ll = 31604938272LL;
161    S64 ll = 98765432101234LL;
162    comm::functions::codeInteger64 (buffer, ll);
163    dataBlock.clear ();
164    dataBlock.append (buffer, sizeof (ll));
165    cout << anna::functions::asString ("Integer64: %lld - 0x%llx", ll, ll);
166    cout << anna::functions::asString (dataBlock) << endl << endl;
167    ll = comm::functions::decodeInteger64 (buffer);
168    cout << anna::functions::asString ("Integer64-2: %lld - 0x%llx\n\n", ll, ll);
169   
170    cout << "Sizeof (float/int): " << sizeof (float) << "/" << sizeof (int) << endl;
171
172    ff = -123.3232;
173    int ii;
174    anna_memcpy (&ii, &ff, sizeof (ff));
175    comm::functions::codeInteger (buffer, ii);
176    cout << "Float I: " << ff << " (" << anna::functions::asHexString (ii) << ") " << endl;
177
178    float ff2;
179    ii = comm::functions::decodeInteger (buffer);
180    anna_memcpy (&ff2, &ii, sizeof (ff));
181    cout << "Float O: " << ff2 << " (" << anna::functions::asHexString (ii) << ") " << endl;
182
183    dd = -123123123.3232;
184    S64 ii64;
185    anna_memcpy (&ii64, &dd, sizeof (dd));
186    comm::functions::codeInteger64 (buffer, ii64);
187    cout << "Double I: " << dd << " (" << anna::functions::asHexString (ii64) << ") " << endl;
188
189    double dd2;
190    ii64 = comm::functions::decodeInteger64 (buffer);
191    anna_memcpy (&dd2, &ii64, sizeof (dd));
192    cout << "Double O: " << dd2 << " (" << anna::functions::asHexString (ii64) << ") " << endl;
193 }
194