Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame^] | 1 | //===--- 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 | |
| 17 | namespace clang { |
| 18 | namespace ast_matchers { |
| 19 | namespace dynamic { |
| 20 | |
| 21 | VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) { |
| 22 | *this = Other; |
| 23 | } |
| 24 | |
| 25 | VariantValue::VariantValue(const DynTypedMatcher &Matcher) : Type(VT_Nothing) { |
| 26 | setMatcher(Matcher); |
| 27 | } |
| 28 | |
| 29 | VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) { |
| 30 | setString(String); |
| 31 | } |
| 32 | |
| 33 | VariantValue::~VariantValue() { reset(); } |
| 34 | |
| 35 | VariantValue &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 | |
| 52 | void 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 | |
| 67 | bool VariantValue::isString() const { |
| 68 | return Type == VT_String; |
| 69 | } |
| 70 | |
| 71 | const std::string &VariantValue::getString() const { |
| 72 | assert(isString()); |
| 73 | return *Value.String; |
| 74 | } |
| 75 | |
| 76 | void VariantValue::setString(const std::string &NewValue) { |
| 77 | reset(); |
| 78 | Type = VT_String; |
| 79 | Value.String = new std::string(NewValue); |
| 80 | } |
| 81 | |
| 82 | bool VariantValue::isMatcher() const { |
| 83 | return Type == VT_Matcher; |
| 84 | } |
| 85 | |
| 86 | const DynTypedMatcher &VariantValue::getMatcher() const { |
| 87 | assert(isMatcher()); |
| 88 | return *Value.Matcher; |
| 89 | } |
| 90 | |
| 91 | void VariantValue::setMatcher(const DynTypedMatcher &NewValue) { |
| 92 | reset(); |
| 93 | Type = VT_Matcher; |
| 94 | Value.Matcher = NewValue.clone(); |
| 95 | } |
| 96 | |
| 97 | void 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 |