summaryrefslogtreecommitdiffstats
path: root/languages/cpp/typedecoration.h
blob: e142d8370a8caa168f51f5e23244903ce71fc92d (plain)
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
109
110
111
112
113
114
115
116
117
118
119
/***************************************************************************
  copyright            : (C) 2006 by David Nolden
  email                : david.nolden.kdevelop@art-master.de
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef __TYPEDECORATION_H__
#define __TYPEDECORATION_H__


class TypeDecoration {

  public:
	TypeDecoration() {
	}

	///Removes the decoration from the given string
	TypeDecoration( TQString& str ) {
      init( str );
    }

    ~TypeDecoration() {}

	///Removes the decoration from the assigned
	TypeDecoration& operator = ( TQString& str ) {
	  clear();
      init( str );
      return *this;
    }

    TQString apply( const TQString& str ) const {
      TQString ret = str;
      if ( !ret.startsWith( m_decoration_front ) )
        ret = m_decoration_front + ret;

      if ( !ret.endsWith( m_decoration_back ) )
        ret = ret + m_decoration_back;
      return ret;
    }

    void operator += ( const TypeDecoration& rhs ) {
      if ( !m_decoration_front.contains( rhs.m_decoration_front ) )
        m_decoration_front += rhs.m_decoration_front;
      if ( !m_decoration_back.contains( rhs.m_decoration_back ) )
        m_decoration_back += rhs.m_decoration_back;
    }

    void clear() {
      m_decoration_front = TQString();
      m_decoration_back = TQString();
    }

	void  prepend( const TQString& str ) {
		m_decoration_front = str + m_decoration_front;
	}
	
	/*bool smaller( const TypeDecoration& rhs ) const {
	}

	int depth() const {
		
	}*/

  private:
    void init( TQString& str ) {
      str = str.stripWhiteSpace();

      static const TQString cnst = "const";
      static const TQString ref = "&";
	    if ( str.startsWith( cnst ) ) {
		    str.remove( 0, cnst.length() );
		    if( str.isEmpty() || ( !str[0].isLetterOrNumber() && str[0] != '_' ) ) {
				m_decoration_front += cnst + " ";
			    str = str.stripWhiteSpace();
		    } else {
			    str = cnst + str; ///The const was not alone
		    }
	    }

	  if( str.endsWith( cnst ) ) {
		  str.remove( str.length() - cnst.length(), cnst.length() );
		  if( str.isEmpty() || ( !str[str.length()-1].isLetterOrNumber() && str[str.length()-1] != '_' ) ) {
			  m_decoration_back = (m_decoration_back + " " + cnst);
			  str = str.stripWhiteSpace();
		  } else {
			  str = str + cnst; ///The const was not alone
		  }
	  }
	    
	  if ( str.endsWith( ref ) ) {
	      m_decoration_back = ref + m_decoration_back;
		  str = str.remove( str.length() - ref.length(), ref.length() ).stripWhiteSpace();
		  
		  if( str.endsWith( cnst ) ) {
			  str.remove( str.length() - cnst.length(), cnst.length() );
			  if( str.isEmpty() || ( !str[str.length()-1].isLetterOrNumber() && str[str.length()-1] != '_' ) ) {
				  m_decoration_back = m_decoration_back + " " + cnst;
				  str = str.stripWhiteSpace();
			  } else {
				  str = str + cnst; ///The const was not alone
			  }
		  }
	  }
    }

    TQString m_decoration_front, m_decoration_back;
};

#endif 
// kate: indent-mode csands; tab-width 4;