observers.cpp revision 12855:588919e0e4aa
1/***************************************************************************** 2 3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 4 more contributor license agreements. See the NOTICE file distributed 5 with this work for additional information regarding copyright ownership. 6 Accellera licenses this file to you under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with the 8 License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 implied. See the License for the specific language governing 16 permissions and limitations under the License. 17 18 *****************************************************************************/ 19 20#define SC_ENABLE_OBSERVERS 21#define SC_INCLUDE_FX 22#include "systemc.h" 23 24#if SC_CPLUSPLUS < 201103L 25# define override /* empty */ 26#endif // SC_CPLUSPLUS >= 201103L 27 28template<typename Observed, typename Base> 29class observer: public Base { 30 virtual void construct(const Observed&) override { 31 std::cout << "construct" << std::endl; 32 } 33 virtual void destruct(const Observed&) override { 34 std::cout << "destruct" << std::endl; 35 } 36 virtual void read(const Observed&) override { 37 std::cout << "read" << std::endl; 38 } 39 virtual void write(const Observed&) override { 40 std::cout << "write" << std::endl; 41 } 42}; 43 44template<typename T, typename Observed, typename Base> 45void observe(char const *name) { 46 std::cout << std::endl; 47 std::cout << name << std::endl; 48 49 observer<Observed, Base> o; 50 T value(&o); 51 52 std::cout << "to_short "; value.to_short(); 53 std::cout << "to_ushort "; value.to_ushort(); 54 std::cout << "to_int "; value.to_int(); 55 std::cout << "to_uint "; value.to_uint(); 56 std::cout << "to_long "; value.to_long(); 57 std::cout << "to_ulong "; value.to_ulong(); 58 std::cout << "to_int64 "; value.to_int64(); 59 std::cout << "to_uint64 "; value.to_uint64(); 60 std::cout << "to_float "; value.to_float(); 61 std::cout << "to_double "; value.to_double(); 62} 63 64int sc_main(int, char *[]) { 65 observe<sc_fxval, sc_fxval, sc_dt::sc_fxval_observer>("sc_fxval"); 66 observe<sc_fxval_fast, sc_fxval_fast, sc_dt::sc_fxval_fast_observer>("sc_fxval_fast"); 67 observe<sc_fixed<1, 2>, sc_fxnum, sc_dt::sc_fxnum_observer>("sc_fixed"); 68 observe<sc_fixed_fast<3, 4>, sc_fxnum_fast, sc_dt::sc_fxnum_fast_observer>("sc_fixed_fast"); 69 observe<sc_ufixed<5, 6>, sc_fxnum, sc_dt::sc_fxnum_observer>("sc_ufixed"); 70 observe<sc_ufixed_fast<7, 8>, sc_fxnum_fast, sc_dt::sc_fxnum_fast_observer>("sc_ufixed_fast"); 71 72 return 0; 73} 74