File: field.H

package info (click to toggle)
mffm-timecode 1.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 928 kB
  • ctags: 265
  • sloc: cpp: 1,145; makefile: 94
file content (108 lines) | stat: -rw-r--r-- 3,587 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
  mffm Time Code
  Time Code for multimedia systems

  Copyright (C) 2000, 2001 Matt R. Flax <flatmax@ieee.org>
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You have received a copy of the GNU Lesser General Public License
  along with this library.
*/
#ifndef FIELD_H_
#define FIELD_H_

#include <math.h>

#include "counter.H"

//template <class FTYPE>
//#define FTYPE int
class field : public counter {
  unsigned int digitCount;

  /// Sets the digit count and display array
  void countDigits(void){
    // Work out the required digit count ...
    double mVal=maxCount;
    digitCount=0;
    while (mVal>pow(10.0,(int) ++digitCount)-1.0);
    // Set up the digit array ...
    digits=NULL;
    if (!(digits=new unsigned char[digitCount])){
      std::cerr <<"field: digit malloc error"<<std::endl;
      exit(-1);
    }
  }

  /// Sets the digits array to the digit value of the counter
  void syncDigits(void){
    //std::cout<<"syncdigits :: "<<std::endl;
    if (count==maxCount) count=0;
    int cnt = count;
    register double temp;
    for (int i=digitCount-1;i>=0;i--){
      temp=pow(10.0, i);
      digits[i]=(char)((double)cnt/temp);
      //std::cout<<(int)digits[i]<<std::endl;
      cnt-=digits[i]*(int)temp;
    }
  }

public:
  unsigned char *digits;

  field(int minimumVal, int startVal, int maximumVal) : counter(minimumVal, startVal, maximumVal){
#if DEBUG > 1
    std::cout<<"field::field(minimumVal, startVal, maximumVal)"<<std::endl;
#endif
    digits=NULL;
    // Set up the digit array and count
    countDigits();
    // Sync the digits with the counter value
    syncDigits();
  }

  ~field(void){
#if DEBUG > 1
    std::cout<<"field::~field"<<std::endl;
#endif
    //    std::cout<<"~field: in"<<std::endl;
    if (digits) delete [] digits;
    //    std::cout<<"~field: out"<<std::endl;
  }
  
  // The following alter the counter and must be overloaded
  field& operator =(const counter& f){counter::operator =(f); syncDigits(); return *this;}
  field& operator =(const int f){
    //  std::cout<<"equate"<<std::endl;
counter::operator =(f);
//std::cout<<"about to sync"<<std::endl;
 syncDigits(); return *this;}
  field& operator+=(const counter& f){counter::operator+=(f); syncDigits(); return *this;}
  field& operator+=(const int& f){counter::operator+=(f); syncDigits(); return *this;}
  field& operator-=(const counter& f){counter::operator-=(f); syncDigits(); return *this;}
  field& operator-=(const int& f){counter::operator-=(f); syncDigits(); return *this;}
  field& operator*=(const counter& f){counter::operator*=(f); syncDigits(); return *this;}
  field& operator*=(const int& f){counter::operator*=(f); syncDigits(); return *this;}
  field& operator/=(const counter& f){counter::operator/=(f); syncDigits(); return *this;}
  field& operator/=(const int& f){counter::operator/=(f); syncDigits(); return *this;}

  friend std::ostream& operator <<(std::ostream& o, field& f) {
    std::cout<<(counter)f<<":\t";
    for (int i=f.digitCount-1; i>=0; i--)
      o << (long)f.digits[i];
    return o;
  }

  int digitCnt(void){return digitCount;}
};
#endif //FIELD_H_