blob: e310fbfc58f05df708e1baa64ea883548f1b5afd [file] [log] [blame]
Manuel Klimekf7f295f2013-05-14 09:13:00 +00001//===--- VariantValue.cpp - Polymorphic value type -*- C++ -*-===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Polymorphic value type.
12///
13//===----------------------------------------------------------------------===//
14
15#include "clang/ASTMatchers/Dynamic/VariantValue.h"
16
17namespace clang {
18namespace ast_matchers {
19namespace dynamic {
20
21VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) {
22 *this = Other;
23}
24
25VariantValue::VariantValue(const DynTypedMatcher &Matcher) : Type(VT_Nothing) {
26 setMatcher(Matcher);
27}
28
29VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) {
30 setString(String);
31}
32
33VariantValue::~VariantValue() { reset(); }
34
35VariantValue &VariantValue::operator=(const VariantValue &Other) {
36 if (this == &Other) return *this;
37 reset();
38 switch (Other.Type) {
39 case VT_String:
40 setString(Other.getString());
41 break;
42 case VT_Matcher:
43 setMatcher(Other.getMatcher());
44 break;
45 case VT_Nothing:
46 Type = VT_Nothing;
47 break;
48 }
49 return *this;
50}
51
52void VariantValue::reset() {
53 switch (Type) {
54 case VT_String:
55 delete Value.String;
56 break;
57 case VT_Matcher:
58 delete Value.Matcher;
59 break;
60 // Cases that do nothing.
61 case VT_Nothing:
62 break;
63 }
64 Type = VT_Nothing;
65}
66
67bool VariantValue::isString() const {
68 return Type == VT_String;
69}
70
71const std::string &VariantValue::getString() const {
72 assert(isString());
73 return *Value.String;
74}
75
76void VariantValue::setString(const std::string &NewValue) {
77 reset();
78 Type = VT_String;
79 Value.String = new std::string(NewValue);
80}
81
82bool VariantValue::isMatcher() const {
83 return Type == VT_Matcher;
84}
85
86const DynTypedMatcher &VariantValue::getMatcher() const {
87 assert(isMatcher());
88 return *Value.Matcher;
89}
90
91void VariantValue::setMatcher(const DynTypedMatcher &NewValue) {
92 reset();
93 Type = VT_Matcher;
94 Value.Matcher = NewValue.clone();
95}
96
97void VariantValue::takeMatcher(DynTypedMatcher *NewValue) {
98 reset();
99 Type = VT_Matcher;
100 Value.Matcher = NewValue;
101}
102
103} // end namespace dynamic
104} // end namespace ast_matchers
105} // end namespace clang