ModesMixin.h 1.85 KB
Newer Older
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
1
//-*-C++-*-
2
// Author: Michael S. Summers (ORNL)
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
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
/** \ingroup JsonParser */
/*@{*/

/*! \file ModesMixin.h  
 *
 *  
 *
 */

#ifndef  JsonParser_ModesMixin_H
#define  JsonParser_ModesMixin_H

#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>

namespace JsonParser {
  
  class ModesMixin {
  public:

    /* These modes can be pushed on the stack. */
    typedef enum modes {
      MODE_ARRAY = 1, 
      MODE_DONE = 2,  
      MODE_KEY = 3,   
      MODE_OBJECT = 4
    } ModeType;

    std::vector<ModeType> stack;
    
    ModesMixin():
      stack(1,MODE_DONE)
    {}
    
    //======================================================================

    std::string modeName(ModeType m) {
      switch (m) {
      case MODE_ARRAY:
	return "MODE_ARRAY";
      case MODE_DONE:
	return "MODE_DONE";
      case MODE_KEY:
	return "MODE_KEY";
      case MODE_OBJECT:
	return "MODE_OBJECT";
      default:
	return "MODE_UNKNOWN";
      }
    }

    //======================================================================

    void push(ModeType mode) {
      stack.push_back(mode);
    }
    
    void pop(ModeType expectedMode) {
      if (stack.size() == 0) {
	std::ostringstream msg;
	msg << "JsonParser.pop was expecting mode " << modeName(expectedMode) << " to be on the back of the stack. \n"
	    << "However the stack was empty!\n";
	throw std::logic_error(msg.str());
      }
      if (expectedMode != stack.back()) {
	std::ostringstream msg;
	msg << "JsonParser.pop was expecting mode " << modeName(expectedMode) << " to be on the back of the stack. \n"
	    << "However the back of the stack contained " << modeName(stack.back()) << "\n";
	throw std::logic_error(msg.str());
      }
      stack.pop_back();
    }
    
    const ModeType& currentMode() {
      return stack.back();
    }
  };

} // end namespace JsonParser


/*@}*/
#endif