Home | History | Annotate | Download | only in testing
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 #ifndef TENSORFLOW_LITE_TESTING_MESSAGE_H_
     16 #define TENSORFLOW_LITE_TESTING_MESSAGE_H_
     17 
     18 #include <memory>
     19 #include <string>
     20 #include <vector>
     21 
     22 namespace tflite {
     23 namespace testing {
     24 
     25 // A Message is a textual protobuf-like structure that looks like:
     26 //    tag {
     27 //      f : "values"
     28 //      child {
     29 //        a : 1
     30 //       }
     31 //    }
     32 // This class provides the framework for processing message but does not
     33 // associate any particular behavior to fields and submessage. In order
     34 // to properly parse a stream this class must be derived.
     35 class Message {
     36  public:
     37   // Reads a stream, tokenizes it and create a new message under the given
     38   // top-level message. Returns true if the parsing succeeded.
     39   static bool Read(std::istream* input, Message* message);
     40 
     41   Message() {}
     42   virtual ~Message() {}
     43 
     44   // Called when a new field is found. For example, when:
     45   //   f : "values"
     46   // is found, it triggers:
     47   //   SetField("f", "values");
     48   virtual void SetField(const std::string& name, const std::string& value) {}
     49 
     50   // Called when a submessage is started. For example, when:
     51   //   child {
     52   // is found, it triggers
     53   //   AddChild("child");
     54   // If nullptr is returned, the contents of the submessage will be ignored.
     55   // Otherwise, the returned Message will be used to handle new fields and new
     56   // submessages. The caller should not take ownership of the returned pointer.
     57   virtual Message* AddChild(const std::string& name) { return nullptr; }
     58 
     59   // Called when a submessage is completed, that is, whenever a '}' is found.
     60   virtual void Finish() {}
     61 
     62  protected:
     63   // Takes ownership of the given pointer. Subclasses can use this method if
     64   // they don't want to implement their own ownership semantics.
     65   Message* Store(Message* n) {
     66     children_.emplace_back(n);
     67     return n;
     68   }
     69 
     70   // Returns a list of all owned submessages.
     71   const std::vector<std::unique_ptr<Message>>& Children() const {
     72     return children_;
     73   }
     74 
     75  private:
     76   std::vector<std::unique_ptr<Message>> children_;
     77 };
     78 
     79 }  // namespace testing
     80 }  // namespace tflite
     81 
     82 #endif  // TENSORFLOW_LITE_TESTING_MESSAGE_H_
     83