Home | History | Annotate | Download | only in spirit
      1 /*
      2  * Copyright 2017, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef WORD_STREAM_IMPL_H
     18 #define WORD_STREAM_IMPL_H
     19 
     20 #include "word_stream.h"
     21 
     22 #include <string>
     23 
     24 namespace android {
     25 namespace spirit {
     26 
     27 class WordStreamImpl : public WordStream {
     28 public:
     29   WordStreamImpl();
     30   WordStreamImpl(const std::vector<uint32_t> &words);
     31   WordStreamImpl(std::vector<uint32_t> &&words);
     32 
     33   bool empty() const override {
     34     return mWords.empty() || mIter == mWords.end();
     35   }
     36 
     37   uint32_t operator*() { return *mIter; }
     38 
     39   WordStreamImpl &operator>>(uint32_t *RHS) override {
     40     *RHS = *mIter++;
     41     return *this;
     42   }
     43 
     44   WordStreamImpl &operator>>(LiteralContextDependentNumber *RHS) override {
     45     // TODO: check context in the instruction class to decide the actual size.
     46     return *this >> (uint32_t *)(&RHS->intValue);
     47   }
     48 
     49   WordStreamImpl &operator>>(std::string *str) override;
     50 
     51   std::vector<uint32_t> getWords() override { return mWords; }
     52 
     53   WordStreamImpl &operator<<(const uint32_t RHS) override {
     54     mWords.push_back(RHS);
     55     return *this;
     56   }
     57 
     58   WordStreamImpl &
     59   operator<<(const LiteralContextDependentNumber &RHS) override {
     60     // TODO: check context in the instruction class to decide the actual size.
     61     // TODO: maybe a word stream class should never take a context dependent
     62     // type as argument. Always take concrete types such as int32, int64, float,
     63     // double, etc.
     64     return *this << (uint32_t)(RHS.intValue);
     65   }
     66   WordStreamImpl &operator<<(const std::string &str) override;
     67 
     68 private:
     69   std::vector<uint32_t> mWords;
     70   std::vector<uint32_t>::const_iterator mIter;
     71 };
     72 
     73 } // namespace spirit
     74 } // namespace android
     75 
     76 #endif // WORD_STREAM_IMPL_H
     77